Skip to content

Commit

Permalink
Merge pull request #104 from rOpenSpain/displacecan
Browse files Browse the repository at this point in the history
Add a function to ease displacing of Canary objects
  • Loading branch information
dieghernan authored Jan 15, 2024
2 parents 75d9737 + 702c54f commit 1387abd
Show file tree
Hide file tree
Showing 57 changed files with 468 additions and 314 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message: 'To cite package "mapSpain" in publications use:'
type: software
license: GPL-3.0-only
title: 'mapSpain: Administrative Boundaries of Spain'
version: 0.8.0
version: 0.8.0.9000
doi: 10.5281/zenodo.5366622
abstract: Administrative Boundaries of Spain at several levels (Autonomous Communities,
Provinces, Municipalities) based on the 'GISCO' 'Eurostat' database <https://ec.europa.eu/eurostat/web/gisco>
Expand All @@ -31,7 +31,7 @@ preferred-citation:
orcid: https://orcid.org/0000-0001-8457-4658
affiliation: rOpenSpain
year: '2024'
version: 0.8.0
version: 0.8.0.9000
doi: 10.5281/zenodo.5366622
url: https://ropenspain.github.io/mapSpain/
abstract: Administrative Boundaries of Spain at several levels (Autonomous Communities,
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mapSpain
Title: Administrative Boundaries of Spain
Version: 0.8.0
Version: 0.8.0.9000
Authors@R:
person("Diego", "Hernangómez", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-8457-4658", affiliation = "rOpenSpain"))
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(addProviderEspTiles)
export(esp_check_access)
export(esp_clear_cache)
export(esp_detect_cache_dir)
export(esp_dict_region_code)
export(esp_dict_translate)
export(esp_getTiles)
Expand Down Expand Up @@ -35,8 +36,8 @@ export(esp_get_roads)
export(esp_get_simpl_ccaa)
export(esp_get_simpl_prov)
export(esp_make_provider)
export(esp_move_can)
export(esp_set_cache_dir)
export(layer_spatraster)
export(providerEspTileOptions)
importFrom(sf,st_transform)
importFrom(utils,download.file)
Expand Down
12 changes: 11 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# mapSpain (development version)

- Changes on how to handle modifications on Canary Islands objects (#101):
- Add a helper function for displace stand-alone **sf** objects in Canary
Islands: `esp_move_can()`.
- `esp_move_can()` is used internally on all functions.
- Add a new function to show the current cache directory:
`esp_detect_cache_dir()`.
- `mapSpain::layer_spatraster()` removed.

# mapSpain 0.8.0

- Improve download of NUTS data from **giscoR**.
Expand Down Expand Up @@ -87,7 +97,7 @@
- Move minimum version of `giscoR` to v0.2.4
- Fix typos on `esp_dict_translate()` #36.
- Not run examples on tiles, as the server sometimes doesn't respond.
- Refactor `sysdata.rda`.
- Re factor `sysdata.rda`.
- CRAN fixes:
- Removed broken link on `addProviderEspTiles()`.
- Vignette removed (CRAN warning).
Expand Down
26 changes: 26 additions & 0 deletions R/esp_cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,29 @@ esp_hlp_cachedir <- function(cache_dir = NULL) {
}
return(cache_dir)
}

#' Detect cache dir for \CRANpkg{mapSpain}
#'
#' @description
#'
#' Helper function to detect the current cache folder. See
#' [esp_set_cache_dir()]
#'
#'
#' @param x Ignored
#'
#' @return A character with the path to your `cache_dir`.
#'
#' @export
#'
#' @rdname esp_detect_cache_dir
#' @family cache utilities
#' @examples
#' esp_detect_cache_dir()
#'
esp_detect_cache_dir <- function(x = NULL) {
# Cheat linters
cd <- x
cd <- esp_hlp_detect_cache_dir()
cd
}
2 changes: 1 addition & 1 deletion R/esp_check_access.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Check access to SIANE data
#'
#' @concept helper
#' @family helper
#'
#' @description
#' Check if R has access to resources at
Expand Down
10 changes: 5 additions & 5 deletions R/esp_getTiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@
#' library(tidyterra)
#'
#' ggplot(segovia) +
#' geom_spatraster_rgb(data = tile) +
#' geom_spatraster_rgb(data = tile, maxcell = Inf) +
#' geom_sf(fill = NA)
#'
#' # Another provider
#'
#' tile2 <- esp_getTiles(segovia, type = "MDT")
#'
#' ggplot(segovia) +
#' geom_spatraster_rgb(data = tile2) +
#' geom_spatraster_rgb(data = tile2, maxcell = Inf) +
#' geom_sf(fill = NA)
#'
#' # A custom WMS provided
Expand All @@ -127,7 +127,7 @@
#'
#' custom_wms_tile <- esp_getTiles(segovia, custom_wms)
#'
#' autoplot(custom_wms_tile) +
#' autoplot(custom_wms_tile, maxcell = Inf) +
#' geom_sf(data = segovia, fill = NA, color = "red")
#'
#' # A custom WMTS provider
Expand All @@ -141,7 +141,7 @@
#'
#' custom_wmts_tile <- esp_getTiles(segovia, custom_wmts)
#'
#' autoplot(custom_wmts_tile) +
#' autoplot(custom_wmts_tile, maxcell = Inf) +
#' geom_sf(data = segovia, fill = NA, color = "white", linewidth = 2)
#'
#' # Example from https://leaflet-extras.github.io/leaflet-providers/preview/
Expand All @@ -151,7 +151,7 @@
#' )
#' cartodb <- esp_getTiles(segovia, cartodb_voyager, zoommin = 1)
#'
#' autoplot(cartodb) +
#' autoplot(cartodb, maxcell = Inf) +
#' geom_sf(data = segovia, fill = NA, color = "black", linewidth = 1)
#' }
esp_getTiles <- function(x,
Expand Down
34 changes: 6 additions & 28 deletions R/esp_get_can_box.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' Islands.
#'
#' @family political
#' @family Canary Islands
#'
#' @rdname esp_get_can_box
#'
Expand Down Expand Up @@ -158,20 +159,7 @@ esp_get_can_box <- function(style = "right",
moving <- isTRUE(moveCAN) | length(moveCAN) > 1

if (moving) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

can <- sf::st_transform(lall, 3857) + offset
can <- sf::st_sfc(can, crs = 3857)
can <- sf::st_transform(can, sf::st_crs(lall))
lall <- can
lall <- esp_move_can(lall, moveCAN = moveCAN)
}

# Transform
Expand All @@ -188,6 +176,9 @@ esp_get_can_box <- function(style = "right",
#' * [esp_get_can_provinces()] is used to draw a separator line between the two
#' provinces of the Canary Islands.
#'
#' See also [esp_move_can()] to displace stand-alone objects on the Canary
#' Islands.
#'
#' @return `esp_get_can_provinces` returns a `LINESTRING` object.
#'
#' @source
Expand Down Expand Up @@ -216,20 +207,7 @@ esp_get_can_provinces <- function(moveCAN = TRUE,
moving <- isTRUE(moveCAN) | length(moveCAN) > 1

if (moving) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

can <- sf::st_transform(lall, 3857) + offset
can <- sf::st_sfc(can, crs = 3857)
can <- sf::st_transform(can, sf::st_crs(lall))
lall <- can
lall <- esp_move_can(lall, moveCAN = moveCAN)
}

# Transform
Expand Down
19 changes: 2 additions & 17 deletions R/esp_get_capimun.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
#' library(tidyterra)
#'
#' ggplot(points) +
#' geom_spatraster_rgb(data = tile) +
#' geom_spatraster_rgb(data = tile, maxcell = Inf) +
#' geom_sf(data = area, fill = NA, color = "blue") +
#' geom_sf(data = points, aes(fill = type), size = 5, shape = 21) +
#' scale_fill_manual(values = c("green", "red")) +
Expand Down Expand Up @@ -160,25 +160,10 @@ esp_get_capimun <- function(year = Sys.Date(),

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
16 changes: 1 addition & 15 deletions R/esp_get_ccaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,11 @@ esp_get_ccaa_siane <- function(ccaa = NULL,

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
16 changes: 1 addition & 15 deletions R/esp_get_comarca.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,11 @@ esp_get_comarca <- function(region = NULL,

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geom = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
35 changes: 2 additions & 33 deletions R/esp_get_munic.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,11 @@ esp_get_munic <- function(year = "2019",

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down Expand Up @@ -417,27 +402,11 @@ esp_get_munic_siane <- function(year = Sys.Date(),

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)

can <- esp_move_can(can, moveCAN = moveCAN)
# Regenerate
if (nrow(penin) > 0) {
data_sf <- rbind(penin, can)
Expand Down
20 changes: 3 additions & 17 deletions R/esp_get_nuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
#' geographic position of the Canary Islands. When using the output for
#' spatial analysis or using tiles (e.g. with [esp_getTiles()] or
#' [addProviderEspTiles()]) this option should be set to `FALSE` in order to
#' get the actual coordinates, instead of the modified ones.
#' get the actual coordinates, instead of the modified ones. See also
#' [esp_move_can()] for displacing stand-alone \CRANpkg{sf} objects.
#'
#' @examples
#'
Expand Down Expand Up @@ -266,26 +267,11 @@ esp_get_nuts <- function(year = "2016",

if (moving) {
if (length(grep("ES7", data_sf$NUTS_ID)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("ES7", data_sf$NUTS_ID), ]
can <- data_sf[grep("ES7", data_sf$NUTS_ID), ]

# Move can
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
Loading

0 comments on commit 1387abd

Please sign in to comment.