GeoPandas extends pandas functionality to handle geographic and geospatial data operations with GeoSeries and GeoDataFrame classes.
—
GeoPandas provides comprehensive support for reading and writing various geospatial file formats including Shapefile, GeoJSON, GeoPackage, Parquet, and database connections. The I/O functions integrate with multiple backends (Fiona, PyOGRIO) and support spatial filtering, column selection, and format-specific options.
Functions for reading various geospatial file formats into GeoDataFrames.
def read_file(filename, bbox=None, mask=None, rows=None, engine='auto', **kwargs):
"""
Read geospatial file into GeoDataFrame.
Parameters:
- filename: Path to file or file-like object
- bbox: Bounding box to filter features (minx, miny, maxx, maxy)
- mask: Geometry or GeoDataFrame to spatially filter features
- rows: Number of rows to read or slice object
- engine: Backend engine ('auto', 'fiona', 'pyogrio')
- **kwargs: Additional parameters for the backend
Returns:
- GeoDataFrame: Loaded geospatial data
"""
...
def read_parquet(path, columns=None, storage_options=None, bbox=None, **kwargs):
"""
Read Parquet file with geometry data into GeoDataFrame.
Parameters:
- path: Path to Parquet file
- columns: List of columns to read
- storage_options: Storage options for cloud storage
- bbox: Bounding box to filter features (minx, miny, maxx, maxy)
- **kwargs: Additional parameters for pyarrow
Returns:
- GeoDataFrame: Loaded geospatial data
"""
...
def read_feather(path, columns=None, **kwargs):
"""
Read Feather file with geometry data into GeoDataFrame.
Parameters:
- path: Path to Feather file
- columns: List of columns to read
- **kwargs: Additional parameters for pyarrow
Returns:
- GeoDataFrame: Loaded geospatial data
"""
...
def read_postgis(sql, con, geom_col='geom', crs=None, index_col=None, coerce_float=True, parse_dates=None, params=None, chunksize=None):
"""
Read PostGIS database table into GeoDataFrame.
Parameters:
- sql: SQL query or table name
- con: Database connection
- geom_col: Name of geometry column
- crs: Coordinate reference system
- index_col: Column to use as index
- coerce_float: Convert decimal to float
- parse_dates: Columns to parse as dates
- params: Parameters for parameterized queries
- chunksize: Number of rows per chunk
Returns:
- GeoDataFrame: Loaded geospatial data
"""
...
def list_layers(filename):
"""
List available layers in geospatial file.
Parameters:
- filename: Path to geospatial file
Returns:
- pandas.DataFrame: Information about available layers
"""
...Methods available on GeoDataFrame and GeoSeries for writing to various formats.
class GeoDataFrame:
def to_file(self, filename, driver=None, index=None, mode='w', crs=None, engine='auto', **kwargs):
"""
Write GeoDataFrame to geospatial file.
Parameters:
- filename: Output file path
- driver: Output format driver ('ESRI Shapefile', 'GeoJSON', 'GPKG', etc.)
- index: Whether to write index
- mode: File mode ('w' for write, 'a' for append)
- crs: Coordinate reference system for output
- engine: Backend engine ('auto', 'fiona', 'pyogrio')
- **kwargs: Driver-specific parameters
"""
...
def to_parquet(self, path, index=None, compression='snappy', geometry_encoding='WKB', **kwargs):
"""
Write GeoDataFrame to Parquet file.
Parameters:
- path: Output file path
- index: Whether to write index
- compression: Compression algorithm ('snappy', 'gzip', 'brotli', etc.)
- geometry_encoding: Geometry encoding ('WKB', 'WKT', 'geoarrow')
- **kwargs: Additional parameters for pyarrow
"""
...
def to_feather(self, path, index=None, compression=None, **kwargs):
"""
Write GeoDataFrame to Feather file.
Parameters:
- path: Output file path
- index: Whether to write index
- compression: Compression algorithm
- **kwargs: Additional parameters for pyarrow
"""
...
def to_postgis(self, name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None):
"""
Write GeoDataFrame to PostGIS database table.
Parameters:
- name: Table name
- con: Database connection
- schema: Database schema
- if_exists: What to do if table exists ('fail', 'replace', 'append')
- index: Whether to write index
- index_label: Label for index column
- chunksize: Number of rows per chunk
- dtype: Data types for columns
- method: Method for inserting data
"""
...
def to_json(self, na='null', show_bbox=False, drop_id=False, to_wgs84=False, **kwargs):
"""
Export GeoDataFrame as GeoJSON string.
Parameters:
- na: How to handle missing values
- show_bbox: Include bounding box in output
- drop_id: Drop feature IDs
- to_wgs84: Transform to WGS84 before export
- **kwargs: Additional parameters for json.dumps
Returns:
- str: GeoJSON string
"""
...
def to_wkb(self, hex=False, **kwargs):
"""
Export geometries as Well-Known Binary.
Parameters:
- hex: Return hexadecimal string instead of bytes
- **kwargs: Additional parameters for shapely.to_wkb
Returns:
- pandas.DataFrame: DataFrame with WKB geometries
"""
...
def to_wkt(self, **kwargs):
"""
Export geometries as Well-Known Text.
Parameters:
- **kwargs: Additional parameters for shapely.to_wkt
Returns:
- pandas.DataFrame: DataFrame with WKT geometries
"""
...Class methods available on GeoDataFrame and GeoSeries for loading data.
class GeoDataFrame:
@classmethod
def from_file(cls, filename, **kwargs):
"""
Load GeoDataFrame from geospatial file.
Parameters:
- filename: Path to geospatial file
- **kwargs: Parameters passed to read_file
Returns:
- GeoDataFrame: Loaded geospatial data
"""
...
@classmethod
def from_features(cls, features, crs=None, columns=None):
"""
Create GeoDataFrame from sequence of GeoJSON-like features.
Parameters:
- features: Sequence of GeoJSON feature dictionaries
- crs: Coordinate reference system
- columns: Column names to use
Returns:
- GeoDataFrame: Created from features
"""
...
@classmethod
def from_dict(cls, data, geometry=None, crs=None):
"""
Create GeoDataFrame from dictionary.
Parameters:
- data: Dictionary of column data
- geometry: Name of geometry column or geometry data
- crs: Coordinate reference system
Returns:
- GeoDataFrame: Created from dictionary
"""
...
@classmethod
def from_postgis(cls, sql, con, **kwargs):
"""
Load GeoDataFrame from PostGIS database.
Parameters:
- sql: SQL query or table name
- con: Database connection
- **kwargs: Parameters passed to read_postgis
Returns:
- GeoDataFrame: Loaded from database
"""
...
class GeoSeries:
@classmethod
def from_file(cls, filename, **kwargs):
"""
Load GeoSeries from geospatial file.
Parameters:
- filename: Path to geospatial file
- **kwargs: Parameters passed to read_file
Returns:
- GeoSeries: Loaded geometry data
"""
...
@classmethod
def from_wkb(cls, data, index=None, crs=None, on_invalid='raise', **kwargs):
"""
Create GeoSeries from Well-Known Binary data.
Parameters:
- data: Array-like of WKB data
- index: Index for resulting series
- crs: Coordinate reference system
- on_invalid: How to handle invalid geometries ('raise', 'warn', 'ignore')
- **kwargs: Additional parameters
Returns:
- GeoSeries: Created from WKB data
"""
...
@classmethod
def from_wkt(cls, data, index=None, crs=None, on_invalid='raise', **kwargs):
"""
Create GeoSeries from Well-Known Text data.
Parameters:
- data: Array-like of WKT data
- index: Index for resulting series
- crs: Coordinate reference system
- on_invalid: How to handle invalid geometries ('raise', 'warn', 'ignore')
- **kwargs: Additional parameters
Returns:
- GeoSeries: Created from WKT data
"""
...
@classmethod
def from_xy(cls, x, y, z=None, index=None, crs=None, **kwargs):
"""
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)
- index: Index for resulting series
- crs: Coordinate reference system
- **kwargs: Additional parameters
Returns:
- GeoSeries: Series of Point geometries
"""
...import geopandas as gpd
# Read various file formats
gdf = gpd.read_file('data.shp')
gdf = gpd.read_file('data.geojson')
gdf = gpd.read_file('data.gpkg')
gdf = gpd.read_file('data.parquet')
# Read with spatial filtering
bbox = (-74.1, 40.6, -73.9, 40.8) # NYC area
gdf = gpd.read_file('data.shp', bbox=bbox)
# Read specific rows
gdf = gpd.read_file('data.shp', rows=slice(0, 1000))
# Read specific columns
gdf = gpd.read_file('data.shp', columns=['NAME', 'POP2000', 'geometry'])
# Read from URL
url = 'https://example.com/data.geojson'
gdf = gpd.read_file(url)
# List layers in multi-layer file
layers = gpd.list_layers('data.gpkg')
print(layers)# Write to different formats
gdf.to_file('output.shp')
gdf.to_file('output.geojson', driver='GeoJSON')
gdf.to_file('output.gpkg', driver='GPKG')
# Write with specific CRS
gdf.to_file('output.shp', crs='EPSG:4326')
# Write to Parquet with compression
gdf.to_parquet('output.parquet', compression='gzip')
# Export as GeoJSON string
geojson_str = gdf.to_json()
# Export geometries as WKT
wkt_df = gdf.to_wkt()import sqlalchemy
# Read from PostGIS
engine = sqlalchemy.create_engine('postgresql://user:pass@host:port/db')
# Read entire table
gdf = gpd.read_postgis('SELECT * FROM cities', con=engine)
# Read with spatial filter
sql = '''
SELECT * FROM cities
WHERE ST_Intersects(geom, ST_MakeEnvelope(-74.1, 40.6, -73.9, 40.8, 4326))
'''
gdf = gpd.read_postgis(sql, con=engine)
# Write to PostGIS
gdf.to_postgis('new_cities', con=engine, if_exists='replace')# Read from cloud storage with fsspec
gdf = gpd.read_parquet('s3://bucket/data.parquet')
# Read compressed files
gdf = gpd.read_file('data.zip')
# Read from web services
wfs_url = 'WFS:http://example.com/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetCapabilities'
gdf = gpd.read_file(wfs_url)Install with Tessl CLI
npx tessl i tessl/pypi-geopandas