Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Observatoire
observatoire-scripts
Commits
a371da34
Commit
a371da34
authored
Mar 05, 2020
by
Pierre Dittgen
Browse files
Use black
parent
6628652f
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/legacy/generate_geo_jsons.py
View file @
a371da34
...
...
@@ -27,13 +27,13 @@ def compute_description(row, met_cursor):
""" Compute (sort of) wiki description """
data
=
{
'
nom
'
:
row
[
'
nom
'
],
'
type
'
:
row
[
'
type
'
],
'
metrics
'
:
mc
.
compute_metrics_tree
(
row
[
'
siren
'
],
met_cursor
)
"
nom
"
:
row
[
"
nom
"
],
"
type
"
:
row
[
"
type
"
],
"
metrics
"
:
mc
.
compute_metrics_tree
(
row
[
"
siren
"
],
met_cursor
)
,
}
fd
=
io
.
StringIO
()
mc
.
write_md_content
(
data
,
fd
,
with_title
=
False
,
mode
=
'
geo
'
)
mc
.
write_md_content
(
data
,
fd
,
with_title
=
False
,
mode
=
"
geo
"
)
fd
.
seek
(
0
)
desc
=
fd
.
read
()
fd
.
close
()
...
...
@@ -50,18 +50,18 @@ def output_feature(fd, row, metrics_cursor):
""" Outputs feature information """
props
=
{
'
name
'
:
row
[
'
nom
'
],
'
description
'
:
compute_description
(
row
,
metrics_cursor
)
"
name
"
:
row
[
"
nom
"
],
"
description
"
:
compute_description
(
row
,
metrics_cursor
)
,
}
feature_data
=
{
'
id
'
:
row
[
'
siren
'
],
'
properties
'
:
props
,
'
type
'
:
'
Feature
'
,
'
geometry
'
:
manage_geom
(
row
[
'
coords
'
]),
"
id
"
:
row
[
"
siren
"
],
"
properties
"
:
props
,
"
type
"
:
"
Feature
"
,
"
geometry
"
:
manage_geom
(
row
[
"
coords
"
]),
}
fd
.
write
(
json
.
dumps
(
feature_data
,
ensure_ascii
=
False
,
sort_keys
=
True
,
double_precision
=
2
))
fd
.
write
(
json
.
dumps
(
feature_data
,
ensure_ascii
=
False
,
sort_keys
=
True
))
def
generate_geojson_file
(
category
,
conn
,
geojson_filepath
:
Path
):
...
...
@@ -69,17 +69,23 @@ def generate_geojson_file(category, conn, geojson_filepath: Path):
metrics_cursor
=
conn
.
cursor
()
cursor
=
conn
.
cursor
()
type_constraints
=
"= '{}'"
.
format
(
category
)
if
category
!=
'EPCI'
else
"IN ('CA', 'CC', 'CU', 'MET', 'EPT')"
type_constraints
=
(
"= '{}'"
.
format
(
category
)
if
category
!=
"EPCI"
else
"IN ('CA', 'CC', 'CU', 'MET', 'EPT')"
)
sql_select
=
"""SELECT * FROM orga_geometry
WHERE type {}
AND NOT coords IS NULL
AND coords != ''"""
.
format
(
type_constraints
)
AND coords != ''"""
.
format
(
type_constraints
)
cursor
.
execute
(
sql_select
)
rows
=
cursor
.
fetchall
()
metrics_cursor
=
conn
.
cursor
()
with
geojson_filepath
.
open
(
mode
=
'
wt
'
,
encoding
=
'
utf-8
'
)
as
gj_fd
:
with
geojson_filepath
.
open
(
mode
=
"
wt
"
,
encoding
=
"
utf-8
"
)
as
gj_fd
:
gj_fd
.
write
(
GEOJSON_HEADER
)
...
...
@@ -95,23 +101,23 @@ def generate_geojson_file(category, conn, geojson_filepath: Path):
def
main
():
""" Generates Geo JSON file """
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'
db_file
'
,
type
=
Path
,
help
=
'
SQLite database file
'
)
parser
.
add_argument
(
'
output_dir
'
,
type
=
Path
,
help
=
'
Where to generate GeoJSON files
'
)
parser
.
add_argument
(
"
db_file
"
,
type
=
Path
,
help
=
"
SQLite database file
"
)
parser
.
add_argument
(
"
output_dir
"
,
type
=
Path
,
help
=
"
Where to generate GeoJSON files
"
)
args
=
parser
.
parse_args
()
if
not
args
.
db_file
.
is_file
():
parser
.
error
(
'
SQLite DB file [{}] not found
'
.
format
(
str
(
args
.
db_file
)))
parser
.
error
(
"
SQLite DB file [{}] not found
"
.
format
(
str
(
args
.
db_file
)))
if
not
args
.
output_dir
.
is_dir
():
parser
.
error
(
'
GeoJSON output dir [{}] not found
'
.
format
(
str
(
args
.
output_dir
)))
parser
.
error
(
"
GeoJSON output dir [{}] not found
"
.
format
(
str
(
args
.
output_dir
)))
conn
=
sqlite3
.
connect
(
str
(
args
.
db_file
))
conn
.
row_factory
=
sqlite3
.
Row
for
cat
in
(
'
REG
'
,
'
DEP
'
,
'
COM
'
,
'
EPCI
'
,
'
AGCT
'
,
'
OACT
'
,
'
DSPT
'
):
for
cat
in
(
"
REG
"
,
"
DEP
"
,
"
COM
"
,
"
EPCI
"
,
"
AGCT
"
,
"
OACT
"
,
"
DSPT
"
):
geojson_filepath
=
args
.
output_dir
/
'
odservatoire_{}.json
'
.
format
(
cat
.
lower
())
geojson_filepath
=
args
.
output_dir
/
"
odservatoire_{}.json
"
.
format
(
cat
.
lower
())
generate_geojson_file
(
cat
,
conn
,
geojson_filepath
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
main
()
Write
Preview
Supports
Markdown
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