libpysal.weights.fuzzy_contiguity

libpysal.weights.fuzzy_contiguity(gdf, tolerance=0.005, buffering=False, drop=True, buffer=None, predicate='intersects', **kwargs)[source]

Fuzzy contiguity spatial weights

Parameters:
gdf: GeoDataFrame
tolerance: float

The percentage of the length of the minimum side of the bounding rectangle for the GeoDataFrame to use in determining the buffering distance.

buffering: boolean

If False (default) joins will only be detected for features that intersect (touch, contain, within). If True then features will be buffered and intersections will be based on buffered features.

drop: boolean

If True (default), the buffered features are removed from the GeoDataFrame. If False, buffered features are added to the GeoDataFrame.

bufferfloat

Specify exact buffering distance. Ignores tolerance.

predicate{‘intersects’, ‘within’, ‘contains’, ‘overlaps’, ‘crosses’, ‘touches’}

The predicate to use for determination of neighbors. Default is ‘intersects’. If None is passed, neighbours are determined based on the intersection of bounding boxes.

**kwargs: keyword arguments

optional arguments for pysal.weights.W

Returns:
w: PySAL W

Spatial weights based on fuzzy contiguity. Weights are binary.

Notes

This relaxes the notion of contiguity neighbors for the case of feature collections that violate the condition of planar enforcement. It handles three types of conditions present in such collections that would result in islands when using the regular PySAL contiguity methods. The first are edges for nearby polygons that should be shared, but are digitized separately for the individual polygons and the resulting edges do not coincide, but instead the edges intersect. The second case is similar to the first, only the resultant edges do not intersect but are “close”. The final case arises when one polygon is “inside” a second polygon but is not encoded to represent a hole in the containing polygon.

Detection of the second case will require setting buffering=True and exploring different values for tolerance.

The buffering check assumes the geometry coordinates are projected.

References

Planar Enforcement: http://ibis.geog.ubc.ca/courses/klink/gis.notes/ncgia/u12.html#SEC12.6

Examples

>>> import libpysal
>>> from libpysal.weights import fuzzy_contiguity
>>> import geopandas as gpd
>>> rs = libpysal.examples.get_path('map_RS_BR.shp')
>>> rs_df = gpd.read_file(rs)
>>> wq = libpysal.weights.Queen.from_dataframe(rs_df)
>>> len(wq.islands)
29
>>> wq[0]
{}
>>> wf = fuzzy_contiguity(rs_df)
>>> wf.islands
[]
>>> wf[0] == dict({239: 1.0, 59: 1.0, 152: 1.0, 23: 1.0, 107: 1.0})
True

Example needing to use buffering

>>> from shapely.geometry import Polygon
>>> p0 = Polygon([(0,0), (10,0), (10,10)])
>>> p1 = Polygon([(10,1), (10,2), (15,2)])
>>> p2 = Polygon([(12,2.001), (14, 2.001), (13,10)])
>>> gs = gpd.GeoSeries([p0,p1,p2])
>>> gdf = gpd.GeoDataFrame(geometry=gs)
>>> wf = fuzzy_contiguity(gdf)
>>> wf.islands
[2]
>>> wfb = fuzzy_contiguity(gdf, buffering=True)
>>> wfb.islands
[]
>>> wfb[2]
{1: 1.0}

Example with a custom index

>>> rs_df_ix = rs_df.set_index("NM_MUNICIP")
>>> wf_ix = fuzzy_contiguity(rs_df)
>>> wf_ix.neighbors["TAVARES"]
['SÃO JOSÉ DO NORTE', 'MOSTARDAS']