Reference Guide#

Nonplanar Edges#

geoplanar.non_planar_edges(gdf)#

Find coincident nonplanar edges

Parameters:
gdfGeoDataFrame with polygon (multipolygon) GeoSeries
Returns:
missinglibpysal.graph.Graph

graph encoding nonplanar relationships between polygons

Examples

>>> c1 = [[0,0], [0, 10], [10, 10], [10, 0], [0, 0]]
>>> p1 = Polygon(c1)
>>> c2 = [[10, 2], [10, 8], [20, 8], [20, 2], [10, 2]]
>>> p2 = Polygon(c2)
>>> gdf = geopandas.GeoDataFrame(geometry=[p1, p2])
>>> geoplanar.non_planar_edges(gdf).adjacency
focal  neighbor
0      0           0
1      1           0
Name: weight, dtype: int64
geoplanar.is_planar_enforced(gdf, allow_gaps=False)#

Test if a geodataframe has any planar enforcement violations

Parameters:
gdf: GeoDataFrame with polygon geoseries for geometry
allow_gaps: boolean

If True, allow gaps in the polygonal coverage

Returns:
boolean
geoplanar.fix_npe_edges(gdf, inplace=False)#

Fix all npe intersecting edges in geoseries.

Returns:
gdf: GeoDataFrame with geometries respected planar edges.

Examples

>>> c1 = [[0,0], [0, 10], [10, 10], [10, 0], [0, 0]]
>>> p1 = Polygon(c1)
>>> c2 = [[10, 2], [10, 8], [20, 8], [20, 2], [10, 2]]
>>> p2 = Polygon(c2)
>>> gdf = geopandas.GeoDataFrame(geometry=[p1, p2])
>>> geoplanar.non_planar_edges(gdf)
defaultdict(set, {0: {1}})
>>> gdf1 = geoplanar.fix_npe_edges(gdf)
>>> geoplanar.non_planar_edges(gdf1)
defaultdict(set, {})
geoplanar.planar_enforce(gdf)#
geoplanar.insert_intersections(poly_a, poly_b)#

Correct two npe intersecting polygons by inserting intersection points on intersecting edges

geoplanar.self_intersecting_rings(gdf)#
geoplanar.check_validity(gdf)#

Gaps#

geoplanar.gaps(gdf)#

Find gaps in a geodataframe.

A gap (emply sliver polygon) is a set of points that:

  • are not contained by any of the geometries in the geoseries

  • are not contained by the external polygon

Parameters:
gdfGeoDataFrame with polygon (multipolygon) GeoSeries
Returns:
_gapsGeoDataFrame with gap polygons

Examples

>>> p1 = box(0,0,10,10)
>>> p2 = Polygon([(10,10), (12,8), (10,6), (12,4), (10,2), (20,5)])
>>> gdf = geopandas.GeoDataFrame(geometry=[p1,p2])
>>> h = geoplanar.gaps(gdf)
>>> h.area
array([4., 4.])
geoplanar.fill_gaps(gdf, gap_df=None, strategy='largest', inplace=False)#

Fill gaps in a GeoDataFrame by merging them with neighboring polygons.

Parameters:
gdfGeoDataFrame

A GeoDataFrame containing polygon or multipolygon geometries.

gap_dfGeoDataFrame, optional

A GeoDataFrame containing the gaps to be filled. If None, gaps will be automatically detected within gdf.

strategy{‘smallest’, ‘largest’, ‘compact’, None}, default ‘largest’
Strategy to determine how gaps are merged with neighboring polygons:
  • ‘smallest’: Merge each gap with the smallest neighboring polygon.

  • ‘largest’ : Merge each gap with the largest neighboring polygon.

  • ‘compact’Merge each gap with the neighboring polygon that results in

    the new polygon having the highest compactness (isoperimetric quotient).

  • NoneMerge each gap with the first available neighboring polygon

    (order is indeterminate).

inplacebool, default False

If True, modify the input GeoDataFrame in place. Otherwise, return a new GeoDataFrame with the gaps filled.

Returns:
GeoDataFrame or None

A new GeoDataFrame with gaps filled if inplace is False. Otherwise, modifies gdf in place and returns None.

geoplanar.snap(geometry, threshold)#

Snap geometries that are within threshold to each other

Only one of the pair of geometries identified as nearby will be snapped, the one with the lower index.

If the snapping heuristics leads to an invalid geometry, the function attempts to fix it using shapely.make_valid(), which may lead to multi-part geometries. If that happens, only the largest component is returned. Occasionally, this may lead to improper snapping.

Parameters:
geometryGeoDataFrame | GeoSeries

geometries to snap. Geometry type needs to be Polygon for all of them.

thresholdfloat

max distance between geometries to snap threshold should be ~10% larger than the distance between polygon edges to ensure snapping

Returns:
GeoSeries

GeoSeries with snapped geometries

Holes#

geoplanar.missing_interiors(gdf)#

Find any missing interiors.

For a planar enforced polygon layer, there should be no cases of a polygon being contained in another polygon. Instead the “contained” polygon is a hole in the “containing” polygon.

Parameters:
gdfGeoDataFrame with polygon (multipolygon) GeoSeries
Returns:
pairslist

tuples for each violation (i,j), where i is the index of the containing polygon, j is the index of the contained polygon

Examples

>>> p1 = box(0,0,10,10)
>>> p2 = box(1,1, 3,3)
>>> p3 = box(7,7, 9,9)
>>> gdf = geopandas.GeoDataFrame(geometry=[p1,p2,p3])
>>> mi = geoplanar.missing_interiors(gdf)
>>> mi
[(0, 1), (0, 2)]
geoplanar.add_interiors(gdf, inplace=False)#

Add any missing interiors.

For a planar enforced polygon layer, there should be no cases of a polygon being contained in another polygon. Instead the “contained” polygon is a hole in the “containing” polygon. This function finds and corrects any such violations.

Parameters:
gdfGeoDataFrame with polygon (multipolygon) GeoSeries
inplace: boolean (default: False)

Change the geoseries of current dataframe

Returns:
gdfGeoDataFrame

Examples

>>> p1 = box(0,0,10,10)
>>> p2 = box(1,1, 3,3)
>>> p3 = box(7,7, 9,9)
>>> gdf = geopandas.GeoDataFrame(geometry=[p1,p2,p3])
>>> gdf.area
0   100.0
1     4.0
2     4.0
>>> mi = geoplanar.missing_interiors(gdf)
>>> mi
[(0, 1), (0, 2)]
>>> gdf1 = geoplanar.add_interiors(gdf)
>>> gdf1.area
0    92.0
1     4.0
2     4.0

Overlaps#

geoplanar.is_overlapping(gdf)#

Test for overlapping features in geoseries.

geoplanar.overlaps(gdf)#

Check for overlapping geometries in the GeoDataFrame.

Parameters:
gdf: GeoDataFrame with polygon geometries
Returns:
array-like: Pairs of indices with overlapping geometries.
geoplanar.trim_overlaps(gdf, strategy='largest', inplace=False)#

Trim overlapping polygons

Parameters:
gdf: geodataframe with polygon geometries
strategy{‘smallest’, ‘largest’, ‘compact’, None}, default ‘largest’
Strategy to determine which polygon to trim.
  • ‘smallest’: Trim the smallest polygon.

  • ‘largest’ : Trim the largest polygon.

  • ‘compact’Trim the polygon yielding the most compact modified polygon.

    (isoperimetric quotient).

  • None : Trim either polygon non-deterministically but performantly.

Returns:
gdf: geodataframe with corrected geometries
geoplanar.merge_overlaps(gdf, merge_limit, overlap_limit)#

Merge overlapping polygons based on a set of conditions.

Overlapping polygons smaller than merge_limit are merged to a neighboring polygon.

Polygons larger than merge_limit are merged to neighboring if they share area larger than area * overlap_limit.

Parameters:
gdfGeoDataFrame

GeoDataFrame with polygon or mutli polygon geometry

merge_limitfloat

area of overlapping polygons that are to be merged with neighbors no matter the size of the overlap

overlap_limitfloat (0-1)

ratio of area of an overlapping polygon that has to be shared with other polygon to merge both into one

Returns:
GeoDataFrame

Notes

The original index is not preserved.

geoplanar.merge_touching(gdf, index, largest=None)#

Merge or remove polygons based on a set of conditions.

If polygon does not share any boundary with another polygon, remove. If it shares some boundary with a neighbouring polygon, join to that polygon. If largest=None it picks one randomly, otherwise it picks the polygon with which it shares the largest (True) or the smallest (False) boundary.

Parameters:
gdfGeoDataFrame

GeoDataFrame with polygon or mutli polygon geometry

indexlist of indexes

list of indexes of polygons in gdf to merge or remove

largestbool (default None)

Merge with the polygon with the largest (True) or smallest (False) shared boundary. If None, merge with any neighbor non-deterministically but performantly.

Returns:
GeoDataFrame

Notes

The original index is not preserved.