generate vector zonal stat features for distances to features

create_distance_zonal_stats[source]

create_distance_zonal_stats(aoi:GeoDataFrame, data:GeoDataFrame, max_distance:float, aggregations:List[typing.Dict[str, typing.Any]]=[], distance_col:str='nearest')

Computes zonal stats based on nearest matching data geometry within max_distance. Note that setting a too high max_distance (or None) will incur a performance cost.

Type Default Details
aoi GeoDataFrame Area of interest for which zonal stats are to be computed for
data GeoDataFrame Source gdf of region/areas containing data to compute zonal stats from
max_distance float max distance to compute distance for (the larger the slower the join), set to None for no limit
aggregations typing.List[typing.Dict[str, typing.Any]] None aggregations
distance_col str nearest column name of the distance column, set to None if not wanted in results

Test data

Simple squares

Given an aoi (simple_aoi) and geodataframe containing sample data (simple_data)

simple_aoi
geometry
0 POLYGON ((0.000 0.000, 0.000 1.000, 1.000 1.00...
1 POLYGON ((1.000 0.000, 1.000 1.000, 2.000 1.00...
2 POLYGON ((2.000 0.000, 2.000 1.000, 3.000 1.00...
simple_data
geometry population internet_speed
0 POLYGON ((0.250 0.000, 0.250 1.000, 1.250 1.00... 100 20.0
1 POLYGON ((1.250 0.000, 1.250 1.000, 2.250 1.00... 200 10.0
2 POLYGON ((2.250 0.000, 2.250 1.000, 3.250 1.00... 300 5.0

We also have simple point data which do not intersect with our AOIs.

simple_point_data
geometry population internet_speed
0 POINT (0.500 3.000) 100 20.0
1 POINT (0.500 4.000) 600 120.0
2 POINT (0.500 5.000) 1100 220.0
3 POINT (0.500 6.000) 1600 320.0
4 POINT (0.500 7.000) 2100 420.0
5 POINT (1.500 3.000) 200 10.0
6 POINT (1.500 4.000) 700 110.0
7 POINT (1.500 5.000) 1200 210.0
8 POINT (1.500 6.000) 1700 310.0
9 POINT (1.500 7.000) 2200 410.0
10 POINT (2.500 3.000) 300 5.0
11 POINT (2.500 4.000) 800 105.0
12 POINT (2.500 5.000) 1300 205.0
13 POINT (2.500 6.000) 1800 305.0
14 POINT (2.500 7.000) 2300 405.0
ax = plt.axes()
ax = simple_data.plot(
    ax=ax, color=["orange", "brown", "purple"], edgecolor="yellow", alpha=0.4
)
ax = simple_aoi.plot(ax=ax, facecolor="none", edgecolor=["r", "g", "b"])
ax = simple_point_data.plot(ax=ax)
No description has been provided for this image

The red,green,blue outlines are the 3 regions of interest (aoi) while the orange,brown, purple areas are the data areas.The blue dots are data which do not intersect our AOIs.

%%time
results = create_distance_zonal_stats(
    simple_aoi,
    simple_point_data,
    max_distance=7,
    aggregations=[
        dict(func="count"),
        dict(func="sum", column="population"),
        dict(func="mean", column="internet_speed"),
    ],
)
CPU times: user 32.1 ms, sys: 11 ms, total: 43.1 ms
Wall time: 36 ms
results
geometry index_count population_sum internet_speed_mean nearest
0 POLYGON ((0.000 0.000, 0.000 1.000, 1.000 1.00... 1 100 20.0 2.0
1 POLYGON ((1.000 0.000, 1.000 1.000, 2.000 1.00... 1 200 10.0 2.0
2 POLYGON ((2.000 0.000, 2.000 1.000, 3.000 1.00... 1 300 5.0 2.0
%%time
results2 = create_distance_zonal_stats(
    simple_aoi,
    simple_data,
    max_distance=1,
    aggregations=[
        dict(func="count"),
        dict(func="sum", column="population"),
        dict(func="mean", column="internet_speed"),
    ],
)
CPU times: user 38.5 ms, sys: 0 ns, total: 38.5 ms
Wall time: 35 ms
results2
geometry index_count population_sum internet_speed_mean nearest
0 POLYGON ((0.000 0.000, 0.000 1.000, 1.000 1.00... 1 100 20.0 0.0
1 POLYGON ((1.000 0.000, 1.000 1.000, 2.000 1.00... 2 300 15.0 0.0
2 POLYGON ((2.000 0.000, 2.000 1.000, 3.000 1.00... 2 500 7.5 0.0