-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosm_diff.py
More file actions
executable file
·248 lines (222 loc) · 7.05 KB
/
Copy pathosm_diff.py
File metadata and controls
executable file
·248 lines (222 loc) · 7.05 KB
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
#!/usr/bin/env -S uv run python
from pathlib import Path
import h3
import httpx
from funcy import log_durations
from slugify import slugify
from tqdm import tqdm
import geojson
dataDirectory = Path("rowery_wawa")
shapefilePath = dataDirectory / "Warszawa.shp"
outputDirectory = Path("osm_diffs")
OVERPASS_URL = "https://overpass-api.de/api/interpreter" # "http://localhost:12345/api/interpreter"
MISSING_COUNT_THRESHOLD = 10
MISSING_COUNT_PERCENTAGE_THRESHOLD = 0.2
H3_RESOLUTION = 12
OSM_NEIGHBOURHOOD_SIZE = 1
log_duration = log_durations(lambda msg: print("⌛ " + msg))
def openDataGeojson():
with Path("geojson/latest.geojson").open() as f:
return geojson.load(f)
def getOSMDataFromOverpass():
bbox = "(51.8,20.3,52.7,21.7)"
query = f"""
[out:json][timeout:25];
(
way["cycleway"~"(lane|track)"]{bbox};
way[bicycle=designated]{bbox};
way["oneway:bicycle"=no]{bbox};
way[highway=cycleway]{bbox};
way[~"cycleway:(both|left|right)"~"lane"]{bbox};
);
convert item ::=::,::geom=geom(),_osm_type=type();
out geom;
"""
with log_duration("download data from Overpass"):
response = httpx.post(
OVERPASS_URL,
data=dict(data=query),
timeout=30.0,
headers={
"User-Agent": "https://github.com/openstreetmap-polska/Warszawskie_dane_rowerowe"
},
)
response.raise_for_status()
with log_duration("parsing Overpass response"):
return geojson.loads(response.text)["elements"]
def h3LineLatLng(start: tuple[float, float], end: tuple[float, float]) -> set[str]:
startH3 = h3.latlng_to_cell(start[1], start[0], H3_RESOLUTION)
endH3 = h3.latlng_to_cell(end[1], end[0], H3_RESOLUTION)
if startH3 == endH3 or h3.grid_distance(startH3, endH3) == 1:
return {startH3, endH3}
middle = ((start[0] + end[0]) / 2, (start[1] + end[1]) / 2)
return h3LineLatLng(start, middle) | h3LineLatLng(middle, end)
def processLineIntoH3Set(
line: list[tuple[float, float]], result: set[str], neighbourhood_size: int = 0
) -> set[str]:
for pointA, pointB in zip(line[:-1], line[1:], strict=False):
for point in h3LineLatLng(pointA, pointB):
result.update(h3.grid_ring(point, neighbourhood_size))
return result
@log_duration
def processOSMDataIntoH3Set(osmData) -> set[str]:
result = set()
for element in osmData:
if element["geometry"]["type"] != "LineString":
print(f"Unsupported geometry type {element['geometry']['type']}")
continue
coords = element["geometry"]["coordinates"]
result = processLineIntoH3Set(
coords, result, neighbourhood_size=OSM_NEIGHBOURHOOD_SIZE
)
return result
def outputMissingFeaturesGeojson(name: str, missingFeatures):
nameSlugified = slugify(name, lowercase=False)
outputFile = outputDirectory / (nameSlugified + ".geojson")
if len(missingFeatures) == 0:
if outputFile.exists():
outputFile.unlink()
return
with outputFile.open("w") as f:
geojson.dump(
geojson.FeatureCollection(missingFeatures), fp=f, separators=(",", ":")
)
def processDistrict(district, districtFeatures, osmH3Set: set[str]):
missingFeatures = []
for feature in districtFeatures:
featureH3Set = set()
if feature["geometry"]["type"] == "LineString":
featureH3Set = processLineIntoH3Set(
feature["geometry"]["coordinates"], featureH3Set
)
elif feature["geometry"]["type"] == "MultiLineString":
for line in feature["geometry"]["coordinates"]:
featureH3Set = processLineIntoH3Set(line, featureH3Set)
else:
print(f"Unsupported geometry type {feature['geometry']['type']}")
continue
count = len(featureH3Set)
missing = featureH3Set - osmH3Set
missingCount = len(missing)
if (
missingCount >= MISSING_COUNT_THRESHOLD
or missingCount / count > MISSING_COUNT_PERCENTAGE_THRESHOLD
):
# print(count, missingCount, feature["properties"]["LOKALIZ"])
missingFeatures.append(feature)
outputMissingFeaturesGeojson(district, missingFeatures)
@log_duration
def generateOSMDiff(warsawData, osmH3Set: set[str]):
warsawDistricts = [
"Bemowo",
"Białołęka",
"Bielany",
"Mokotów",
"Ochota",
"Praga-Północ",
"Praga-Południe",
"Rembertów",
"Śródmieście",
"Targówek",
"Ursus",
"Ursynów",
"Wawer",
"Wesoła",
"Wilanów",
"Włochy",
"Wola",
"Żoliborz",
]
supportedTowns = [
# keep-sorted start
"Baranów",
"Brwinów",
"Błonie",
"Cegłów",
"Celestynów",
"Czosnów",
"Dobre",
"Dębe Wielkie",
"Grodzisk Mazowiecki",
"Grójec",
"Góra Kalwaria",
"Halinów",
"Izabelin",
"Jabłonna",
"Jadów",
"Jaktorów",
"Jakubów",
"Józefów",
"Kampinos",
"Karczew",
"Kałuszyn",
"Klembów",
"Kobyłka",
"Konstancin-Jeziorna",
"Kołbiel",
"Legionowo",
"Leszno",
"Lesznowola",
"Marki",
"Michałowice",
"Mińsk Mazowiecki",
"Mrozy",
"Nadarzyn",
"Nasielsk",
"Nieporęt",
"Nowy Dwór Mazowiecki",
"Ostrówek",
"Otwock",
"Ożarów Mazowiecki",
"Piaseczno",
"Piastów",
"Podkowa Leśna",
"Pogorzel",
"Pomiechówek",
"Prażmów",
"Pruszków",
"Radziejowice",
"Radzymin",
"Raszyn",
"Serock",
"Stare Babice",
"Sulejówek",
"Tarczyn",
"Tłuszcz",
"Wieliszew",
"Wiązowna",
"Wołomin",
"Zakroczym",
"Zielonka",
"Ząbki",
"Łomianki",
"Żabia Wola",
# keep-sorted end
]
areasAnalyzed = warsawDistricts + supportedTowns
allAreas = {
feature["properties"]["DZIELNICA"] for feature in warsawData["features"]
}
print(f"Skipping: {allAreas - set(areasAnalyzed)}")
analyzedFeatures = [
feature
for feature in warsawData["features"]
if feature["properties"]["BUDOWA"] != "tak"
and feature["properties"]["TYP_TRASY"] != "inny"
and feature["properties"]["DZIELNICA"] in areasAnalyzed
]
for areaName in tqdm(areasAnalyzed):
districtFeatures = [
feature
for feature in analyzedFeatures
if feature["properties"]["DZIELNICA"] == areaName
]
processDistrict(areaName, districtFeatures, osmH3Set)
def main():
outputDirectory.mkdir(exist_ok=True)
warsawData = openDataGeojson()
osmData = getOSMDataFromOverpass()
h3Set = processOSMDataIntoH3Set(osmData)
generateOSMDiff(warsawData, h3Set)
if __name__ == "__main__":
main()