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

Fuzzy contiguity spatial weights

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.


Specify exact buffering distance. Ignores tolerance.

**kwargs: keyword arguments

optional arguments for pysal.weights.W

w: PySAL W

Spatial weights based on fuzzy contiguity. Weights are binary.


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.


Planar Enforcement:


>>> 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)
>>> 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})

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
>>> wfb = fuzzy_contiguity(gdf, buffering=True)
>>> wfb.islands
>>> wfb[2]
{1: 1.0}