diff --git a/README.md b/README.md index d9aa99c..71513b5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This README aims to document functionality of backend as well as required steps ## Table of Contents - [First Steps](#first-steps) - +- [Location Data](#location-data) ## First Steps @@ -22,15 +22,14 @@ python migrate - Start server in development mode: `python manage.py runserver` -## Load Geo data -Import geodata +## Location data + +To load initial location data use: `python manage.py addgeo` ```python -import os from geo.models import Region -from django.contrib.gis.geos import GeometryCollection, GEOSGeometry +from django.contrib.gis.geos import GEOSGeometry, MultiPolygon import json -from django.contrib.gis.geos import MultiPolygon path='gadm36_ESP_1.json' diff --git a/core/management/commands/addgeo.py b/core/management/commands/addgeo.py index 7d1ccd3..c27ceb7 100644 --- a/core/management/commands/addgeo.py +++ b/core/management/commands/addgeo.py @@ -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':