initial commit

This commit is contained in:
valentineautos
2025-12-05 09:55:19 +00:00
parent 1279a6b24b
commit a0ff39772d

51
osm_to_geojson.py Normal file
View File

@@ -0,0 +1,51 @@
import osmium
import json
class FuelHandler(osmium.SimpleHandler):
def __init__(self):
super().__init__()
self.features = []
self.count = 0
def add_feature(self, obj, lat, lon):
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [lon, lat]
},
"properties": {
"id": obj.id,
"name": obj.tags.get("name", "")
}
}
self.features.append(feature)
self.count += 1
def node(self, n):
if n.tags.get("amenity") == "fuel" and n.location.valid():
self.add_feature(n, n.location.lat, n.location.lon)
def way(self, w):
if w.tags.get("amenity") == "fuel" and w.nodes:
lats = [n.lat for n in w.nodes if n.location.valid()]
lons = [n.lon for n in w.nodes if n.location.valid()]
if lats and lons:
lat = sum(lats) / len(lats)
lon = sum(lons) / len(lons)
self.add_feature(w, lat, lon)
# Run extraction
handler = FuelHandler()
handler.apply_file("uk-latest.osm.pbf", locations=True, idx="flex_mem")
# Create final GeoJSON FeatureCollection
geojson = {
"type": "FeatureCollection",
"features": handler.features
}
with open("fuel_all.geojson", "w", encoding="utf-8") as f:
json.dump(geojson, f)
print(f"Extracted {handler.count} fuel stations into fuel_all.geojson")