@geospatial-python

Process vector and raster geospatial data with GeoPandas, Shapely, PyProj, and Rasterio. Prevent CRS/unit errors, lon-lat flips, invalid geometries, and misaligned raster-vector clips.

Geospatial Python

When to Use

  • Measure areas, distances, buffers, spatial joins, or overlays on vector datasets with GeoPandas and Shapely.
  • Align vector geometries with GeoTIFF/COG rasters for clipping, sampling, or zonal statistics in Rasterio.
  • Debug wrong metric measurements, empty spatial joins, TopologyException errors, or silent CRS mismatches.

Prerequisites

Identify installed versions of geopandas, shapely, pyproj, and rasterio, the CRS of every vector and raster input (gdf.crs, src.crs), coordinate axis order (GeoJSON and Shapely use (x, y) = (lon, lat) for geographic CRS), and the spatial extent of the study area. Never assume an input file is in EPSG:4326 without inspecting its metadata first.

Enforce CRS hygiene before geometry math

  1. Distinguish setting a missing CRS (set_crs("EPSG:4326") only when the file lacks metadata and you know the true CRS) from reprojecting coordinates (to_crs("EPSG:32610")). Calling set_crs(..., allow_override=True) on already-projected coordinates corrupts the data without transforming values.
  2. Never compute area, length, centroids, or metric buffers in a geographic CRS (EPSG:4326, degrees). Reproject to an appropriate projected CRS first: a local UTM zone (for example gdf.estimate_utm_crs()) for city/regional work, or an equal-area CRS (such as EPSG:6933 or a regional Albers/Lambert projection) for continental or multi-zone area totals.
  3. Verify that both inputs share the exact same CRS (left.crs == right.crs) before gpd.sjoin, gpd.overlay, or raster sampling/masking. Reproject the lighter layer explicitly with to_crs(target_crs) rather than hoping downstream functions reject a mismatch.

Repair invalid geometries and check raster alignment

  • Validate input geometries with gdf.geometry.is_valid before unions, overlays, or clipping. Repair self-intersections or ring-orientation defects with shapely.make_valid(...) instead of blindly applying buffer(0) (which can silently drop parts of bowtie polygons).
  • When combining vector layers with rasterio rasters, compare vector.crs against src.crs, check bounding-box overlap against src.bounds, and handle src.nodata explicitly so sentinel fill values (such as -9999) do not pollute averages or sums.
  • For large rasters, read bounded windows (src.read(window=...)) rather than loading the entire raster into memory.

Example

A pipeline computed polygon areas directly in EPSG:4326, encountered a self-intersecting bowtie polygon, and sampled a projected GeoTIFF using unaligned WGS84 coordinates.

Run the worked CRS, geometry repair, and raster alignment test to reproduce the geographic-area warning, verify metric reprojection to 1.0 km², repair the bowtie with make_valid, and align vector coordinates to an in-memory GeoTIFF before sampling.

Limitations

  • Choosing an equal-area vs conformal projection depends on whether the analysis preserves area, distance, or local shape; verify distortion tolerances for high-latitude or polar regions.
  • Antimeridian (±180° longitude) crossings and vertical datum transformations require specialized handling beyond simple to_crs calls.
  • Multi-gigabyte Cloud-Optimized GeoTIFFs (COGs) and PostGIS database queries require streaming/windowed I/O and server-side spatial indexes outside this local workflow.

Safety and Side Effects

Work on read-only copies or temporary output directories before overwriting geospatial datasets. Verify CRS metadata and sample values after reprojection or resampling. Keep private location tracks or restricted imagery out of examples and logs.