Skip to content

Commit 85cb536

Browse files
authored
Merge pull request #280 from martinfleis/fuzzy
ENH: allow specific buffer in fuzzy_contiguity
2 parents 57c44c7 + 5e9e83e commit 85cb536

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

libpysal/weights/tests/test_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def test_fuzzy_contiguity(self):
248248
wf = fuzzy_contiguity(rs_df)
249249
self.assertEqual(wf.islands, [])
250250
self.assertEqual(set(wf.neighbors[0]), set([239, 59, 152, 23, 107]))
251+
buff = fuzzy_contiguity(rs_df, buffering=True, buffer=.1)
252+
self.assertEqual(set(buff.neighbors[0]), set([119, 239, 59, 152, 23, 107]))
251253

252254

253255
suite = unittest.TestLoader().loadTestsFromTestCase(Testutil)

libpysal/weights/util.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def w_local_cluster(w):
553553
neighbors of observation :math:`i` are to being a clique:
554554
555555
.. math::
556-
556+
557557
c_i = | \{w_{j,k}\} |/ (k_i(k_i - 1)): j,k \in N_i
558558
559559
where :math:`N_i` is the set of neighbors to :math:`i`, :math:`k_i =
@@ -1454,7 +1454,7 @@ def nonplanar_neighbors(w, geodataframe, tolerance=0.001, **kwargs):
14541454
return w
14551455

14561456
@requires('geopandas')
1457-
def fuzzy_contiguity(gdf, tolerance=0.005, buffering=False, drop=True, **kwargs):
1457+
def fuzzy_contiguity(gdf, tolerance=0.005, buffering=False, drop=True, buffer=None, **kwargs):
14581458
"""
14591459
Fuzzy contiguity spatial weights
14601460
@@ -1472,6 +1472,10 @@ def fuzzy_contiguity(gdf, tolerance=0.005, buffering=False, drop=True, **kwargs)
14721472
14731473
drop: boolean
14741474
If True (default), the buffered features are removed from the GeoDataFrame. If False, buffered features are added to the GeoDataFrame.
1475+
1476+
buffer : float
1477+
Specify exact buffering distance. Ignores `tolerance`.
1478+
14751479
**kwargs: keyword arguments
14761480
optional arguments for :class:`pysal.weights.W`
14771481
@@ -1545,11 +1549,12 @@ def fuzzy_contiguity(gdf, tolerance=0.005, buffering=False, drop=True, **kwargs)
15451549
15461550
"""
15471551
if buffering:
1548-
# buffer each shape
1549-
minx, miny, maxx, maxy = gdf.total_bounds
1550-
buffer = tolerance * 0.5 * abs(min(maxx-minx, maxy-miny))
1552+
if not buffer:
1553+
# buffer each shape
1554+
minx, miny, maxx, maxy = gdf.total_bounds
1555+
buffer = tolerance * 0.5 * abs(min(maxx-minx, maxy-miny))
15511556
# create new geometry column
1552-
new_geometry = gpd.GeoSeries([feature.buffer(buffer) for feature in gdf.geometry])
1557+
new_geometry = gdf.geometry.buffer(buffer)
15531558
gdf['_buffer'] = new_geometry
15541559
old_geometry_name = gdf.geometry.name
15551560
gdf.set_geometry('_buffer', inplace=True)

0 commit comments

Comments
 (0)