CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rasterio

Fast and direct raster I/O for use with Numpy and SciPy

Pending
Overview
Eval results
Files

dataset-io.mddocs/

Dataset I/O

Core functionality for opening, reading, and writing raster datasets. Rasterio supports numerous formats through GDAL including GeoTIFF, NetCDF, HDF5, JPEG2000, and many others.

Capabilities

Opening Datasets

Opens raster datasets for reading or writing with comprehensive format and option support.

def open(fp, mode='r', driver=None, width=None, height=None, count=None, 
         dtype=None, crs=None, transform=None, nodata=None, sharing=False, 
         opener=None, **kwargs):
    """
    Open a dataset for reading or writing.
    
    Parameters:
    - fp (str or Path): Dataset path or URI
    - mode (str): 'r' for read, 'w' for write, 'r+' for read-write, 'a' for append
    - driver (str): Format driver name (e.g., 'GTiff', 'NetCDF')
    - width (int): Raster width in pixels (write mode)
    - height (int): Raster height in pixels (write mode)
    - count (int): Number of bands (write mode)
    - dtype (str or numpy.dtype): Data type (write mode)
    - crs (CRS): Coordinate reference system (write mode)
    - transform (Affine): Geospatial transformation (write mode)
    - nodata (number): NoData value (write mode)
    - sharing (bool): Enable dataset sharing between threads
    - opener (callable): Custom file opener function
    - **kwargs: Additional driver-specific options
    
    Returns:
    DatasetReader or DatasetWriter: Dataset object
    """

Usage examples:

# Read-only access
with rasterio.open('input.tif') as dataset:
    data = dataset.read()

# Create new raster
profile = {
    'driver': 'GTiff',
    'dtype': 'float32',
    'width': 256, 'height': 256, 'count': 3,
    'crs': 'EPSG:4326',
    'transform': rasterio.transform.from_bounds(-180, -90, 180, 90, 256, 256)
}
with rasterio.open('output.tif', 'w', **profile) as dst:
    dst.write(data)

# Open with specific driver and options
with rasterio.open('data.nc', driver='NetCDF', sharing=False) as dataset:
    band_data = dataset.read(1)

Copying Datasets

Copies raster datasets with optional format conversion and processing options.

def copy(src_path, dst_path, driver=None, dtype=None, compress=None, 
         photometric=None, **kwargs):
    """
    Copy a raster dataset to a new location with optional transformations.
    
    Parameters:
    - src_path (str): Source dataset path
    - dst_path (str): Destination dataset path  
    - driver (str): Output format driver
    - dtype (str or numpy.dtype): Output data type
    - compress (str): Compression method ('lzw', 'deflate', 'jpeg', etc.)
    - photometric (str): Photometric interpretation
    - **kwargs: Additional driver-specific options
    
    Returns:
    None
    """

Usage example:

# Simple copy
rasterio.copy('input.tif', 'output.tif')

# Copy with compression
rasterio.copy('input.tif', 'compressed.tif', compress='lzw')

# Convert format and data type
rasterio.copy('input.tif', 'output.jpg', driver='JPEG', dtype='uint8')

Dataset Classes

DatasetReader

Read-only access to raster datasets with comprehensive metadata and data access methods.

class DatasetReader:
    """Read-only raster dataset."""
    
    # Properties
    profile: dict
    meta: dict
    driver: str
    mode: str
    name: str
    width: int
    height: int
    shape: tuple[int, int]
    count: int
    dtypes: list[str]
    nodatavals: list[float]
    crs: CRS
    transform: Affine
    bounds: BoundingBox
    res: tuple[float, float]
    
    def read(self, indexes=None, out=None, window=None, masked=False, 
             out_shape=None, resampling=Resampling.nearest, fill_value=None, 
             out_dtype=None):
        """
        Read raster data.
        
        Parameters:
        - indexes (int or sequence): Band index(es) to read (1-based)
        - out (numpy.ndarray): Pre-allocated output array
        - window (Window): Spatial subset to read
        - masked (bool): Return masked array with nodata values masked
        - out_shape (tuple): Output array shape for resampling
        - resampling (Resampling): Resampling algorithm
        - fill_value (number): Fill value for areas outside dataset bounds
        - out_dtype (numpy.dtype): Output data type
        
        Returns:
        numpy.ndarray: Raster data array
        """
    
    def sample(self, xy, indexes=None, masked=False):
        """
        Sample raster values at coordinates.
        
        Parameters:
        - xy (iterable): (x, y) coordinate pairs
        - indexes (sequence): Band indexes to sample
        - masked (bool): Return masked values
        
        Returns:
        generator: Sampled values
        """
    
    def index(self, x, y, op=math.floor, precision=None):
        """Convert geographic coordinates to pixel coordinates."""
    
    def xy(self, row, col, offset='center'):
        """Convert pixel coordinates to geographic coordinates."""
    
    def window(self, left, bottom, right, top):
        """Create window from geographic bounds."""
    
    def window_transform(self, window):
        """Get transform for windowed data."""
    
    def block_windows(self, bidx=0):
        """Generate block windows for efficient processing."""
    
    def block_shapes(self):
        """Get internal block shapes."""
    
    def colormap(self, bidx):
        """Get colormap for band."""
    
    def checksum(self, bidx):
        """Calculate checksum for band."""

DatasetWriter

Write-enabled raster dataset for creating and modifying raster files.

class DatasetWriter(DatasetReader):
    """Write-enabled raster dataset."""
    
    def write(self, arr, indexes=None, window=None):
        """
        Write raster data.
        
        Parameters:
        - arr (numpy.ndarray): Data to write
        - indexes (int or sequence): Band index(es) to write to
        - window (Window): Spatial subset to write to
        
        Returns:
        None
        """
    
    def write_colormap(self, bidx, colormap):
        """Write colormap for band."""
    
    def write_mask(self, mask_array, window=None):
        """Write dataset mask."""
    
    def update_tags(self, **kwargs):
        """Update dataset tags/metadata."""
    
    def set_band_description(self, bidx, value):
        """Set band description."""
    
    def set_band_unit(self, bidx, value):
        """Set band unit."""
    
    def build_overviews(self, factors, resampling=Resampling.nearest):
        """Build overview images."""

Memory Files

In-memory raster operations for processing data without disk I/O.

class MemoryFile:
    """In-memory file-like raster dataset."""
    
    def __init__(self, file_or_bytes=None):
        """
        Initialize memory file.
        
        Parameters:
        - file_or_bytes (bytes or file-like): Initial data
        """
    
    def open(self, **kwargs):
        """Open as raster dataset."""
    
    def getvalue(self):
        """Get bytes content."""
    
    def close(self):
        """Close memory file."""

class ZipMemoryFile(MemoryFile):
    """Compressed in-memory raster file."""
    
    def __init__(self, file_or_bytes=None):
        """Initialize compressed memory file."""

Usage examples:

# Create raster in memory
with rasterio.MemoryFile() as memfile:
    with memfile.open(driver='GTiff', height=100, width=100, 
                      count=1, dtype='uint8') as dataset:
        dataset.write(data, 1)
    
    # Get bytes for storage or transmission
    raster_bytes = memfile.getvalue()

# Work with compressed data
with rasterio.ZipMemoryFile() as memfile:
    # Process compressed raster data
    pass

Utility Functions

Additional dataset manipulation utilities.

def pad(array, transform, pad_width, mode=None, **kwargs):
    """
    Pad array and adjust affine transform matrix.
    
    Parameters:
    - array (numpy.ndarray): Input array, for best results a 2D array
    - transform (Affine): Transform object mapping pixel space to coordinates
    - pad_width (int): Number of pixels to pad array on all four sides
    - mode (str or function): Method for determining padded values
    - **kwargs: Additional options (see numpy.pad for details)
    
    Returns:
    tuple: (padded_array, padded_transform) tuple
    """

def band(ds, bidx):
    """
    A dataset and one or more of its bands.
    
    Parameters:
    - ds (DatasetReader): An opened rasterio dataset object
    - bidx (int or sequence): Band number(s), index starting at 1
    
    Returns:
    Band: Named tuple with dataset, band index, dtype, and shape
    """

def delete(path):
    """
    Delete raster dataset and associated files.
    
    Parameters:
    - path (str): Dataset path to delete
    
    Returns:
    None
    """

Usage examples:

# Pad array with zeros and adjust transform
padded_array, padded_transform = rasterio.pad(data, original_transform, 10, mode='constant', constant_values=0)

# Delete dataset and auxiliary files
rasterio.delete('temporary.tif')

Install with Tessl CLI

npx tessl i tessl/pypi-rasterio

docs

cli.md

crs.md

data-types.md

dataset-io.md

features.md

index.md

processing.md

transformations.md

windowing.md

tile.json