uspješno učitana skripta
This commit is contained in:
0
plovidba_aplikacija/management/commands/__init__.py
Normal file
0
plovidba_aplikacija/management/commands/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.gis.geos import Point
|
||||
from plovidba_aplikacija.models import ObjektSigurnosti
|
||||
from django.db import transaction
|
||||
|
||||
# dodani podaci
|
||||
class Command(BaseCommand):
|
||||
help = """
|
||||
Programatically create entries for plovidba_aplikacija model ObjektSigurnosti
|
||||
"""
|
||||
def handle(self, *args, **options):
|
||||
json_rpath = r'C:\Users\Student1\Desktop\plovidba\myenv\plovidba_projekt\plovidba_aplikacija\management\commands\testnipodaci.json'
|
||||
|
||||
created_entries = 0
|
||||
existing_entries = 0
|
||||
|
||||
with open(json_rpath, encoding='utf-8') as r:
|
||||
data = json.load(r)
|
||||
|
||||
with transaction.atomic():
|
||||
for feature in data.get('features', []):
|
||||
properties = feature.get('properties', {})
|
||||
geometry = feature.get('geometry', {})
|
||||
|
||||
latitude = geometry.get('coordinates', [])[1]
|
||||
longitude = geometry.get('coordinates', [])[0]
|
||||
naziv_objekta = properties.get('naziv_objekta', '')
|
||||
|
||||
if not (isinstance(latitude, (float, int)) and isinstance(longitude, (float, int))):
|
||||
print("Skipping invalid coordinates.")
|
||||
continue
|
||||
|
||||
print(f"Latitude: {latitude}, Longitude: {longitude}, Naziv Objekta: {naziv_objekta}")
|
||||
|
||||
|
||||
obj, created = ObjektSigurnosti.objects.get_or_create(
|
||||
naziv=naziv_objekta,
|
||||
lokacija=Point(float(longitude), float(latitude))
|
||||
)
|
||||
if created:
|
||||
created_entries +=1
|
||||
print("Kreiran OS {} - {}".format(obj.naziv, obj.lokacija))
|
||||
else:
|
||||
existing_entries += 1
|
||||
print("Existing OS {} - {}".format(obj.naziv, obj.lokacija))
|
||||
|
||||
print("Created: {}".format(created_entries))
|
||||
print("Existing:".format(existing_entries))
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
@@ -1,24 +0,0 @@
|
||||
import json
|
||||
from django.contrib.gis.geos import Point
|
||||
from plovidba_aplikacija.models import ObjektSigurnosti
|
||||
|
||||
# dodani podaci
|
||||
def run():
|
||||
with open(r'C:\Users\Student1\Desktop\plovidba\myenv\plovidba_projekt\plovidba_aplikacija\scripts\testnipodaci.json') as file:
|
||||
podaci = json.load(file)
|
||||
|
||||
features = podaci.get('features', [])
|
||||
for feature in features:
|
||||
properties = feature.get('properties', {})
|
||||
naziv = properties.get('naziv_objekta', '')
|
||||
|
||||
geometrija = feature.get('geometrija', {})
|
||||
koordinate = geometrija.get('koordinate', [])
|
||||
|
||||
if koordinate:
|
||||
latitude, longitude = koordinate[0], koordinate[1]
|
||||
lokacija = Point(latitude, longitude, srid=3765)
|
||||
ObjektSigurnosti.objects.create(naziv=naziv, lokacija=lokacija)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user