First commit
This commit is contained in:
42
scripts/places/places-geojson-generate.py
Normal file
42
scripts/places/places-geojson-generate.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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.")
|
||||
|
||||
Reference in New Issue
Block a user