GeoPandas Documentation

repository·main·Indexed 26 days ago

https://github.com/geopandas/geopandas

GeoPandas is a Python library that extends pandas to provide support for geographic data. It introduces GeoSeries and GeoDataFrame types to enable geometric operations on spatial datasets, utilizing shapely for geometry objects, pyproj for coordinate reference system transformations, and pyogrio for reading and writing vector data files. The library integrates with matplotlib for plotting geospatial data.

Tokens
57.1K
Snippets
154
Records
344
Agent score
83%

What's inside GeoPandas

  1. Overview of GeoPandas core types

    main

    GeoPandas extends pandas to support geographic data. It provides two primary data structures that are subclasses of standard pandas objects:

    • GeoSeries: A subclass of pandas.Series designed to hold geometric data.
    • GeoDataFrame: A subclass of pandas.DataFrame that allows for tabular data with a geometry column.

    These objects can operate on shapely geometry objects and perform various geometric operations.

  2. Overview of GeoPandas

    main

    GeoPandas is an open source Python library designed to make working with geospatial data easier. It extends the pandas data structures to allow spatial operations on geometric types.

    Key characteristics:

    • Spatial Operations: Performs geometric operations using shapely.
    • File Access: Uses pyogrio for reading and writing geospatial files.
    • Visualization: Uses matplotlib for plotting geospatial data.
    • Functionality: Provides a high-level interface for multiple geometries, enabling Python-based spatial operations that would typically require a spatial database like PostGIS.
  3. Understand the GeoPandas public API surface

    main

    The public API of GeoPandas consists of all classes and functions exposed in the geopandas.* namespace, as well as those explicitly listed in the official API reference.

    Note on Private Modules: The geopandas.array and geopandas.base modules are considered private. Stable functionality in these modules is not guaranteed, and you should avoid relying on them for production code.

  4. Access GeoPandas documentation

    main

    GeoPandas documentation is organized into four primary sections to help users at different stages of expertise:

    • User Guide: Explains basic functionality and core concepts.
    • Advanced Guide: Covers complex topics for users who already understand the basics.
    • Examples: A gallery of practical usage examples.
    • API Reference: Detailed technical documentation for every class, method, function, and attribute implemented by GeoPandas.
  5. Spatial Analysis and Machine Learning with PySAL

    main

    The PySAL (Python Spatial Analysis Library) ecosystem provides specialized tools for geospatial data science. Key components include:

    • libpysal: Foundational algorithms and data structures. Includes io (I/O for geospatial formats), weights (spatial weights matrices), cg (computational geometry like Voronoi tessellations), and examples.
    • esda: Methods for global and local spatial autocorrelation analysis.
    • segregation: Calculates over 40 different segregation indices for quantitative analysis.
    • mgwr: Scalable algorithms for Geographically Weighted Regression (GWR) models.
    • tobler: Functionality for areal interpolation and dasymetric mapping.

    Other notable analysis tools:

    • movingpandas: Handles movement data using a Trajectory class based on GeoPandas GeoDataFrames.
    • momepy: Quantitative analysis of urban form (urban morphometrics).
    • geosnap: Tools for modeling and visualizing social and spatial neighborhood dynamics.
    • mesa-geo: Enables GeoAgents with Shapely shape attributes within a GeoSpace.
    • Pyspatialml: Applies scikit-learn machine learning models to raster data stacks using geopandas and rasterio.
  6. Understand GeoPandas dependencies

    main

    GeoPandas relies on several core libraries to provide its geospatial capabilities. Understanding these dependencies helps in troubleshooting installation issues or understanding how GeoPandas handles specific tasks:

    • pandas: Provides the fundamental data structures for tabular and time-series data.
    • Shapely: Handles manipulation and analysis of planar geometric objects (backed by GEOS).
    • pyogrio: Provides the API for reading and writing vector data files (like Shapefile, GeoPackage, and GeoJSON) using GDAL.
    • pyproj: Manages cartographic projections and coordinate transformations (interface to PROJ).
  7. Understand GeoSeries data structure

    main

    A GeoSeries is a subclass of pandas.Series that acts as a vector where each entry is a set of geometric shapes (Shapely objects).

    Supported geometric types include:

    • Points / Multi-Points
    • Lines / Multi-Lines
    • Polygons / Multi-Polygons

    Note that entries in a GeoSeries do not need to be of the same geometric type, though some export operations may require uniformity.

  8. Update GeoPandas documentation

    main

    GeoPandas documentation is located in the doc folder. Changes are made within the doc/source directory. The project uses a mixture of reStructuredText (.rst) and MyST (.md) syntax. Docstrings must follow the Numpy Docstring standard. Jupyter notebooks used for examples should be stored without output and are processed via nbsphinx.

    To verify your documentation changes, build the HTML version using Sphinx:

    1. Navigate to the doc folder.
    2. Run make html.
    3. View the results in doc/build/html.

    If you encounter errors, you can build the documentation in a dedicated environment using the environment.yml file provided in the doc folder:

    cd doc
    conda env create -f environment.yml
    conda activate geopandas_docs
    python -m ipykernel install --user --name geopandas_docs
    make html
  9. Understand GeoDataFrame and the active geometry column

    main

    A GeoDataFrame is a tabular data structure containing a GeoSeries. It features an "active geometry column" which determines which column spatial methods (like .area or .centroid) act upon.

    Managing the Active Geometry

    • Accessing the active geometry: Use the .geometry attribute (e.g., gdf.geometry). This returns the active column regardless of its name.
    • Finding the active column name: Use gdf.geometry.name or gdf.active_geometry_name.
    • Changing the active geometry: Use the .set_geometry(name) method to switch the active status to a different column.
    • Renaming the active geometry: Use .rename_geometry(name).

    Important: If you rename a column using standard pandas .rename(), you must also call .set_geometry() to ensure the new column is recognized as the active geometry.

    Note on naming: gdf.geometry refers to the active geometry column, not necessarily a column named "geometry". To access a specific column named "geometry" when it is not the active one, use gdf['geometry'].

  10. Create a feature branch for development

    main

    Always create a new feature branch for your changes to keep your main branch clean and production-ready. Use git checkout -b <branch-name> to create and switch to a new branch. To update your branch with the latest changes from the upstream main branch, use git fetch upstream followed by git rebase upstream/main.

    git checkout -b shiny-new-feature
    
    # To update your branch with latest upstream changes:
    git fetch upstream
    git rebase upstream/main
  11. Perform set operations (Intersection and Difference)

    main

    You can use set-theoretic operations to find the intersection or difference between geometries. In a GeoDataFrame, you can access the geometry column via .geometry or use the shorthand operators:

    • Intersection: Use .intersection() or the & operator.
    • Difference: Use .difference() or the - operator.

    Example: boros.geometry - mp calculates the difference between the borough geometries and a multipolygon mp.