File size: 7,527 Bytes
124ba77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
import logging
import re
from typing import List
from .reader import OSMData, OSMElement, OSMNode, OSMWay
IGNORE_TAGS = {"source", "phone", "entrance", "inscription", "note", "name"}
def parse_levels(string: str) -> List[float]:
"""Parse string representation of level sequence value."""
try:
cleaned = string.replace(",", ";").replace(" ", "")
return list(map(float, cleaned.split(";")))
except ValueError:
logging.debug("Cannot parse level description from `%s`.", string)
return []
def filter_level(elem: OSMElement):
level = elem.tags.get("level")
if level is not None:
levels = parse_levels(level)
# In the US, ground floor levels are sometimes marked as level=1
# so let's be conservative and include it.
if not (0 in levels or 1 in levels):
return False
layer = elem.tags.get("layer")
if layer is not None:
layer = parse_levels(layer)
if len(layer) > 0 and max(layer) < 0:
return False
return (
elem.tags.get("location") != "underground"
and elem.tags.get("parking") != "underground"
)
def filter_node(node: OSMNode):
return len(node.tags.keys() - IGNORE_TAGS) > 0 and filter_level(node)
def is_area(way: OSMWay):
if way.nodes[0] != way.nodes[-1]:
return False
if way.tags.get("area") == "no":
return False
filters = [
"area",
"building",
"amenity",
"indoor",
"landuse",
"landcover",
"leisure",
"public_transport",
"shop",
]
for f in filters:
if f in way.tags and way.tags.get(f) != "no":
return True
if way.tags.get("natural") in {"wood", "grassland", "water"}:
return True
return False
def filter_area(way: OSMWay):
return len(way.tags.keys() - IGNORE_TAGS) > 0 and is_area(way) and filter_level(way)
def filter_way(way: OSMWay):
return not filter_area(way) and way.tags != {} and filter_level(way)
def parse_node(tags):
keys = tags.keys()
for key in [
"amenity",
"natural",
"highway",
"barrier",
"shop",
"tourism",
"public_transport",
"emergency",
"man_made",
]:
if key in keys:
if "disused" in tags[key]:
continue
return f"{key}:{tags[key]}"
return None
def parse_area(tags):
if "building" in tags:
group = "building"
kind = tags["building"]
if kind == "yes":
for key in ["amenity", "tourism"]:
if key in tags:
kind = tags[key]
break
if kind != "yes":
group += f":{kind}"
return group
if "area:highway" in tags:
return f'highway:{tags["area:highway"]}'
for key in [
"amenity",
"landcover",
"leisure",
"shop",
"highway",
"tourism",
"natural",
"waterway",
"landuse",
]:
if key in tags:
return f"{key}:{tags[key]}"
return None
def parse_way(tags):
keys = tags.keys()
for key in ["highway", "barrier", "natural"]:
if key in keys:
return f"{key}:{tags[key]}"
return None
def match_to_group(label, patterns):
for group, pattern in patterns.items():
if re.match(pattern, label):
return group
return None
class Patterns:
areas = dict(
building="building($|:.*?)*",
parking="amenity:parking",
playground="leisure:(playground|pitch)",
grass="(landuse:grass|landcover:grass|landuse:meadow|landuse:flowerbed|natural:grassland)",
park="leisure:(park|garden|dog_park)",
forest="(landuse:forest|natural:wood)",
water="(natural:water|waterway:*)",
)
# + ways: road, path
# + node: fountain, bicycle_parking
ways = dict(
fence="barrier:(fence|yes)",
wall="barrier:(wall|retaining_wall)",
hedge="barrier:hedge",
kerb="barrier:kerb",
building_outline="building($|:.*?)*",
cycleway="highway:cycleway",
path="highway:(pedestrian|footway|steps|path|corridor)",
road="highway:(motorway|trunk|primary|secondary|tertiary|service|construction|track|unclassified|residential|.*_link)",
busway="highway:busway",
tree_row="natural:tree_row", # maybe merge with node?
)
# + nodes: bollard
nodes = dict(
tree="natural:tree",
stone="(natural:stone|barrier:block)",
crossing="highway:crossing",
lamp="highway:street_lamp",
traffic_signal="highway:traffic_signals",
bus_stop="highway:bus_stop",
stop_sign="highway:stop",
junction="highway:motorway_junction",
bus_stop_position="public_transport:stop_position",
gate="barrier:(gate|lift_gate|swing_gate|cycle_barrier)",
bollard="barrier:bollard",
shop="(shop.*?|amenity:(bank|post_office))",
restaurant="amenity:(restaurant|fast_food)",
bar="amenity:(cafe|bar|pub|biergarten)",
pharmacy="amenity:pharmacy",
fuel="amenity:fuel",
bicycle_parking="amenity:(bicycle_parking|bicycle_rental)",
charging_station="amenity:charging_station",
parking_entrance="amenity:parking_entrance",
atm="amenity:atm",
toilets="amenity:toilets",
vending_machine="amenity:vending_machine",
fountain="amenity:fountain",
waste_basket="amenity:(waste_basket|waste_disposal)",
bench="amenity:bench",
post_box="amenity:post_box",
artwork="tourism:artwork",
recycling="amenity:recycling",
give_way="highway:give_way",
clock="amenity:clock",
fire_hydrant="emergency:fire_hydrant",
pole="man_made:(flagpole|utility_pole)",
street_cabinet="man_made:street_cabinet",
)
# + ways: kerb
class Groups:
areas = list(Patterns.areas)
ways = list(Patterns.ways)
nodes = list(Patterns.nodes)
def group_elements(osm: OSMData):
elem2group = {
"area": {},
"way": {},
"node": {},
}
for node in filter(filter_node, osm.nodes.values()):
label = parse_node(node.tags)
if label is None:
continue
group = match_to_group(label, Patterns.nodes)
if group is None:
group = match_to_group(label, Patterns.ways)
if group is None:
continue # missing
elem2group["node"][node.id_] = group
for way in filter(filter_way, osm.ways.values()):
label = parse_way(way.tags)
if label is None:
continue
group = match_to_group(label, Patterns.ways)
if group is None:
group = match_to_group(label, Patterns.nodes)
if group is None:
continue # missing
elem2group["way"][way.id_] = group
for area in filter(filter_area, osm.ways.values()):
label = parse_area(area.tags)
if label is None:
continue
group = match_to_group(label, Patterns.areas)
if group is None:
group = match_to_group(label, Patterns.ways)
if group is None:
group = match_to_group(label, Patterns.nodes)
if group is None:
continue # missing
elem2group["area"][area.id_] = group
return elem2group
|