Skip to main content

UCRS a single wrapper class that accepts any CRS input and lazily converts to an instance of any library you need (e.g. cartopy, osgeo).

Project description

UCRS - Unified CRS

PyPI version Python 3.10+ License: MIT

Stop juggling CRS formats. Start using UCRS.

Working with geospatial data in Python means dealing with different CRS representations:

  • pyprojpyproj.CRS
  • cartopycartopy.crs.CRS and cartopy.crs.Projection
  • GDAL/osgeoosgeo.osr.SpatialReference

Converting between these is well-documented but tedious. UCRS provides a single class that accepts any CRS input and seamlessly converts to whatever you need.

Features

  • Universal Input: Accepts EPSG codes, WKT, PROJ strings, WKT files, or library-specific CRS objects
  • Lazy Conversion: Only imports and converts when you access a specific property
  • Cached Results: Conversions are cached - repeated access returns the same object
  • Type Safe: Full type annotations for IDE support
  • Inheritance: UCRS is a pyproj.CRS, so it works anywhere pyproj does

Quick Start

from ucrs import UCRS

# Create from any CRS representation
crs = UCRS(4326)                         # EPSG code
crs = UCRS("EPSG:4326")                  # EPSG string
crs = UCRS("+proj=longlat +datum=WGS84") # PROJ string
crs = UCRS(wkt_string)                   # WKT
crs = UCRS(pyproj.CRS.from_epsg(4326))   # pyproj.CRS
crs = UCRS(cartopy.crs.PlateCarree())    # cartopy CRS
crs = UCRS(srs)                          # osgeo.osr.SpatialReference
crs = UCRS.from_file("path/to/crs.wkt")  # WKT from file

# Use as pyproj.CRS (UCRS inherits from it)
crs.to_epsg()        # Returns 4326
crs.is_geographic    # Returns True
crs.to_wkt()         # WKT string

# Convert to other libraries (lazy, cached)
crs.cartopy          # Returns cartopy CRS (requires cartopy)
crs.osgeo            # Returns SpatialReference (requires GDAL)

Example Use Cases

Working with matplotlib and cartopy:

import matplotlib.pyplot as plt
from ucrs import UCRS

# Create from EPSG code
crs = UCRS(3857)  # Web Mercator

# Use in cartopy plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=crs.cartopy)
ax.coastlines()

Converting between GDAL and pyproj:

from osgeo import gdal
from ucrs import UCRS

# Get CRS from GDAL dataset
dataset = gdal.Open("myfile.tif")
gdal_srs = dataset.GetSpatialRef()

# Convert to UCRS
crs = UCRS(gdal_srs)

# Now use pyproj methods
print(f"EPSG: {crs.to_epsg()}")
print(f"Is projected: {crs.is_projected}")

Transforming coordinates:

from pyproj import Transformer
from ucrs import UCRS

wgs84 = UCRS(4326)
web_mercator = UCRS(3857)

# Since UCRS inherits from pyproj.CRS, use it directly
transformer = Transformer.from_crs(wgs84, web_mercator)
x, y = transformer.transform(40.7128, -74.0060)  # NYC coordinates

Loading CRS from WKT files:

from pathlib import Path
from ucrs import UCRS

# Many GIS workflows store CRS definitions in .wkt or .prj files
crs = UCRS.from_file("projection.prj")  # Shapefile projection file
crs = UCRS.from_file(Path("data/crs.wkt"))  # Also accepts Path objects

# Use immediately with any library
print(f"EPSG: {crs.to_epsg()}")
ax.set_projection(crs.cartopy)
dataset.SetProjection(crs.osgeo.ExportToWkt())

How It Works

UCRS inherits from pyproj.CRS, making it a drop-in replacement that works anywhere a pyproj CRS is expected. All conversions follow this pattern:

  1. Input (any format) → pyproj.CRS (during initialization)
  2. pyproj.CRSOutput format (via lazy, cached properties)

Since UCRS inherits from pyproj.CRS, you can use all pyproj methods directly. Conversions to cartopy and osgeo are performed lazily when their properties are first accessed, then cached for subsequent use.

Requirements

  • Python 3.10+
  • pyproj (required)
  • cartopy (optional)
  • GDAL (optional)

Installation

# Minimal installation (pyproj only)
pip install ucrs

# With cartopy support
pip install ucrs[cartopy]

# With GDAL support
pip install ucrs[gdal]

# With all optional dependencies
pip install ucrs[complete]

License

MIT License - see LICENSE file for details.

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

ucrs-0.4.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

ucrs-0.4.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file ucrs-0.4.0.tar.gz.

File metadata

  • Download URL: ucrs-0.4.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.18.8-arch2-1

File hashes

Hashes for ucrs-0.4.0.tar.gz
Algorithm Hash digest
SHA256 6350159ffaeed0e0db0690cec68734d2e59cf56189658a8c24aaf9c0adc456d4
MD5 91c94e4048e8663db78c853a5ff8817d
BLAKE2b-256 aa17720c78721c53225dbf838f5c330f668e3e49f13fd07e786c13b9e5a2ce3d

See more details on using hashes here.

File details

Details for the file ucrs-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: ucrs-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.18.8-arch2-1

File hashes

Hashes for ucrs-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42985fd8c0492f4dbedb7d0aedbbf3f386f7e671f17b00b4e63811c3e71c6f70
MD5 0103dec34556b5ad727b7d84d1a6ac48
BLAKE2b-256 3bd81b9ced75589419758827b0a011f2880cf02bd329cac79dd3c4a93975fe8e

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