import csv
import json
from collections import defaultdict

csv_file = "members.csv"
geojson_file = "data/pois.geojson"

GROUP_TOLERANCE = 0.01  # 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.")

