Skip to main content

swisstopo geospatial Python utilities

Project description

PyPI version fury.io Documentation Status CI/CD pre-commit.ci status codecov GitHub license

swisstopopy

swisstopo geospatial Python utilities.

Features

STAC API utilities

Easily filter swisstopo STAC collections based on geospatial extents, dates, file extensions or data resolutions:

import contextily as cx
import swisstopopy

region = "EPFL"
client = swisstopopy.SwissTopoClient(region)

alti3d_gdf = client.get_collection_gdf(
    swisstopopy.SWISSALTI3D_COLLECTION_ID,
)
ax = alti3d_gdf.plot(alpha=0.1)
cx.add_basemap(ax, crs=alti3d_gdf.crs)

tiles

Filter to get the latest data for each tile:

latest_alti3d_gdf = swisstopopy.get_latest(alti3d_gdf)
latest_alti3d_gdf.head()
id collection type stac_version bbox stac_extensions links geometry.type geometry.coordinates properties.datetime properties.created properties.updated geometry assets.checksum:multihash assets.created assets.eo:gsd assets.href assets.proj:epsg assets.type assets.updated
0 swissalti3d_2021_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2021-01-01 00:00:00+00:00 2021-09-02 16:46:12.695971+00:00 2025-01-18 02:03:21.314035+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 1220A1EB829DC0AEFA4B10F687F5C05FC2AA4F59F6B622... 2021-09-02T19:09:22.472399Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 application/x.ascii-xyz+zip 2025-01-18T00:05:10.539193Z
1 swissalti3d_2021_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2021-01-01 00:00:00+00:00 2021-09-02 16:46:12.695971+00:00 2025-01-18 02:03:21.314035+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 12203761E09265F46BC92A89AB60D7003202574ADBED5B... 2021-09-02T16:46:13.207732Z 2.0 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 application/x.ascii-xyz+zip 2025-01-17T23:06:08.845852Z
2 swissalti3d_2021_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2021-01-01 00:00:00+00:00 2021-09-02 16:46:12.695971+00:00 2025-01-18 02:03:21.314035+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 1220B9AD77D6DD070409D209F5ADF17EC7253FC3FE6CEE... 2021-09-03T00:35:29.891683Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-18T01:04:21.184877Z
3 swissalti3d_2021_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2021-01-01 00:00:00+00:00 2021-09-02 16:46:12.695971+00:00 2025-01-18 02:03:21.314035+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 122093E32E6D175B9F148409FCAA8708073303A8E94A8E... 2021-09-03T01:36:23.594881Z 2.0 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-18T02:03:21.314035Z
4 swissalti3d_2021_2532-1152 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5524006, 46.5158382, 6.5655778, 46.5249343] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5525481, 46.5158382], [6.5655778, 46.5159... 2021-01-01 00:00:00+00:00 2021-09-02 16:56:28.144164+00:00 2025-01-18 02:16:20.318007+00:00 POLYGON ((6.56558 46.51584, 6.56558 46.52493, ... 122020D17CB98AAE4FECDBC563D0673AF8797EFD2D74C6... 2021-09-02T20:24:35.219858Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 application/x.ascii-xyz+zip 2025-01-18T00:17:16.038110Z

or filter by other metadata attributes such as ground resolution and/or file extensions:

alti3d_gdf[
    (alti3d_gdf["assets.eo:gsd"] == 0.5)
    & alti3d_gdf["assets.href"].str.endswith(".tif")
]
id collection type stac_version bbox stac_extensions links geometry.type geometry.coordinates properties.datetime properties.created properties.updated geometry assets.checksum:multihash assets.created assets.eo:gsd assets.href assets.proj:epsg assets.type assets.updated
0 swissalti3d_2019_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2019-01-01 00:00:00+00:00 2021-02-10 10:47:06.111266+00:00 2025-01-16 14:51:17.195380+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 1220BEF35C33758E7EA4744487F4D8248AABFD50018615... 2021-02-10T10:47:06.715269Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-14T19:16:07.401260Z
4 swissalti3d_2019_2532-1152 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5524006, 46.5158382, 6.5655778, 46.5249343] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5525481, 46.5158382], [6.5655778, 46.5159... 2019-01-01 00:00:00+00:00 2021-02-10 10:49:41.964859+00:00 2025-01-16 15:06:19.535010+00:00 POLYGON ((6.56558 46.51584, 6.56558 46.52493, ... 12205790D9862A7BFB265E59B08814D8E44227441DD80B... 2021-02-10T10:49:42.615492Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-14T19:26:09.858728Z
8 swissalti3d_2019_2533-1152 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5654325, 46.5159392, 6.5786075, 46.5250338] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5655778, 46.5159392], [6.5786075, 46.5160... 2019-01-01 00:00:00+00:00 2021-02-10 10:49:43.859413+00:00 2025-01-16 15:06:20.632461+00:00 POLYGON ((6.57861 46.51594, 6.57861 46.52503, ... 1220788F6FB6067294E92CE78ACBC070B25CB9632D5910... 2021-02-10T10:49:44.535488Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-14T19:26:10.809428Z
12 swissalti3d_2021_2532-1151 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5525481, 46.5068432, 6.565723, 46.5159392] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5526955, 46.5068432], [6.565723, 46.50694... 2021-01-01 00:00:00+00:00 2021-09-02 16:46:12.695971+00:00 2025-01-18 02:03:21.314035+00:00 POLYGON ((6.56572 46.50684, 6.56572 46.51594, ... 1220B9AD77D6DD070409D209F5ADF17EC7253FC3FE6CEE... 2021-09-03T00:35:29.891683Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-18T01:04:21.184877Z
16 swissalti3d_2021_2532-1152 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5524006, 46.5158382, 6.5655778, 46.5249343] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5525481, 46.5158382], [6.5655778, 46.5159... 2021-01-01 00:00:00+00:00 2021-09-02 16:56:28.144164+00:00 2025-01-18 02:16:20.318007+00:00 POLYGON ((6.56558 46.51584, 6.56558 46.52493, ... 122089C474AEF62CC4692D7A944AFC0A13162D3DD8D0CF... 2021-09-03T00:49:42.276133Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-18T01:18:11.426964Z
20 swissalti3d_2021_2533-1152 ch.swisstopo.swissalti3d Feature 0.9.0 [6.5654325, 46.5159392, 6.5786075, 46.5250338] [https://stac-extensions.github.io/timestamps/... [{'rel': 'self', 'href': 'https://data.geo.adm... Polygon [[[6.5655778, 46.5159392], [6.5786075, 46.5160... 2021-01-01 00:00:00+00:00 2021-09-02 16:56:34.537212+00:00 2025-01-18 02:17:06.919621+00:00 POLYGON ((6.57861 46.51594, 6.57861 46.52503, ... 1220B8B7EA9C14DD41021131A873BD5B76DC0642CBC374... 2021-09-03T00:49:54.457903Z 0.5 https://data.geo.admin.ch/ch.swisstopo.swissal... 2056.0 image/tiff; application=geotiff; profile=cloud... 2025-01-18T01:18:15.270233Z

STAC data processing

Automated generation of geospatial datasets: building footprints with estimated heights, DEM and tree canopy. For example, a tree canopy raster for any given part of Switzerland can be obtained as in:

import rasterio as rio
from rasterio import plot

dst_filepath = "tree-canopy.tif"
swisstopopy.get_tree_canopy_raster(region, dst_filepath)

with rio.open(dst_filepath) as src:
    plot.show(src)

tree-canopy

See the overview notebook and the API documentation for more details on the geospatial dataset generation functions.

Installation

You can install swisstopopy using pip:

# or pip install -e git+ssh://git@github.com/martibosch/swisstopopy
pip install https://github.com/martibosch/swisstopopy/archive/main.zip

Note that the get_tree_canopy_raster requires PDAL and its Python bindings, which are not installed by default with swisstopopy. The easiest way to install such requirements is using conda/mamba, e.g.: conda install -c conda-forge python-pdal.

Notes

The SwissTopoClient class can be used to process any collection of the swisstopo STAC API, and basic features succh as geospatial and datetime filtering should work out of the box. However, filtering based on further metadata such as the resolution is only fully supported for the following collections:

Acknowledgements

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swisstopopy-0.1.0.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swisstopopy-0.1.0-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file swisstopopy-0.1.0.tar.gz.

File metadata

  • Download URL: swisstopopy-0.1.0.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for swisstopopy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ea50692f28f40e7faebeb4a917272db1636eaf6a0ddd41225302af7a464a16c
MD5 ee9bcfd6e2d8257859f877d12d92dba7
BLAKE2b-256 0c8248f7fb8b742daba5a1e77199371bf149334554ad3a198230c1fb64c221d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for swisstopopy-0.1.0.tar.gz:

Publisher: release.yml on martibosch/swisstopopy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file swisstopopy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: swisstopopy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for swisstopopy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8642e3129a8ec34b7d33e5fdfa5fc33374c007b5291ff071c218e95f825d8447
MD5 4c92089617b69ddf703e3bd0f5cee430
BLAKE2b-256 50806bfa91ab28bda2c91730a953718a5e4fb60314b7a5a309dbc4bd9fccd698

See more details on using hashes here.

Provenance

The following attestation bundles were made for swisstopopy-0.1.0-py3-none-any.whl:

Publisher: release.yml on martibosch/swisstopopy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page