diff --git a/R/utils-upload.R b/R/utils-upload.R index 038b81b6..a0e0dd3d 100755 --- a/R/utils-upload.R +++ b/R/utils-upload.R @@ -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) @@ -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)) } diff --git a/tests/testthat/test-ee_utils_get_crs_web.R b/tests/testthat/test-ee_utils_get_crs_web.R new file mode 100644 index 00000000..69034e95 --- /dev/null +++ b/tests/testthat/test-ee_utils_get_crs_web.R @@ -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")) +}) \ No newline at end of file