image

Tutorial: Geo-Funktionen in Jupyter Notebook¶

Hier sind wichtige Geo-Funktionen in Jupyter Notebook, wie Kartenfenster mit Leafmap erstellen oder WMS- bzw. WFS-Dienste integrieren, zusammengetragen. Bevor wir mit den Geo-Funktionen in Jupyter Notebook beginnen, müssen wir sicherstellen, dass die erforderlichen Pakete installiert sind. Dazu gehören Leafmap, Geopandas, owslib und weitere, je nach den spezifischen Funktionen, die wir verwenden möchten.

Installation Leafmap, Geopandas, etc.¶

In [ ]:
#!pip install leafmap geopandas owslib
In [ ]:
import leafmap
import geopandas as gpd
import owslib

Erstelle ein Kartenfenster mit Leafmap¶

Kartenfenster zentriert auf Deutschland¶

In [ ]:
# Kartenfenster zentriert auf Deutschland
m = leafmap.Map(center=(51.1657, 10.4515), zoom=6)
m

Anpassung der Höhe und Breite¶

In [ ]:
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m

Anpassung der Kontrollfelder¶

In [ ]:
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6,
    draw_control=False,
    measure_control=False,
    fullscreen_control=False,
    attribution_control=True,
)
m

Anpassung der Hintergrundkarte¶

In [ ]:
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m.add_basemap("OpenTopoMap")
m

Einbinden WMS¶

In [ ]:
# Einbinden des DGM200 Deutschland Relief als WMS
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
wms_url = 'https://sgx.geodatenzentrum.de/wms_dgm200?'
m.add_wms_layer(
    url=wms_url,
    layers='relief',
    name='DGM200_Relief',
    attribution='© Bundesamt für Kartographie und Geodäsie 2024', # Quellenvermerk
    format='image/png',
    shown=True,
)
m

Anzeigen Layerauswahl¶

In [ ]:
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
wms_url = 'https://sgx.geodatenzentrum.de/wms_dgm200?'
m.add_wms_layer(
    url=wms_url,
    layers='relief',
    name='DGM200_Relief',
    attribution='© Bundesamt für Kartographie und Geodäsie 2024',
    format='image/png',
    shown=True,
)
# Anzeige Layerauswahl
m.add_layer_control()
m

Einbinden WFS¶

In [ ]:
# Installation requests
!pip install requests
In [ ]:
import requests
import leafmap

# URL des WebFeatureService zu Bundeslandgrenzen in Deutschland
wfs_url = "https://sgx.geodatenzentrum.de/wfs_vg250?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=vg250:vg250_lan&OUTPUTFORMAT=json&SRSNAME=urn:ogc:def:crs:EPSG::4326"

# Herunterladen der Daten in JSON
response = requests.get(wfs_url)
geojson_data = response.json()

# Visualisieren der Daten mit leafmap
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6,
                draw_control=False,
                measure_control=False,
                fullscreen_control=False,
                attribution_control=True)
m.add_geojson(geojson_data, layer_name="vg250_lan")
m.add_layer_control()
m
In [ ]:
# Visualisieren der Daten mit style
style = {
    "stroke": True,
    "color": "grey",
    "weight": 2,
    "opacity": 1,
    "fill": True,
    "fillColor": "grey",
    "fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}

# Ausgabe in Karte
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6,
    draw_control=False,
    measure_control=False,
    fullscreen_control=False,
    attribution_control=True,)
m.add_geojson(geojson_data, layer_name="vg250_lan", style=style, hover_style=hover_style,
)
m.add_layer_control()
m

Analyse des WFS mit owslib¶

In [ ]:
from owslib.wfs import WebFeatureService
wfs_url = 'https://sgx.geodatenzentrum.de/wfs_vg250?request=GetCapabilities&service=WFS'
wfs = WebFeatureService(wfs_url, version='2.0.0')
In [ ]:
print(f'WFS title: {wfs.identification.title}')
print(f'WFS abstract: {wfs.identification.abstract}')
print(f'Provider name: {wfs.provider.name}')
print(f'Provider address: {wfs.provider.contact.address}')
In [ ]:
list(wfs.contents)

Einbinden KML¶

In [ ]:
# Installation fiona
!pip install fiona
In [ ]:
import leafmap 
import fiona
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
in_kml = 'https://geodaten.bayern.de/odd/a/lod2/citygml/meta/kml/gemeinde.kml'
m.add_kml(in_kml, layer_name="BY_LOD2_KML")
m

Einbinden Shapefile¶

In [ ]:
import leafmap
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
in_shp = 'https://www.radroutenplaner-deutschland.de/downloads/Shape/Radnetz_Deutschland_Shape.zip'
m.add_shp(in_shp, layer_name="DE_Radnetz_Shape")
m

Analyse Shapefile als GeoDataFrame¶

In [ ]:
# setze GeoDataFrame
gdf = gpd.read_file(in_shp, zip='zip')
# Ausgabe GeoDataFrame
gdf
In [ ]:
# Zeige die ersten Zeilen des Geodataframes an
print(gdf.head())
In [ ]:
# Zeige Informationen zum Geodataframe an
print(gdf.info())

Einbinden GPX¶

In [ ]:
import requests
import geopandas as gpd
import leafmap

# GPX-Datei herunterladen
url = 'https://www.radroutenplaner-deutschland.de/downloads/GPX/Radnetz_Deutschland_D08.gpx'
r = requests.get(url)
with open('Radnetz_Deutschland_D08.gpx', 'wb') as file:
    file.write(r.content)

# GPX-Datei in ein GeoDataFrame konvertieren
gdf = gpd.read_file('Radnetz_Deutschland_D08.gpx', layer='tracks')

# Leafmap-Karte erstellen
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)

# GeoDataFrame zur Karte hinzufügen
m.add_gdf(gdf, layer_name="Radnetz Deutschland D08")
m

Speichern als GeoJSON¶

In [ ]:
# GeoDataFrame als GeoJSON speichern
gdf.to_file('Radnetz_Deutschland_D08.geojson', driver='GeoJSON')

Hinweis: Dateien lassen sich in Leafmap auch direkt über das Ordner-Symbol in der Toolbar einbinden. Damit ist eine einfache Überprüfung möglich, ob die GeoJSON-Datei korrekt gespeichert wurde und im Kartenfenster angezeigt werden kann.

In [ ]:
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m
In [ ]: