Fast and direct raster I/O for use with Numpy and SciPy
—
Comprehensive coordinate reference system support including EPSG codes, PROJ4 strings, WKT definitions, and CRS transformations. The CRS class provides a unified interface for working with different coordinate systems.
Central class for coordinate reference system representation and manipulation.
class CRS:
"""Coordinate Reference System representation."""
def __init__(self, initialdata=None, **kwargs):
"""
Initialize CRS from various inputs.
Parameters:
- initialdata (str, int, dict, CRS): EPSG code, PROJ4 string, WKT, or CRS dict
- **kwargs: Additional CRS parameters
"""
# Properties
wkt: str # Well-Known Text representation
data: dict # CRS parameters as dictionary
to_proj4_dict: dict # PROJ4 parameters as dictionary
is_geographic: bool # True if geographic coordinate system
is_projected: bool # True if projected coordinate system
is_valid: bool # True if CRS is valid
linear_units: str # Linear units (e.g., 'metre')
linear_units_factor: tuple[str, float] # Units name and conversion factor
axis_info: list # Axis information
@classmethod
def from_epsg(cls, code):
"""
Create CRS from EPSG code.
Parameters:
- code (int): EPSG coordinate system code
Returns:
CRS: Coordinate reference system
"""
@classmethod
def from_proj4(cls, proj4):
"""
Create CRS from PROJ4 string.
Parameters:
- proj4 (str): PROJ4 coordinate system definition
Returns:
CRS: Coordinate reference system
"""
@classmethod
def from_wkt(cls, wkt):
"""
Create CRS from Well-Known Text.
Parameters:
- wkt (str): WKT coordinate system definition
Returns:
CRS: Coordinate reference system
"""
@classmethod
def from_user_input(cls, value):
"""
Create CRS from various user input formats.
Parameters:
- value (str, int, dict, CRS): Various CRS representations
Returns:
CRS: Coordinate reference system
"""
@classmethod
def from_string(cls, string):
"""
Create CRS from string representation.
Parameters:
- string (str): CRS string (PROJ4, WKT, or authority string)
Returns:
CRS: Coordinate reference system
"""
@classmethod
def from_dict(cls, proj_dict):
"""
Create CRS from PROJ4 dictionary.
Parameters:
- proj_dict (dict): PROJ4 parameters as dictionary
Returns:
CRS: Coordinate reference system
"""
def to_epsg(self):
"""
Get EPSG code if available.
Returns:
int or None: EPSG code
"""
def to_proj4(self):
"""
Convert to PROJ4 string representation.
Returns:
str: PROJ4 string
"""
def to_wkt(self, version='WKT2_2019', pretty=False):
"""
Convert to Well-Known Text representation.
Parameters:
- version (str): WKT version ('WKT1_GDAL', 'WKT2_2015', 'WKT2_2019')
- pretty (bool): Format for readability
Returns:
str: WKT string
"""
def to_dict(self):
"""
Convert to PROJ4 dictionary.
Returns:
dict: PROJ4 parameters
"""
def to_string(self):
"""
Convert to string representation.
Returns:
str: String representation
"""
def equals(self, other):
"""
Test equality with another CRS.
Parameters:
- other (CRS): CRS to compare
Returns:
bool: True if equivalent
"""
def is_exact_same(self, other):
"""
Test exact equivalence with another CRS.
Parameters:
- other (CRS): CRS to compare
Returns:
bool: True if exactly the same
"""Usage examples:
from rasterio.crs import CRS
# Create from EPSG code
crs_4326 = CRS.from_epsg(4326) # WGS84
crs_3857 = CRS.from_epsg(3857) # Web Mercator
# Create from PROJ4 string
crs_proj4 = CRS.from_proj4('+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs')
# Create from WKT
wkt = '''GEOGCS["WGS 84",DATUM["WGS_1984",...]]'''
crs_wkt = CRS.from_wkt(wkt)
# Create from various inputs
crs_auto = CRS.from_user_input('EPSG:4326')
crs_auto = CRS.from_user_input(4326)
crs_auto = CRS.from_user_input('+proj=longlat +datum=WGS84')
# Convert between formats
epsg_code = crs_4326.to_epsg() # 4326
proj4_str = crs_4326.to_proj4() # '+proj=longlat +datum=WGS84 +no_defs'
wkt_str = crs_4326.to_wkt() # Well-Known Text representation
# Check properties
print(crs_4326.is_geographic) # True
print(crs_3857.is_projected) # True
print(crs_4326.linear_units) # 'degree'
print(crs_3857.linear_units) # 'metre'
# Compare CRS
print(crs_4326.equals(CRS.from_epsg(4326))) # TrueUtilities for working with CRS validity and metadata.
def is_geographic_crs(crs):
"""
Test if CRS is geographic.
Parameters:
- crs (CRS): Coordinate reference system
Returns:
bool: True if geographic
"""
def is_projected_crs(crs):
"""
Test if CRS is projected.
Parameters:
- crs (CRS): Coordinate reference system
Returns:
bool: True if projected
"""Standard coordinate reference systems commonly used in geospatial applications:
# Geographic coordinate systems
WGS84 = CRS.from_epsg(4326) # World Geodetic System 1984
NAD83 = CRS.from_epsg(4269) # North American Datum 1983
ETRS89 = CRS.from_epsg(4258) # European Terrestrial Reference System 1989
# Projected coordinate systems
WEB_MERCATOR = CRS.from_epsg(3857) # Web Mercator (Google Maps)
UTM_33N = CRS.from_epsg(32633) # UTM Zone 33N
ALBERS_USA = CRS.from_epsg(5070) # Albers Equal Area Conic (USA)
# Usage in dataset operations
with rasterio.open('input.tif') as src:
# Check dataset CRS
print(f"Dataset CRS: {src.crs}")
print(f"Is geographic: {src.crs.is_geographic}")
# Compare with target CRS
target_crs = CRS.from_epsg(3857)
needs_reprojection = not src.crs.equals(target_crs)CRS operations can raise specific exceptions for invalid or incompatible coordinate systems:
class CRSError(RasterioError):
"""Coordinate reference system related errors."""Common error scenarios:
from rasterio.errors import CRSError
try:
# Invalid EPSG code
invalid_crs = CRS.from_epsg(999999)
except CRSError as e:
print(f"Invalid EPSG code: {e}")
try:
# Invalid PROJ4 string
invalid_crs = CRS.from_proj4('+proj=invalid +datum=WGS84')
except CRSError as e:
print(f"Invalid PROJ4: {e}")
# Check validity before operations
crs = CRS.from_user_input(user_input)
if not crs.is_valid:
raise CRSError(f"Invalid CRS: {crs}")Install with Tessl CLI
npx tessl i tessl/pypi-rasterio