import rasterio
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt

# --- CONFIG ---
raster_path = 'geodata/s2_2025.tif'
geojson_path = 'geodata/Trinkbrunnen_Berlin.geojson'

# --- LOAD RASTER ---
with rasterio.open(raster_path) as src:
    map_img = src.read([1, 2, 3])
    map_img = np.transpose(map_img, (1, 2, 0))
    map_transform = src.transform
    map_crs = src.crs
    map_bounds = src.bounds
    map_width = src.width
    map_height = src.height

# --- LOAD GEOJSON ---
gdf = gpd.read_file(geojson_path)
if gdf.crs != map_crs:
    gdf = gdf.to_crs(map_crs)

# --- PROJECT POINTS TO PIXELS ---
pixel_positions = []
for geom in gdf.geometry:
    if geom and geom.type == 'Point':
        x, y = geom.x, geom.y
        px, py = ~map_transform * (x, y)
        pixel_positions.append((int(px), int(py)))

# --- VISUALIZE ---

plt.figure(figsize=(10, 10))
plt.imshow(map_img)
for px, py in pixel_positions:
    plt.scatter(px, py, c='deepskyblue', s=40, edgecolors='black', label='Trinkbrunnen')
plt.title('Trinkbrunnen auf der Berlin-Karte')
plt.axis('off')
plt.savefig('datenvisualisierung/trinkbrunnen_karte.png', bbox_inches='tight', dpi=200)
plt.show()

# --- EXPORT PIXEL POSITIONS FOR GAME ---
# Save as npy for easy loading in pygame
np.save('trinkbrunnen_pixel_positions.npy', np.array(pixel_positions))

print(f"{len(pixel_positions)} Trinkbrunnen-Pixelpositionen wurden in trinkbrunnen_pixel_positions.npy gespeichert.")
