From dd1c1447769c68e7e0cef4bb7ed58fcf2ba72d0c Mon Sep 17 00:00:00 2001 From: mhesselbarth Date: Wed, 11 Oct 2023 11:41:44 +0200 Subject: [PATCH 1/2] Trying to fix #91 --- R/internal_clean_coordinate.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/internal_clean_coordinate.R b/R/internal_clean_coordinate.R index 3c8db84..b73627a 100644 --- a/R/internal_clean_coordinate.R +++ b/R/internal_clean_coordinate.R @@ -30,7 +30,7 @@ reproj <- function(ref) { ras_create <- function(x, lat, lon, thinning_res){ # get data extend - ex <- terra::ext(terra::vect(x[, c(lon, lat)])) + thinning_res * 2 + ex <- terra::ext(terra::vect(x[, c(lon, lat)], geom=c(lon, lat))) + thinning_res * 2 # check for boundary conditions if(ex[1] < -180 | ex[2] > 180 | ex[3] < -90 | ex[4] >90){ @@ -72,18 +72,18 @@ ras_dist <- function(x, lat, lon, ras, weights) { midp <- data.frame(terra::as.points(ras)) # retain only cells that contain points - midp <- midp[midp$layer %in% unique(pts),] + midp <- midp[midp$lyr.1 %in% unique(pts),] # order - midp <- midp[match(unique(pts), midp$layer),] + midp <- midp[match(unique(pts), midp$lyr.1),] # calculate geospheric distance between raster cells with points dist <- geosphere::distm(midp[, c("x", "y")], fun = geosphere::distHaversine) / 1000 # set rownames and colnames to cell IDs - dist <- as.data.frame(dist, row.names = as.integer(midp$layer)) - names(dist) <- midp$layer + dist <- as.data.frame(dist, row.names = as.integer(midp$lyr.1)) + names(dist) <- midp$lyr.1 if(weights){ # approximate within cell distance as half From 8d8cdd5256dbfd6126e0f2a71caf4de05c04aa92 Mon Sep 17 00:00:00 2001 From: mhesselbarth Date: Wed, 11 Oct 2023 11:54:50 +0200 Subject: [PATCH 2/2] Match points #91 --- R/internal_clean_coordinate.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/internal_clean_coordinate.R b/R/internal_clean_coordinate.R index b73627a..bc6ecdd 100644 --- a/R/internal_clean_coordinate.R +++ b/R/internal_clean_coordinate.R @@ -72,10 +72,10 @@ ras_dist <- function(x, lat, lon, ras, weights) { midp <- data.frame(terra::as.points(ras)) # retain only cells that contain points - midp <- midp[midp$lyr.1 %in% unique(pts),] + midp <- midp[midp$lyr.1 %in% unique(pts$lyr.1),] # order - midp <- midp[match(unique(pts), midp$lyr.1),] + midp <- midp[match(unique(pts$lyr.1), midp$lyr.1),] # calculate geospheric distance between raster cells with points dist <- geosphere::distm(midp[, c("x", "y")],