Skip to main content

Python library to download DTM data from various providers.

Project description

Maps4FS PYDTMDL PYGDMDL
Maps4FS API Maps4FS UI Maps4FS Data
Maps4FS Upgrader Maps4FS Stats Maps4FS Bot

Quick Start

Install the package using pip:

pip install pydtmdl

Then, you can use it in your Python scripts:

from pydtmdl import DTMProvider

# Prepare coordinates of the center point and size (in meters).
coords = 45.285460396731374, 20.237491178279715  # Center point of the region of interest.
size = 2048  # Size of the region in meters (2048x2048 m).

# Get the best provider for the given coordinates.
best_provider = DTMProvider.get_best(coords)
print(f"Best provider: {best_provider.name()}")

# Create an instance of the provider with the given coordinates and size.
provider = best_provider(coords, size=size)

# Get the DTM data as a numpy array.
np_data = provider.image

Overview

pydtmdl is a Python library designed to provide access to Digital Terrain Models (DTMs) from various providers. It supports multiple providers, each with its own resolution and data format. The library allows users to easily retrieve DTM data for specific geographic coordinates and sizes.

Note, that some providers may require additional settings, such as API keys or selection of a specific dataset. More details can be found in the demo script and in the providers source code.

The library will retrieve all the required tiles, merge them, window them and return the result as a numpy array. If additional processing is required, such as normalization or resizing, it can be done using OpenCV or other libraries (example code is provided in the demo script).

What is a DTM?

First of all, it's important to understand what a DTM is.
There are two main types of elevation models: Digital Terrain Model (DTM) and Digital Surface Model (DSM). The DTM represents the bare earth surface without any objects like buildings or vegetation. The DSM, on the other hand, represents the earth's surface including all objects.

DTM vs DSM, example 1

DTM vs DSM, example 2

The library is focused on the DTM data and the DSM sources are not supported and will not be added in the future. The reason for this is that the DTM data is more suitable for terrain generation in games, as it provides a more accurate representation of the earth's surface without any objects.

Supported DTM providers

coverage map

In addition to SRTM 30m, which provides global coverage, the map above highlights all countries and/or regions where higher resolution coverage is provided by one of the DTM providers.

Provider Name Resolution Developer
🌎 SRTM30 30 meters iwatkot
🌎 ArcticDEM 2 meters kbrandwijk
🌎 REMA Antarctica 2 meters kbrandwijk
🇺🇸 USGS 1-90 meters ZenJakey
🏴󠁧󠁢󠁥󠁮󠁧󠁿 England 1 meter kbrandwijk
🏴󠁧󠁢󠁳󠁣󠁴󠁿 Scotland 0.25-1 meter kbrandwijk
🏴󠁧󠁢󠁷󠁬󠁳󠁿󠁧󠁢󠁷󠁬󠁳󠁿 Wales 1 meter garnwenshared
🇩🇪 Hessen, Germany 1 meter kbrandwijk
🇩🇪 Niedersachsen, Germany 1 meter kbrandwijk
🇩🇪 Bayern, Germany 1 meter H4rdB4se
🇩🇪 Nordrhein-Westfalen, Germany 1 meter kbrandwijk
🇩🇪 Mecklenburg-Vorpommern, Germany 1-25 meter kbrandwijk
🇩🇪 Baden-Württemberg, Germany 1 meter kbrandwijk
🇩🇪 Sachsen-Anhalt, Germany 1 meter kbrandwijk
🇩🇪 Thüringen, Germany 1 meter H4rdB4se
🇨🇦 Canada 1 meter kbrandwijk
🇧🇪 Flanders, Belgium 1 meter kbrandwijk
🇫🇷 France 1 meter kbrandwijk
🇮🇹 Italy 10 meter kbrandwijk
🇳🇴 Norway 1 meter kbrandwijk
🇪🇸 Spain 5 meter kbrandwijk
🇫🇮 Finland 2 meter kbrandwijk
🇩🇰 Denmark 0.4 meter kbrandwijk
🇸🇪 Sweden 1 meter GustavPersson
🇨🇭 Switzerland 0.5-2 meter kbrandwijk
🇨🇿 Czech Republic 5 meter kbrandwijk
🇱🇹 Lithuania 1 meter Tox3

Contributing

Contributions are welcome! If you want to add your own DTM provider, please follow this guide.
You can also contribute by reporting issues, suggesting improvements, or helping with documentation.

What a DTM provider does?

A DTM provider is a service that provides elevation data for a given location. While there's plenty of DTM providers available, only the ones that provide a free and open access to their data can be used in this library.

The base provider class, DTMProvider that all DTM providers inherit from, is responsible for all processing of DEM data. Individual DTM providers are responsible only for downloading the DTM tile(s) for the area.

The process for generating the elevation data is:

  • Download all DTM tiles for the desired map area (implemented by each DTM provider)
  • If the DTM provider downloaded multiple tiles, merge these tiles into one
  • If the tile uses a different projection, reproject it to EPSG:4326, which is used for all other data (like OSM)
  • Extract the map area from the tile (some providers, like SRTM, return big tiles that are larger than just the desired area)

How to implement a DTM provider?

So the DTM provider is a simple class, that receives coordinate of the center point, the size of the region of interest and should download all the needed DTM tiles and return a numpy array with the elevation data.

Example of a DTM provider

➡️ Existing providers can be found in the providers folder.

Let's take a look at an example of a DTM provider implementation.

Step 1: define description of the provider.

class SRTM30Provider(DTMProvider):
    """Provider of Shuttle Radar Topography Mission (SRTM) 30m data."""

    _code = "srtm30"
    _name = "SRTM 30 m"
    _region = "Global"
    _icon = "🌎"
    _resolution = 30.0

    _url = "https://elevation-tiles-prod.s3.amazonaws.com/skadi/{latitude_band}/{tile_name}.hgt.gz"

    _instructions = "When working with SRTM provider..."

So, we inherit from the DTMProvider class, add some properties to identify the Provider (such as code and region). The most important part is the _url property, which is a template for the URL to download the elevation data. But if your provider uses some other approach, you can reimplement related methods.

If you want some additional information or guides you can set the _instructions property.

Step 2 (optional): use the DTMProviderSetting class to define your own settings (if needed).

class SRTM30ProviderSettings(DTMProviderSettings):
    """Settings for the SRTM 30 m provider."""

    enable_something: bool = True
    input_something: int = 255

Also, you will need to add a new _settings property to the provider class.

class SRTM30Provider(DTMProvider):
    ...
    _settings = SRTM30ProviderSettings

If those are provided you'll later be able to use the user_settings property to access the settings. In the example it would look like this:

enable_something = self.user_settings.enable_something
input_something = self.user_settings.input_something

Step 3: implement the download_tiles method.

    def download_tiles(self):
        """Download SRTM tiles."""
        north, south, east, west = self.get_bbox()

        tiles = []
        # Look at each corner of the bbox in case the bbox spans across multiple tiles
        for pair in [(north, east), (south, west), (south, east), (north, west)]:
            tile_parameters = self.get_tile_parameters(*pair)
            tile_name = tile_parameters["tile_name"]
            decompressed_tile_path = os.path.join(self.hgt_directory, f"{tile_name}.hgt")

            if not os.path.isfile(decompressed_tile_path):
                compressed_tile_path = os.path.join(self.gz_directory, f"{tile_name}.hgt.gz")
                if not self.get_or_download_tile(compressed_tile_path, **tile_parameters):
                    raise FileNotFoundError(f"Tile {tile_name} not found.")

                with gzip.open(compressed_tile_path, "rb") as f_in:
                    with open(decompressed_tile_path, "wb") as f_out:
                        shutil.copyfileobj(f_in, f_out)
            tiles.append(decompressed_tile_path)

        return tiles

This method uses the helper method get_bbox to get the coordinates of the bounding box of the map area. If your DTM provider requires you to provide the coordinates in a different projection, you need to make sure you convert. For an example of this, see the transform_bbox method in nrw.py. Then, it determines which tiles are needed, downloads them all to a temporary folder and extracts them. The base class provides a _tile_directory property for convenience that points to a temp folder for your provider. Finally, it returns a list of file paths to the downloaded tiles.

As you can see, it's pretty simple to implement a DTM provider. You can use any source of elevation data, as long as it's free and open. NOTE: If a DTM Provider requires an API key, paid subscription, or any other form of payment, you will be fully responsible for setting up your own access to the provider. The provider in the library will expose the settings needed to provide your authentication key or other required information.

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

pydtmdl-0.0.5.tar.gz (42.7 kB view details)

Uploaded Source

Built Distribution

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

pydtmdl-0.0.5-py3-none-any.whl (55.2 kB view details)

Uploaded Python 3

File details

Details for the file pydtmdl-0.0.5.tar.gz.

File metadata

  • Download URL: pydtmdl-0.0.5.tar.gz
  • Upload date:
  • Size: 42.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pydtmdl-0.0.5.tar.gz
Algorithm Hash digest
SHA256 82b1178f469e964f948b6778f5532b74343fa0caa98482830ee591d8fbb97494
MD5 a4f1e27c20ecab1498a8778d46ada324
BLAKE2b-256 53d7a765bb15c76f3e404e4f9e8d7a2f4e8a8d9e2ab1d648ec38e7a518f0314a

See more details on using hashes here.

File details

Details for the file pydtmdl-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: pydtmdl-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 55.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pydtmdl-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c09e4c8016848935f559e280a7455cacab9215c1d47f0b64976cf579ed0b1ff9
MD5 94bd809ecbadb6fd75cc195aaf4d1b14
BLAKE2b-256 5b4e75eb465676b13a3c5f5e26ed8fc5c6f5c639d3cfe5584309f70bdc901d45

See more details on using hashes here.

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