fixed to datasets and addgeo to correctly create geo instances

This commit is contained in:
Sam
2021-01-22 10:32:04 +00:00
parent 7c31052436
commit 1a1ada4def
4 changed files with 69 additions and 38 deletions

View File

@@ -6,16 +6,22 @@ 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.objecs.all().delete()
Province.objecs.all().delete()
Region.objecs.all().delete()
Country.objecs.all().delete()
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')
@@ -24,45 +30,64 @@ class Command(BaseCommand):
locations = json.loads(open(locations_file).read())
# REGIONS
region_counter = 0
geo_file='gadm36_ESP_1.json'
geo_file='gadm36_ESP.json' # geo boundary data for regions
geo_data = json.loads(open(geo_file).read())
for feature in geo_data['features']:
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, country=country, geo=geom_geos)
region_counter += 1
"""
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 Object {location['fields']['name']}...")
logging.info(f"Creating Region instance {location['fields']['name']}...")
new_region = None
name = location['fields']['name']
Region.objects.create(name=name, country=country, id=location['pk'])
region_counter += 1
"""
# 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 Object {location['fields']['name']}...")
logging.info(f"Creating Province instance {location['fields']['name']}...")
name = location['fields']['name']
Province.objects.create(name=name, region=Region.objects.get(id=location['fields']['region']), id=location['pk'])
# get parent region
# import ipdb; ipdb.set_trace()
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
print('Creating cities...')
for location in locations:
if location['model'] == 'locations.city':
name = location['fields']['name']
@@ -73,3 +98,7 @@ class Command(BaseCommand):
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}")