modified addgeo command
This commit is contained in:
11
README.md
11
README.md
@@ -5,7 +5,7 @@ This README aims to document functionality of backend as well as required steps
|
|||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
- [First Steps](#first-steps)
|
- [First Steps](#first-steps)
|
||||||
|
- [Location Data](#location-data)
|
||||||
|
|
||||||
## First Steps
|
## First Steps
|
||||||
|
|
||||||
@@ -22,15 +22,14 @@ python migrate
|
|||||||
- Start server in development mode: `python manage.py runserver`
|
- Start server in development mode: `python manage.py runserver`
|
||||||
|
|
||||||
|
|
||||||
## Load Geo data
|
## Location data
|
||||||
Import geodata
|
|
||||||
|
To load initial location data use: `python manage.py addgeo`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import os
|
|
||||||
from geo.models import Region
|
from geo.models import Region
|
||||||
from django.contrib.gis.geos import GeometryCollection, GEOSGeometry
|
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
|
||||||
import json
|
import json
|
||||||
from django.contrib.gis.geos import MultiPolygon
|
|
||||||
|
|
||||||
path='gadm36_ESP_1.json'
|
path='gadm36_ESP_1.json'
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
|
||||||
|
|
||||||
from geo.models import City, Region, Province, Country
|
from geo.models import City, Region, Province, Country
|
||||||
import json
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
|
||||||
@@ -17,20 +20,39 @@ class Command(BaseCommand):
|
|||||||
# create country for spain
|
# create country for spain
|
||||||
country = Country.objects.create(name='España')
|
country = Country.objects.create(name='España')
|
||||||
|
|
||||||
path = 'locations.json'
|
locations_file = 'locations.json'
|
||||||
locations = json.loads(open(path).read())
|
locations = json.loads(open(locations_file).read())
|
||||||
|
|
||||||
|
# REGIONS
|
||||||
region_counter = 0
|
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:
|
for location in locations:
|
||||||
if location['model'] == 'locations.region':
|
if location['model'] == 'locations.region':
|
||||||
logging.info(f"Creating Region Object {location['fields']['name']}...")
|
logging.info(f"Creating Region Object {location['fields']['name']}...")
|
||||||
name = location['fields']['name']
|
name = location['fields']['name']
|
||||||
Region.objects.create(name=name, country=country, id=location['pk'])
|
Region.objects.create(name=name, country=country, id=location['pk'])
|
||||||
region_counter += 1
|
region_counter += 1
|
||||||
|
"""
|
||||||
|
# PROVINCES
|
||||||
province_counter = 0
|
province_counter = 0
|
||||||
# Provinces loop
|
|
||||||
for location in locations:
|
for location in locations:
|
||||||
if location['model'] == 'locations.province':
|
if location['model'] == 'locations.province':
|
||||||
logging.info(f"Creating Province Object {location['fields']['name']}...")
|
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.objects.create(name=name, region=Region.objects.get(id=location['fields']['region']), id=location['pk'])
|
||||||
province_counter += 1
|
province_counter += 1
|
||||||
|
|
||||||
|
# CITIES
|
||||||
city_counter = 0
|
city_counter = 0
|
||||||
# Cities loop
|
|
||||||
print('Creating cities...')
|
print('Creating cities...')
|
||||||
for location in locations:
|
for location in locations:
|
||||||
if location['model'] == 'locations.city':
|
if location['model'] == 'locations.city':
|
||||||
|
|||||||
Reference in New Issue
Block a user