working on adding geospatial data to geo models
This commit is contained in:
@@ -26,12 +26,31 @@ class Command(BaseCommand):
|
|||||||
Region.objects.all().delete()
|
Region.objects.all().delete()
|
||||||
Country.objects.all().delete()
|
Country.objects.all().delete()
|
||||||
|
|
||||||
# load GADM data data source
|
# load GADM data
|
||||||
ds = DataSource('datasets/gadm36_ESP.gpkg')
|
ds = DataSource('datasets/gadm36_ESP.gpkg')
|
||||||
|
logging.info('GeoPackage data:')
|
||||||
|
for layer in ds:
|
||||||
|
logging.info(f"Layer {layer.name}:\n\t- Layers: {len(layer)}\n\t- Type: {layer.geom_type.name}\n\t- Features: {layer.num_feat}")
|
||||||
|
|
||||||
# create country for spain
|
# create country for spain
|
||||||
country_ds = ds[0]
|
# country = Country.objects.create(name='España')
|
||||||
country = Country.objects.create(name='España')
|
# country instances
|
||||||
|
logging.info("loading country instances")
|
||||||
|
country_counter = 0
|
||||||
|
for feature in ds[0]:
|
||||||
|
# calculate geometry data
|
||||||
|
geom = GEOSGeometry(str(feature.geom))
|
||||||
|
polygon_list = []
|
||||||
|
for polygon in geom:
|
||||||
|
polygon_list.append(polygon)
|
||||||
|
geom_geos = MultiPolygon(polygon_list)
|
||||||
|
# create instance
|
||||||
|
name = feature.get('NAME_0')
|
||||||
|
if name == 'Spain':
|
||||||
|
SPAIN = Country.objects.create(name='España',geo=geom_geos)
|
||||||
|
Country.objects.create(name=name,geo=geom_geos)
|
||||||
|
country_counter += 1
|
||||||
|
logging.info(f"Country instance created for: {name}")
|
||||||
|
|
||||||
locations_file = 'datasets/locations.json'
|
locations_file = 'datasets/locations.json'
|
||||||
locations = json.loads(open(locations_file).read())
|
locations = json.loads(open(locations_file).read())
|
||||||
@@ -40,7 +59,6 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
geo_file='datasets/gadm36_ESP.json' # geo boundary data for regions
|
geo_file='datasets/gadm36_ESP.json' # geo boundary data for regions
|
||||||
geo_data = json.loads(open(geo_file).read())
|
geo_data = json.loads(open(geo_file).read())
|
||||||
|
|
||||||
logging.info("Starting Region creation")
|
logging.info("Starting Region creation")
|
||||||
print("Starting Region creation")
|
print("Starting Region creation")
|
||||||
region_counter = 0
|
region_counter = 0
|
||||||
@@ -48,28 +66,24 @@ class Command(BaseCommand):
|
|||||||
if location['model'] == 'locations.region':
|
if location['model'] == 'locations.region':
|
||||||
logging.info(f"Creating Region instance {location['fields']['name']}...")
|
logging.info(f"Creating Region instance {location['fields']['name']}...")
|
||||||
new_region = None
|
new_region = None
|
||||||
|
|
||||||
name = location['fields']['name']
|
name = location['fields']['name']
|
||||||
# Bypass for lack of geo data on ceuta and melilla
|
# Bypass for lack of geo data on ceuta and melilla
|
||||||
if name in ('Ceuta', 'Melilla'):
|
if name in ('Ceuta', 'Melilla'):
|
||||||
new_region = Region.objects.create(name=name, country=country, id=location['pk'])
|
new_region = Region.objects.create(name=name, country=SPAIN, id=location['pk'])
|
||||||
logging.info(f"Region {name} created (without GADM data)")
|
logging.info(f"Region {name} created (without GADM data)")
|
||||||
else:
|
else:
|
||||||
for feature in geo_data['features']:
|
for feature in ds[1]:
|
||||||
if feature['properties']['NAME_1'] == name:
|
if feature.get('NAME_1') == name:
|
||||||
logging.debug(f"Region {name} found in GADM data")
|
logging.debug(f"Region {name} found in GADM data")
|
||||||
# calculate geometry data
|
# calculate geometry data
|
||||||
geom = GEOSGeometry(str(feature['geometry']))
|
geom = GEOSGeometry(str(feature.geom))
|
||||||
if feature['geometry']['type'] == "MultiPolygon":
|
polygon_list = []
|
||||||
poly_list = []
|
for polygon in geom:
|
||||||
for poly in geom:
|
polygon_list.append(polygon)
|
||||||
poly_list.append(poly)
|
geom_geos = MultiPolygon(polygon_list)
|
||||||
else:
|
|
||||||
poly_list = geom
|
|
||||||
geom_geos = MultiPolygon(poly_list)
|
|
||||||
# create instance
|
# create instance
|
||||||
new_region = Region.objects.create(
|
new_region = Region.objects.create(
|
||||||
name=name, country=country, geo=geom_geos, id=location['pk']
|
name=name, country=SPAIN, geo=geom_geos, id=location['pk']
|
||||||
)
|
)
|
||||||
logging.info(f"Region {name} created")
|
logging.info(f"Region {name} created")
|
||||||
region_counter += 1
|
region_counter += 1
|
||||||
@@ -85,10 +99,27 @@ class Command(BaseCommand):
|
|||||||
if location['model'] == 'locations.province':
|
if location['model'] == 'locations.province':
|
||||||
logging.info(f"Creating Province instance {location['fields']['name']}...")
|
logging.info(f"Creating Province instance {location['fields']['name']}...")
|
||||||
name = location['fields']['name']
|
name = location['fields']['name']
|
||||||
# get parent region
|
new_province = None
|
||||||
parent_region = Region.objects.get(id=location['fields']['region'])
|
for feature in ds[2]:
|
||||||
Province.objects.create(name=name, region=parent_region, id=location['pk'])
|
import ipdb; ipdb.set_trace()
|
||||||
province_counter += 1
|
if feature.get('NAME_1') == name:
|
||||||
|
logging.debug(f"Province {name} found in GADM data")
|
||||||
|
# calculate geometry data
|
||||||
|
geom = GEOSGeometry(str(feature.geom))
|
||||||
|
polygon_list = []
|
||||||
|
for polygon in geom:
|
||||||
|
polygon_list.append(polygon)
|
||||||
|
geom_geos = MultiPolygon(polygon_list)
|
||||||
|
# create instance
|
||||||
|
parent_region = Region.objects.get(id=location['fields']['region'])
|
||||||
|
new_province = Province.objects.create(
|
||||||
|
name=name, region=parent_region, geo=geom_geos, id=location['pk']
|
||||||
|
)
|
||||||
|
logging.info(f"Province {name} created")
|
||||||
|
region_counter += 1
|
||||||
|
break
|
||||||
|
if new_province is None:
|
||||||
|
logging.warning(f"No province named {name} found in GADM data")
|
||||||
|
|
||||||
# CITIES
|
# CITIES
|
||||||
print("Starting City creation")
|
print("Starting City creation")
|
||||||
|
|||||||
118
core/management/commands/loadgisdata.py
Normal file
118
core/management/commands/loadgisdata.py
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
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}")
|
||||||
Reference in New Issue
Block a user