You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I read into what gdal_proximity does. It calculates the nearest distance to masked areas. This requires iterating over each cell. In numpy this will be slow, but we implemented similar functionalities in the package pyflwdir that iterates many times, which is quite fast. A function signature like below is worth trying (not tested).
importnumpyasnpimportnumbaasnb# create a njit (no-python just-in-time compiling) function below. This code will be converted to machine code# and compiled on-the-fly making it much much faster. cache=True ensures the compilation is stored for the next# time the function is used.@nb.njit(cache=True)defmv_proximity(mask):
"""Calculates nearest distance of missing values to non-missing values. Parameters ---------- mask : np.ndarray [bool] 2D array with True at locations with data, and False at locations without data Returns ------- np.ndarray 2D array containing distances to nearest non-missing value """distance=np.ones_like(data, dtype=np.float32) *np.inf# start with infinite distances per grid cellrows, cols=distance.shape# it may be faster to first compute indexes of masked cellsforrowinrange(rows):
forcolinrange(cols):
ifmask[row, col]:
# same here, it may be faster to first index logical values before running through them. Not yet carefully thought through...forkinrange(rows):
forlinrange(cols):
dist=np.sqrt((row-k) **2+ (col-l) **2)
ifdist<distance[k, l]:
distance[k, l] =distreturndistance
This should be tested for speed and compared against results of gdal_proximity.
The text was updated successfully, but these errors were encountered:
I read into what
gdal_proximity
does. It calculates the nearest distance to masked areas. This requires iterating over each cell. In numpy this will be slow, but we implemented similar functionalities in the packagepyflwdir
that iterates many times, which is quite fast. A function signature like below is worth trying (not tested).This should be tested for speed and compared against results of
gdal_proximity
.The text was updated successfully, but these errors were encountered: