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.
#!pip install leafmap geopandas owslib
import leafmap
import geopandas as gpd
import owslib
# Kartenfenster mit Leafmap erstellen
m = leafmap.Map()
m
# Kartenfenster zentriert auf Deutschland
m = leafmap.Map(center=(51.1657, 10.4515), zoom=6)
m
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m
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
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m.add_basemap("OpenTopoMap")
m
# 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
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
# Installation requests
!pip install requests
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()
# CRS-Information hinzufügen
geojson_data['crs'] = {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::4326"
}
}
# 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
# 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
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')
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}')
list(wfs.contents)
import leafmap
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
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
# setze GeoDataFrame
gdf = gpd.read_file(in_shp, zip='zip')
# Ausgabe GeoDataFrame
gdf
# Zeige die ersten Zeilen des Geodataframes an
print(gdf.head())
# Zeige Informationen zum Geodataframe an
print(gdf.info())
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
# 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.
m = leafmap.Map(height="700px", width="600px", center=(51.1657, 10.4515), zoom=6)
m