|
8 | 8 | import geopandas as gpd
|
9 | 9 |
|
10 | 10 |
|
| 11 | +def radius(gpd_df, cpt, radius): |
| 12 | + """ |
| 13 | + Get a list of indices of objects within radius. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + gpd_df : GeoDataFrame |
| 18 | + geopandas gdf containing point objects to analyse |
| 19 | + cpt : shapely.Point |
| 20 | + shapely point representing the center of radius |
| 21 | + radius : float |
| 22 | + radius |
| 23 | +
|
| 24 | + Returns |
| 25 | + ------- |
| 26 | + list |
| 27 | + Return only the neighbour indices, sorted by distance in ascending order |
| 28 | +
|
| 29 | + Reference |
| 30 | + --------- |
| 31 | + https://stackoverflow.com/questions/44622233/rtree-count-points-in-the-neighbourhoods-within-each-point-of-another-set-of-po |
| 32 | +
|
| 33 | + """ |
| 34 | + # Spatial index |
| 35 | + sindex = gpd_df.sindex |
| 36 | + # Bounding box of rtree search (West, South, East, North) |
| 37 | + bbox = (cpt.x - radius, cpt.y - radius, cpt.x + radius, cpt.y + radius) |
| 38 | + # Potential neighbours |
| 39 | + good = [] |
| 40 | + for n in sindex.intersection(bbox): |
| 41 | + dist = cpt.distance(gpd_df['geometry'][n]) |
| 42 | + if dist < radius: |
| 43 | + good.append((dist, n)) |
| 44 | + # Sort list in ascending order by `dist`, then `n` |
| 45 | + good.sort() |
| 46 | + # Return only the neighbour indices, sorted by distance in ascending order |
| 47 | + return [x[1] for x in good] |
| 48 | + |
| 49 | + |
11 | 50 | def frequency(objects, look_for, column_name, id_column='uID'):
|
12 | 51 | """
|
13 | 52 | Calculate frequency (count) of objects in a given radius.
|
@@ -36,43 +75,7 @@ def frequency(objects, look_for, column_name, id_column='uID'):
|
36 | 75 | ---------
|
37 | 76 |
|
38 | 77 | """
|
39 |
| - def radius(gpd_df, cpt, radius): |
40 |
| - """ |
41 |
| - Get a list of indices of objects within radius. |
42 |
| -
|
43 |
| - Parameters |
44 |
| - ---------- |
45 |
| - gpd_df : GeoDataFrame |
46 |
| - geopandas gdf containing point objects to analyse |
47 |
| - cpt : shapely.Point |
48 |
| - shapely point representing the center of radius |
49 |
| - radius : float |
50 |
| - radius |
51 |
| -
|
52 |
| - Returns |
53 |
| - ------- |
54 |
| - list |
55 |
| - Return only the neighbour indices, sorted by distance in ascending order |
56 |
| -
|
57 |
| - Reference |
58 |
| - --------- |
59 |
| - https://stackoverflow.com/questions/44622233/rtree-count-points-in-the-neighbourhoods-within-each-point-of-another-set-of-po |
60 |
| -
|
61 |
| - """ |
62 |
| - # Spatial index |
63 |
| - sindex = gpd_df.sindex |
64 |
| - # Bounding box of rtree search (West, South, East, North) |
65 |
| - bbox = (cpt.x - radius, cpt.y - radius, cpt.x + radius, cpt.y + radius) |
66 |
| - # Potential neighbours |
67 |
| - good = [] |
68 |
| - for n in sindex.intersection(bbox): |
69 |
| - dist = cpt.distance(gpd_df['geometry'][n]) |
70 |
| - if dist < radius: |
71 |
| - good.append((dist, n)) |
72 |
| - # Sort list in ascending order by `dist`, then `n` |
73 |
| - good.sort() |
74 |
| - # Return only the neighbour indices, sorted by distance in ascending order |
75 |
| - return [x[1] for x in good] |
| 78 | + |
76 | 79 | # define new column
|
77 | 80 |
|
78 | 81 | print('Calculating frequency...')
|
|
0 commit comments