Files
consumocuidado-server/core/management/commands/addgeo.py
2021-01-25 12:28:46 +00:00

104 lines
4.3 KiB
Python

import logging
import json
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
from geo.models import City, Region, Province, Country
logging.basicConfig(
filename='logs/addgeo.log',
filemode='w',
format='%(levelname)s:%(message)s',
level=logging.INFO,
)
class Command(BaseCommand):
def handle(self, *args, **kwargs):
print('Deleting all instances of Country, Region, Province, City')
logging.info('Deleting all instances of Country, Region, Province, City')
City.objects.all().delete()
Province.objects.all().delete()
Region.objects.all().delete()
Country.objects.all().delete()
# create country for spain
country = Country.objects.create(name='España')
locations_file = 'locations.json'
locations = json.loads(open(locations_file).read())
# REGIONS
geo_file='gadm36_ESP.json' # geo boundary data for regions
geo_data = json.loads(open(geo_file).read())
logging.info("Starting Region creation")
print("Starting Region creation")
region_counter = 0
for location in locations:
if location['model'] == 'locations.region':
logging.info(f"Creating Region instance {location['fields']['name']}...")
new_region = None
name = location['fields']['name']
# Bypass for lack of geo data on ceuta and melilla
if name in ('Ceuta', 'Melilla'):
new_region = Region.objects.create(name=name, country=country, id=location['pk'])
logging.info(f"Region {name} created (without GADM data)")
else:
for feature in geo_data['features']:
if feature['properties']['NAME_1'] == name:
logging.debug(f"Region {name} found in GADM data")
# calculate geometry data
geom = GEOSGeometry(str(feature['geometry']))
if feature['geometry']['type'] == "MultiPolygon":
poly_list = []
for poly in geom:
poly_list.append(poly)
else:
poly_list = geom
geom_geos = MultiPolygon(poly_list)
# create instance
new_region = Region.objects.create(
name=name, country=country, geo=geom_geos, id=location['pk']
)
logging.info(f"Region {name} created")
region_counter += 1
break
if new_region is None:
logging.warning(f"No region named {name} found in GADM data")
# PROVINCES
print("Starting Province creation")
logging.info("Starting Province creation")
province_counter = 0
for location in locations:
if location['model'] == 'locations.province':
logging.info(f"Creating Province instance {location['fields']['name']}...")
name = location['fields']['name']
# get parent region
parent_region = Region.objects.get(id=location['fields']['region'])
Province.objects.create(name=name, region=parent_region, id=location['pk'])
province_counter += 1
# CITIES
print("Starting City creation")
logging.info("Starting City creation")
city_counter = 0
for location in locations:
if location['model'] == 'locations.city':
name = location['fields']['name']
City.objects.create(name=name, province=Province.objects.get(id=location['fields']['province']), id=location['pk'])
city_counter += 1
logging.info(f"Region instances created: {region_counter}")
logging.info(f"Province instances created: {province_counter}")
logging.info(f"City instances created: {city_counter}")
print(f"Region instances created: {region_counter}")
print(f"Province instances created: {province_counter}")
print(f"City instances created: {city_counter}")