Skip to content

Commit

Permalink
second try for threshold; #618
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Mar 17, 2023
1 parent 1a528ed commit c7b0b81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions R/warp.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ rename_xy_dimensions = function(x, dims) {

# transform grid x to dimensions target
# x is a stars object, target is a dimensions object
transform_grid_grid = function(x, target, threshold = Inf) {
transform_grid_grid = function(x, target, threshold) {
stopifnot(inherits(x, "stars"), inherits(target, "dimensions"))
x = rename_xy_dimensions(x, target) # so we can match by name
xy_names = attr(target, "raster")$dimensions
Expand All @@ -83,9 +83,6 @@ 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 @@ -95,8 +92,13 @@ 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, drop = FALSE], pts, 1)
fnn = FNN::get.knnx(cc_x <- st_coordinates(x)[, 1:2, drop = FALSE], pts, 1)
i = fnn$nn.index - 1
if (is.na(threshold)) {
p12 = st_as_sf(as.data.frame(cc_x[1:2,]), coords = 1:2)
threshold = st_distance(p12)[1,2]
message(paste("threshold set to", threshold, ": set a larger value if you see missing values where they shouldn't be"))
}
i[fnn$nn.dist > threshold] = NA
ny = dim(x)[1]
cbind(i %% ny, i %/% ny) + 1
Expand Down Expand Up @@ -163,8 +165,7 @@ 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 * .71)) {
# .71: a bit over 0.5 * sqrt(2)
method = "near", threshold = NA_real_) {

if (!inherits(src, "stars_proxy"))
src = st_normalize(src)
Expand Down
2 changes: 1 addition & 1 deletion man/st_warp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7b0b81

Please sign in to comment.