#!/usr/bin/env python3
"""
geopandasconvert_fixed.py
BoopLabs Pupmap Europe/Nordics GeoJSON generator
Version: 2025-10-03
"""

import geopandas as gpd

# ---------- User paths ----------
cities_shp = "ne_10m_populated_places.shp"
roads_shp  = "ne_10m_roads.shp"
output_dir = "/mnt/storage/docker-data/pupmap/www/data/sources/"

# ---------- Europe + Nordics filter ----------
europe_nordics = [
    'ALB','AND','AUT','BEL','BIH','BGR','HRV','CYP','CZE','DNK','EST','FIN','FRA','DEU',
    'GRC','HUN','ISL','IRL','ITA','LVA','LTU','LUX','MLT','MDA','MCO','MNE','NLD','NOR',
    'POL','PRT','ROU','RUS','SMR','SRB','SVK','SVN','ESP','SWE','CHE','UKR','GBR','VAT'
]

# ---------- Convert cities ----------
print("Processing cities...")

cities = gpd.read_file(cities_shp)

# Keep only Europe/Nordics
cities_filtered = cities[cities['ADM0_A3'].isin(europe_nordics)].copy()

# Ensure expected properties exist
cities_filtered = cities_filtered.rename(columns={
    'NAME': 'name',
    'ADM0NAME': 'country',
    'POP_MAX': 'population'
})

# Keep only required columns
cities_filtered = cities_filtered[['name','country','population','geometry']]

# Save to GeoJSON
cities_geojson_path = output_dir + "world_cities_europe.geojson"
cities_filtered.to_file(cities_geojson_path, driver='GeoJSON')
print(f"Saved {cities_geojson_path} ({len(cities_filtered)} features)")

# ---------- Convert roads ----------
print("Processing roads...")

roads = gpd.read_file(roads_shp)

# Keep only Europe/Nordics
roads_filtered = roads[roads['ADM0_A3'].isin(europe_nordics)].copy()

# Rename the main type column to 'type' (if needed)
if 'TYPE' in roads_filtered.columns:
    roads_filtered = roads_filtered.rename(columns={'TYPE': 'type'})
elif 'fclass' in roads_filtered.columns:
    roads_filtered = roads_filtered.rename(columns={'fclass': 'type'})
else:
    roads_filtered['type'] = 'road'  # fallback

# Keep only necessary columns
roads_filtered = roads_filtered[['type','geometry']]

# Remove duplicate column names (sometimes shapefiles have duplicates)
roads_filtered = roads_filtered.loc[:, ~roads_filtered.columns.duplicated()]

# Save to GeoJSON
roads_geojson_path = output_dir + "roads_europe.geojson"
roads_filtered.to_file(roads_geojson_path, driver='GeoJSON')
print(f"Saved {roads_geojson_path} ({len(roads_filtered)} features)")

