Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Mayer
CircularGreenSimCity
Commits
b10a37bd
Commit
b10a37bd
authored
May 06, 2024
by
Eric Duminil
Browse files
Trying to recognize standard widths
parent
1711eae1
Changes
1
Show whitespace changes
Inline
Side-by-side
python_scripts/add_trees_to_open_street_map/add_trees.py
View file @
b10a37bd
...
...
@@ -8,7 +8,7 @@ Trees are exported in a CSV table, a PNG diagram and an HTML interactive map.
"""
import
pickle
from
pathlib
import
Path
from
collections
import
namedtuple
from
collections
import
namedtuple
,
Counter
import
folium
import
matplotlib.pyplot
as
plt
...
...
@@ -42,8 +42,7 @@ MIN_DISTANCE = TREE_DISTANCE * 0.5 # [m]
# For display purposes only:
GRID
=
100
# [m]
IGNORE_ROADS
=
set
([
'primary'
,
'unclassified'
,
'secondary'
,
'secondary_link'
,
'trunk'
,
'trunk_link'
,
'primary_link'
])
DEFAULT_WIDTHS
=
{
'unclassified'
:
0
}
SCRIPT_DIR
=
Path
(
__file__
).
resolve
().
parent
...
...
@@ -88,7 +87,7 @@ def get_osm_roads(bounds: Bounds):
return
ways
def
set_plot
(
bounds
,
to_local_coordinates
):
def
set_plot
(
bounds
:
Bounds
,
to_local_coordinates
):
x_min
,
y_min
=
to_local_coordinates
.
transform
(
bounds
.
W
,
bounds
.
S
)
x_max
,
y_max
=
to_local_coordinates
.
transform
(
bounds
.
E
,
bounds
.
N
)
ax
=
plt
.
axes
()
...
...
@@ -103,15 +102,40 @@ def set_plot(bounds, to_local_coordinates):
return
ax
def
get_default_widths
(
ways
:
list
)
->
dict
[
str
,
float
]
:
"""Check existing OSM highways, and extract the most common width for each type"""
width_counters
=
{}
for
way
in
ways
:
width
=
float
(
way
.
tags
.
get
(
"width"
,
0
))
way_type
=
way
.
tags
.
get
(
"highway"
)
print
(
way_type
,
width
)
if
width
:
if
way_type
not
in
width_counters
:
width_counters
[
way_type
]
=
Counter
()
width_counters
[
way_type
][
width
]
+=
1
return
{
w
:
c
.
most_common
(
1
)[
0
][
0
]
for
w
,
c
in
width_counters
.
items
()}
def
place_trees
(
forest
:
Forest
,
ways
:
list
,
region
:
str
,
to_local
,
tree_distance
:
float
,
min_distance_2
:
float
)
->
Forest
:
local_region
=
transform
(
to_local
.
transform
,
region
)
default_widths
=
{
**
get_default_widths
(
ways
),
**
DEFAULT_WIDTHS
}
print
(
f
"Default widths:
{
default_widths
}
"
)
for
way
in
ways
:
way_type
=
way
.
tags
.
get
(
"highway"
)
width
=
float
(
way
.
tags
.
get
(
"width"
,
0
))
highway
=
way
.
tags
.
get
(
"highway"
)
if
highway
in
IGNORE_ROADS
:
if
width
:
# Defined in OSM
color
=
'blue'
else
:
width
=
default_widths
.
get
(
way_type
,
0
)
if
width
:
# From OSM most common width
color
=
'orange'
else
:
# Unknown
color
=
'gray'
road_xy_s
=
[
to_local
.
transform
(
node
.
lon
,
node
.
lat
)
for
node
in
way
.
nodes
]
...
...
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