Skip to content

Commit

Permalink
Merge pull request #106 from JanLinnenbrink/master
Browse files Browse the repository at this point in the history
minor changes to geodist for similar results on Linux/Windows
  • Loading branch information
HannaMeyer committed Mar 26, 2024
2 parents 8ccb08a + 6265006 commit 50f57ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions R/geodist.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ geodist <- function(x,



x <- sf::st_transform(x,4326)
if(type == "feature"){
if(is.null(variables)){
variables <- names(modeldomain)
Expand All @@ -159,9 +158,9 @@ geodist <- function(x,
if(class(x)[1]=="sfc_POINT"){
x <- sf::st_as_sf(x)
}
x <- sf::st_as_sf(terra::extract(modeldomain, x, na.rm=FALSE,bind=TRUE))
x <- sf::st_transform(x,4326)
x <- sf::st_as_sf(terra::extract(modeldomain, terra::vect(x), na.rm=FALSE,bind=TRUE))
}
x <- sf::st_transform(x,4326)
if(!is.null(testdata)){
if(any(!variables%in%names(testdata))){# extract variable values of raster:
testdata <- sf::st_transform(testdata,sf::st_crs(modeldomain))
Expand Down Expand Up @@ -198,6 +197,7 @@ geodist <- function(x,
}
}
if(type != "feature") {
x <- sf::st_transform(x,4326)
catVars <- NULL
}
if (type=="time" & is.null(timevar)){
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-geodist.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test_that("geodist works space with points and preddata in geographic space", {
test_that("geodist works with points and preddata in feature space", {

aoi <- sf::st_as_sfc("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", crs="epsg:25832")
tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:25832") |>
tpoints <- sf::st_as_sfc("MULTIPOINT ((1.5 1.5), (1.5 2.5), (2.5 2.5), (2.5 3.5), (1.5 4.5), (5.5 4.5))", crs="epsg:25832") |>
sf::st_cast("POINT")
set.seed(1)
ppoints <- suppressWarnings(sf::st_sample(aoi, 20, type="regular")) |>
Expand All @@ -94,8 +94,8 @@ test_that("geodist works with points and preddata in feature space", {
mean_sample2sample <- round(mean(dist[dist$what=="sample-to-sample","dist"]), 4)
mean_prediction_to_sample <- round(mean(dist[dist$what=="prediction-to-sample","dist"]), 4)

expect_equal(mean_sample2sample, 0.4316)
expect_equal(mean_prediction_to_sample, 0.8328)
expect_equal(mean_sample2sample, 0.3814)
expect_equal(mean_prediction_to_sample, 1.0783)


})
Expand All @@ -122,7 +122,7 @@ test_that("geodist works with points and raster in geographic space", {

test_that("geodist works with points and raster in feature space", {

tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:25832") |>
tpoints <- sf::st_as_sfc("MULTIPOINT ((1.5 1.5), (1.5 2.5), (2.5 2.5), (2.5 3.5), (1.5 4.5), (5.5 4.5))", crs="epsg:25832") |>
sf::st_cast("POINT")

raster <- terra::rast(nrows=10, ncols=10, nlyrs=1, xmin=0, xmax=10,
Expand All @@ -133,15 +133,15 @@ test_that("geodist works with points and raster in feature space", {
type = "feature")

mean_sample2sample <- round(mean(dist[dist$what=="sample-to-sample","dist"]), 4)
expect_equal(mean_sample2sample, 0.4316)
expect_equal(mean_sample2sample, 0.3814)


})


test_that("geodist works with points and stars raster in geographic space", {

tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:25832") |>
tpoints <- sf::st_as_sfc("MULTIPOINT ((1.5 1.5), (1.5 2.5), (2.5 2.5), (2.5 3.5), (1.5 4.5), (5.5 4.5))", crs="epsg:25832") |>
sf::st_cast("POINT")

raster <- terra::rast(nrows=10, ncols=10, nlyrs=1, xmin=0, xmax=10,
Expand All @@ -153,7 +153,7 @@ test_that("geodist works with points and stars raster in geographic space", {
type = "feature")

mean_sample2sample <- round(mean(dist[dist$what=="sample-to-sample","dist"]), 4)
expect_equal(mean_sample2sample, 0.4316)
expect_equal(mean_sample2sample, 0.3814)


})
Expand Down Expand Up @@ -208,8 +208,8 @@ test_that("geodist works with points and test data in feature space", {
mean_sample2sample <- round(mean(dist[dist$what=="sample-to-sample","dist"]), 4)
mean_test_to_sample <- round(mean(dist[dist$what=="test-to-sample","dist"]), 4)

expect_equal(mean_sample2sample, 0.4316)
expect_equal(mean_test_to_sample, 0.8783)
expect_equal(mean_sample2sample, 0.3814)
expect_equal(mean_test_to_sample, 1.4524)


})
Expand Down

0 comments on commit 50f57ae

Please sign in to comment.