I have a sf object with some drought data of Spain, and I want to project this data into a leaflet map. The data source is: https://www.miteco.gob.es/es/biodiversidad/temas/desertificacion-restauracion/lucha-contra-la-desertificacion/lch_pand_descargas.aspx
I am using the function esp_get_country()
from the library mapSpain, to get the boundaries of the country.
The problem is that, in the sf object, Canary Islands are represented far away from the boundary map. How can I project Canary Islands polygons from the sf as they are in the boundary map?
I have tried the functions st_crs()
to get the crs from the boundary map and the function st_transform()
to reproject the sf object, but it didn't do anything.
The code I am using is this:
# get the boundary map from package 'mapSpain'
spain_coun_sf <- esp_get_country()
# import the drought data from Canary Islands (extraction of the whole drought data)
drought_data_canarias <- readOGR(
dsn = path,
layer = "pand_c",
verbose = FALSE
)
drought_data_canarias <- st_as_sf(drought_data_canarias)
drought_data_canarias <- st_transform(drought_data_canarias , crs = st_crs(spain_coun_sf))
leaflet(spain_coun_sf) %>% addPolygons() %>% addPolygons(data = deser_sf_canarias)
You can avoid the displacement of the Canary Island using the option moveCAN = FALSE
. This is available in all the functions of the package, and all the doc packages have an specific section about this:
https://ropenspain.github.io/mapSpain/reference/esp_get_country.html#displacing-the-canary-islands
Displacing the Canary Islands
While
moveCAN
is useful for visualization, it would alter the actual geographic position of the Canary Islands. When using the output for spatial analysis or using tiles (e.g. withesp_getTiles()
oraddProviderEspTiles()
) this option should be set toFALSE
in order to get the actual coordinates, instead of the modified ones.
See a full reprex here. Since you use Canarias layer only I recommend you to use esp_get_ccaa("Canarias", moveCAN = FALSE)
to focus only on that CCAA:
library(mapSpain)
library(ggplot2)
spain_coun_sf_nomoved <- esp_get_country()
ggplot(spain_coun_sf_nomoved) +
geom_sf()
# Deactivate
spain_coun_sf <- esp_get_country(moveCAN = FALSE)
ggplot(spain_coun_sf) +
geom_sf()
# Your reprex, use CCAA instead to focus in Canarias
canarias <- esp_get_ccaa("Canarias", moveCAN = FALSE)
# Download file
tempfile <- tempfile(fileext = ".zip")
download.file("https://www.miteco.gob.es/es/biodiversidad/servicios/banco-datos-naturaleza/0904712280184ea3_tcm30-177178.zip", tempfile)
unzip(tempfile, junkpaths = TRUE, exdir = tempdir())
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
file_path <- list.files(tempdir(), pattern = "shp$", full.names = TRUE)
drought_data_canarias <- st_read(file_path)
#> Reading layer `pand_c' from data source
#> `C:\Users\XXXX\RtmpCOGdcp\pand_c.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 9642 features and 3 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 188300.1 ymin: 3060674 xmax: 653718.9 ymax: 3235418
#> Projected CRS: WGS 84 / UTM zone 28N
# Project everything to 4326 for leaflet
canarias_4326 <- st_transform(canarias, 4326)
drought_data_canarias_4326 <- st_transform(drought_data_canarias, 4326)
ggplot(canarias_4326) +
geom_sf() +
geom_sf(data=drought_data_canarias_4326, aes(fill=DESER_CLA), color=NA) +
scale_fill_viridis_c()
library(leaflet)
leaflet(canarias_4326) %>%
addTiles() %>%
addPolygons() %>%
addPolygons(data = drought_data_canarias_4326)
Created on 2022-05-10 by the reprex package (v2.0.1)