Geospatial Raster datatype library for Python.
Project description
rastr
A lightweight geospatial raster datatype library for Python focused on simplicity.
For more details, read the documentation: https://rastr.readthedocs.io/en/stable/.
Overview
rastr provides an intuitive interface for creating, reading, manipulating, and exporting geospatial raster data in Python.
Features
- 🧮 Complete raster arithmetic: Full support for mathematical operations (
+,-,*,/) between rasters and scalars. - 📊 Flexible visualization: Built-in plotting with matplotlib and interactive mapping with folium.
- 🗺️ Geospatial analysis tools: Contour generation, Gaussian blurring, and spatial sampling.
- 🛠️ Data manipulation: Fill NaN values, extrapolate missing data, and resample to different resolutions.
- 🔗 Seamless integration: Works with GeoPandas, rasterio, and the broader Python geospatial ecosystem.
- ↔️ Vector-to-raster workflows: Convert GeoDataFrame polygons, points, and lines to raster format.
Installation
# With uv
uv add rastr
# With pip
pip install rastr
Quick Start
from pyproj.crs.crs import CRS
from rasterio.transform import from_origin
from rastr import Raster, RasterMeta
from rastr.create import full_raster
# Create an example raster
raster = Raster.example()
# Write to and read from a file
raster.to_file("raster.tif")
raster = Raster.read_file("raster.tif")
# Basic arithmetic operations
doubled = raster * 2
summed = raster + 10
combined = raster + doubled
# Visualize the data
ax = raster.plot(cbar_label="Values")
# Interactive web mapping (requires folium)
m = raster.explore(opacity=0.8, colormap="plasma")
# Sample values at specific coordinates
xy_points = [(100.0, 200.0), (150.0, 250.0)]
values = raster.sample(xy_points)
# Generate contour lines
contours = raster.contour(levels=[0.1, 0.5, 0.9], smoothing=True)
# Apply spatial operations
blurred = raster.blur(sigma=2.0) # Gaussian blur
filled = raster.extrapolate(method="nearest") # Fill NaN values via nearest-neighbours
resampled = raster.resample(cell_size=0.5) # Change resolution
# Export to file
raster.to_file("output.tif")
# Convert to GeoDataFrame for vector analysis
gdf = raster.as_geodataframe(name="elevation")
Quick Reference
from rastr import Raster
Data access
Raster.bbox- bounding box polygon.Raster.bounds- bounding box as(xmin, ymin, xmax, ymax).Raster.cell_size- cell size.Raster.crs- coordinate reference system.Raster.shape- raster shape (rows, columns).Raster.transform- affine transform.Raster.sample(xy)- sample raster values at given coordinates.
I/O
Raster.read_file(path)- read raster from file.Raster.to_file(path)- write raster to file.Raster.to_clipboard()- copy raster data to clipboard in a tabular format.
Geometric Operations
Raster.crop(bounds)- remove cells outside given bounds.Raster.pad(width)- add NaN border around raster.Raster.resample(cell_size)- resample raster to a new cell size.Raster.taper_border(width)- gradually reduce values to zero at the border.Raster.gdf()- Vectorize to a GeoDataFrame of cell polygons and values.
NaN Management and value replacements
Raster.clip(polygon)- replace values outside a polygon with NaN.Raster.extrapolate()- fill NaN values via nearest-neighbours.Raster.fillna(value)- fill NaN values with a specified value.Raster.replace(to_replace, value)- replace specific cell values.Raster.replace_polygon(polygon, value)- replace cell values within a polygon.Raster.trim_nan()- remove border rows/columns that are entirely NaN.
Image Processing
Raster.blur(radius)- apply Gaussian blur.Raster.dilate(radius)- apply morphological dilation.Raster.sobel()- apply Sobel filter (edge detection/gradient).
Visualization
Raster.explore()- interactive web map visualization with folium.Raster.plot()- matplotlib static plot with colorbar.Raster.contour(levels)- get a GeoDataFrame of contour lines.
Cell-wise Operations
Raster.apply(func)- apply a function to cell values.Raster.abs()- absolute value of cell values.Raster.astype(dtype)- cast cell values to a specified dtype.Raster.clamp()- clip cell values to an(a_min, a_max)range.Raster.exp()- exponential of cell values.Raster.log()- logarithm of cell values.Raster.max()- maximum of cell values.Raster.mean()- mean of cell values.Raster.median()- median of cell values.Raster.min()- minimum of cell values.Raster.normalize()- normalize cell values to [0, 1].Raster.quantile(q)- quantile of cell values.Raster.std()- standard deviation of cell values.Raster.sum()- sum of cell values.Raster.unique()- array of unique cell values.
Limitations
Current version limitations:
- Only Single-band rasters are supported.
- In-memory processing only (streaming support planned).
- Only float dtypes (integer support planned).
Similar Projects
- rasters is a project with similar goals of providing a dedicated raster datatype in Python with higher-level interfaces for GIS operations. Unlike
rastr, it has support for multi-band rasters, and has some more advanced functionality for Earth Science applications. Both projects are relatively new and under active development. - rasterio is a core dependency of
rastrand provides low-level raster I/O and processing capabilities. - rioxarray extends
xarrayfor raster data with geospatial support viarasterio.
Contributing
See the CONTRIBUTING.md file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rastr-0.13.1.tar.gz.
File metadata
- Download URL: rastr-0.13.1.tar.gz
- Upload date:
- Size: 404.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
532264207d5cefb61e49669e84ef02332e0c6aeaa1b94ce56ef621cde3bdaaa2
|
|
| MD5 |
55abdbd068110333e4e97847db0b45c8
|
|
| BLAKE2b-256 |
5911e86d946ca572888e7b43f7862fc1876bfd46f1b9eeacfb35318270c57f3e
|
File details
Details for the file rastr-0.13.1-py3-none-any.whl.
File metadata
- Download URL: rastr-0.13.1-py3-none-any.whl
- Upload date:
- Size: 45.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f14fc03bae34c770837572b7a98796531b97e50ca2c598bcb0d76d93e9acd05
|
|
| MD5 |
73051012130a55f91a5676f71977219c
|
|
| BLAKE2b-256 |
4e76200f59cd673d1ce18051a486503d7c2498f64196d6c3778b3e8ffd016190
|