GeoPandas extends pandas functionality to handle geographic and geospatial data operations with GeoSeries and GeoDataFrame classes.
—
GeoPandas provides two main data structures that extend pandas functionality with geospatial capabilities: GeoDataFrame and GeoSeries. These classes maintain all pandas functionality while adding geometry column support, spatial operations, and coordinate reference system management.
GeoDataFrame extends pandas DataFrame to support geometry columns and spatial operations. It maintains a reference to the active geometry column and provides methods for spatial analysis, coordinate transformations, and geospatial file I/O.
class GeoDataFrame(DataFrame):
def __init__(data=None, index=None, columns=None, dtype=None, copy=None, geometry=None, crs=None):
"""
Initialize GeoDataFrame with optional geometry column and CRS.
Parameters:
- data: DataFrame-like data
- index: Index to use for resulting frame
- columns: Column labels to use for resulting frame
- dtype: Data type to force
- copy: Copy data from inputs
- geometry: Column name or array-like of geometry objects
- crs: Coordinate Reference System
"""
...
@property
def geometry(self) -> GeoSeries:
"""Active geometry column as GeoSeries."""
...
@geometry.setter
def geometry(self, col):
"""Set the active geometry column."""
...
@property
def active_geometry_name(self) -> str:
"""Name of the active geometry column."""
...
@property
def crs(self):
"""Coordinate Reference System of the geometry column."""
...
@crs.setter
def crs(self, value):
"""Set the Coordinate Reference System."""
...
@property
def total_bounds(self) -> tuple:
"""Return bounding box of all geometries as (minx, miny, maxx, maxy)."""
...
@property
def sindex(self):
"""Spatial index for efficient spatial queries."""
...
def set_geometry(self, col, drop=False, inplace=False, crs=None):
"""
Set the active geometry column.
Parameters:
- col: Column name or array-like geometry data
- drop: Delete the column used for geometry
- inplace: Whether to return a new GeoDataFrame or modify in place
- crs: Coordinate reference system to use
Returns:
- GeoDataFrame or None if inplace=True
"""
...
def rename_geometry(self, col, inplace=False):
"""
Rename the active geometry column.
Parameters:
- col: New name for geometry column
- inplace: Whether to return a new GeoDataFrame or modify in place
Returns:
- GeoDataFrame or None if inplace=True
"""
...
def set_crs(self, crs, allow_override=False, inplace=False):
"""
Set the Coordinate Reference System.
Parameters:
- crs: CRS to set (string, dict, or pyproj.CRS)
- allow_override: Allow overriding existing CRS
- inplace: Whether to return a new GeoDataFrame or modify in place
Returns:
- GeoDataFrame or None if inplace=True
"""
...
def to_crs(self, crs=None, epsg=None, inplace=False):
"""
Transform geometries to a new coordinate reference system.
Parameters:
- crs: Target CRS (string, dict, or pyproj.CRS)
- epsg: EPSG code for target CRS
- inplace: Whether to return a new GeoDataFrame or modify in place
Returns:
- GeoDataFrame or None if inplace=True
"""
...
def estimate_utm_crs(self, datum_name='WGS 84'):
"""
Estimate the most appropriate UTM CRS for the geometries.
Parameters:
- datum_name: Name of the datum to use
Returns:
- pyproj.CRS: Estimated UTM CRS
"""
...
@classmethod
def from_arrow(cls, table, geometry=None, **kwargs):
"""
Create GeoDataFrame from Apache Arrow Table.
Parameters:
- table: Arrow Table containing the data
- geometry: Column name to use as geometry column
- **kwargs: Additional arguments
Returns:
- GeoDataFrame: New GeoDataFrame instance
"""
...
def to_geo_dict(self, **kwargs):
"""
Export as GeoJSON-like dictionary.
Parameters:
- **kwargs: Additional arguments for GeoJSON serialization
Returns:
- dict: GeoJSON-like dictionary representation
"""
...
def iterfeatures(self, na='null', precision=None, drop_id=False, to_wgs84=False, **kwargs):
"""
Iterate over rows as GeoJSON-like feature dictionaries.
Parameters:
- na: How to handle null values
- precision: Coordinate precision for output
- drop_id: Whether to drop the id field
- to_wgs84: Transform to WGS84 before output
- **kwargs: Additional arguments
Yields:
- dict: GeoJSON-like feature dictionaries
"""
...
def dissolve(self, by=None, aggfunc='first', as_index=True, level=None, sort=True, observed=False, dropna=True):
"""
Dissolve geometries based on grouping variables.
Parameters:
- by: Column name(s) to group by
- aggfunc: Aggregation function for non-geometry columns
- as_index: Return group labels as the index
- level: Level(s) to group by if index is MultiIndex
- sort: Sort the result by group keys
- observed: Use observed values for categorical groupers
- dropna: Drop groups with null values
Returns:
- GeoDataFrame: Dissolved geometries
"""
...
def explode(self, column=None, ignore_index=False, index_parts=False):
"""
Explode multi-part geometries into separate rows.
Parameters:
- column: Column to explode (defaults to geometry column)
- ignore_index: Reset index in the result
- index_parts: Include part index in result
Returns:
- GeoDataFrame: Exploded geometries
"""
...
def cx(self):
"""Coordinate-based indexer for spatial selection using bounding box."""
...GeoSeries extends pandas Series to hold geometry objects with spatial methods and properties. It provides the foundation for geometric operations and spatial analysis in GeoPandas.
class GeoSeries(Series):
def __init__(data=None, index=None, crs=None, **kwargs):
"""
Initialize GeoSeries with geometry data and CRS.
Parameters:
- data: Array-like geometry data
- index: Index to use for resulting series
- crs: Coordinate Reference System
- **kwargs: Additional Series parameters
"""
...
@property
def crs(self):
"""Coordinate Reference System of the geometries."""
...
@crs.setter
def crs(self, value):
"""Set the Coordinate Reference System."""
...
@property
def geometry(self) -> 'GeoSeries':
"""Return self (for consistency with GeoDataFrame)."""
...
@property
def x(self) -> Series:
"""X coordinates of Point geometries."""
...
@property
def y(self) -> Series:
"""Y coordinates of Point geometries."""
...
@property
def z(self) -> Series:
"""Z coordinates of Point geometries."""
...
@property
def m(self) -> Series:
"""M coordinates of Point geometries."""
...
@property
def total_bounds(self) -> tuple:
"""Return bounding box of all geometries as (minx, miny, maxx, maxy)."""
...
@property
def sindex(self):
"""Spatial index for efficient spatial queries."""
...
def set_crs(self, crs, allow_override=False, inplace=False):
"""
Set the Coordinate Reference System.
Parameters:
- crs: CRS to set (string, dict, or pyproj.CRS)
- allow_override: Allow overriding existing CRS
- inplace: Whether to return a new GeoSeries or modify in place
Returns:
- GeoSeries or None if inplace=True
"""
...
def to_crs(self, crs=None, epsg=None, inplace=False):
"""
Transform geometries to a new coordinate reference system.
Parameters:
- crs: Target CRS (string, dict, or pyproj.CRS)
- epsg: EPSG code for target CRS
- inplace: Whether to return a new GeoSeries or modify in place
Returns:
- GeoSeries or None if inplace=True
"""
...
def estimate_utm_crs(self, datum_name='WGS 84'):
"""
Estimate the most appropriate UTM CRS for the geometries.
Parameters:
- datum_name: Name of the datum to use
Returns:
- pyproj.CRS: Estimated UTM CRS
"""
...
@classmethod
def from_arrow(cls, arr, **kwargs):
"""
Create GeoSeries from Apache Arrow array.
Parameters:
- arr: Arrow array containing geometry data
- **kwargs: Additional arguments
Returns:
- GeoSeries: New GeoSeries instance
"""
...
@classmethod
def from_wkb(cls, data, index=None, crs=None, **kwargs):
"""
Create GeoSeries from Well-Known Binary (WKB) data.
Parameters:
- data: Array-like of WKB bytes
- index: Index to use for resulting series
- crs: Coordinate Reference System
- **kwargs: Additional arguments
Returns:
- GeoSeries: New GeoSeries instance
"""
...
@classmethod
def from_wkt(cls, data, index=None, crs=None, **kwargs):
"""
Create GeoSeries from Well-Known Text (WKT) data.
Parameters:
- data: Array-like of WKT strings
- index: Index to use for resulting series
- crs: Coordinate Reference System
- **kwargs: Additional arguments
Returns:
- GeoSeries: New GeoSeries instance
"""
...
@classmethod
def from_xy(cls, x, y, z=None, index=None, crs=None, **kwargs):
"""
Create GeoSeries of Point geometries from coordinate arrays.
Parameters:
- x: Array-like of x coordinates
- y: Array-like of y coordinates
- z: Array-like of z coordinates (optional)
- index: Index to use for resulting series
- crs: Coordinate Reference System
- **kwargs: Additional arguments
Returns:
- GeoSeries: New GeoSeries of Point geometries
"""
...Functions for creating geometry arrays and series from various input formats.
def points_from_xy(x, y, z=None, crs=None):
"""
Create Point geometries from x, y coordinates.
Parameters:
- x: Array-like of x coordinates
- y: Array-like of y coordinates
- z: Array-like of z coordinates (optional)
- crs: Coordinate Reference System
Returns:
- GeoSeries: Series of Point geometries
"""
...Functions for testing equality of geospatial objects.
def assert_geoseries_equal(left, right, check_dtype=True, check_index_type=True, check_series_type=True, check_names=True, check_crs=True, check_geom_type=False, check_less_precise=False, normalize=False):
"""
Assert that two GeoSeries are equal.
Parameters:
- left: GeoSeries to compare
- right: GeoSeries to compare
- check_dtype: Whether to check dtype equivalence
- check_index_type: Whether to check index type equivalence
- check_series_type: Whether to check series type equivalence
- check_names: Whether to check names equivalence
- check_crs: Whether to check CRS equivalence
- check_geom_type: Whether to check geometry type
- check_less_precise: Whether to use less precise comparison
- normalize: Whether to normalize geometries before comparison
"""
...
def assert_geodataframe_equal(left, right, check_dtype=True, check_index_type=True, check_frame_type=True, check_names=True, check_crs=True, check_geom_type=False, check_less_precise=False, normalize=False):
"""
Assert that two GeoDataFrames are equal.
Parameters:
- left: GeoDataFrame to compare
- right: GeoDataFrame to compare
- check_dtype: Whether to check dtype equivalence
- check_index_type: Whether to check index type equivalence
- check_frame_type: Whether to check frame type equivalence
- check_names: Whether to check names equivalence
- check_crs: Whether to check CRS equivalence
- check_geom_type: Whether to check geometry type
- check_less_precise: Whether to use less precise comparison
- normalize: Whether to normalize geometries before comparison
"""
...Function for displaying version information about GeoPandas and its dependencies.
def show_versions():
"""
Print version information for GeoPandas and its dependencies.
This function prints version information for GeoPandas, Python, and all
the dependencies that are installed on the system.
"""
...import geopandas as gpd
import pandas as pd
from shapely.geometry import Point, Polygon
# From existing DataFrame with geometry column
df = pd.DataFrame({
'City': ['New York', 'London', 'Tokyo'],
'Population': [8400000, 8900000, 13960000]
})
geometry = [Point(-74.0, 40.7), Point(-0.1, 51.5), Point(139.7, 35.7)]
gdf = gpd.GeoDataFrame(df, geometry=geometry, crs='EPSG:4326')
# From scratch with geometry data
gdf = gpd.GeoDataFrame({
'id': [1, 2, 3],
'name': ['Area A', 'Area B', 'Area C'],
'geometry': [
Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]),
Polygon([(1, 0), (2, 0), (2, 1), (1, 1)]),
Polygon([(0, 1), (1, 1), (1, 2), (0, 2)])
]
}, crs='EPSG:4326')
# Using points_from_xy
gdf = gpd.GeoDataFrame({
'location': ['A', 'B', 'C']
}, geometry=gpd.points_from_xy([-1, 0, 1], [1, 0, -1], crs='EPSG:4326'))# Access geometry properties
print(gdf.geometry.area)
print(gdf.geometry.centroid)
print(gdf.total_bounds)
# Set and rename geometry columns
gdf = gdf.set_geometry('geometry')
gdf = gdf.rename_geometry('geom')
# Work with coordinate reference systems
print(gdf.crs)
gdf = gdf.set_crs('EPSG:4326')
gdf_utm = gdf.to_crs(gdf.estimate_utm_crs())# Create GeoSeries
from shapely.geometry import Point, LineString
gs = gpd.GeoSeries([
Point(0, 0),
LineString([(0, 0), (1, 1)]),
Point(1, 1)
], crs='EPSG:4326')
# Access coordinate properties for Points
points = gs[gs.geom_type == 'Point']
print(points.x) # X coordinates
print(points.y) # Y coordinates
# Spatial indexing
print(gs.sindex) # Spatial index for queriesInstall with Tessl CLI
npx tessl i tessl/pypi-geopandas