Skip to content

Commit

Permalink
Merge pull request #364 from MarShaikh/master
Browse files Browse the repository at this point in the history
Updated link in ee_utils_get_crs_web to use the GitHub URL for spatialreference.org data.
  • Loading branch information
ambarja authored Aug 2, 2024
2 parents 0785fd6 + 28f5ac1 commit 483658e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
20 changes: 14 additions & 6 deletions R/utils-upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,9 @@ ee_utils_get_crs <- function(code) {
#' @param code The projection code.
#' @noRd
ee_utils_get_crs_web <- function(code) {
codetype <- tolower(strsplit(code,":")[[1]][1])
ee_code <- strsplit(code,":")[[1]][2]
codetype <- tolower(strsplit(code, ":")[[1]][1])
ee_code <- strsplit(code, ":")[[1]][2]

if (codetype == 'epsg') {
format <- "wkt"
link <- sprintf('https://epsg.io/%s.%s', ee_code, format)
Expand All @@ -644,11 +645,18 @@ ee_utils_get_crs_web <- function(code) {
crs_wkt <- tryCatch(
expr = suppressWarnings(readLines(link)),
error = function(e) {
message(sprintf("%s is down using %s ...", bold("spatialreference.org"), bold("web.archive.org")))
link <- sprintf("https://web.archive.org/web/https://spatialreference.org/ref/%s/%s/%s/", codetype, ee_code, format)
suppressWarnings(readLines(link))
message(sprintf("%s is down using %s ...", bold("spatialreference.org"), bold("GitHub backup")))
sr_org_data <- jsonlite::fromJSON("https://raw.githubusercontent.com/OSGeo/spatialreference.org/master/scripts/sr-org.json")
match_index <- which(sr_org_data$code == ee_code)

if (length(match_index) == 0) {
# Instead of stopping, return a warning and a default value or NULL
warning(paste("SR-ORG code", ee_code, "not found in the dataset. Returning NULL."))
return(NULL)
}
sr_org_data$ogcwkt[match_index]
}
)
}
ee_utils_py_to_r(crs_wkt)
return(ee_utils_py_to_r(crs_wkt))
}
47 changes: 47 additions & 0 deletions tests/testthat/test-ee_utils_get_crs_web.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
context("rgee: ee_utils_get_crs_web test")

# -------------------------------------------------------------------------
ee_utils_get_crs_web <- rgee:::ee_utils_get_crs_web
ee_utils_get_crs <- rgee:::ee_utils_get_crs

test_that("ee_utils_get_crs handles different code types", {

# Test EPSG code
epsg_result <- ee_utils_get_crs("EPSG:4326")
expect_true(grepl("GEOGCS", epsg_result))
expect_true(grepl("WGS 84", epsg_result))

# Test ESRI code
esri_result <- ee_utils_get_crs("ESRI:54009")
expect_true(grepl("PROJCS", esri_result))
expect_true(grepl("World_Mollweide", esri_result))

# Test SR-ORG code
sr_org_result <- ee_utils_get_crs("SR-ORG:6864")
expect_true(grepl("PROJCS", sr_org_result))
expect_true(grepl("Pseudo-Mercator", sr_org_result))

# Test that ee_utils_get_crs correctly uses ee_utils_get_crs_web for SR-ORG codes
sr_org_direct <- ee_utils_get_crs_web("SR-ORG:6864")
expect_equal(sr_org_result, rgee:::ee_utils_py_to_r(sr_org_direct))
})

test_that("ee_utils_get_crs_web handles different scenarios correctly", {
# Test EPSG code
epsg_result <- ee_utils_get_crs_web("EPSG:3857")
expect_true(grepl("PROJCS", epsg_result))
expect_true(grepl("WGS 84 / Pseudo-Mercator", epsg_result))

# Test SR-ORG code
sr_org_result <- ee_utils_get_crs_web("SR-ORG:7483")
expect_true(grepl("PROJCS", sr_org_result))
expect_true(grepl("WGS 84 / Pseudo-Mercator", sr_org_result))

# Test non-existent SR-ORG code
expect_error(result <- ee_utils_get_crs_web("SR-ORG:99999"))
})

test_that("ee_utils_get_crs_web handles network errors gracefully", {
# Test with an invalid URL to simulate a network error
expect_error(ee_utils_get_crs_web("INVALID:1234"))
})

0 comments on commit 483658e

Please sign in to comment.