119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
import logging
|
|
import json
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.conf import settings
|
|
|
|
from django.contrib.gis.geos import GeometryCollection, GEOSGeometry
|
|
from django.contrib.gis.geos import MultiPolygon
|
|
from django.contrib.gis.gdal import DataSource
|
|
|
|
|
|
from geo.models import Country, Region, Province, City
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Load GIS data into database'
|
|
|
|
def handle(self, *args, **kwargs):
|
|
|
|
print(self.help)
|
|
|
|
|
|
ds = DataSource('datasets/gadm36_ESP.gpkg')
|
|
|
|
for layer in ds:
|
|
print(f"Layer {layer.name}:\n \
|
|
\t- Layers: {len(layer)}\n \
|
|
\t- Type: {layer.geom_type.name}\n\
|
|
\t- Features: {layer.num_feat}")
|
|
|
|
# country instances
|
|
logging.info("loading country instances")
|
|
country_counter = 0
|
|
for feature in ds[0]:
|
|
geom = GEOSGeometry(str(feature['geometry']))
|
|
if feature['geometry']['type'] == "MultiPolygon":
|
|
poly_list = []
|
|
for poly in geom:
|
|
poly_list.append(poly)
|
|
print(poly_list)
|
|
else:
|
|
poly_list = geom
|
|
|
|
geom_geos = MultiPolygon(poly_list)
|
|
|
|
name = feature['properties']['NAME_1']
|
|
if name == 'España':
|
|
SPAIN = Country.objects.create(name=name,poly=geom_geos)
|
|
Country.objects.create(name=name,poly=geom_geos)
|
|
country_counter += 1
|
|
logging.info(f"Country instance created for: {name}")
|
|
|
|
# region instances
|
|
logging.info("loading region instances")
|
|
region_counter = 0
|
|
for feature in ds[1]:
|
|
geom = GEOSGeometry(str(feature['geometry']))
|
|
if feature['geometry']['type'] == "MultiPolygon":
|
|
poly_list = []
|
|
for poly in geom:
|
|
poly_list.append(poly)
|
|
print(poly_list)
|
|
else:
|
|
poly_list = geom
|
|
|
|
geom_geos = MultiPolygon(poly_list)
|
|
|
|
name = feature['properties']['NAME_1']
|
|
Region.objects.create(name=name,poly=geom_geos, country=SPAIN)
|
|
region_counter += 1
|
|
logging.info(f"Region instance created for: {name}")
|
|
|
|
# province instances
|
|
logging.info("loading province instances")
|
|
province_counter = 0
|
|
for feature in ds[2]:
|
|
geom = GEOSGeometry(str(feature['geometry']))
|
|
if feature['geometry']['type'] == "MultiPolygon":
|
|
poly_list = []
|
|
for poly in geom:
|
|
poly_list.append(poly)
|
|
print(poly_list)
|
|
else:
|
|
poly_list = geom
|
|
|
|
geom_geos = MultiPolygon(poly_list)
|
|
|
|
name = feature['properties']['NAME_1']
|
|
parent_region = Region.objects.get(id=location['fields']['region'])
|
|
Province.objects.create(name=name,poly=geom_geos, country=SPAIN)
|
|
province_counter += 1
|
|
logging.info(f"Province instance created for: {name}")
|
|
|
|
# city instances
|
|
logging.info("loading city instances")
|
|
city_counter = 0
|
|
for feature in ds[4]:
|
|
geom = GEOSGeometry(str(feature['geometry']))
|
|
if feature['geometry']['type'] == "MultiPolygon":
|
|
poly_list = []
|
|
for poly in geom:
|
|
poly_list.append(poly)
|
|
print(poly_list)
|
|
else:
|
|
poly_list = geom
|
|
|
|
geom_geos = MultiPolygon(poly_list)
|
|
|
|
name = feature['properties']['NAME_1']
|
|
City.objects.create(name=name,poly=geom_geos, country=SPAIN)
|
|
city_counter += 1
|
|
logging.debug(f"City instance created for: {name}")
|
|
|
|
logging.info(f"Country instances created: {country_counter}")
|
|
logging.info(f"Region instances created: {region_counter}")
|
|
logging.info(f"Province instances created: {[province_counter]}")
|
|
logging.info(f"City instances created: {city_counter}")
|