CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-srtm-py

Python parser for the Shuttle Radar Topography Mission elevation data

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

image-generation.mddocs/

Image Generation

Create elevation maps and visualizations as PIL images or numpy arrays with customizable color schemes, elevation ranges, and geographic bounds.

Capabilities

Elevation Image Generation

Generate visual representations of elevation data as images or arrays with customizable parameters for geographic bounds, resolution, and color schemes.

class GeoElevationData:
    def get_image(
        self,
        size: Tuple[int, int],
        latitude_interval: Tuple[float, float],
        longitude_interval: Tuple[float, float],
        max_elevation: float,
        min_elevation: float = 0,
        unknown_color: Color = Color(255, 255, 255, 255),
        zero_color: Color = Color(0, 0, 255, 255),
        min_color: Color = Color(0, 0, 0, 255),
        max_color: Color = Color(0, 255, 0, 255),
        mode: str = 'image'
    ) -> Any: ...

Parameters:

  • size: Tuple of (width, height) in pixels for the output image
  • latitude_interval: Tuple of (min_latitude, max_latitude) defining the geographic bounds
  • longitude_interval: Tuple of (min_longitude, max_longitude) defining the geographic bounds
  • max_elevation: Maximum elevation value for color mapping (in meters)
  • min_elevation: Minimum elevation value for color mapping (in meters). Default: 0
  • unknown_color: Color for unknown/missing elevation data. Default: white
  • zero_color: Color for zero elevation (sea level). Default: blue
  • min_color: Color for minimum elevation values. Default: black
  • max_color: Color for maximum elevation values. Default: green
  • mode: Output format - 'image' for PIL Image object, 'array' for numpy array. Default: 'image'

Returns: PIL Image object (mode='image') or numpy array (mode='array')

Usage Example:

import srtm

elevation_data = srtm.get_data()

# Generate elevation image for Alpine region
image = elevation_data.get_image(
    size=(800, 600),              # 800x600 pixel image
    latitude_interval=(45.5, 46.5),   # 1 degree latitude range
    longitude_interval=(7.0, 8.0),    # 1 degree longitude range  
    max_elevation=4000,           # Scale colors up to 4000m
    min_elevation=500             # Scale colors from 500m
)

# Save as PNG
image.save('alpine_elevation.png')

# Display image properties
print(f"Image size: {image.size}")
print(f"Image mode: {image.mode}")

Custom Color Schemes

Define custom color mappings for different elevation ranges to create visually appealing and informative elevation maps.

Usage Example:

import srtm
from srtm.utils import Color

elevation_data = srtm.get_data()

# Define custom colors for different elevation zones
image = elevation_data.get_image(
    size=(1000, 800),
    latitude_interval=(44.0, 46.0),
    longitude_interval=(6.0, 9.0),
    max_elevation=3000,
    min_elevation=200,
    # Custom color scheme
    unknown_color=Color(red=255, green=255, blue=255, alpha=255),  # White for unknown
    zero_color=Color(red=70, green=130, blue=180, alpha=255),      # Steel blue for sea level
    min_color=Color(red=34, green=139, blue=34, alpha=255),       # Forest green for min elevation
    max_color=Color(red=139, green=69, blue=19, alpha=255)        # Saddle brown for max elevation
)

image.save('custom_colors_elevation.png')

Numpy Array Output

Generate elevation data as numpy arrays for scientific analysis and custom processing.

Usage Example:

import srtm
import numpy as np
import matplotlib.pyplot as plt

elevation_data = srtm.get_data()

# Generate elevation array
elevation_array = elevation_data.get_image(
    size=(400, 300),
    latitude_interval=(45.0, 46.0),
    longitude_interval=(7.0, 8.0),
    max_elevation=2500,
    mode='array'  # Return numpy array instead of PIL image
)

print(f"Array shape: {elevation_array.shape}")
print(f"Elevation range: {elevation_array.min():.1f}m to {elevation_array.max():.1f}m")

# Create custom visualization with matplotlib
plt.figure(figsize=(12, 8))
plt.imshow(elevation_array, cmap='terrain', origin='lower')
plt.colorbar(label='Elevation (m)')
plt.title('Elevation Map - Custom Processing')
plt.xlabel('Longitude Index')
plt.ylabel('Latitude Index')
plt.show()

# Statistical analysis
mean_elevation = np.mean(elevation_array)
std_elevation = np.std(elevation_array)
print(f"Mean elevation: {mean_elevation:.1f}m")
print(f"Standard deviation: {std_elevation:.1f}m")

High-Resolution Elevation Maps

Create detailed elevation maps for specific regions with high pixel density.

Usage Example:

import srtm

elevation_data = srtm.get_data(
    srtm1=True,  # Use higher resolution SRTM1 data where available
    srtm3=True
)

# Generate high-resolution image of mountain peak area
high_res_image = elevation_data.get_image(
    size=(2000, 2000),           # High resolution 2000x2000
    latitude_interval=(45.95, 46.05),  # Small area (0.1 degrees)
    longitude_interval=(7.85, 7.95),   # Small area (0.1 degrees)
    max_elevation=4500,
    min_elevation=1500
)

# Save high-quality image
high_res_image.save('mountain_peak_hd.png', 'PNG', quality=95)
print(f"Generated {high_res_image.size[0]}x{high_res_image.size[1]} elevation map")

Batch Image Generation

Generate multiple elevation images for different regions or zoom levels efficiently.

Usage Example:

import srtm
import os

elevation_data = srtm.get_data(batch_mode=True)  # Memory efficient

# Define regions of interest
regions = [
    {'name': 'Alps_North', 'lat': (46.0, 47.0), 'lon': (7.0, 8.0)},
    {'name': 'Alps_Central', 'lat': (45.5, 46.5), 'lon': (7.5, 8.5)},
    {'name': 'Alps_South', 'lat': (45.0, 46.0), 'lon': (8.0, 9.0)},
]

output_dir = 'elevation_maps'
os.makedirs(output_dir, exist_ok=True)

for region in regions:
    print(f"Generating image for {region['name']}...")
    
    image = elevation_data.get_image(
        size=(800, 600),
        latitude_interval=region['lat'],
        longitude_interval=region['lon'],
        max_elevation=4000,
        min_elevation=500
    )
    
    filename = os.path.join(output_dir, f"{region['name']}_elevation.png")
    image.save(filename)
    print(f"Saved {filename}")

print(f"Generated {len(regions)} elevation maps in {output_dir}/")

Comparative Elevation Analysis

Generate multiple images with different parameters to compare elevation representations.

Usage Example:

import srtm

elevation_data = srtm.get_data()

# Same region with different elevation scaling
region_bounds = {
    'size': (600, 400),
    'latitude_interval': (45.2, 45.8),
    'longitude_interval': (7.2, 7.8)
}

# Full elevation range
full_range = elevation_data.get_image(
    **region_bounds,
    max_elevation=4000,
    min_elevation=0
)
full_range.save('elevation_full_range.png')

# Mid-elevation focus  
mid_elevation = elevation_data.get_image(
    **region_bounds,
    max_elevation=2500,
    min_elevation=1000
)
mid_elevation.save('elevation_mid_range.png')

# High elevation focus
high_elevation = elevation_data.get_image(
    **region_bounds,
    max_elevation=4000,
    min_elevation=2000
)
high_elevation.save('elevation_high_range.png')

print("Generated comparative elevation maps:")
print("- elevation_full_range.png (0-4000m)")
print("- elevation_mid_range.png (1000-2500m)") 
print("- elevation_high_range.png (2000-4000m)")

Color Specification

Colors can be specified using the Color namedtuple or as keyword arguments:

from collections import namedtuple
Color = namedtuple('Color', ['red', 'green', 'blue', 'alpha'])

Color Parameters:

  • red: Red component (0-255)
  • green: Green component (0-255)
  • blue: Blue component (0-255)
  • alpha: Alpha/transparency component (0-255)

Usage Example:

from srtm.utils import Color

# Define colors as namedtuples
water_color = Color(red=65, green=105, blue=225, alpha=255)  # Royal blue
land_color = Color(red=34, green=139, blue=34, alpha=255)   # Forest green

# Use in image generation
image = elevation_data.get_image(
    size=(400, 300),
    latitude_interval=(45.0, 46.0), 
    longitude_interval=(7.0, 8.0),
    max_elevation=2000,
    zero_color=water_color,  # Use for sea level
    min_color=land_color     # Use for minimum elevation
)

Output Formats

PIL Image Output (Default)

  • Returns PIL Image object
  • Supports standard image operations (save, resize, rotate, etc.)
  • Compatible with image processing libraries
  • Easy conversion to different formats (PNG, JPEG, TIFF, etc.)

Numpy Array Output

  • Returns numpy array with elevation values
  • Enables scientific computing and analysis
  • Compatible with matplotlib, scipy, scikit-image
  • Allows custom visualization and processing algorithms

Performance and Memory Considerations

  • Large Images: High-resolution images require significant memory
  • Geographic Bounds: Larger areas require more SRTM file downloads
  • Batch Mode: Use batch_mode=True for generating multiple images
  • Caching: SRTM files are cached locally to speed up subsequent operations
  • Memory Management: Arrays are more memory-efficient than images for large datasets

Install with Tessl CLI

npx tessl i tessl/pypi-srtm-py

docs

advanced-interpolation.md

data-management.md

elevation-queries.md

gpx-processing.md

image-generation.md

index.md

tile.json