Files
pupmap/scripts/places/places-geojson-generate.py
2025-10-03 22:48:16 +01:00

43 lines
1.1 KiB
Python

import csv
import json
from collections import defaultdict
csv_file = "places.csv"
geojson_file = "data/places.geojson"
GROUP_TOLERANCE = 0.00001 # cluster distance in degrees
def round_coord(coord):
return round(coord / GROUP_TOLERANCE) * GROUP_TOLERANCE
clusters = defaultdict(list)
with open(csv_file, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
lat = float(row['latitude'])
lon = float(row['longitude'])
name = row['name']
key = (round_coord(lat), round_coord(lon))
clusters[key].append(name)
features = []
for (lat, lon), names in clusters.items():
features.append({
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [lon, lat] },
"properties": {
"names": names,
"count": len(names),
"popup": ", ".join(names)
}
})
geojson = { "type": "FeatureCollection", "features": features }
with open(geojson_file, "w", encoding='utf-8') as f:
json.dump(geojson, f, indent=2)
print(f"Generated {geojson_file} with {len(features)} clustered POIs.")