uspješno učitana skripta

This commit is contained in:
emaric 2024-01-10 10:26:23 +01:00
parent 3709991263
commit 6994f43490
13 changed files with 167 additions and 27 deletions

View File

@ -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

View File

@ -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

Binary file not shown.

101
plovidba_projekt/env.py Normal file
View File

@ -0,0 +1,101 @@
import os
__all__ = [
'BASE_DIR', 'ABS_PATH', 'ENV_BOOL', 'ENV_NUM', 'ENV_STR', 'ENV_LIST', 'PARDIR', 'ENV_TUPLE'
]
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PARDIR = os.pardir
APPLICATION_NAME = "rgi-dev"
ENV_PATH = os.path.join('/etc/secrets/', APPLICATION_NAME)
def ABS_PATH(*args):
return os.path.join(BASE_DIR, *args)
def ENV_BOOL(name, default=False):
"""
Get a boolean value from environment variable.
If the environment variable is not set or value is not one or "true" or
"false", the default value is returned instead.
"""
if name not in os.environ:
return default
if os.environ[name].lower() in ['true', 'yes', '1']:
return True
elif os.environ[name].lower() in ['false', 'no', '0']:
return False
else:
return default
def ENV_NUM(name, default=None):
"""
Get a integer value from environment variable.
If the environment variable is not set, the default value is returned
instead.
"""
return int(os.environ.get(name, default))
def ENV_STR(name, default=None):
"""
Get a string value from environment variable.
If the environment variable is not set, the default value is returned
instead.
"""
return os.environ.get(name, default)
def ENV_LIST(name, separator, default=None):
"""
Get a list of string values from environment variable.
If the environment variable is not set, the default value is returned
instead.
"""
if default is None:
default = []
if name not in os.environ:
return default
return os.environ[name].split(separator)
def ENV_TUPLE(name, separator, default=None):
"""
Get a tuple of string values from environment variable.
If the environment variable is not set, the default value is returned
instead.
"""
if default is None:
default = ()
if name not in os.environ:
return default
return tuple(os.environ[name].split(separator))
def _load_env_file():
if os.path.exists(os.path.join(BASE_DIR, ".env")):
envfile = os.path.join(BASE_DIR, ".env")
else:
envfile = os.path.join(ENV_PATH, ".env")
if os.path.isfile(envfile):
for line in open(envfile):
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, v = line.split('=', 1)
os.environ[k] = v
_load_env_file()

View File

@ -19,6 +19,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
import os
from osgeo import gdal
from .env import BASE_DIR, ENV_BOOL, ENV_LIST, ENV_NUM, ENV_STR, PARDIR # noqa
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -46,7 +47,9 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'django.contrib.gis',
'django_extensions',
# 3rd party
'rest_framework',
# Custom apps:
'plovidba_aplikacija',
]
@ -127,6 +130,15 @@ USE_I18N = True
USE_TZ = True
PROJ_LIB = ENV_STR("PROJ_LIB", None)
GDAL_DATA = ENV_STR("GDAL_DATA", None)
if PROJ_LIB:
os.environ["PROJ_LIB"] = PROJ_LIB
if GDAL_DATA:
os.environ["GDAL_DATA"] = GDAL_DATA
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
@ -138,5 +150,3 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
GDAL_LIBRARY_PATH = r'C:\Users\Student1\GDAL\gdal304.dll'