forked from pysal/momepy
/
distribution.py
918 lines (772 loc) · 29.2 KB
/
distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# distribution.py
# definitions of spatial distribution characters
import math
import networkx as nx
import numpy as np
import pandas as pd
from tqdm.auto import tqdm # progress bar
from .utils import _azimuth
__all__ = [
"Orientation",
"SharedWalls",
"SharedWallsRatio",
"StreetAlignment",
"CellAlignment",
"Alignment",
"NeighborDistance",
"MeanInterbuildingDistance",
"NeighboringStreetOrientationDeviation",
"BuildingAdjacency",
"Neighbors",
]
class Orientation:
"""
Calculate the orientation of object
Captures the deviation of orientation from cardinal directions.
Defined as an orientation of the longext axis of bounding rectangle in range 0 - 45.
Orientation of LineStrings is represented by the orientation of line
connecting first and last point of the segment.
Adapted from :cite:`schirmer2015`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
Examples
--------
>>> buildings_df['orientation'] = momepy.Orientation(buildings_df).series
100%|██████████| 144/144 [00:00<00:00, 630.54it/s]
>>> buildings_df['orientation'][0]
41.05146788287027
"""
def __init__(self, gdf, verbose=True):
self.gdf = gdf
# define empty list for results
results_list = []
def _dist(a, b):
return math.hypot(b[0] - a[0], b[1] - a[1])
for geom in tqdm(gdf.geometry, total=gdf.shape[0], disable=not verbose):
if geom.type in ["Polygon", "MultiPolygon", "LinearRing"]:
# TODO: vectorize once minimum_rotated_rectangle is in geopandas
bbox = list(geom.minimum_rotated_rectangle.exterior.coords)
axis1 = _dist(bbox[0], bbox[3])
axis2 = _dist(bbox[0], bbox[1])
if axis1 <= axis2:
az = _azimuth(bbox[0], bbox[1])
else:
az = _azimuth(bbox[0], bbox[3])
elif geom.type in ["LineString", "MultiLineString"]:
coords = geom.coords
az = _azimuth(coords[0], coords[-1])
else:
results_list.append(np.nan)
continue
if 90 > az >= 45:
diff = az - 45
az = az - 2 * diff
elif 135 > az >= 90:
diff = az - 90
az = az - 2 * diff
diff = az - 45
az = az - 2 * diff
elif 181 > az >= 135:
diff = az - 135
az = az - 2 * diff
diff = az - 90
az = az - 2 * diff
diff = az - 45
az = az - 2 * diff
results_list.append(az)
self.series = pd.Series(results_list, index=gdf.index)
class SharedWalls:
"""
Calculate the length of shared walls of adjacent elements (typically buildings)
.. math::
\\textit{length of shared walls}
Note that data needs to be topologically correct. Overlapping polygons will lead to
incorrect results.
Adapted from :cite:`hamaina2012a`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing gdf to analyse
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
Examples
--------
>>> buildings_df['swr'] = momepy.SharedWalls(buildings_df).series
See also
--------
SharedWallsRatio
"""
def __init__(self, gdf):
self.gdf = gdf
inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
left = gdf.geometry.take(inp).reset_index(drop=True)
right = gdf.geometry.take(res).reset_index(drop=True)
intersections = left.intersection(right).length
results = intersections.groupby(inp).sum().reset_index(
drop=True
) - gdf.geometry.length.reset_index(drop=True)
results.index = gdf.index
self.series = results
class SharedWallsRatio(SharedWalls):
"""
Calculate shared walls ratio of adjacent elements (typically buildings)
.. math::
\\textit{length of shared walls} \\over perimeter
Note that data needs to be topologically correct. Overlapping polygons will lead to
incorrect results.
Adapted from :cite:`hamaina2012a`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing gdf to analyse
perimeters : str, list, np.array, pd.Series (default None, optional)
the name of the dataframe column, ``np.array``, or ``pd.Series`` where is
stored perimeter value
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
perimeters : GeoDataFrame
Series containing used perimeters values
Examples
--------
>>> buildings_df['swr'] = momepy.SharedWallsRatio(buildings_df).series
>>> buildings_df['swr'][10]
0.3424804411228673
See also
--------
SharedWalls
"""
def __init__(self, gdf, perimeters=None):
super(SharedWallsRatio, self).__init__(gdf)
if perimeters is None:
self.perimeters = gdf.geometry.length
elif isinstance(perimeters, str):
self.perimeters = gdf[perimeters]
else:
self.perimeters = perimeters
self.series = self.series / self.perimeters
class StreetAlignment:
"""
Calculate the difference between street orientation and orientation of object in
degrees
Orientation of street segment is represented by the orientation of line
connecting first and last point of the segment. Network ID linking each object
to specific street segment is needed. Can be generated by
:func:`momepy.get_network_id`.
Either ``network_id`` or both ``left_network_id`` and ``right_network_id``
are required.
.. math::
\\left|{\\textit{building orientation} - \\textit{street orientation}}\\right|
Parameters
----------
left : GeoDataFrame
GeoDataFrame containing objects to analyse
right : GeoDataFrame
GeoDataFrame containing street network
orientations : str, list, np.array, pd.Series
the name of the dataframe column, ``np.array``, or ``pd.Series`` where is
stored object orientation value
(can be calculated using :class:`momepy.Orientation`)
network_id : str (default None)
the name of the column storing network ID in both left and right
left_network_id : str, list, np.array, pd.Series (default None)
the name of the left dataframe column, ``np.array``, or ``pd.Series`` where is
stored object network ID
right_network_id : str, list, np.array, pd.Series (default None)
the name of the right dataframe column, ``np.array``, or ``pd.Series`` of
streets with unique network id (has to be defined beforehand)
(can be defined using :func:`momepy.unique_id`)
Attributes
----------
series : Series
Series containing resulting values
left : GeoDataFrame
original left GeoDataFrame
right : GeoDataFrame
original right GeoDataFrame
network_id : str
the name of the column storing network ID in both left and right
left_network_id : Series
Series containing used left ID
right_network_id : Series
Series containing used right ID
Examples
--------
>>> buildings_df['street_alignment'] = momepy.StreetAlignment(buildings_df,
... streets_df,
... 'orientation',
... 'nID',
... 'nID').series
>>> buildings_df['street_alignment'][0]
0.29073888476702336
"""
def __init__(
self,
left,
right,
orientations,
network_id=None,
left_network_id=None,
right_network_id=None,
):
self.left = left
self.right = right
self.network_id = network_id
left = left.copy()
right = right.copy()
if network_id:
left_network_id = network_id
right_network_id = network_id
else:
if left_network_id is None and right_network_id is not None:
raise ValueError("left_network_id not set.")
if left_network_id is not None and right_network_id is None:
raise ValueError("right_network_id not set.")
if left_network_id is None and right_network_id is None:
raise ValueError(
"Network ID not set. Use either network_id or left_network_id "
"and right_network_id."
)
if not isinstance(orientations, str):
left["mm_o"] = orientations
orientations = "mm_o"
self.orientations = left[orientations]
if not isinstance(left_network_id, str):
left["mm_nid"] = left_network_id
left_network_id = "mm_nid"
self.left_network_id = left[left_network_id]
if not isinstance(right_network_id, str):
right["mm_nis"] = right_network_id
right_network_id = "mm_nis"
self.right_network_id = right[right_network_id]
right["_orientation"] = Orientation(right, verbose=False).series
merged = left[[left_network_id, orientations]].merge(
right[[right_network_id, "_orientation"]],
left_on=left_network_id,
right_on=right_network_id,
how="left",
)
self.series = np.abs(merged[orientations] - merged["_orientation"])
self.series.index = left.index
class CellAlignment:
"""
Calculate the difference between cell orientation and orientation of object
.. math::
\\left|{\\textit{building orientation} - \\textit{cell orientation}}\\right|
Parameters
----------
left : GeoDataFrame
GeoDataFrame containing objects to analyse
right : GeoDataFrame
GeoDataFrame containing tessellation cells (or relevant spatial units)
left_orientations : str, list, np.array, pd.Series
the name of the left dataframe column, ``np.array``, or ``pd.Series`` where is
stored object orientation value
(can be calculated using :class:`momepy.Orientation`)
right_orientations : str, list, np.array, pd.Series
the name of the right dataframe column, ``np.array``, or ``pd.Series`` where is
stored object orientation value
(can be calculated using :class:`momepy.Orientation`)
left_unique_id : str
the name of the ``left`` dataframe column with unique id shared between ``left``
and ``right`` gdf
right_unique_id : str
the name of the ``right`` dataframe column with unique id shared between
``left`` and ``right`` gdf
Attributes
----------
series : Series
Series containing resulting values
left : GeoDataFrame
original left GeoDataFrame
right : GeoDataFrame
original right GeoDataFrame
left_orientations : Series
Series containing used left orientations
right_orientations : Series
Series containing used right orientations
left_unique_id : Series
Series containing used left ID
right_unique_id : Series
Series containing used right ID
Examples
--------
>>> buildings_df['cell_alignment'] = momepy.CellAlignment(buildings_df,
... tessellation_df,
... 'bl_orient',
... 'tes_orient',
... 'uID',
... 'uID').series
>>> buildings_df['cell_alignment'][0]
0.8795123936951939
"""
def __init__(
self,
left,
right,
left_orientations,
right_orientations,
left_unique_id,
right_unique_id,
):
self.left = left
self.right = right
left = left.copy()
right = right.copy()
if not isinstance(left_orientations, str):
left["mm_o"] = left_orientations
left_orientations = "mm_o"
self.left_orientations = left[left_orientations]
if not isinstance(right_orientations, str):
right["mm_o"] = right_orientations
right_orientations = "mm_o"
self.right_orientations = right[right_orientations]
self.left_unique_id = left[left_unique_id]
self.right_unique_id = right[right_unique_id]
comp = left[[left_unique_id, left_orientations]].merge(
right[[right_unique_id, right_orientations]],
left_on=left_unique_id,
right_on=right_unique_id,
how="left",
)
if left_orientations == right_orientations:
left_orientations = left_orientations + "_x"
right_orientations = right_orientations + "_y"
self.series = np.absolute(comp[left_orientations] - comp[right_orientations])
self.series.index = left.index
class Alignment:
"""
Calculate the mean deviation of solar orientation of objects on adjacent cells
from an object
.. math::
\\frac{1}{n}\\sum_{i=1}^n dev_i=\\frac{dev_1+dev_2+\\cdots+dev_n}{n}
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
spatial_weights : libpysal.weights, optional
spatial weights matrix
orientations : str, list, np.array, pd.Series
the name of the left dataframe column, ``np.array``, or ``pd.Series`` where is
stored object orientation value
(can be calculated using :class:`momepy.Orientation`)
unique_id : str
name of the column with unique id used as ``spatial_weights`` index.
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
orientations : Series
Series containing used orientation values
sw : libpysal.weights
spatial weights matrix
id : Series
Series containing used unique ID
Examples
--------
>>> buildings_df['alignment'] = momepy.Alignment(buildings_df,
... sw,
... 'uID',
... bl_orient).series
100%|██████████| 144/144 [00:01<00:00, 140.84it/s]
>>> buildings_df['alignment'][0]
18.299481296455237
"""
def __init__(self, gdf, spatial_weights, unique_id, orientations, verbose=True):
self.gdf = gdf
self.sw = spatial_weights
self.id = gdf[unique_id]
# define empty list for results
results_list = []
gdf = gdf.copy()
if not isinstance(orientations, str):
gdf["mm_o"] = orientations
orientations = "mm_o"
self.orientations = gdf[orientations]
data = gdf.set_index(unique_id)[orientations]
# iterating over rows one by one
for index, orient in tqdm(
data.iteritems(), total=data.shape[0], disable=not verbose
):
if index in spatial_weights.neighbors.keys():
neighbours = spatial_weights.neighbors[index]
if neighbours:
orientation = data.loc[neighbours]
results_list.append(abs(orientation - orient).mean())
else:
results_list.append(np.nan)
else:
results_list.append(np.nan)
self.series = pd.Series(results_list, index=gdf.index)
class NeighborDistance:
"""
Calculate the mean distance to adjacent buildings (based on ``spatial_weights``)
If no neighbours are found, return ``np.nan``.
.. math::
\\frac{1}{n}\\sum_{i=1}^n dist_i=\\frac{dist_1+dist_2+\\cdots+dist_n}{n}
Adapted from :cite:`schirmer2015`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
spatial_weights : libpysal.weights
spatial weights matrix based on unique_id
unique_id : str
name of the column with unique id used as ``spatial_weights`` index.
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
sw : libpysal.weights
spatial weights matrix
id : Series
Series containing used unique ID
Examples
--------
>>> buildings_df['neighbour_distance'] = momepy.NeighborDistance(buildings_df,
... sw,
... 'uID').series
100%|██████████| 144/144 [00:00<00:00, 345.78it/s]
>>> buildings_df['neighbour_distance'][0]
29.18589019096464
"""
def __init__(self, gdf, spatial_weights, unique_id, verbose=True):
self.gdf = gdf
self.sw = spatial_weights
self.id = gdf[unique_id]
# define empty list for results
results_list = []
data = gdf.set_index(unique_id).geometry
# iterating over rows one by one
for index, geom in tqdm(
data.iteritems(), total=data.shape[0], disable=not verbose
):
if geom is not None and index in spatial_weights.neighbors.keys():
neighbours = spatial_weights.neighbors[index]
building_neighbours = data.loc[neighbours]
if len(building_neighbours) > 0:
results_list.append(
building_neighbours.geometry.distance(geom).mean()
)
else:
results_list.append(np.nan)
else:
results_list.append(np.nan)
self.series = pd.Series(results_list, index=gdf.index)
class MeanInterbuildingDistance:
"""
Calculate the mean interbuilding distance
Interbuilding distances are calculated between buildings on adjacent cells based on
``spatial_weights``, while the extent is defined as order of contiguity.
.. math::
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
unique_id : str
name of the column with unique id used as ``spatial_weights`` index
spatial_weights : libpysal.weights
spatial weights matrix
order : int
Order of contiguity defining the extent
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
sw : libpysal.weights
spatial weights matrix
id : Series
Series containing used unique ID
sw_higher : libpysal.weights
Spatial weights matrix of higher order
order : int
Order of contiguity.
Notes
-----
Fix UserWarning.
Examples
--------
>>> buildings_df['mean_interbuilding_distance'] = momepy.MeanInterbuildingDistance(
... buildings_df,
... sw,
... 'uID'
... ).series
Computing mean interbuilding distances...
100%|██████████| 144/144 [00:00<00:00, 317.42it/s]
>>> buildings_df['mean_interbuilding_distance'][0]
29.305457092042744
"""
def __init__(
self,
gdf,
spatial_weights,
unique_id,
spatial_weights_higher=None,
order=3,
verbose=True,
):
self.gdf = gdf
self.sw = spatial_weights
self.id = gdf[unique_id]
data = gdf.set_index(unique_id).geometry
# define empty list for results
results_list = []
# define adjacency list from lipysal
adj_list = spatial_weights.to_adjlist()
adj_list["weight"] = (
data.loc[adj_list.focal]
.reset_index(drop=True)
.distance(data.loc[adj_list.neighbor].reset_index(drop=True))
.values
)
# generate graph
G = nx.from_pandas_edgelist(
adj_list, source="focal", target="neighbor", edge_attr="weight"
)
print("Computing mean interbuilding distances...") if verbose else None
# iterate over subgraphs to get the final values
for uid in tqdm(data.index, total=data.shape[0], disable=not verbose):
try:
sub = nx.ego_graph(G, uid, radius=order)
results_list.append(
np.nanmean([x[-1] for x in list(sub.edges.data("weight"))])
)
except Exception:
results_list.append(np.nan)
self.series = pd.Series(results_list, index=gdf.index)
class NeighboringStreetOrientationDeviation:
"""
Calculate the mean deviation of solar orientation of adjacent streets
Orientation of street segment is represented by the orientation of line
connecting first and last point of the segment.
.. math::
\\frac{1}{n}\\sum_{i=1}^n dev_i=\\frac{dev_1+dev_2+\\cdots+dev_n}{n}
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing street network to analyse
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
orientation : Series
Series containing used street orientation values
Examples
--------
>>> streets_df['orient_dev'] = momepy.NeighboringStreetOrientationDeviation(
... streets_df
... ).series
>>> streets_df['orient_dev'][6]
7.043096518688273
"""
def __init__(self, gdf):
self.gdf = gdf
self.orientation = gdf.geometry.apply(self._orient)
inp, res = gdf.sindex.query_bulk(gdf.geometry, predicate="intersects")
itself = inp == res
inp = inp[~itself]
res = res[~itself]
left = self.orientation.take(inp).reset_index(drop=True)
right = self.orientation.take(res).reset_index(drop=True)
deviations = (left - right).abs()
results = deviations.groupby(inp).mean()
match = gdf.iloc[list(results.index)]
match["result"] = results.to_list()
self.series = match.result
def _orient(self, geom):
start = geom.coords[0]
end = geom.coords[-1]
az = _azimuth(start, end)
if 90 > az >= 45:
diff = az - 45
az = az - 2 * diff
elif 135 > az >= 90:
diff = az - 90
az = az - 2 * diff
diff = az - 45
az = az - 2 * diff
elif 181 > az >= 135:
diff = az - 135
az = az - 2 * diff
diff = az - 90
az = az - 2 * diff
diff = az - 45
az = az - 2 * diff
return az
class BuildingAdjacency:
"""
Calculate the level of building adjacency
Building adjacency reflects how much buildings tend to join together into larger
structures.
It is calculated as a ratio of joined built-up structures and buildings within
the extent defined in ``spatial_weights_higher``.
Adapted from :cite:`vanderhaegen2017`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
spatial_weights_higher : libpysal.weights
spatial weights matrix
unique_id : str
name of the column with unique id used as ``spatial_weights`` index
spatial_weights : libpysal.weights, optional
spatial weights matrix - If None, Queen contiguity matrix will be calculated
based on gdf. It is to denote adjacent buildings (note: based on unique ID).
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
sw_higher : libpysal.weights
spatial weights matrix
id : Series
Series containing used unique ID
sw : libpysal.weights
spatial weights matrix
Examples
--------
>>> buildings_df['adjacency'] = momepy.BuildingAdjacency(buildings_df,
... swh,
... unique_id='uID').series
Calculating spatial weights...
Spatial weights ready...
Calculating adjacency: 100%|██████████| 144/144 [00:00<00:00, 335.55it/s]
>>> buildings_df['adjacency'][10]
0.23809523809523808
"""
def __init__(
self, gdf, spatial_weights_higher, unique_id, spatial_weights=None, verbose=True
):
self.gdf = gdf
self.sw_higher = spatial_weights_higher
self.id = gdf[unique_id]
results_list = []
# if weights matrix is not passed, generate it from gdf
if spatial_weights is None:
print("Calculating spatial weights...") if verbose else None
from libpysal.weights import Queen
spatial_weights = Queen.from_dataframe(
gdf, silence_warnings=True, ids=unique_id
)
print("Spatial weights ready...") if verbose else None
self.sw = spatial_weights
patches = dict(zip(gdf[unique_id], spatial_weights.component_labels))
for uid in tqdm(
self.id,
total=gdf.shape[0],
disable=not verbose,
desc="Calculating adjacency",
):
if uid in spatial_weights_higher.neighbors.keys():
neighbours = spatial_weights_higher.neighbors[uid].copy()
if neighbours:
neighbours.append(uid)
patches_sub = [patches[x] for x in neighbours]
patches_nr = len(set(patches_sub))
results_list.append(patches_nr / len(neighbours))
else:
results_list.append(np.nan)
else:
results_list.append(np.nan)
self.series = pd.Series(results_list, index=gdf.index)
class Neighbors:
"""
Calculate the number of neighbours captured by ``spatial_weights``
If ``weighted=True``, number of neighbours will be divided by the perimeter of
object to return relative value.
Adapted from :cite:`hermosilla2012`.
Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
spatial_weights : libpysal.weights
spatial weights matrix
unique_id : str
name of the column with unique id used as ``spatial_weights`` index
weighted : bool (default False)
if ``True``, number of neighbours will be divided by the perimeter of object,
to return relative value
verbose : bool (default True)
if True, shows progress bars in loops and indication of steps
Attributes
----------
series : Series
Series containing resulting values
gdf : GeoDataFrame
original GeoDataFrame
values : Series
Series containing used values
sw : libpysal.weights
spatial weights matrix
id : Series
Series containing used unique ID
weighted : bool
used weighted value
Examples
--------
>>> sw = libpysal.weights.contiguity.Queen.from_dataframe(tessellation_df,
... ids='uID')
>>> tessellation_df['neighbours'] = momepy.Neighbors(tessellation_df,
... sw,
... 'uID').series
100%|██████████| 144/144 [00:00<00:00, 6909.50it/s]
>>> tessellation_df['neighbours'][0]
4
"""
def __init__(self, gdf, spatial_weights, unique_id, weighted=False, verbose=True):
self.gdf = gdf
self.sw = spatial_weights
self.id = gdf[unique_id]
self.weighted = weighted
neighbours = []
for index, geom in tqdm(
gdf.set_index(unique_id).geometry.iteritems(),
total=gdf.shape[0],
disable=not verbose,
):
if index in spatial_weights.neighbors.keys():
if weighted is True:
neighbours.append(
spatial_weights.cardinalities[index] / geom.length
)
else:
neighbours.append(spatial_weights.cardinalities[index])
else:
neighbours.append(np.nan)
self.series = pd.Series(neighbours, index=gdf.index)