@@ -88,13 +88,12 @@ def spatial_kernel_density(
88
88
kde_pnts = pd .DataFrame (kdt .query_ball_point (xy , r = radius ), columns = ["nn" ])
89
89
90
90
# Filter out points that have no neighbours within the search radius
91
- # and therefore their KDE value will be 0
92
91
kde_pnts ["num" ] = kde_pnts .nn .apply (len )
93
92
kde_pnts = kde_pnts .query ("num > 0" ).drop (columns = ["num" ])
94
93
95
94
# Slow implementation iterating over every pixel / point
96
95
ndv = - 9999
97
- Z_scalar = (ndv + np .zeros_like (xc )).flatten ()
96
+ z_scalar = (ndv + np .zeros_like (xc )).flatten ()
98
97
99
98
# TODO vectorise this
100
99
# calculate the KDE value for every point that has neighbours
@@ -106,34 +105,34 @@ def spatial_kernel_density(
106
105
if weight_col :
107
106
weights = [points .at [i , weight_col ] for i in row .nn ]
108
107
109
- Z_scalar [row .Index ] = quartic (
108
+ z_scalar [row .Index ] = quartic (
110
109
distances = distances ,
111
110
radius = radius ,
112
111
weights = weights ,
113
112
scaled = scaled ,
114
113
)
115
114
116
115
# create the output raster
117
- Z = Z_scalar .reshape (xc .shape )
116
+ z = z_scalar .reshape (xc .shape )
118
117
with rasterio .open (
119
118
fp = output_path ,
120
119
mode = "w" ,
121
120
driver = output_driver ,
122
- height = Z .shape [0 ],
123
- width = Z .shape [1 ],
121
+ height = z .shape [0 ],
122
+ width = z .shape [1 ],
124
123
count = 1 ,
125
- dtype = Z .dtype ,
124
+ dtype = z .dtype ,
126
125
crs = CRS .from_user_input (points .crs ),
127
126
transform = rasterio .transform .from_bounds (
128
127
west = bounds .min_x ,
129
128
south = bounds .min_y ,
130
129
east = bounds .max_x ,
131
130
north = bounds .max_y ,
132
- width = Z .shape [1 ],
133
- height = Z .shape [0 ],
131
+ width = z .shape [1 ],
132
+ height = z .shape [0 ],
134
133
),
135
134
nodata = ndv ,
136
135
) as dst :
137
136
# numpy arrays start at the "bottom left", whereas rasters are written
138
137
# from the "top left", hence flipping the array up-down before writing
139
- dst .write (np .flipud (Z ), 1 )
138
+ dst .write (np .flipud (z ), 1 )
0 commit comments