Created
August 18, 2023 15:02
-
-
Save maxcollombin/67480ba865df47d99c5cfad48fde858d to your computer and use it in GitHub Desktop.
Script to get the bounding box of all the layers of WMTS and WMS services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
wms_wmts_swisstopo.py | |
Script to get the bounding box of all the layers of WMTS and WMS services. | |
Author: [Maxime Collombin] | |
Date: [14/08/2023] | |
""" | |
# -*- coding: utf-8 -*- | |
from owslib.wmts import WebMapTileService | |
# from owslib.wms import WebMapService | |
from shapely.geometry import Polygon | |
import pandas as pd | |
import geopandas as gpd | |
# Replace with the URL of the WMTS or WMS service you want to query | |
wmts_url = "https://wmts.geo.admin.ch/EPSG/2056/1.0.0/WMTSCapabilities.xml?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetCapabilities" | |
# wms_url = "https://wms.geo.admin.ch/?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities" | |
# Connect to the WMTS service | |
wmts = WebMapTileService(wmts_url) | |
# wms = WebMapService(wms_url) | |
# Get a list of all available layers | |
layers = list(wmts.contents) | |
# layers = list(wms.contents) | |
# Print the count and names of the layers | |
# print(f"Total number of layers: {len(layers)}") | |
# print("Layer names:") | |
# for layer_name in layers: | |
# print(layer_name) | |
for layer_name in layers: | |
layer = wmts.contents[layer_name] | |
# layer = wms.contents[layer_name] | |
bbox_wgs84 = layer.boundingBoxWGS84 | |
# print the wmts topleft corner | |
# print(str(bbox_wgs84[0]) + ',' + str(bbox_wgs84[3])) | |
# df = pd.DataFrame(columns=['layer_name', 'bbox_wgs84']) | |
# for layer_name in layers: | |
# # layer = wmts.contents[layer_name] | |
# layer = wms.contents[layer_name] | |
# bbox_wgs84 = layer.boundingBoxWGS84 | |
# df = df.append({'layer_name': layer_name, 'bbox_wgs84': bbox_wgs84}, ignore_index=True) | |
# get the unique values of bbox_wgs84 column | |
# unique_bbox = df['bbox_wgs84'].unique() | |
# print(df['bbox_wgs84'][0][0]) | |
# create a Polygon from the bbox_wgs84 column | |
# df['geometry'] = df['bbox_wgs84'].apply(lambda x: Polygon([(x[0], x[1]), (x[2], x[1]), (x[2], x[3]), (x[0], x[3])])) | |
# search for the layer with the name 'ch.swisstopo.pixelkarte-farbe' | |
# df[df['layer_name'] == 'ch.swisstopo.pixelkarte-farbe'] | |
# create a GeoDataFrame from the DataFrame | |
# gdf = gpd.GeoDataFrame(df, geometry='geometry') | |
# save the GeoDataFrame to a GeoJSON file | |
# gdf.to_file('wms_swisstopo.geojson', driver='GeoJSON') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment