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,
TopologyExceptionerrors, 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
- 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")). Callingset_crs(..., allow_override=True)on already-projected coordinates corrupts the data without transforming values. - 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 examplegdf.estimate_utm_crs()) for city/regional work, or an equal-area CRS (such asEPSG:6933or a regional Albers/Lambert projection) for continental or multi-zone area totals. - Verify that both inputs share the exact same CRS (
left.crs == right.crs) beforegpd.sjoin,gpd.overlay, or raster sampling/masking. Reproject the lighter layer explicitly withto_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_validbefore unions, overlays, or clipping. Repair self-intersections or ring-orientation defects withshapely.make_valid(...)instead of blindly applyingbuffer(0)(which can silently drop parts of bowtie polygons). - When combining vector layers with
rasteriorasters, comparevector.crsagainstsrc.crs, check bounding-box overlap againstsrc.bounds, and handlesrc.nodataexplicitly 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 simpleto_crscalls. - 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.
Related Skills and Documentation
- For labeled multi-dimensional array alignment on gridded scientific data, see Xarray Coordinate Alignment.
- For tabular modeling and analysis workflows, see Data Scientist.
- See the official documentation for GeoPandas projections, Shapely
make_valid, and Rasterio coordinate systems.