Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CircularGreenSimCity
CircularGreenSimCity
Commits
97ec5d80
Commit
97ec5d80
authored
Nov 20, 2024
by
Eric Duminil
Browse files
OSM script as python
parent
49ec3120
Changes
1
Hide whitespace changes
Inline
Side-by-side
python_scripts/add_trees_to_open_street_map/export_OSM_roads_to_csv.py
0 → 100644
View file @
97ec5d80
from
pathlib
import
Path
import
pickle
import
pandas
as
pd
from
pyproj
import
Transformer
from
shapely
import
LineString
EPSG_ID
=
25832
to_local
=
Transformer
.
from_crs
(
"EPSG:4326"
,
f
"EPSG:
{
EPSG_ID
}
"
,
always_xy
=
True
)
def
load_cache
(
cache_path
):
with
open
(
Path
(
'cache'
)
/
'49_802698__49_804055__9_942167__9_945563.pickle'
,
'rb'
)
as
f
:
return
pickle
.
load
(
f
)
def
get_way_dict
(
way
):
url
=
f
'https://www.openstreetmap.org/way/
{
way
.
id
}
'
xy_s
=
[
to_local
.
transform
(
node
.
lon
,
node
.
lat
)
for
node
in
way
.
nodes
]
path
=
LineString
(
xy_s
)
way_info
=
{
'id'
:
way
.
id
,
'URL'
:
f
'=HYPERLINK("
{
url
}
"; "
{
url
}
")'
,
'length [m]'
:
round
(
path
.
length
,
1
)}
return
way_info
|
way
.
tags
NUMERICAL_COLUMNS
=
[
'maxspeed [km/h]'
,
'width [m]'
,
'length [m]'
]
def
create_table
(
ways
):
df
=
pd
.
DataFrame
([
get_way_dict
(
way
)
for
way
in
ways
]).
set_index
(
'id'
)
df
=
df
.
rename
(
columns
=
{
'width'
:
'width [m]'
,
'maxspeed'
:
'maxspeed [km/h]'
})
for
col
in
NUMERICAL_COLUMNS
:
df
[
col
]
=
pd
.
to_numeric
(
df
[
col
])
return
df
for
cache_path
in
Path
(
'cache'
).
glob
(
'*.pickle'
):
print
(
f
"Parsing
{
cache_path
}
"
)
df
=
create_table
(
load_cache
(
cache_path
))
csv_output
=
Path
(
'output'
)
/
f
'OSM_infos_
{
cache_path
.
stem
}
.csv'
print
(
f
" Exporting to
{
csv_output
}
"
)
df
.
to_csv
(
csv_output
,
sep
=
';'
,
decimal
=
','
,
encoding
=
'utf-8-sig'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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