Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Open In Colab Open In Kaggle

geopandas extends the pandas dataframe with a geometry column and a coordinate reference system (crs), so points, lines, and polygons can be filtered, joined, measured, and mapped with familiar syntax. This subchapter builds a small set of Swiss weather stations and hazard zones, reprojects between the geographic crs WGS84 (EPSG:4326) and the projected Swiss crs LV95 (EPSG:2056), and works through spatial joins, buffers, dissolves, and overlays. At the end, the generated code measures distance and area in degrees instead of a projected crs — the single most common geospatial mistake.

1.6.1 GeoDataFrame and shapely Geometries

A GeoDataFrame is a pandas DataFrame with an active geometry column of shapely objects (Point, LineString, Polygon) and an attached crs. The crs states how the coordinates map to the Earth; without it, they are numbers in an arbitrary plane.

     name            geometry
0   Basel  POINT (7.59 47.56)
1  Zurich  POINT (8.54 47.37)
2    Bern  POINT (7.44 46.95)
3  Lugano     POINT (8.95 46)
crs: 4326 | geometry type: Point

1.6.2 Reading and Writing Vector Formats

geopandas reads and writes shapefiles (.shp), GeoJSON (.geojson), and GeoPackage (.gpkg) through read_file/to_file. GeoPackage is a single-file, modern default; a shapefile is really several files and has column-name and size limits.

reread shape: (4, 2) | crs preserved: 4326
reread shape: (4, 2) | crs preserved: 4326
shapefile sidecar files: ['stations.cpg', 'stations.dbf', 'stations.geojson', 'stations.gpkg', 'stations.prj', 'stations.shp', 'stations.shx']

1.6.3 Coordinate Reference Systems and Reprojection

Inspect the crs with .crs; change it with to_crs. Reprojecting from geographic WGS84 to the projected Swiss system LV95 (EPSG:2056) converts degrees to metres, which is what makes metric measurement meaningful.

before: 4326 -> after: 2056
Basel WGS84 (deg): 7.59 47.56
Basel LV95 (m):   2611393.1 1267709.5

1.6.4 Spatial Predicates and Joins

Spatial predicates (within, intersects, contains) test topological relationships. A spatial join (sjoin) attaches attributes from one layer to another by location rather than by a shared key.

stations in a hazard zone: ['Basel', 'Zurich']

1.6.5 Buffer, Dissolve, Overlay, and Centroid

buffer grows a geometry by a distance (in crs units, so project first); dissolve merges geometries that share an attribute; overlay combines two layers by a set operation such as intersection; .centroid returns a single representative point for a polygon — handy for labelling a zone or measuring to it without carrying the whole shape around.

buffer area: 1254.6 km^2
polygons after dissolve: 1
dissolved hazard-zone centroid: 8.0 47.5
overlay produced 1 piece(s), area 1254.6 km^2
Basel-Bern straight-line route length: 68.8 km

1.6.6 Plotting a Map

geopandas plots geometries directly on a matplotlib axes; layers are drawn by calling .plot() on the same axes. The cell below first flags each station with .isin(), which checks whether a value appears in a given list or Series — here, whether a station’s name is among the names already found inside a hazard zone.

<Figure size 500x500 with 1 Axes>

When generated code lies: measuring in degrees

Asked for the distance between two stations, an assistant calls .distance() on the data as loaded — in EPSG:4326. The result is a number of degrees, not metres. geopandas even warns, but the value looks plausible and flows on into whatever comes next.

Basel->Bern distance in degrees (wrong): 0.6282
1x1 degree box area in deg^2 (wrong): 1.0
geopandas warned about a geographic crs: True

Geometry is in a geographic CRS. Results from 'distance' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

Basel->Bern distance: 68.8 km
1x1 degree box area: 8378.0 km^2

Summary

ConceptRule to remember
GeoDataFrameA DataFrame with a geometry column and a crs.
The crsWhat gives coordinates physical meaning; read it with .crs, change it with to_crs.
Formatsread_file/to_file handle shp, geojson, and gpkg; prefer GeoPackage to shapefile.
MeasuringArea, length, and distance only in a projected metric crs — never in degrees.
Relating layersSpatial predicates and sjoin join two layers by location.
Combining geometriesbuffer (project first), dissolve (merge by attribute), overlay (set operations), .centroid.

Resources