Full Package Reference

This page contains the full reference for the gerrychain package, and is included primarily for the sake of developers trying to hunt down errors or understand the codebase. For a more user-friendly introduction to the usage of the package, please see the API Reference.

Accept

Chain

Grid

Metagraph

Tree

Constraints

Graph

This module provides a set of functions to help determine and manipulate the adjacency of geometries within a particular graph. The functions in this module are used internally to ensure that the geometry data that we are working with is sufficiently well-defined to be used for analysis.

Some of the type hints in this module are intentionally left unspecified because of import issues.

gerrychain.graph.adjacency.intersections_with_neighbors(geometries)[source]

Generator yielding tuples of the form (id, {neighbor_id: intersection}). The intersections may be empty!

Parameters

geometries (shapely.geometry.BaseGeometry) – A Shapeley geometry object.

Returns

A generator yielding tuples of the form (id, {neighbor_id: intersection})

Return type

Generator

gerrychain.graph.adjacency.neighboring_geometries(geometries, tree=None)[source]

Generator yielding tuples of the form (id, (ids of neighbors)).

Parameters
  • geometries (shapely.geometry.BaseGeometry) – A Shapeley geometry object to construct the tree from

  • tree (shapely.strtree.STRtree, optional) – A Sort-Tile-Recursive tree for spatial indexing. Default is None.

Returns

A generator yielding tuples of the form (id, (ids of neighbors))

Return type

Generator

gerrychain.graph.adjacency.neighbors(df: <Mock name='mock.GeoDataFrame' id='140019760569600'>, adjacency: str) Dict[source]
gerrychain.graph.adjacency.queen(geometries)[source]
Parameters

geometries (shapely.geometry.BaseGeometry) – A Shapeley geometry object.

Returns

The queen adjacency dictionary for the given collection of polygons.

Return type

Dict

gerrychain.graph.adjacency.rook(geometries)[source]
Parameters

geometries (shapely.geometry.BaseGeometry) – A Shapeley geometry object.

Returns

The rook adjacency dictionary for the given collection of polygons.

Return type

Dict

gerrychain.graph.adjacency.str_tree(geometries)[source]

Add ids to geometries and create a STR tree for spatial indexing. Use this for all spatial operations!

Parameters

geometries (shapely.geometry.BaseGeometry) – A Shapely geometry object to construct the tree from.

Returns

A Sort-Tile-Recursive tree for spatial indexing.

Return type

shapely.strtree.STRtree

gerrychain.graph.adjacency.warn_for_overlaps(intersection_pairs)[source]
Parameters

intersection_pairs (Iterable) – An iterable of tuples of the form (id, {neighbor_id: intersection})

Returns

A generator yielding tuples of intersection pairs

Return type

Generator

Raises

UserWarning if there are overlaps among the given polygons

This module contains functions for working with GeoDataFrames and Shapely geometries. Specifically, it contains functions for handling and reprojecting the Universal Transverse Mercator projection, and for identifying bad geometries within a given GeoDataFrame.

exception gerrychain.graph.geo.GeometryError[source]

Bases: Exception

Wrapper error class for projection failures. Changing a map’s projection may create invalid geometries, which may or may not be repairable using the .buffer(0) trick.

gerrychain.graph.geo.explain_validity(geo) str[source]

Given a geometry that is shapely interpretable, explain the validity. Light wrapper around shapely’s explain_validity.

Parameters

geo (shapely.geometry.BaseGeometry) – Shapely geometry object

Returns

Explanation for the validity of the geometry

Return type

str

gerrychain.graph.geo.identify_utm_zone(df: <Mock name='mock.GeoDataFrame' id='140019760569600'>) int[source]

Given a GeoDataFrame, identify the Universal Transverse Mercator zone number for the centroid of the geometries in the dataframe.

gerrychain.graph.geo.invalid_geometries(df)[source]

Given a GeoDataFrame, returns a list of row indices with invalid geometries.

Parameters

df (geopandas.GeoDataFrame) – The GeoDataFrame to examine

Returns

List of row indices with invalid geometries

Return type

list of int

gerrychain.graph.geo.reprojected(df)[source]
Returns a copy of df, projected into the coordinate reference system of a suitable

Universal Transverse Mercator zone.

Parameters

df (geopandas.GeoDataFrame) – The GeoDataFrame to reproject

Returns

A copy of df, projected into the coordinate reference system of a suitable UTM zone.

Return type

geopandas.GeoDataFrame

gerrychain.graph.geo.utm_of_point(point)[source]

Given a point, return the Universal Transverse Mercator zone number for that point.

Meta

Metrics

Partition

Proposals

Updaters