modified addgeo command

This commit is contained in:
Sam
2021-01-21 13:45:56 +00:00
parent 830a056cf8
commit 984bea0038
2 changed files with 34 additions and 13 deletions

View File

@@ -1,8 +1,11 @@
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
import json
class Command(BaseCommand):
@@ -17,20 +20,39 @@ class Command(BaseCommand):
# create country for spain
country = Country.objects.create(name='España')
path = 'locations.json'
locations = json.loads(open(path).read())
locations_file = 'locations.json'
locations = json.loads(open(locations_file).read())
# REGIONS
region_counter = 0
# Regions loop
geo_file='gadm36_ESP_1.json'
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
"""
for location in locations:
if location['model'] == 'locations.region':
logging.info(f"Creating Region Object {location['fields']['name']}...")
name = location['fields']['name']
Region.objects.create(name=name, country=country, id=location['pk'])
region_counter += 1
"""
# PROVINCES
province_counter = 0
# Provinces loop
for location in locations:
if location['model'] == 'locations.province':
logging.info(f"Creating Province Object {location['fields']['name']}...")
@@ -38,8 +60,8 @@ class Command(BaseCommand):
Province.objects.create(name=name, region=Region.objects.get(id=location['fields']['region']), id=location['pk'])
province_counter += 1
# CITIES
city_counter = 0
# Cities loop
print('Creating cities...')
for location in locations:
if location['model'] == 'locations.city':