Commit 9e009c5a authored by Renán Sosa Guillen's avatar Renán Sosa Guillen

db population

parent b0133c01
# -*- coding: utf-8 -*-
"""
This script saves in DB the info from geojson files.
Receives as parameter the path where geojson files are located.
Usage:
$ cd GeoInt_SIDT
$ python manage.py populate_db [path/of/geojson/files]
Example:
$ cd GeoInt_SIDT
$ python manage.py populate_db /home/emmanuelhp/Documentos/geojson/
"""
from django.core.management.base import BaseCommand, CommandError
from catalog.models import Polygon
import json, unicodedata
class Command(BaseCommand):
def handle(self, *args, **options):
#base path
base_path = '/home/emmanuelhp/Documentos/geojson/'
# loads files
for index in range(1,33):
with open(base_path + str(index)+'.geojson') as f:
data = json.load(f)
print ("Index: ", index)
for feat in data['features']:
polygon = Polygon(
name = self.strip_accents(feat['properties']['NOMGEO'].lower()),
json_info = json.dumps(feat, ensure_ascii=True)
)
polygon.save()
# print ("============= "+ data['name'] + " ===================")
# for feature in data['features']:
# print ("NOMGEO: ", feature['properties']['NOMGEO'])
# print ("CVE_ENT: ", feature['properties']['CVE_ENT'])
# print ("CVE_MUN: ", feature['properties']['CVE_MUN'])
# print ("CVEGEO: ", feature['properties']['CVEGEO'])
# print ("coordinates: ", feature['geometry']['coordinates'])
# print ("_____________________________________________")
def strip_accents(self, s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
def add_arguments(self, parser):
## positional arguments
parser.add_argument('geojson_path', nargs=1, type=str)
def handle(self, *args, **options):
## base geojson files path
base_path = options['geojson_path'][0]
# loading files
for index in range(1,33):
with open(base_path + str(index) + ".geojson") as f:
data = json.load(f)
print ("Index: ", index)
for feat in data['features']:
polygon = Polygon(
name = self.strip_accents(feat['properties']['NOMGEO'].lower()),
json_info = json.dumps(feat, ensure_ascii=True)
)
polygon.save()
# print ("============= "+ data['name'] + " ===================")
# for feature in data['features']:
# print ("NOMGEO: ", feature['properties']['NOMGEO'])
# print ("CVE_ENT: ", feature['properties']['CVE_ENT'])
# print ("CVE_MUN: ", feature['properties']['CVE_MUN'])
# print ("CVEGEO: ", feature['properties']['CVEGEO'])
# print ("coordinates: ", feature['geometry']['coordinates'])
# print ("_____________________________________________")
def strip_accents(self, s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment