rnaturalearth/ 0000755 0001762 0000144 00000000000 15133463702 013132 5 ustar ligges users rnaturalearth/tests/ 0000755 0001762 0000144 00000000000 14536055621 014277 5 ustar ligges users rnaturalearth/tests/testthat/ 0000755 0001762 0000144 00000000000 15133463702 016134 5 ustar ligges users rnaturalearth/tests/testthat/test_ne_file_name.R 0000644 0001762 0000144 00000003520 14777246403 021731 0 ustar ligges users library(httr)
url_exists <- function(url) {
url <- sanitize_gdal_url(url)
res <- HEAD(url)
status_code(res) == 200L
}
# a non exhaustive list of ne download urls
# initially taken from those in data_download_script.r
# fmt: skip
urls <- c(
ne_file_name(scale = 110L, type = "countries", category = "cultural"),
ne_file_name(scale = 110L, type = "map_units", category = "cultural"),
ne_file_name(scale = 110L, type = "sovereignty", category = "cultural"),
ne_file_name(scale = 50L, type = "countries", category = "cultural"),
ne_file_name(scale = 50L, type = "map_units", category = "cultural"),
ne_file_name(scale = 50L, type = "sovereignty", category = "cultural"),
ne_file_name(scale = 10L, type = "countries", category = "cultural"),
ne_file_name(scale = 10L, type = "map_units", category = "cultural"),
ne_file_name(scale = 10L, type = "sovereignty", category = "cultural"),
ne_file_name(scale = 50L, type = "states", category = "cultural"),
ne_file_name(scale = 10L, type = "states", category = "cultural"),
ne_file_name(scale = 110L, type = "tiny_countries", category = "cultural"),
ne_file_name(scale = 110L, type = "coastline", category = "physical"),
ne_file_name(scale = 50L, type = "coastline", category = "physical"),
ne_file_name(scale = 10L, type = "coastline", category = "physical")
)
url_expect_fail <- ne_file_name(
scale = 110L,
type = "expect_fail",
category = "cultural"
)
test_that("urls for downloads created by the package exist", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_true(all(vapply(urls, url_exists, logical(1L))))
})
test_that("a bogus url does not exist", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_false(url_exists(url_expect_fail))
})
rnaturalearth/tests/testthat/test_returnclass.R 0000644 0001762 0000144 00000003205 14536570273 021673 0 ustar ligges users test_that("type of object returned is sf", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
world <- ne_countries()
africa <- ne_countries(continent = "africa")
france <- ne_countries(country = "france")
expect_is(world, "sf")
expect_is(africa, "sf")
expect_is(france, "sf")
})
test_that("type of object returned is SpatVector", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
world <- ne_countries(returnclass = "sv")
africa <- ne_countries(continent = "africa", returnclass = "sv")
france <- ne_countries(country = "france", returnclass = "sv")
expect_is(world, "SpatVector")
expect_is(africa, "SpatVector")
expect_is(france, "SpatVector")
})
# Check if I should do all the tests for all the scales.
test_that("type of object returned is either sf or SpatVector for coastlines", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_true(inherits(ne_coastline(returnclass = "sf", scale = 10), "sf"))
expect_true(inherits(ne_coastline(returnclass = "sf", scale = 50), "sf"))
expect_true(inherits(ne_coastline(returnclass = "sf", scale = 110), "sf"))
expect_true(inherits(ne_coastline(returnclass = "sv"), "SpatVector"))
})
test_that("type of object returned is either sf or SpatVector for states", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_true(inherits(ne_states(returnclass = "sf"), "sf"))
expect_true(inherits(ne_states(returnclass = "sv"), "SpatVector"))
})
rnaturalearth/tests/testthat/test_ne_countries.R 0000644 0001762 0000144 00000002554 14536571403 022025 0 ustar ligges users test_that("scale argument gives expected relative sizes of objects", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_gt(
object.size(ne_countries(scale = "medium")),
object.size(ne_countries(scale = "small"))
)
expect_gt(
object.size(ne_countries(scale = 50)),
object.size(ne_countries(scale = 110))
)
})
test_that("equivalent scale arguments as numeric or text give identical results", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_identical(
ne_countries(scale = "medium"),
ne_countries(scale = 50)
)
expect_identical(
ne_countries(scale = "small"),
ne_countries(scale = 110)
)
})
test_that("Error message if incorrect filters are applied", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_error(ne_countries(continent = "madeupname"))
expect_error(ne_countries(country = "madeupname"))
expect_error(ne_countries(geounit = "madeupname"))
expect_error(ne_countries(sovereignty = "madeupname"))
})
test_that("Error message if incorrect type argument", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_error(ne_countries(type = "madeupname"))
})
rnaturalearth/tests/testthat/test_ne_states.R 0000644 0001762 0000144 00000002772 14536570413 021317 0 ustar ligges users # TODO: Change to sf in rnaturalearthhires
test_that("type of object returned is as expected", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_is(ne_states(country = "france"), "sf")
expect_is(ne_states(geounit = "france"), "sf")
expect_is(ne_states(country = "united kingdom"), "sf")
expect_is(ne_states(geounit = "england"), "sf")
})
test_that("filter by country gives bigger object than by geounit", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_gt(
object.size(ne_states(country = "france")),
object.size(ne_states(geounit = "france"))
)
expect_gt(
object.size(ne_states(country = "united kingdom")),
object.size(ne_states(geounit = "england"))
)
})
test_that("equivalent country and iso_a2 arguments give identical results", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_identical(
ne_states(country = "france"),
ne_states(iso_a2 = "FR")
)
expect_identical(
ne_states(country = "united kingdom"),
ne_states(iso_a2 = "GB")
)
})
test_that("Error message if incorrect filters are applied", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
expect_error(ne_states(country = "madeupname"))
expect_error(ne_states(geounit = "madeupname"))
expect_error(ne_states(iso_a2 = "madeupname"))
})
rnaturalearth/tests/testthat/test_ne_download.R 0000644 0001762 0000144 00000005365 15126010320 021602 0 ustar ligges users test_that("ne_download() can download data and return the associated file when load = FALSE", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
# fmt: skip
urls <- c(
ne_download( scale = 110L, type = "countries", category = "cultural", load = FALSE),
ne_download(scale = 110L, type = "map_units", category = "cultural", load = FALSE),
ne_download(scale = 110L, type = "sovereignty", category = "cultural", load = FALSE),
ne_download(scale = 50L, type = "countries", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "countries", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "map_units", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "sovereignty", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "coastline", category = "physical", load = FALSE),
ne_download(scale = 10L, type = "parks_and_protected_lands_line", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "parks_and_protected_lands_point", category = "cultural", load = FALSE),
ne_download(scale = 10L, type = "parks_and_protected_lands_scale_rank", category = "cultural", load = FALSE),
ne_download(scale = 110L, type = "countries", category = "cultural", load = FALSE, returnclass = "sv"),
ne_download(scale = 10L, type = "parks_and_protected_lands_scale_rank", category = "cultural", load = FALSE, returnclass = "sv"),
ne_download(scale = 50L, type = "MSR_50M", category = "raster", load = FALSE)
)
expect_true(all(file.exists(urls)))
})
# fmt: on
test_that("ne_download() uses caching when load = TRUE and destdir is not tempdir()", {
skip_on_cran()
skip_if_not_installed("sf")
# Create a temporary directory for caching
cache_dir <- file.path(tempdir(), "ne_cache_test")
dir.create(cache_dir, showWarnings = FALSE)
on.exit(unlink(cache_dir, recursive = TRUE), add = TRUE)
# First call: should download and save
result1 <- ne_download(
scale = 110L,
type = "countries",
category = "cultural",
destdir = cache_dir,
load = TRUE
)
# Check that the file was created
expected_file <- file.path(cache_dir, "ne_110m_admin_0_countries.gpkg")
expect_true(file.exists(expected_file))
# Check that result is an sf object
expect_s3_class(result1, "sf")
# Second call: should load from cache (not re-download)
result2 <- ne_download(
scale = 110L,
type = "countries",
category = "cultural",
destdir = cache_dir,
load = TRUE
)
# Check that result is still an sf object
expect_s3_class(result2, "sf")
# Results should be identical
expect_identical(nrow(result1), nrow(result2))
expect_identical(ncol(result1), ncol(result2))
})
rnaturalearth/tests/testthat/test_ne_load.R 0000644 0001762 0000144 00000002446 15034746211 020724 0 ustar ligges users test_that("ne_load works for raster files", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
scale <- 50L
type <- "OB_50M"
category <- "raster"
tmpdir <- tempdir()
ne_download(
scale = scale,
type = type,
category = category,
destdir = tmpdir,
load = FALSE
)
rst <- ne_load(
scale = scale,
type = type,
category = category,
destdir = tmpdir
)
expect_s4_class(rst, "SpatRaster")
})
test_that("ne_load works for vector files", {
skip_on_cran()
skip_if_not_installed("rnaturalearthdata")
skip_if_not_installed("rnaturalearthhires")
tmpdir <- tempdir()
scale_small <- 50L
ne_download(
scale = scale_small,
destdir = tmpdir,
load = FALSE
)
vect <- ne_load(scale = scale_small, destdir = tmpdir, returnclass = "sv")
expect_s4_class(vect, "SpatVector")
vect <- ne_load(scale = scale_small, destdir = tmpdir, returnclass = "sf")
expect_s3_class(vect, "sf")
scale_medium <- "medium"
ne_download(
scale = scale_medium,
type = "land",
category = "physical",
destdir = tmpdir,
load = FALSE
)
vect <- ne_load(
scale = scale_medium,
type = "land",
category = "physical",
destdir = tmpdir
)
expect_s3_class(vect, "sf")
})
rnaturalearth/tests/testthat.R 0000644 0001762 0000144 00000000106 14412335132 016246 0 ustar ligges users library(testthat)
library(rnaturalearth)
test_check("rnaturalearth")
rnaturalearth/MD5 0000644 0001762 0000144 00000020163 15133463702 013444 0 ustar ligges users 10c16870c0243027e487f071bbb21f56 *DESCRIPTION
8b5f0971f0b628904e4103119628a47d *LICENSE
784df093d49b88fee62eee77b9d1e232 *NAMESPACE
99ab888de5696ea05b2f6aad374f3f53 *R/check_data_exist.R
f8f87ce9cedeb0cd5110659a5fe15d5a *R/check_scale.R
50c102dd9f0a72d1e7363e4d59e1f64c *R/datasets.R
c1866ed50da47edece9591901d137143 *R/deprecate_sp.R
17a284455df3f1c97029ff52b7a85fc2 *R/find_data.R
be363129d86219cb59556452a8e08d32 *R/get_data.R
0a6fc88c72b74d99823ed93ac72d132c *R/install-rnaturalearthdata.R
9e3b22207219522da427dd392b955d7d *R/install-rnaturalearthhires.R
e4981d082ba3b1f5e8ee075a5feb5cab *R/ne_coastline.R
4286350e77a8603ead08e92f7e66fe29 *R/ne_countries.R
0abd9e01f8629a7cd9ed516839df0a74 *R/ne_download.R
939c93e9d9a3c2e16ce8ed380760c060 *R/ne_file_name.R
c85cb08f5a545cec4b0b43637084e502 *R/ne_load.R
47cce236e82cf69fc06bc10f2e632f26 *R/ne_states.R
df6cc46bc7fae1a55b713f3d5065b35a *R/rnaturalearth-package.R
541815b1390ded172111c0b6c405a1e2 *R/utils.R
b3eb59c04f37dc0d0bc5288168a513ca *README.md
92d3bfbf53482e6f98ca7f104001a49a *build/vignette.rds
1cfc0dba54b056606e4a66f25a216ad7 *data/countries110.rda
80684b12910f6c0e67a2330462fae571 *data/df_layers_cultural.rda
f7f6e965e447b5b21d559306beebdcef *data/df_layers_physical.rda
4d0a6e36c51838b8997cd4796ffec1eb *inst/doc/finding-data.Rmd
23d3f7070641edda81e07c37a9b24d8c *inst/doc/finding-data.html
b3c7f129a61fc5df44d271a37aca104a *inst/doc/language-support.Rmd
1b74def497cb3b098a6c9c6bea83587e *inst/doc/language-support.html
695486c9852275a2002b1abc1c39bc27 *inst/doc/rnaturalearth.Rmd
5bea1417fbe6a260935bc171a55f4b71 *inst/doc/rnaturalearth.html
a6a045b2487706c7aa0fb81954b41be1 *inst/doc/what-is-a-country.Rmd
76fa02d088fcb7b5694de0ab080c390b *inst/doc/what-is-a-country.html
36dc78d189a81dcbdfdd3619cf8e9da1 *inst/extdata/data_list_cultural.csv
d7fcdf63783cbad931a2a1b2f66ac81a *inst/extdata/data_list_physical.csv
ee52064357f5b042872629507b7c710b *man/check_data_exist.Rd
7bfb7a39e75834b91517ec4b5abe9f45 *man/check_rnaturalearthdata.Rd
ed1ce0094a5169214b53253c6553d3d9 *man/check_rnaturalearthhires.Rd
a165a06a857fd7980914d578f63303e5 *man/check_scale.Rd
27b6aa066b85e4910effdd959692f1f6 *man/convert_spatial_class.Rd
230d6ed2b7ee46898c60a125adbfe9a6 *man/countries.Rd
17bb5c7a2894241f7fcbb7ae4b482644 *man/df_layers_cultural.Rd
0f8f1d170cd071cd2e40b282b52850af *man/df_layers_physical.Rd
6bd8840056d547e014d2e0cb766ef4ea *man/figures/README-unnamed-chunk-2-1.png
1d886778c33dcffbf8ffb0baaaa7be19 *man/figures/README-unnamed-chunk-2-1.svg
8b4e8c3051c3b37be26b3dc96af9ae3a *man/figures/README-unnamed-chunk-2-2.png
c31f0a1cdb3d2e771a45b74c27a855ce *man/figures/README-unnamed-chunk-2-2.svg
db413f1f140b495290abd4f5ec4386f7 *man/figures/README-unnamed-chunk-2-3.png
c4dfefc73624eba9c32e5f9c2fee8784 *man/figures/README-unnamed-chunk-2-3.svg
9189eb0a4566bd4072c2d72f7487b222 *man/figures/logo.png
3c46e6587b1c3641620069f120f88e63 *man/get_data.Rd
efceba0be8d8ed76aac8ef867d755f1e *man/install_rnaturalearthdata.Rd
01f9d0eb183e076f01ffab786495b893 *man/install_rnaturalearthhires.Rd
7d4c9b812f0b53dd7c616d83a7dd3564 *man/layer_name.Rd
7920e75c2790542b7c90201f14cb755f *man/make_dest_path.Rd
c9927c61a75111d1a825eea18c6c0d87 *man/ne_coastline.Rd
4509e2eeca83a4313549f9cd56cfefec *man/ne_countries.Rd
492c474d351f2e8551d51bc6b45aceb7 *man/ne_download.Rd
d08b1de4267289628fd82690ad0b52cc *man/ne_file_name.Rd
7a6d2a52c7cd0a26867e59abf9c2cc2f *man/ne_find_vector_data.Rd
64e431de5558015a9f04bf4aa392755e *man/ne_git_contents.Rd
29bf675d992a8415824439a1dae522b2 *man/ne_git_layer_names.Rd
a550c3ac163836dd543034dd154eba26 *man/ne_load.Rd
c596ea4adf03655603aab2dfb20add30 *man/ne_states.Rd
33c9c52408534541a3211aeecfad02ea *man/normalize_type.Rd
9bd086e6489ccdab5a1a133c7c25641a *man/read_spatial_vector.Rd
8399a13e208ad1570ef3d42147195aeb *man/rnaturalearth-package.Rd
5c62f3162b4d2f2fab6a7df68b2bc10d *man/sanitize_gdal_url.Rd
063a58f6bf7e679356c4c42cbd797fb3 *tests/testthat.R
cd0885db6f71415dafe7120d3919e77f *tests/testthat/test_ne_countries.R
49ed6dac8273e16c0b957c2542c44ce5 *tests/testthat/test_ne_download.R
d9aa84ecdebaf90109918405d44eca75 *tests/testthat/test_ne_file_name.R
612de5d1e29081b7238f1b0a066ac668 *tests/testthat/test_ne_load.R
19187d8114a7fba4079da4a2b0f2bc1e *tests/testthat/test_ne_states.R
1ae9298560e17a61e382dde2acd4cfaf *tests/testthat/test_returnclass.R
6e02a3c74f51a7ce96cec87a62b4b390 *vignettes/create_cran_vignettes.R
4d0a6e36c51838b8997cd4796ffec1eb *vignettes/finding-data.Rmd
6e7671eb73c370196803fa2f3ccd3a4a *vignettes/finding-data.Rmd-10-1.png
2b559de7fd64e7df3a7c83b2a205eb6d *vignettes/finding-data.Rmd-12-1.png
af41d7b7892d297a9a3301024e692325 *vignettes/finding-data.Rmd-12-2.png
4dbc4222b08f16bfad438d40bf6be545 *vignettes/finding-data.Rmd-12-3.png
6626d02b07050bb68e85d8d84eb5923a *vignettes/finding-data.Rmd-12-4.png
e1e8bf98dbfa341794b2948dde296c13 *vignettes/finding-data.Rmd-12-5.png
36ad755db282a43e1510340e13532a2f *vignettes/finding-data.Rmd-12-6.png
f347de81b53de60df4269382a3eec55c *vignettes/finding-data.Rmd-12-7.png
546d099e96e8b463c028091614235076 *vignettes/finding-data.Rmd-12-8.png
a29483df32b9614bc5e1db8e5a31c233 *vignettes/finding-data.Rmd-3-1.png
33280b4f057c16ad9b50346666c76ba2 *vignettes/finding-data.Rmd-5-1.png
95f67c1a7d490041dd4a989a634ddb5c *vignettes/finding-data.Rmd-7-1.png
e2ec06f4bd4b11978ab33dad5418544d *vignettes/finding-data.Rmd-7-2.png
807007eec58cfb6de5c8552c7ce54c2e *vignettes/finding-data.Rmd-7-3.png
befb76939a35c52e6553e69b1f021d1f *vignettes/finding-data.Rmd-7-4.png
f3b0561ffa8f0d54cb31a1776967d9db *vignettes/finding-data.Rmd-7-5.png
19f941be24fad507a31b262dcb58401d *vignettes/finding-data.Rmd-7-6.png
bc08bd13fe1c08ae145b219235a0b945 *vignettes/finding-data.Rmd.orig
b3c7f129a61fc5df44d271a37aca104a *vignettes/language-support.Rmd
4c8a1cd7e40990bb85579e557b6e84de *vignettes/language-support.Rmd-3-1.png
70e917362656ab6c8b6e18d5e3405548 *vignettes/language-support.Rmd-3-10.png
42e678d88887ef4005e6248f7b63b655 *vignettes/language-support.Rmd-3-2.png
4075ab9176e0b64317e37315eeb35c81 *vignettes/language-support.Rmd-3-3.png
fbd5b967c8866685e72c2c7726f1ea3a *vignettes/language-support.Rmd-3-4.png
9980e08c20e9fdd539a48d962418db68 *vignettes/language-support.Rmd-3-5.png
0c9d9cdafe4c27cae45f71bbc902b5e6 *vignettes/language-support.Rmd-3-6.png
c5e01569c026dedd3ae5531ed0abaf67 *vignettes/language-support.Rmd-3-7.png
f05b8e32babf8b27602fc23c9ae3054d *vignettes/language-support.Rmd-3-8.png
f55ccdfb0085b480beb3d1b78c9c3a5e *vignettes/language-support.Rmd-3-9.png
084223a2789226bbaca734e4c8b8e101 *vignettes/language-support.Rmd.orig
695486c9852275a2002b1abc1c39bc27 *vignettes/rnaturalearth.Rmd
d493163a5c2807c0ff96527bd8259f31 *vignettes/rnaturalearth.Rmd-3-1.png
8d3e35986903e67af829c4853af2d736 *vignettes/rnaturalearth.Rmd-3-2.png
f5f0266639940bddca4b2890961c15c9 *vignettes/rnaturalearth.Rmd-3-3.png
8d3e35986903e67af829c4853af2d736 *vignettes/rnaturalearth.Rmd-3-4.png
67bb59bcd24341561e3dd925dac12bbc *vignettes/rnaturalearth.Rmd-3-5.png
57fb1ed625d510ede8ccc1320a09c90c *vignettes/rnaturalearth.Rmd-5-1.png
d8506137b9deb35a1fe0b91ac540a79b *vignettes/rnaturalearth.Rmd.orig
a6a045b2487706c7aa0fb81954b41be1 *vignettes/what-is-a-country.Rmd
ca5d34b8ad9bae76c6037f623b4d780f *vignettes/what-is-a-country.Rmd-3-1.png
6f3868d7a3073471cf0b4d4297294410 *vignettes/what-is-a-country.Rmd-3-2.png
653a4b99120695b2100331c1761820f9 *vignettes/what-is-a-country.Rmd-3-3.png
bada1b3ea2f1dfa013a10d93293fa572 *vignettes/what-is-a-country.Rmd-3-4.png
3ca0dcf2be22d27a7acab4788698eade *vignettes/what-is-a-country.Rmd-3-5.png
d16e9cbcc9f5d0c6680b35c41f6d57ac *vignettes/what-is-a-country.Rmd-3-6.png
fe4b5112a3639569c3e8aa54460a27cf *vignettes/what-is-a-country.Rmd-3-7.png
9c3983356fbb7a93ad88316eb932908c *vignettes/what-is-a-country.Rmd-4-1.png
db79f007145b9b0f18b92413144b75a1 *vignettes/what-is-a-country.Rmd-4-2.png
e8e79a39a857f6dcf340d42d0178a114 *vignettes/what-is-a-country.Rmd-4-3.png
b15c969bb5b32f972e6689965638ff90 *vignettes/what-is-a-country.Rmd-5-1.png
a147ebd4a67ee982ef7722d00a7217f8 *vignettes/what-is-a-country.Rmd-5-2.png
8af31c4fc2aeff0654231b0e0625aab2 *vignettes/what-is-a-country.Rmd-5-3.png
c2963630698823432086f29def8603b8 *vignettes/what-is-a-country.Rmd-5-4.png
9d5c472dffbc6d759777fb85a8d6783c *vignettes/what-is-a-country.Rmd.orig
rnaturalearth/R/ 0000755 0001762 0000144 00000000000 15127775531 013344 5 ustar ligges users rnaturalearth/R/ne_states.R 0000644 0001762 0000144 00000006640 15063246026 015451 0 ustar ligges users #' Get natural earth world state (admin level 1) polygons
#'
#' returns state polygons (administrative level 1) for specified countries
#'
#' @inherit ne_download
#'
#' @param country a character vector of country names.
#' @param geounit a character vector of geounit names.
#' @param iso_a2 a character vector of iso_a2 country codes
#' @param spat_object an optional alternative states map
#'
#' @details
#' By default, this function uses the scale = 10 data from the
#' rnaturalearthhires package. While data at scales 50 and 110 exist,
#' they are not used as defaults because they contain fewer countries
#' (e.g. scale 50 only includes Australia, Brazil, Canada and United
#' States of America). For finer control, use \code{\link{ne_download}}.
#'
#' \code{ne_download( scale = 10L, type = "states", category = "cultural")}
#'
#' \code{ne_download( scale = 50L, type = "states", category = "cultural")}
#'
#' \code{ne_download( scale = 110L, type = "states", category = "cultural")}
#'
#' @aliases ne_admin1
#'
#' @examples
#'
#' # comparing using country and geounit to filter
#' if (requireNamespace("rnaturalearthhires")) {
#' spdf_france_country <- ne_states(country = "france")
#' spdf_france_geounit <- ne_states(geounit = "france")
#'
#' plot(spdf_france_country)
#' plot(spdf_france_geounit)
#'
#' plot(ne_states(country = "united kingdom"))
#' plot(ne_states(geounit = "england"))
#' }
#'
#' @export
ne_states <- function(
country = NULL,
geounit = NULL,
iso_a2 = NULL,
spat_object = NULL,
returnclass = c("sf", "sv")
) {
returnclass <- match.arg(returnclass)
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}
# set map from one stored this adds potential to add or pass other potential
# state maps, e.g. without lakes but no checking is done, may not be needed
if (is.null(spat_object)) {
check_rnaturalearthhires()
spat_object <- rnaturalearthhires::states10
}
# set default filter
filter <- TRUE
# filter by country name (admin field in ne)
if (!is.null(country)) {
# check if field in data, for passed data or if changed in future
if (!("admin" %in% names(spat_object))) {
cli::cli_abort("No admin field in the data : {.val {names(spat_object)}}")
}
filter_country <- tolower(spat_object[["admin"]]) %in% tolower(country)
filter <- filter & filter_country
if (sum(filter_country) == 0L) {
cli::cli_abort("No such country ({.val {country}}) in the data.")
}
}
# filter by geounit
if (!is.null(geounit)) {
if (!("geonunit" %in% names(spat_object))) {
cli::cli_abort(
"No geonunit field in the data : {names(spat_object)}"
)
}
filter_geounit <- tolower(spat_object[["geonunit"]]) %in% tolower(geounit)
filter <- filter & filter_geounit
if (sum(filter_geounit) == 0L) {
cli::cli_abort("No such geounit ({.val {geounit}}) in the data.")
}
}
# filter by iso_a2
if (!is.null(iso_a2)) {
if (!("iso_a2" %in% names(spat_object))) {
cli::cli_abort(
"No {.var iso_a2} field in the data : {names(spat_object)}."
)
}
filter_iso_a2 <- tolower(spat_object[["iso_a2"]]) %in% tolower(iso_a2)
filter <- filter & filter_iso_a2
if (sum(filter_iso_a2) == 0L) {
cli::cli_abort("No such iso_a2 ({.val {iso_a2}}) in the data.")
}
}
# Convert to the desired class
convert_spatial_class(spat_object[filter, ], returnclass)
}
rnaturalearth/R/ne_download.R 0000644 0001762 0000144 00000016705 15127775427 015775 0 ustar ligges users #' Download data from Natural Earth and (optionally) read into R
#'
#' returns downloaded data as a spatial object or the filename if
#' \code{load=FALSE}. if \code{destdir} is specified the data can be reloaded in
#' a later R session using \code{\link{ne_load}} with the same arguments.
#'
#' @param scale The scale of map to return, one of `110`, `50`, `10` or `small`,
#' `medium`, `large`.
#'
#' @param type type of natural earth file to download one of 'countries',
#' 'map_units', 'map_subunits', 'sovereignty', 'states' OR the portion of any
#' natural earth vector url after the scale and before the . e.g. for
#' 'ne_50m_urban_areas.zip' this would be 'urban_areas'. See Details. OR the
#' raster filename e.g. for 'MSR_50M.zip' this would be 'MSR_50M'
#'
#' @param category one of natural earth categories : 'cultural', 'physical',
#' 'raster'
#'
#' @param destdir where to save files, defaults to \code{tempdir()},
#' \code{getwd()} is also possible.
#'
#' @param load `TRUE` to load the spatial object into R, `FALSE` to return the
#' filename of the downloaded object. If the requested object is a vector, it
#' will be saved as a GPKG file. If a raster is requested, it will be saved as
#' a GeoTIFF file.
#'
#' @details Note that the filename of the requested object will be returned if
#' `load = FALSE`.
#'
#' @details
#' **Caching behavior:** When `load = TRUE` and `destdir` is set to a persistent
#' directory (not `tempdir()`), the function will check if the file already
#' exists locally. If it does, it will load from the cached file instead of
#' re-downloading. If the file doesn't exist, it will download from Natural Earth,
#' save a copy to `destdir`, and then load it into R. This allows you to use the
#' same code whether it's the first download or a subsequent load, avoiding
#' redundant downloads.
#'
#' @details
#' If `load = TRUE` and `destdir = tempdir()` (the default), the download will
#' be handled using the GDAL virtual file system, allowing direct access to the
#' data without writing it to disk.
#'
#' @seealso \code{\link{ne_load}}, pre-downloaded data are available using
#' \code{\link{ne_countries}}, \code{\link{ne_states}}. Other geographic data
#' are available in the raster package : \code{\link[raster]{getData}}.
#'
#' @param returnclass A string determining the spatial object to return. Either
#' "sf" for for simple feature (from `sf`, the default) or "sv" for a
#' `SpatVector` (from `terra`).
#'
#' @return An object of class `sf` for simple feature (from `sf`, the default)
#' or `SpatVector` (from `terra`).
#'
#' @examples \dontrun{
#' # Download and load into R (default behavior, no local copy saved)
#' spdf_world <- ne_download(scale = 110, type = "countries")
#' plot(spdf_world)
#'
#' # Download with caching: first call downloads and saves, subsequent calls load from cache
#' # First call: downloads from Natural Earth and saves to "my_maps" directory
#' spdf_world <- ne_download(scale = 110, type = "countries", destdir = "my_maps")
#'
#' # Second call: loads from cached file in "my_maps" (no re-download)
#' spdf_world2 <- ne_download(scale = 110, type = "countries", destdir = "my_maps")
#'
#' # This works the same across R sessions - same code, caches automatically
#'
#' # You can also use ne_load() to explicitly load from cache
#' spdf_world3 <- ne_load(scale = 110, type = "countries", destdir = "my_maps")
#'
#' # Download and save without loading into R
#' file_path <- ne_download(
#' scale = 110, type = "countries",
#' destdir = "my_maps", load = FALSE
#' )
#'
#' # Raster example with caching
#' rst <- ne_download(
#' scale = 50, type = "MSR_50M",
#' category = "raster", destdir = "my_maps"
#' )
#' # Subsequent calls will use cached version
#' rst2 <- ne_download(
#' scale = 50, type = "MSR_50M",
#' category = "raster", destdir = "my_maps"
#' )
#'
#' library(terra)
#' terra::plot(rst)
#' # end dontrun
#' }
#'
#' @export
ne_download <- function(
scale = 110L,
type = "countries",
category = c("cultural", "physical", "raster"),
destdir = tempdir(),
load = TRUE,
returnclass = c("sf", "sv")
) {
category <- match.arg(category)
returnclass <- match.arg(returnclass)
if (!dir.exists(destdir)) {
cli::cli_abort("{.arg destdir} must be an existing directory")
}
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}
scale <- check_scale(scale)
gdal_url <- ne_file_name(
scale = scale,
type = type,
category = category
)
# Check if we should use cached file
# When load = TRUE and destdir is not tempdir, use caching behavior
use_cache <- load && !identical(destdir, tempdir())
if (use_cache) {
spatial_file_path <- make_dest_path(gdal_url, category, destdir)
# If file exists in cache, load it
if (file.exists(spatial_file_path)) {
cli::cli_inform(
"Loading {.file {basename(spatial_file_path)}} from cache..."
)
if (category == "raster") {
return(terra::rast(spatial_file_path))
} else {
layer <- layer_name(type, scale)
return(read_spatial_vector(
spatial_file_path,
layer = layer,
returnclass = returnclass
))
}
}
}
cli::cli_inform("Reading {.file {basename(gdal_url)}} from naturalearth...")
if (category == "raster") {
# Try without subfolder first (TYPE.zip/TYPE.tif), then with subfolder
# (TYPE.zip/TYPE/TYPE.tif) since Natural Earth zips have inconsistent
# structure
spatial_object <- tryCatch(
suppressWarnings(terra::rast(gdal_url)),
error = function(e) {
type_name <- tools::file_path_sans_ext(basename(gdal_url))
gdal_url_with_folder <- file.path(
dirname(gdal_url),
type_name,
basename(gdal_url)
)
terra::rast(gdal_url_with_folder)
}
)
} else {
layer <- layer_name(type, scale)
spatial_object <- read_spatial_vector(
gdal_url,
layer = layer,
returnclass = returnclass
)
}
if (load) {
# If using cache directory, save a copy for future use
if (use_cache) {
spatial_file_path <- make_dest_path(gdal_url, category, destdir)
cli::cli_inform(
"Writing {.file {basename(spatial_file_path)}} to {.path {destdir}}..."
)
if (category == "raster") {
terra::writeRaster(
spatial_object,
spatial_file_path,
overwrite = TRUE,
gdal = c(
"COMPRESS=ZSTD",
"PREDICTOR=2",
"TILED=YES",
"TFW=NO",
"BLOCKXSIZE=256",
"BLOCKYSIZE=256"
)
)
} else if (returnclass == "sf") {
sf::write_sf(spatial_object, spatial_file_path, delete_dsn = TRUE)
} else {
terra::writeVector(spatial_object, spatial_file_path, overwrite = TRUE)
}
}
return(spatial_object)
}
spatial_file_path <- make_dest_path(gdal_url, category, destdir)
cli::cli_inform(
"Writing {.file {basename(spatial_file_path)}} to {.path {destdir}}..."
)
if (category == "raster") {
terra::writeRaster(
spatial_object,
spatial_file_path,
overwrite = TRUE,
gdal = c(
"COMPRESS=ZSTD",
"PREDICTOR=2",
"TILED=YES",
"TFW=NO",
"BLOCKXSIZE=256",
"BLOCKYSIZE=256"
)
)
} else if (returnclass == "sf") {
sf::write_sf(spatial_object, spatial_file_path, delete_dsn = TRUE)
} else {
terra::writeVector(spatial_object, spatial_file_path, overwrite = TRUE)
}
invisible(spatial_file_path)
}
rnaturalearth/R/ne_countries.R 0000644 0001762 0000144 00000005513 15121020554 016146 0 ustar ligges users #' Get natural earth world country polygons
#'
#' returns world country polygons at a specified scale, or points of
#' tiny_countries
#'
#' @inherit ne_download
#'
#' @param type country type, one of 'countries', 'map_units', 'sovereignty',
#' 'tiny_countries'
#'
#' @param continent a character vector of continent names to get countries from.
#'
#' @param country a character vector of country names.
#'
#' @param geounit a character vector of geounit names.
#'
#' @param sovereignty a character vector of sovereignty names.
#'
#' @aliases ne_admin0
#'
#' @examples
#' world <- ne_countries()
#' africa <- ne_countries(continent = "africa")
#' france <- ne_countries(country = "france")
#'
#' plot(world$geometry)
#' plot(africa$geometry)
#' plot(france$geometry)
#'
#' # get as SpatVector
#' world <- ne_countries(returnclass = "sv")
#' terra::plot(world)
#'
#' tiny_countries <- ne_countries(type = "tiny_countries", scale = 50)
#' plot(tiny_countries)
#'
#' @export
ne_countries <- function(
scale = 110L,
type = "countries",
continent = NULL,
country = NULL,
geounit = NULL,
sovereignty = NULL,
returnclass = c("sf", "sv")
) {
returnclass <- match.arg(returnclass)
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}
spat_object <- get_data(scale = scale, type = type)
# some large scale NE data still have old uppercase fieldnames, this to
# correct
names(spat_object) <- tolower(names(spat_object))
# set default filter
filter <- TRUE
# filter by continent
if (!is.null(continent)) {
filter <- tolower(spat_object[["continent"]]) %in% tolower(continent)
if (sum(filter) == 0L) {
cli::cli_abort("No such continent ({.val {continent}}) in the data.")
}
}
# filter by country name (admin field in ne) todo I might be able to add the
# name field from ne in here too
if (!is.null(country)) {
filter_country <- tolower(spat_object[["admin"]]) %in% tolower(country)
filter <- filter & filter_country
if (sum(filter_country) == 0L) {
cli::cli_abort("No such country ({.val {country}}) in the data.")
}
}
# filter by geounit
if (!is.null(geounit)) {
filter_geounit <- tolower(spat_object[["geounit"]]) %in% tolower(geounit)
filter <- filter & filter_geounit
if (sum(filter_geounit) == 0L) {
cli::cli_abort("No such geounit ({.val {geounit}}) in the data.")
}
}
# filter by sovereignty (BEWARE its called sovereignt in ne)
if (!is.null(sovereignty)) {
filter_sovereignty <- tolower(spat_object[["sovereignt"]]) %in%
tolower(sovereignty)
filter <- filter & filter_sovereignty
if (sum(filter_sovereignty) == 0L) {
cli::cli_abort("No such sovereignty ({.val {sovereignty}}) in the data")
}
}
# Convert to the desired class
convert_spatial_class(spat_object[filter, ], returnclass)
}
rnaturalearth/R/find_data.R 0000644 0001762 0000144 00000012740 15121020554 015362 0 ustar ligges users #' Return a dataframe of available vector layers on Natural Earth
#'
#' Checks the Natural Earth Github repository for current vector layers and
#' provides the file name required in the type argument of ne_download.
#'
#' @inherit ne_download
#'
#' @param category one of natural earth categories : 'cultural', 'physical'
#'
#' @param getmeta whether to get url of the metadata for each layer
#'
#' @return dataframe with two variables: layer and metadata
#'
#' @export
#'
#' @examples \dontrun{
#' ne_find_vector_data(scale = 10, category = "physical")
#' }
ne_find_vector_data <- function(
scale = 110L,
category = c("cultural", "physical"),
getmeta = FALSE
) {
## check permitted category (no way to check against available rasters)
category <- match.arg(category)
## check on permitted scales, convert names to numeric
scale <- check_scale(scale)
## Available paths include: 10m_cultural, 10m_physical,
## 50m_cultural, etc...
# scale has already been checked in check_scale, and category in match.arg
path <- paste0(scale, "m_", category)
## call to ne_git_contents returns a list with contents of
## github directory (based on specified path), github api
## response, and http path.
resp <- ne_git_contents(path = path)
## call to git layer names returns a list of lists with
## valid layer names and metadata link
layers <- ne_git_layer_names(x = resp, scale = scale, getmeta = getmeta)
## I think returning as a data.frame makes the most sense
if (getmeta) {
layers <- data.frame(
layer = layers[["layer"]],
scale = scale,
metadata = layers[["metalink"]]
)
} else {
layers <- data.frame(
layer = layers[["layer"]],
scale = scale
)
}
return(layers)
}
#' Return contents of Natural Earth Github directory
#'
#' Uses the Github API to return contents of Natural Earth Github directories.
#' @param path string, one of: \code{'110m_physical'}, \code{'110m_cultural'},
#' \code{'50m_physical'}, \code{'50m_cultural'}, \code{'10m_physical'},
#' \code{'10m_cultural'}
#'
#' @return list. Includes parsed json content, http path, and response
#' code.
#'
#' @keywords internal
ne_git_contents <- function(path) {
## create pathnames to natural earth vector folders on github use httr::GET
## and the github api to access the github contents API. Note that this is
## rate limited, so someone could get locked. Probably don't want to include
## this in CRAN tests.
path <- paste0("/repos/nvkelso/natural-earth-vector/contents/", path)
url <- httr::modify_url("https://api.github.com", path = path)
ua <- httr::user_agent("http://github.com/ropensci/rnaturalearth")
resp <- httr::GET(url, ua)
if (httr::http_type(resp) != "application/json") {
cli::cli_abort("API did not return json")
}
df <- httr::content(x = resp, as = "text", encoding = "UTF-8")
df <- jsonlite::fromJSON(df)
if (httr::status_code(resp) != 200L) {
cli::cli_abort(
sprintf(
"GitHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
df[["message"]],
df[["documentation_url"]]
)
)
}
structure(
list(
content = df,
path = path,
response = resp
)
)
}
#' Create list of layer names and metadata links
#'
#' Parses Natural Earth Github folder content for layer names and metadata
#' links.
#'
#' @param x object returned by ne_git_contents
#'
#' @param scale one of \code{110}, \code{50}, \code{10}
#'
#' @param getmeta whether to get url of the metadata for each layer
#'
#' @return list of lists with layer names and metadata links.
#' @keywords internal
ne_git_layer_names <- function(x, scale, getmeta) {
## uses the output from ne_git_contents
## creates a list of available layer names and
## list of metadata links
if (httr::status_code(x$response) != 200L) {
cli::cli_abort(
sprintf(
"GitHub API request failed [%s]\n%s\n<%s>",
httr::status_code(x$response),
x$content$message,
x$content$documentation_url
)
)
}
## Create the pattern that matches the prefix that should be removed
prefix <- paste0("ne_", scale, "m_")
## clean and return layer names
l <- x$content
# gets just readme because 1 per layer
l <- regmatches(l$name, regexpr("^(.*).README.html", l$name))
# cuts off parts of filenames to get just the layer name
l <- gsub(".README.html", "", l)
l <- gsub(prefix, "", l)
# if we didn't need links to metadata then this function could stop here
if (!getmeta) {
## return a list with just layer name
return(structure(list(layer = l)))
}
## clean and return links to the metadata on NaturalEarthData.com
m <- x$content$download_url
m <- regmatches(m, regexpr("^(.*).README.html", m))
findlinks <- function(x) {
page <- httr::content(httr::GET(x))
link <- regmatches(
page,
regexpr(
'',
page
)
)
link <- gsub("", "", link)
}
## iterate findlinks function through each html page to
## pull out the metadata link. Adding an optional status bar
## since downloads could stall.
## pbapply is suggested,
## if user doesn't have pbapply installed, lapply is used.
m <- if (requireNamespace("pbapply", quietly = TRUE)) {
pbapply::pblapply(m, findlinks)
} else {
lapply(m, findlinks)
}
m <- unlist(m)
## return a list with layer name and metadata link
structure(list(layer = l, metalink = m))
}
rnaturalearth/R/ne_coastline.R 0000644 0001762 0000144 00000002014 15121020554 016105 0 ustar ligges users #' Get natural earth world coastline
#'
#' returns world coastline at specified scale
#'
#' @inherit ne_download
#'
#' @examples
#' if (requireNamespace("rnaturalearthdata")) {
#' coast <- ne_coastline()
#' plot(coast)
#' }
#'
#' @export
ne_coastline <- function(scale = 110L, returnclass = c("sf", "sv")) {
returnclass <- match.arg(returnclass)
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}
# check for the data packages and try to install if not there
if (scale == 10L) {
check_rnaturalearthhires()
} else {
check_rnaturalearthdata()
}
# check on permitted scales, convert names to numeric
scale <- check_scale(scale)
# choose which map based on scale
sldf <- NULL
if (scale == 110L) {
sldf <- rnaturalearthdata::coastline110
} else if (scale == 50L) {
sldf <- rnaturalearthdata::coastline50
} else if (scale == 10L) {
sldf <- rnaturalearthhires::coastline10
}
# Convert to the desired class
convert_spatial_class(sldf, returnclass)
}
rnaturalearth/R/deprecate_sp.R 0000644 0001762 0000144 00000000456 14765615305 016131 0 ustar ligges users deprecate_sp <- function(what, env = rlang::caller_env()) {
lifecycle::deprecate_stop(
when = "1.0.0",
what = what,
details = "Please use `sf` objects with {rnaturalearth}, Spatial objects (sp) are no longer supported. For example: `ne_download(returnclass = 'sf')`",
env = env
)
}
rnaturalearth/R/install-rnaturalearthhires.R 0000644 0001762 0000144 00000003003 15062323506 021023 0 ustar ligges users #' Check whether to install rnaturalearthhires and install if necessary
#'
#' If the rnaturalearthhires package is not installed, install it from GitHub
#' using pak. If it is not up to date, reinstall it.
#'
#' @export
check_rnaturalearthhires <- function() {
# TODO: This should be done dynamically by checking the version on Github
rnaturalearthhires_version <- "1.0.0.9000"
if (!requireNamespace("rnaturalearthhires", quietly = TRUE)) {
cli::cli_inform(
"The {.pkg rnaturalearthhires} package needs to be installed."
)
install_rnaturalearthhires()
} else if (
utils::packageVersion("rnaturalearthhires") < rnaturalearthhires_version
) {
cli::cli_inform(
"The {.pkg rnaturalearthhires} package needs to be updated."
)
install_rnaturalearthhires()
}
}
#' Install the naturalearthhires package after checking with the user
#' @export
install_rnaturalearthhires <- function() {
input <- 1L
if (interactive()) {
input <- utils::menu(
c("Yes", "No"),
title = "Install the rnaturalearthhires package?"
)
}
if (input != 1L) {
cli::cli_abort(
"The {.pkg rnaturalearthhires} package is necessary for that method.\n Please try installing the package yourself with {.code pak::pkg_install(\"ropensci/rnaturalearthhires\")}"
)
}
cli::cli_inform("Installing the {.pkg rnaturalearthhires} package.")
tryCatch(
pak::pkg_install("ropensci/rnaturalearthhires"),
error = function(e) {
cli::cli_inform(conditionMessage(e))
}
)
}
rnaturalearth/R/rnaturalearth-package.R 0000644 0001762 0000144 00000000135 14777246403 017734 0 ustar ligges users #' @keywords internal
"_PACKAGE"
## usethis namespace: start
## usethis namespace: end
NULL
rnaturalearth/R/ne_load.R 0000644 0001762 0000144 00000006035 15063246026 015063 0 ustar ligges users #' load a Natural Earth vector that has already been downloaded to R using
#' \code{\link{ne_download}}
#'
#' returns loaded data as a spatial object.
#'
#' @inherit ne_download
#'
#' @param type type of natural earth file one of 'countries', 'map_units',
#' 'map_subunits', 'sovereignty', 'states' OR the portion of any natural
#' earth vector url after the scale and before the . e.g. for
#' 'ne_50m_urban_areas.zip' this would be 'urban_areas' OR the raster
#' filename e.g. for 'MSR_50M.zip' this would be 'MSR_50M'
#'
#' @param category one of natural earth categories : 'cultural', 'physical',
#' 'raster'
#'
#' @param destdir folder to load files from, default = tempdir()
#'
#' @param file_name OPTIONAL name of file (excluding path) instead of natural
#' earth attributes
#'
#' @details This function should be used after first downloading the data with
#' \code{ne_download(load = FALSE)}. The downloaded file can then be loaded
#' using this function.
#'
#' @seealso \code{\link{ne_download}}
#'
#' @examples \dontrun{
#' # download followed by load from tempdir() works in same R session
#' spdf_world <- ne_download(
#' scale = 110,
#' type = "countries"
#' )
#' spdf_world2 <- ne_load(
#' scale = 110,
#' type = "countries"
#' )
#'
#' # download followed by load from specified directory works between R sessions
#' spdf_world <- ne_download(
#' scale = 110,
#' type = "countries",
#' destdir = getwd()
#' )
#' spdf_world2 <- ne_load(
#' scale = 110,
#' type = "countries",
#' destdir = getwd()
#' )
#'
#' # for raster download & load
#' rst <- ne_download(
#' scale = 50,
#' type = "OB_50M",
#' category = "raster",
#' destdir = getwd(),
#' load = FALSE
#' )
#'
#' # load after having downloaded
#' rst <- ne_load(
#' scale = 50,
#' type = "OB_50M",
#' category = "raster",
#' destdir = getwd()
#' )
#'
#' # plot
#' library(terra)
#' plot(rst)
#' # end dontrun
#' }
#'
#' @export
ne_load <- function(
scale = 110L,
type = "countries",
category = c("cultural", "physical", "raster"),
destdir = tempdir(),
file_name = NULL,
returnclass = c("sf", "sv")
) {
category <- match.arg(category)
returnclass <- match.arg(returnclass)
scale <- check_scale(scale)
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}
if (is.null(file_name)) {
file_name <- ne_file_name(scale = scale, type = type, category = category)
}
spatial_file_path <- make_dest_path(file_name, category, destdir)
error_msg <- "The file {.path {spatial_file_path}} seems not to exist in your local folder {.path {destdir}}. Did you download it using {.fn rnaturalearth::ne_download}?"
if (!file.exists(spatial_file_path)) {
cli::cli_abort(error_msg)
}
if (category == "raster") {
rst <- terra::rast(spatial_file_path)
return(rst)
} else {
layer <- layer_name(type, scale)
# read in data as either sf of spatvector
spatial_object <- read_spatial_vector(
spatial_file_path,
layer = layer,
returnclass
)
return(spatial_object)
}
}
rnaturalearth/R/check_data_exist.R 0000644 0001762 0000144 00000004300 15121020554 016724 0 ustar ligges users #' check whether the requested data exist on Natural Earth
#'
#' checks from a list dependent on type, category and scale. If it returns FALSE
#' the data may still exist on the website. Doesn't yet do checking on raster
#' names because I found the naming convention too tricky.
#'
#' @inherit ne_download
#'
#' @param type type of natural earth file to download one of 'countries',
#' 'map_units', 'map_subunits', 'sovereignty', 'states' OR the portion of any
#' natural earth vector url after the scale and before the . e.g. for
#' 'ne_50m_urban_areas.zip' this would be 'urban_areas' OR the raster
#' filename e.g. for 'MSR_50M.zip' this would be 'MSR_50M'
#'
#' @param category one of natural earth categories : 'cultural', 'physical',
#' 'raster'
#' @examples
#' check_data_exist(type = "countries", scale = 110, category = "cultural")
#'
#' # Type not in list for this category
#' check_data_exist(type = "airports", scale = 110, category = "physical")
#'
#' # Type in list but scale shows FALSE
#' check_data_exist(type = "airports", scale = 110, category = "cultural")
#'
#' @return TRUE or FALSE
#'
#' @export
check_data_exist <- function(
type,
scale = 110L,
category = c("cultural", "physical", "raster")
) {
# check permitted category
category <- match.arg(category)
# I would need to create a data_list_raster.csv file
if (category == "raster") {
return(TRUE)
}
# check on permitted scales, convert names to numeric
scale <- check_scale(scale)
df_data <- utils::read.csv(
system.file(
"extdata",
paste0("data_list_", category, ".csv"),
package = "rnaturalearth"
)
)
# first check if type is within the list
if (!type %in% df_data[["type"]]) {
cli::cli_warn(
"{.arg {type}} seems not to be in the list for category= {.val {category}} maybe try the other category of c('cultural', 'physical')"
)
return(FALSE)
}
exist <- df_data[df_data[["type"]] == type, paste0("scale", scale)]
if (!exist) {
cli::cli_warn(
"The requested daa seem not to exist in the list of Natural Earth data. Check {.code ?ne_download} or {.url http://www.naturalearthdata.com/features/} to see data availability."
)
}
return(exist)
}
rnaturalearth/R/install-rnaturalearthdata.R 0000644 0001762 0000144 00000003354 15062323506 020633 0 ustar ligges users #' Check whether to install rnaturalearthdata and install if necessary
#'
#' If the rnaturalearthdata package is not installed, install it from GitHub
#' using pak. If it is not up to date, reinstall it.
#'
#' @export
check_rnaturalearthdata <- function() {
rnaturalearthdata_version <- "0.0.0.9000"
if (!requireNamespace("rnaturalearthdata", quietly = TRUE)) {
cli::cli_inform(
"The {.pkg rnaturalearthdata} package needs to be installed."
)
install_rnaturalearthdata()
} else if (
utils::packageVersion("rnaturalearthdata") < rnaturalearthdata_version
) {
cli::cli_inform("The {.pkg rnaturalearthdata} package needs to be updated.")
install_rnaturalearthdata()
}
}
#' Install the naturalearthdata package after checking with the user
#' @export
install_rnaturalearthdata <- function() {
error_func <- function(e) {
cli::cli_abort(
"Failed to install the {.pkg rnaturalearthdata} package.\nPlease try installing the package for yourself using the following command: {.code install.packages(\"rnaturalearthdata\")}"
)
}
# 23/2/17 changed to try install if not interactive to avoid winbuilder
# warning
input <- 1L
if (interactive()) {
input <- utils::menu(
c("Yes", "No"),
title = "Install the rnaturalearthdata package?"
)
}
if (input == 1L) {
cli::cli_inform("Installing the {.pkg rnaturalearthdata} package.")
tryCatch(
utils::install.packages(
"rnaturalearthdata",
repos = c("http://packages.ropensci.org", "http://cran.rstudio.com"),
type = "source"
),
error = error_func,
warning = error_func
)
} else {
cli::cli_abort(
"The {.pkg rnaturalearthdata} package is necessary for that method."
)
}
}
rnaturalearth/R/utils.R 0000644 0001762 0000144 00000004251 15126010320 014602 0 ustar ligges users #' Read Spatial Vector
#'
#' This function reads a spatial vector file and returns either an `sf` object
#' or a `sv` object.
#'
#' @param x A character string specifying the path to the spatial vector file.
#' @param returnclass A character string specifying the class of object to
#' return. Options are "sf" for sf object and "sv" for sv object.
#' @param layer A character string specifying the later to read in case there
#' are more than one in the zip file.
#'
#' @return Either an `sf` object or a `sv` object.
read_spatial_vector <- function(x, layer, returnclass = c("sf", "sv")) {
returnclass <- match.arg(returnclass)
switch(
returnclass,
sf = sf::st_read(x, layer = layer, quiet = TRUE, stringsAsFactors = FALSE),
sv = terra::vect(x, layer = layer)
)
}
#' Convert from/to sf/sv objects
#'
#' @inherit ne_download
#' @param x Object to be converted
#' @return Object of class "sf" or "sv"
convert_spatial_class <- function(x, returnclass = c("sf", "sv")) {
returnclass <- match.arg(returnclass)
switch(returnclass, sf = sf::st_as_sf(x), sv = terra::vect(x))
}
#' @title Extracts the http URL from a VSIZIP URL
#' @description This function takes a VSIZIP URL and extracts the http URL from
#' it
#' @param url A character string representing the VSIZIP URL
#' @return A character string representing the http URL extracted from the
#' VSIZIP URL
sanitize_gdal_url <- function(url) {
url <- sub("^/vsizip//vsicurl/", "", url)
sub("(.+\\.zip)/.*", "\\1", url)
}
#' Create Destination File Path
#'
#' @description Creates a destination file path by combining the directory path
#' with a formatted filename based on the GDAL URL and category.
#'
#' @param gdal_url A character string representing the GDAL URL
#' @param category A character string specifying the data category ("raster" or other)
#' @param destdir A character string specifying the destination directory
#'
#' @return A character string representing the complete destination file path
make_dest_path <- function(gdal_url, category, destdir) {
file.path(
destdir,
sprintf(
"%s.%s",
tools::file_path_sans_ext(basename(gdal_url)),
ifelse(category == "raster", "tif", "gpkg")
)
)
}
rnaturalearth/R/ne_file_name.R 0000644 0001762 0000144 00000006076 15127775427 016105 0 ustar ligges users #' return a natural earth filename based on arguments
#'
#' returns a string that can then be used to download the file.
#'
#' @inherit ne_download
#'
#' @param type type of natural earth file to download one of 'countries',
#' 'map_units', 'map_subunits', 'sovereignty', 'states' OR the portion of any
#' natural earth vector url after the scale and before the . e.g. for
#' 'ne_50m_urban_areas.zip' this would be 'urban_areas' OR the raster
#' filename e.g. for 'MSR_50M.zip' this would be 'MSR_50M'
#'
#' @param category one of natural earth categories : 'cultural', 'physical',
#' 'raster'
#'
#' @examples
#' ne_url <- ne_file_name(scale = 110, type = "countries")
#'
#' @return string
#'
#' @export
ne_file_name <- function(
scale = 110L,
type = "countries",
category = c("cultural", "physical", "raster")
) {
scale <- check_scale(scale)
category <- match.arg(category)
type <- normalize_type(type)
base_url <- "/vsizip//vsicurl/https://naciscdn.org/naturalearth"
# Construct the file path based on the data category
gdal_url <- if (category == "raster") {
file.path(
base_url,
sprintf("%sm", scale),
category,
sprintf("%s.zip", type),
sprintf("%s.tif", type)
)
} else {
file.path(
base_url,
sprintf("%sm", scale),
category,
sprintf("ne_%sm_%s.zip", scale, type)
)
}
return(gdal_url)
}
#' Normalize the type argument for Natural Earth datasets
#'
#' This function standardizes the `type` argument by mapping common names to
#' their respective Natural Earth dataset names.
#'
#' @inheritParams ne_file_name
#'
#' @return A string representing the normalized dataset type.
normalize_type <- function(type) {
if (
type %in%
c(
"countries",
"map_units",
"map_subunits",
"sovereignty",
"tiny_countries",
"boundary_lines_land",
"pacific_groupings",
"breakaway_disputed_areas",
"boundary_lines_disputed_areas",
"boundary_lines_maritime_indicator"
)
) {
return(paste0("admin_0_", type))
}
if (
type %in%
c(
"parks_and_protected_lands_area",
"parks_and_protected_lands_line",
"parks_and_protected_lands_point",
"parks_and_protected_lands_scale_rank"
)
) {
return("parks_and_protected_lands")
}
if (type == "states") {
return("admin_1_states_provinces_lakes")
}
type
}
#' Generate the layer name for a Natural Earth dataset
#'
#' @inheritParams ne_file_name
#' @return A string representing the dataset layer name.
layer_name <- function(type, scale) {
if (
type %in%
c(
"countries",
"map_units",
"map_subunits",
"sovereignty",
"tiny_countries",
"boundary_lines_land",
"pacific_groupings",
"breakaway_disputed_areas",
"boundary_lines_disputed_areas",
"boundary_lines_maritime_indicator"
)
) {
type <- paste0("admin_0_", type)
}
if (type == "states") {
type <- "admin_1_states_provinces_lakes"
}
sprintf("ne_%sm_%s", scale, type)
}
rnaturalearth/R/datasets.R 0000644 0001762 0000144 00000001551 14777246403 015302 0 ustar ligges users #' world country polygons from Natural Earth
#'
#' at 1:110m scale (small). Other data and resolutions are in the packages
#' rnaturalearthdata and rnaturalearthhires.
#'
#' @format A \code{sf} object.
#'
#' @slot data A data frame with country attributes.
#'
#' @aliases countries110
#'
#' @name countries
NULL
#'
#' @source
#' \url{https://naciscdn.org/naturalearth/10m/cultural/ne_10m_admin_0_countries.zip}
#'
#' @rdname countries
"countries110"
#'
#' list of cultural layers available from Natural Earth
#'
#' @format A \code{DataFrame}
#' @aliases df_layers_cultural
#' @name df_layers_cultural
#'
NULL
#' @rdname df_layers_cultural
"df_layers_cultural"
#' list of physical layers available from Natural Earth
#'
#'
#' @format A \code{DataFrame}
#' @aliases df_layers_physical
#' @name df_layers_physical
#'
NULL
#' @rdname df_layers_physical
"df_layers_physical"
rnaturalearth/R/check_scale.R 0000644 0001762 0000144 00000001361 14777246407 015721 0 ustar ligges users #' check that this scale is present in Natural Earth
#'
#' check name or numeric scale representations, return numeric one
#'
#' @param x scale of map to return, one of \code{110}, \code{50}, \code{10} or
#' \code{'small'}, \code{'medium'}, \code{'large'}
#'
#' @return integer scale of map
check_scale <- function(x) {
if (is.numeric(x) && length(x) == 1L) {
if (x %in% c(110L, 50L, 10L)) {
return(x)
}
} else if (is.character(x) && length(x) == 1L) {
xnew <- c(small = 110L, medium = 50L, large = 10L)[tolower(x)]
if (!is.na(xnew)) {
return(unname(xnew))
}
}
cli::cli_abort(
"Invalid {.arg scale} argument. Must be one of {.val [110, 50, 10, 'small', 'medium', 'large']} you provided : {.val {x}}."
)
}
rnaturalearth/R/get_data.R 0000644 0001762 0000144 00000002740 15121020554 015220 0 ustar ligges users #' Get data from within the package
#'
#' returns world country polygons at a specified scale, used by ne_countries()
#'
#' @inherit ne_download
#'
#' @param type country type, one of 'countries', 'map_units', 'sovereignty',
#' 'tiny_countries'
#'
#' @return A \code{sf} object.
get_data <- function(
scale = 110L,
type = c(
"countries",
"map_units",
"sovereignty",
"tiny_countries"
)
) {
# check on permitted scale arg, convert names to numeric
scale <- check_scale(scale)
# check permitted type arg
type <- match.arg(type)
# tiny_countries not available at scale 10
if (type == "tiny_countries" && scale == 10L) {
cli::cli_abort(
"{.arg tiny_countries} are not available at scale 10, use scale 50 or 110."
)
}
# check for the data packages and try to install if not there
# avoid this check for one example dataset in this package (i.e. countries110)
if (scale == 10L) {
check_rnaturalearthhires()
} else if (!(scale == 110L && type == "countries")) {
check_rnaturalearthdata()
}
# choose which map based on type and scale (stored in different packages)
if (scale == 110L && type == "countries") {
sf_object <- getExportedValue("rnaturalearth", paste0(type, scale))
} else if (scale == 110L || scale == 50L) {
sf_object <- getExportedValue("rnaturalearthdata", paste0(type, scale))
} else if (scale == 10L) {
sf_object <- getExportedValue("rnaturalearthhires", paste0(type, scale))
}
return(sf_object)
}
rnaturalearth/vignettes/ 0000755 0001762 0000144 00000000000 15133435721 015142 5 ustar ligges users rnaturalearth/vignettes/finding-data.Rmd-7-1.png 0000644 0001762 0000144 00000015366 14777261331 021242 0 ustar ligges users PNG
IHDR )Jh( pHYs ~ IDATx{\wu$mb%C!MT[nBs36Qm{9).m:dqׯ_a4纾ǫJ( O = G@8B z p # = G@8B z ?QsV~W Ed57BG6כAO®+|V}}{Odkhn{yg/6_VSl[Պ(JE]lR!Ӿ^[iOШàbiuyΝ[b>M3|JZ4yI(C(7.lw}+oڷUhrweڎ"gz[&y'IG~