Geo neighbors
Find neighboring geographical units within a specified radius.
Using libpysal DistanceBand (fast, requires projected coordinates for accuracy).
build_neighbour_network(neighbours)
Build a NetworkX graph from a neighbour dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neighbours
|
dict[str, list[str]]
|
Mapping of unit ID to list of neighbour unit IDs. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
nx.Graph: Undirected graph with units as nodes and neighbour relationships as edges. |
Example
neighbours = {"A": ["B", "C"], "B": ["A"], "C": ["A"]} G = build_neighbour_network(neighbours) print(G.number_of_nodes(), G.number_of_edges()) 3 2
Source code in may/social_networks/builder_functions/geo/geo_neighbors.py
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 | |
find_neighbours(*args, method='libpysal', **kwargs)
Find neighbouring geographical units within a specified radius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Arguments passed to the underlying neighbour finder method. |
()
|
|
method
|
str
|
Method for finding neighbours. Options: 'libpysal' (fast, uses Euclidean distance), 'balltree' (accurate, uses haversine). |
'libpysal'
|
**kwargs
|
Keyword arguments passed to the underlying method, typically: - geo_units (list[GeographicalUnit]): Units with coordinates. - radius_km (float): Search radius in kilometers. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[id, list[id]]
|
dict[str, list[str]]: Mapping of unit ID to list of neighbour unit IDs. |
Example
from may.geography import Geography geo = Geography(data_dir="data/geography") geo.load_from_csv() units = list(geo.get_units_by_level("SGU").values()) neighbours = find_neighbours(units, radius_km=10.0, method='libpysal')
Source code in may/social_networks/builder_functions/geo/geo_neighbors.py
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 | |
register_neighbour_finder(name)
Decorator to register a neighbour finding method in the neighbour_finders registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the neighbour finder under. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Decorator function that registers the wrapped function. |
Example
@register_neighbour_finder("my_method") ... def find_neighbours_my_method(geo_units, radius_km): ... return {} neighbours = neighbour_finders"my_method"
Source code in may/social_networks/builder_functions/geo/geo_neighbors.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |