Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
GeoInt_SIDT
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mario Chirinos Colunga
GeoInt_SIDT
Commits
9e009c5a
Commit
9e009c5a
authored
Jul 16, 2018
by
Renán Sosa Guillen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
db population
parent
b0133c01
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
29 deletions
+48
-29
populate_db.py
catalog/management/commands/populate_db.py
+48
-29
No files found.
catalog/management/commands/populate_db.py
View file @
9e009c5a
# -*- 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'
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment