from owslib.wms import WebMapService
from folium import Map, raster_layers, LayerControl

# Prepare the Web Map Service
wms = WebMapService('https://dienste.gdi-sh.de/WMS_SH_ALKIS_VWG_OpenGBD', version='1.3.0')

# Print information about the Web Map Service
print(f"WMS version: {wms.identification.version}")
print(f"WMS title: {wms.identification.title}")
print(f"Provider name: {wms.provider.name}")

# Get the Layers offered by the Web Map Service
layers = list(wms.contents.keys())
print("Layers: ", layers)

# Get the bounding box of the map of the first layer
bbox = wms.contents[layers[0]].boundingBox
# Calculate the center of the bounding box
center= ( bbox[1]+((bbox[3]-bbox[1]) / 2), bbox[0]+((bbox[2]-bbox[0]) / 2))

# Create the underlying Map
m = Map( location=center, zoom_start=9, tiles="cartodb positron")

# Add the first layer to the map (lines of Gemeindegrenze)
raster_layers.WmsTileLayer(
    url='https://dienste.gdi-sh.de/WMS_SH_ALKIS_VWG_OpenGBD',
    layers='gmd',
    fmt='image/png',
    transparent=True,
    name='Gemeindegrenze',
    control=True,
    overlay=True,
    show=True
).add_to(m)

# Add the second layer to the map (lines of Landkreisgrenze)
raster_layers.WmsTileLayer(
    url='https://dienste.gdi-sh.de/WMS_SH_ALKIS_VWG_OpenGBD',
    layers='lk',
    fmt='image/png',
    transparent=True,
    name='Landkreisgrenze',
    control=True,
    overlay=True,
    show=True
).add_to(m)

# Add a Control Panel to the top right to modify the shown Layers
LayerControl().add_to(m)

m.save("output.html")