Skip to content

Commit

Permalink
set a proper threshold automatically; #618
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Mar 17, 2023
1 parent 84f615c commit a586092
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/warp.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ transform_grid_grid = function(x, target, threshold = Inf) {
from = st_crs(target)
pts = sf::sf_project(from = from, to = st_crs(x), pts = new_pts)

if (threshold == Inf)
threshold = sqrt(prod(sapply(target, function(x) abs(x$delta)))) * 0.71

# at xy (target) locations, get values from x, or put NA
# to array:
d = st_dimensions(x)
Expand All @@ -92,7 +95,7 @@ transform_grid_grid = function(x, target, threshold = Inf) {
stop("package FNN required, please install it first") #nocov
if (st_is_longlat(x))
warning("using Euclidean distance measures on geodetic coordinates")
fnn = FNN::get.knnx(st_coordinates(x)[,1:2], pts, 1)
fnn = FNN::get.knnx(st_coordinates(x)[, 1:2, drop = FALSE], pts, 1)
i = fnn$nn.index - 1
i[fnn$nn.dist > threshold] = NA
ny = dim(x)[1]
Expand Down Expand Up @@ -160,7 +163,8 @@ transform_grid_grid = function(x, target, threshold = Inf) {
#' @export
st_warp = function(src, dest, ..., crs = NA_crs_, cellsize = NA_real_, segments = 100,
use_gdal = FALSE, options = character(0), no_data_value = NA_real_, debug = FALSE,
method = "near", threshold = ifelse(is.na(cellsize), Inf, cellsize / 2)) {
method = "near", threshold = ifelse(is.na(cellsize), Inf, cellsize * .71)) {
# .71: a bit over 0.5 * sqrt(2)

if (!inherits(src, "stars_proxy"))
src = st_normalize(src)
Expand Down

0 comments on commit a586092

Please sign in to comment.