1
+ """
2
+ This module provides a set of functions to help determine and
3
+ manipulate the adjacency of geometries within a particular
4
+ graph. The functions in this module are used internally to ensure
5
+ that the geometry data that we are working with is sufficiently
6
+ well-defined to be used for analysis.
7
+
8
+ Some of the type hints in this module are intentionally left
9
+ unspecified because of import issues.
10
+ """
11
+
1
12
import warnings
13
+ from geopandas import GeoDataFrame
14
+ from typing import Dict
2
15
3
16
4
- def neighbors (df , adjacency ) :
17
+ def neighbors (df : GeoDataFrame , adjacency : str ) -> Dict :
5
18
if adjacency not in ("rook" , "queen" ):
6
19
raise ValueError (
7
20
"The adjacency parameter provided is not supported. "
@@ -12,8 +25,15 @@ def neighbors(df, adjacency):
12
25
13
26
14
27
def str_tree (geometries ):
15
- """Add ids to geometries and create a STR tree for spatial indexing.
28
+ """
29
+ Add ids to geometries and create a STR tree for spatial indexing.
16
30
Use this for all spatial operations!
31
+
32
+ :param geometries: A Shapely geometry object to construct the tree from.
33
+ :type geometries: shapely.geometry.BaseGeometry
34
+
35
+ :returns: A Sort-Tile-Recursive tree for spatial indexing.
36
+ :rtype: shapely.strtree.STRtree
17
37
"""
18
38
from shapely .strtree import STRtree
19
39
@@ -25,7 +45,17 @@ def str_tree(geometries):
25
45
26
46
27
47
def neighboring_geometries (geometries , tree = None ):
28
- """Generator yielding tuples of the form (id, (ids of neighbors))."""
48
+ """
49
+ Generator yielding tuples of the form (id, (ids of neighbors)).
50
+
51
+ :param geometries: A Shapeley geometry object to construct the tree from
52
+ :type geometries: shapely.geometry.BaseGeometry
53
+ :param tree: A Sort-Tile-Recursive tree for spatial indexing. Default is None.
54
+ :type tree: shapely.strtree.STRtree, optional
55
+
56
+ :returns: A generator yielding tuples of the form (id, (ids of neighbors))
57
+ :rtype: Generator
58
+ """
29
59
if tree is None :
30
60
tree = str_tree (geometries )
31
61
@@ -40,8 +70,15 @@ def neighboring_geometries(geometries, tree=None):
40
70
41
71
42
72
def intersections_with_neighbors (geometries ):
43
- """Generator yielding tuples of the form (id, {neighbor_id: intersection}).
73
+ """
74
+ Generator yielding tuples of the form (id, {neighbor_id: intersection}).
44
75
The intersections may be empty!
76
+
77
+ :param geometries: A Shapeley geometry object.
78
+ :type geometries: shapely.geometry.BaseGeometry
79
+
80
+ :returns: A generator yielding tuples of the form (id, {neighbor_id: intersection})
81
+ :rtype: Generator
45
82
"""
46
83
for i , neighbors in neighboring_geometries (geometries ):
47
84
intersections = {
@@ -51,6 +88,16 @@ def intersections_with_neighbors(geometries):
51
88
52
89
53
90
def warn_for_overlaps (intersection_pairs ):
91
+ """
92
+ :param intersection_pairs: An iterable of tuples of
93
+ the form (id, {neighbor_id: intersection})
94
+ :type intersection_pairs: Iterable
95
+
96
+ :returns: A generator yielding tuples of intersection pairs
97
+ :rtype: Generator
98
+
99
+ :raises: UserWarning if there are overlaps among the given polygons
100
+ """
54
101
overlaps = set ()
55
102
for i , intersections in intersection_pairs :
56
103
overlaps .update (
@@ -70,7 +117,13 @@ def warn_for_overlaps(intersection_pairs):
70
117
71
118
72
119
def queen (geometries ):
73
- """Return queen adjacency dictionary for the given collection of polygons."""
120
+ """
121
+ :param geometries: A Shapeley geometry object.
122
+ :type geometries: shapely.geometry.BaseGeometry
123
+
124
+ :returns: The queen adjacency dictionary for the given collection of polygons.
125
+ :rtype: Dict
126
+ """
74
127
intersection_pairs = warn_for_overlaps (intersections_with_neighbors (geometries ))
75
128
76
129
return {
@@ -84,11 +137,18 @@ def queen(geometries):
84
137
85
138
86
139
def rook (geometries ):
87
- """Return rook adjacency dictionary for the given collection of polygons."""
140
+ """
141
+ :param geometries: A Shapeley geometry object.
142
+ :type geometries: shapely.geometry.BaseGeometry
143
+
144
+ :returns: The rook adjacency dictionary for the given collection of polygons.
145
+ :rtype: Dict
146
+ """
88
147
return {
89
148
i : {j : data for j , data in neighbors .items () if data ["shared_perim" ] > 0 }
90
149
for i , neighbors in queen (geometries ).items ()
91
150
}
92
151
93
152
153
+ # Dictionary mapping adjacency types to their corresponding functions.
94
154
adjacencies = {"rook" : rook , "queen" : queen }
0 commit comments