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

db population

parent b0133c01
# -*- coding: utf-8 -*- # -*- 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 django.core.management.base import BaseCommand, CommandError
from catalog.models import Polygon from catalog.models import Polygon
import json, unicodedata import json, unicodedata
class Command(BaseCommand): class Command(BaseCommand):
def handle(self, *args, **options): def add_arguments(self, parser):
#base path ## positional arguments
base_path = '/home/emmanuelhp/Documentos/geojson/' parser.add_argument('geojson_path', nargs=1, type=str)
# loads files
for index in range(1,33): def handle(self, *args, **options):
with open(base_path + str(index)+'.geojson') as f: ## base geojson files path
data = json.load(f) base_path = options['geojson_path'][0]
print ("Index: ", index)
for feat in data['features']: # loading files
polygon = Polygon( for index in range(1,33):
name = self.strip_accents(feat['properties']['NOMGEO'].lower()), with open(base_path + str(index) + ".geojson") as f:
json_info = json.dumps(feat, ensure_ascii=True) data = json.load(f)
) print ("Index: ", index)
for feat in data['features']:
polygon.save() polygon = Polygon(
name = self.strip_accents(feat['properties']['NOMGEO'].lower()),
json_info = json.dumps(feat, ensure_ascii=True)
# print ("============= "+ data['name'] + " ===================") )
# for feature in data['features']:
# print ("NOMGEO: ", feature['properties']['NOMGEO']) polygon.save()
# print ("CVE_ENT: ", feature['properties']['CVE_ENT'])
# print ("CVE_MUN: ", feature['properties']['CVE_MUN'])
# print ("CVEGEO: ", feature['properties']['CVEGEO']) # print ("============= "+ data['name'] + " ===================")
# print ("coordinates: ", feature['geometry']['coordinates']) # for feature in data['features']:
# print ("_____________________________________________") # print ("NOMGEO: ", feature['properties']['NOMGEO'])
# print ("CVE_ENT: ", feature['properties']['CVE_ENT'])
def strip_accents(self, s): # print ("CVE_MUN: ", feature['properties']['CVE_MUN'])
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')) # 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