import geopandas as gpd
import pandas as pd
import folium
import zipfile
import os

# Unpack ZIP file containing geodata to subfolder
zip_file_path = 'parkbench.zip'
output_dir = 'shapefile_extracted'

with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    zip_ref.extractall(output_dir)

# Locate shapefile in subfolder (suffix SHP) and read it
shp_file = [f for f in os.listdir(output_dir) if f.endswith('.shp')][0]
shp_file_path = os.path.join(output_dir, shp_file)
gdf = gpd.read_file(shp_file_path)

# The folium library can only work on WGS84 (EPSG:4326) coordinates.
# This is not provided by the shape file, but geopands can convert this
# on the fly using the to_crs() method:
print("Shapefile coordinate system before conversion:", gdf.crs)
gdf = gdf.to_crs(epsg=4326)
print("Shapefile coordinate system before conversion:", gdf.crs)

# Show all park bench data in terminal as fromatted table
# If you use the default pandas output settings will only show the first five lines
# and it will remove some data for convenience. We don't want this so we disable some
# of pandas' display settings beforehand: 
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('display.max_rows', None)
print(gdf)

# Visualize park benches on a map. Use center of data in shape file as center point
center_lat = gdf.geometry.y.mean()
center_lon = gdf.geometry.x.mean()

# Create OpenStreetMap overlay using folium library
m = folium.Map(location=[center_lat, center_lon], zoom_start=6)

# Add each park bench to overlay
for idx, row in gdf.iterrows():
    lat = row.geometry.y
    lon = row.geometry.x
    ort = row.ort
    folium.Marker([lat, lon], popup=f'{ort}').add_to(m)

# Save finished overlay to map
m.save('output.htm')
print("Overlay has been saved to output.htm")