-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsnap_pour_point_d8.py
More file actions
508 lines (420 loc) · 16.7 KB
/
snap_pour_point_d8.py
File metadata and controls
508 lines (420 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
215
216
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
"""Snap pour points to the highest-accumulation cell within a search radius.
Users typically place pour points manually, but these often land a cell or
two off from the actual drainage channel. This module moves each pour point
to the highest flow-accumulation cell within a circular search radius so
that subsequent ``watershed()`` calls delineate correctly.
Algorithm
---------
For each non-NaN cell in ``pour_points``:
1. Search all cells within ``search_radius`` pixels (Euclidean distance).
2. Among valid (non-NaN) ``flow_accum`` cells in that radius, find the one
with maximum accumulation.
3. Move the pour point label to that cell.
If multiple pour points snap to the same cell, the last one in raster-scan
order wins (deterministic across all backends).
"""
from __future__ import annotations
import numpy as np
import xarray as xr
from numba import cuda
try:
import cupy
except ImportError:
class cupy: # type: ignore[no-redef]
ndarray = False
try:
import dask.array as da
except ImportError:
da = None
from xrspatial.utils import (
_validate_raster,
cuda_args,
has_cuda_and_cupy,
is_cupy_array,
is_dask_cupy,
ngjit,
)
from xrspatial.dataset_support import supports_dataset
def _to_numpy_f64(arr):
"""Convert *arr* to a contiguous numpy float64 array (handles CuPy)."""
if hasattr(arr, 'get'):
arr = arr.get()
return np.asarray(arr, dtype=np.float64)
# =====================================================================
# CPU kernel
# =====================================================================
@ngjit
def _snap_pour_point_cpu(flow_accum, pour_points, search_radius, H, W):
"""Snap each pour point to the max-accumulation cell within *radius*."""
out = np.empty((H, W), dtype=np.float64)
out[:] = np.nan
radius_sq = search_radius * search_radius
for r in range(H):
for c in range(W):
v = pour_points[r, c]
if v != v: # NaN
continue
label = v
best_r = r
best_c = c
fa_val = flow_accum[r, c]
if fa_val == fa_val: # not NaN
best_accum = fa_val
else:
best_accum = -1e308 # ~-inf
r_lo = r - search_radius
r_hi = r + search_radius
c_lo = c - search_radius
c_hi = c + search_radius
if r_lo < 0:
r_lo = 0
if r_hi >= H:
r_hi = H - 1
if c_lo < 0:
c_lo = 0
if c_hi >= W:
c_hi = W - 1
for nr in range(r_lo, r_hi + 1):
for nc in range(c_lo, c_hi + 1):
dr = nr - r
dc = nc - c
if dr * dr + dc * dc > radius_sq:
continue
fa_n = flow_accum[nr, nc]
if fa_n != fa_n: # NaN
continue
if fa_n > best_accum:
best_accum = fa_n
best_r = nr
best_c = nc
out[best_r, best_c] = label
return out
# =====================================================================
# CuPy backend
# =====================================================================
@cuda.jit
def _snap_pour_point_gpu(flow_accum, pp_rows, pp_cols,
snap_rows, snap_cols, search_radius,
n_pp, H, W):
"""Each thread handles one pour point: windowed max search on GPU."""
k = cuda.grid(1)
if k >= n_pp:
return
r = pp_rows[k]
c = pp_cols[k]
best_r = r
best_c = c
fa_val = flow_accum[r, c]
if fa_val == fa_val: # not NaN
best_accum = fa_val
else:
best_accum = -1e308
radius_sq = search_radius * search_radius
r_lo = r - search_radius
if r_lo < 0:
r_lo = 0
r_hi = r + search_radius
if r_hi >= H:
r_hi = H - 1
c_lo = c - search_radius
if c_lo < 0:
c_lo = 0
c_hi = c + search_radius
if c_hi >= W:
c_hi = W - 1
for nr in range(r_lo, r_hi + 1):
for nc in range(c_lo, c_hi + 1):
dr = nr - r
dc = nc - c
if dr * dr + dc * dc > radius_sq:
continue
fa_n = flow_accum[nr, nc]
if fa_n != fa_n: # NaN
continue
if fa_n > best_accum:
best_accum = fa_n
best_r = nr
best_c = nc
snap_rows[k] = best_r
snap_cols[k] = best_c
def _snap_pour_point_cupy(flow_accum_data, pour_points_data, search_radius):
"""Native CuPy: CUDA kernel for windowed max search per pour point.
Flow accumulation data stays on GPU. Only pour point coordinates
(sparse, typically < 100) are transferred CPU/GPU.
"""
import cupy as cp
H, W = flow_accum_data.shape
fa = flow_accum_data.astype(cp.float64)
pp = pour_points_data.astype(cp.float64)
out = cp.full((H, W), cp.nan, dtype=cp.float64)
mask = ~cp.isnan(pp)
if not cp.any(mask):
return out
rows, cols = cp.where(mask)
labels = pp[rows, cols]
n_pp = len(rows)
snap_rows = cp.empty(n_pp, dtype=cp.int64)
snap_cols = cp.empty(n_pp, dtype=cp.int64)
threads = min(256, n_pp)
blocks = (n_pp + threads - 1) // threads
_snap_pour_point_gpu[blocks, threads](
fa, rows, cols, snap_rows, snap_cols,
search_radius, n_pp, H, W)
# Write labels in raster-scan order (last wins if overlap)
# Sort by raster-scan position to ensure deterministic ordering
raster_pos = rows * W + cols
sort_idx = cp.argsort(raster_pos)
for k in sort_idx.get():
out[int(snap_rows[k]), int(snap_cols[k])] = float(labels[k])
return out
# =====================================================================
# Dask backend
# =====================================================================
def _snap_pour_point_dask(flow_accum_data, pour_points_data, search_radius):
"""Dask: extract sparse pour points chunk-by-chunk, windowed search, lazy assembly.
Pour points are sparse (typically < 100 in a multi-million-cell raster).
We never materialize the full pour_points grid: a ``map_blocks`` pass
reduces each chunk to a 1-byte flag, then only the (few) flagged chunks
are loaded to extract coordinates. Small windows of ``flow_accum`` are
sliced for each pour point, and the output is assembled lazily.
"""
H, W = flow_accum_data.shape
chunks_y = pour_points_data.chunks[0]
chunks_x = pour_points_data.chunks[1]
# --- Phase 1: identify which chunks contain pour points --------
# Single dask pass; each chunk is reduced to a scalar flag.
# The scheduler parallelizes reads and releases each chunk after
# the reduction, so peak memory is bounded by thread count × chunk size.
def _has_pp(block):
return np.array(
[[np.any(~np.isnan(np.asarray(block))).item()]],
dtype=np.int8,
)
flags = da.map_blocks(
_has_pp, pour_points_data,
dtype=np.int8,
chunks=tuple((1,) * len(c) for c in pour_points_data.chunks),
).compute() # tiny array: one byte per chunk
# --- Phase 2: load only flagged chunks, extract coordinates ----
points = [] # list of (global_row, global_col, label)
row_off = 0
for iy, cy in enumerate(chunks_y):
col_off = 0
for ix, cx in enumerate(chunks_x):
if flags[iy, ix]:
chunk = np.asarray(
pour_points_data.blocks[iy, ix].compute(),
dtype=np.float64,
)
rs, cs = np.where(~np.isnan(chunk))
for k in range(len(rs)):
points.append((
row_off + int(rs[k]),
col_off + int(cs[k]),
float(chunk[rs[k], cs[k]]),
))
col_off += cx
row_off += cy
# --- Phase 3: snap each pour point via windowed search ---------
snapped = [] # list of (snap_r, snap_c, label)
radius_sq = search_radius * search_radius
for r, c, label in points:
r_lo = max(0, r - search_radius)
r_hi = min(H - 1, r + search_radius)
c_lo = max(0, c - search_radius)
c_hi = min(W - 1, c + search_radius)
# Small window; dask handles cross-chunk slicing
window = np.asarray(
flow_accum_data[r_lo:r_hi + 1, c_lo:c_hi + 1].compute(),
dtype=np.float64,
)
best_r, best_c = r, c
fa_val = window[r - r_lo, c - c_lo]
best_accum = fa_val if not np.isnan(fa_val) else -np.inf
for wr in range(window.shape[0]):
for wc in range(window.shape[1]):
nr = r_lo + wr
nc = c_lo + wc
dr = nr - r
dc = nc - c
if dr * dr + dc * dc > radius_sq:
continue
fa_n = window[wr, wc]
if np.isnan(fa_n):
continue
if fa_n > best_accum:
best_accum = fa_n
best_r = nr
best_c = nc
snapped.append((best_r, best_c, label))
# --- Phase 4: lazy output assembly via map_blocks --------------
snap_rows = np.array([s[0] for s in snapped], dtype=np.int64) if snapped else np.array([], dtype=np.int64)
snap_cols = np.array([s[1] for s in snapped], dtype=np.int64) if snapped else np.array([], dtype=np.int64)
snap_labels = np.array([s[2] for s in snapped], dtype=np.float64) if snapped else np.array([], dtype=np.float64)
_snap_rows = snap_rows
_snap_cols = snap_cols
_snap_labels = snap_labels
def _assemble_block(block, block_info=None):
if block_info is None or 0 not in block_info:
return np.full(block.shape, np.nan, dtype=np.float64)
row_start, row_end = block_info[0]['array-location'][0]
col_start, col_end = block_info[0]['array-location'][1]
h, w = block.shape
out = np.full((h, w), np.nan, dtype=np.float64)
for k in range(len(_snap_rows)):
sr = _snap_rows[k]
sc = _snap_cols[k]
if row_start <= sr < row_end and col_start <= sc < col_end:
out[sr - row_start, sc - col_start] = _snap_labels[k]
return out
dummy = da.zeros((H, W), chunks=flow_accum_data.chunks, dtype=np.float64)
return da.map_blocks(
_assemble_block, dummy,
dtype=np.float64,
meta=np.array((), dtype=np.float64),
)
# =====================================================================
# Dask+CuPy backend
# =====================================================================
def _snap_pour_point_dask_cupy(flow_accum_data, pour_points_data, search_radius):
"""Dask+CuPy: sparse pour-point processing with GPU-resident flow_accum.
Extracts sparse pour point coordinates from CuPy chunks (small
transfers), performs windowed search via dask slicing, and assembles
output as dask+cupy.
"""
import cupy as cp
H, W = flow_accum_data.shape
chunks_y = pour_points_data.chunks[0]
chunks_x = pour_points_data.chunks[1]
# Phase 1: identify chunks with pour points
def _has_pp(block):
b = block.get() if hasattr(block, 'get') else np.asarray(block)
return np.array([[np.any(~np.isnan(b)).item()]], dtype=np.int8)
flags = da.map_blocks(
_has_pp, pour_points_data,
dtype=np.int8,
chunks=tuple((1,) * len(c) for c in pour_points_data.chunks),
).compute()
# Phase 2: extract coordinates from flagged chunks
points = []
row_off = 0
for iy, cy in enumerate(chunks_y):
col_off = 0
for ix, cx in enumerate(chunks_x):
if flags[iy, ix]:
chunk = _to_numpy_f64(
pour_points_data.blocks[iy, ix].compute())
rs, cs = np.where(~np.isnan(chunk))
for k in range(len(rs)):
points.append((
row_off + int(rs[k]),
col_off + int(cs[k]),
float(chunk[rs[k], cs[k]]),
))
col_off += cx
row_off += cy
# Phase 3: windowed search
snapped = []
radius_sq = search_radius * search_radius
for r, c, label in points:
r_lo = max(0, r - search_radius)
r_hi = min(H - 1, r + search_radius)
c_lo = max(0, c - search_radius)
c_hi = min(W - 1, c + search_radius)
window = _to_numpy_f64(
flow_accum_data[r_lo:r_hi + 1, c_lo:c_hi + 1].compute())
best_r, best_c = r, c
fa_val = window[r - r_lo, c - c_lo]
best_accum = fa_val if not np.isnan(fa_val) else -np.inf
for wr in range(window.shape[0]):
for wc in range(window.shape[1]):
nr = r_lo + wr
nc = c_lo + wc
dr = nr - r
dc = nc - c
if dr * dr + dc * dc > radius_sq:
continue
fa_n = window[wr, wc]
if np.isnan(fa_n):
continue
if fa_n > best_accum:
best_accum = fa_n
best_r = nr
best_c = nc
snapped.append((best_r, best_c, label))
# Phase 4: lazy output assembly as dask+cupy
snap_rows = np.array([s[0] for s in snapped], dtype=np.int64) \
if snapped else np.array([], dtype=np.int64)
snap_cols = np.array([s[1] for s in snapped], dtype=np.int64) \
if snapped else np.array([], dtype=np.int64)
snap_labels = np.array([s[2] for s in snapped], dtype=np.float64) \
if snapped else np.array([], dtype=np.float64)
_snap_rows = snap_rows
_snap_cols = snap_cols
_snap_labels = snap_labels
def _assemble_block(block, block_info=None):
if block_info is None or 0 not in block_info:
return cp.full(block.shape, cp.nan, dtype=cp.float64)
row_start, row_end = block_info[0]['array-location'][0]
col_start, col_end = block_info[0]['array-location'][1]
h, w = block.shape
out = np.full((h, w), np.nan, dtype=np.float64)
for k in range(len(_snap_rows)):
sr = _snap_rows[k]
sc = _snap_cols[k]
if row_start <= sr < row_end and col_start <= sc < col_end:
out[sr - row_start, sc - col_start] = _snap_labels[k]
return cp.asarray(out)
dummy = da.zeros((H, W), chunks=flow_accum_data.chunks, dtype=np.float64)
return da.map_blocks(
_assemble_block, dummy,
dtype=np.float64,
meta=cp.array((), dtype=cp.float64),
)
# =====================================================================
# Public API
# =====================================================================
@supports_dataset
def snap_pour_point_d8(flow_accum: xr.DataArray,
pour_points: xr.DataArray,
search_radius: int = 5,
name: str = 'snapped_pour_points') -> xr.DataArray:
"""Snap pour points to the highest-accumulation cell within a radius.
Parameters
----------
flow_accum : xarray.DataArray or xr.Dataset
2D flow accumulation grid.
pour_points : xarray.DataArray
2D raster where non-NaN cells mark pour points (same format
as ``watershed()`` expects). Values are preserved as labels.
search_radius : int, default 5
Maximum search distance in pixels (Euclidean).
name : str, default 'snapped_pour_points'
Name of output DataArray.
Returns
-------
xarray.DataArray or xr.Dataset
Same-shape grid with pour point labels moved to their snapped
locations. Non-pour-point cells are NaN.
"""
_validate_raster(flow_accum, func_name='snap_pour_point', name='flow_accum')
fa_data = flow_accum.data
pp_data = pour_points.data
if isinstance(fa_data, np.ndarray):
fa = fa_data.astype(np.float64)
pp = np.asarray(pp_data, dtype=np.float64)
H, W = fa.shape
out = _snap_pour_point_cpu(fa, pp, search_radius, H, W)
elif has_cuda_and_cupy() and is_cupy_array(fa_data):
out = _snap_pour_point_cupy(fa_data, pp_data, search_radius)
elif has_cuda_and_cupy() and is_dask_cupy(flow_accum):
out = _snap_pour_point_dask_cupy(fa_data, pp_data, search_radius)
elif da is not None and isinstance(fa_data, da.Array):
out = _snap_pour_point_dask(fa_data, pp_data, search_radius)
else:
raise TypeError(f"Unsupported array type: {type(fa_data)}")
return xr.DataArray(out,
name=name,
coords=flow_accum.coords,
dims=flow_accum.dims,
attrs=flow_accum.attrs)