Comments (6)
Okay, I just discovered a super hacky way to solve it: I saved an object with the Canary Islands in their correct geographical position (moveCAN = FALSE
) and another one with their shifted position (moveCAN = TRUE
). I then extracted the polygon coordinates for both of them (for a random island) and subtracted the corresponding longitudes and latitudes, getting a shifting factor of ~7 units for the latitude and of ~4.94 units for the longitude.
I now just wonder if there's a cleaner way of getting the original offset used in the code.
from mapspain.
Hi! The default offset is offset <- c(550000, 920000)
in EPSG:3857 (so 550km North 920 km West in Mercator). I am not able right now to provide a full example on how to do it (away of my PC) but see how this is done internally.
Then, the object (your points) is converted to EPSG:3857, the offset is applied to the geometry column and after that the resulting object is transformed back to its initial CRS.
I would provide a neat example when possible
Lines 261 to 291 in fdab71f
from mapspain.
Thanks for the quick reply, Diego! I actually managed to find that part in the code, but apparently I can't seem to use it correctly. Here's what I've tried so far:
I've uploaded a sample CSV here. It's just a list of AEMET's solar radiation measurement stations and their coordinates in decimal format.
Here's the R code where I try to offset the coordinates of the stations located in the Canary Islands (warning: it's a mess. I know next to nothing about both geography and sf objects):
read_csv("https://gist.githubusercontent.com/pedrotercero3/7d789d11c5b22374c5ad13a47d828526/raw/53bf0bed9ef3decc997a95ffc8178f6ac8b806f1/solar_rad_stations_esp.csv") %>%
mutate(shifted_coords = if_else(
nombre_estacion %in% c("Sta. Cruz de Tenerife", "Izaña", "Maspalomas"),
map2(
lat_dec, long_dec,
~ {
c(.x, .y) %>%
st_point() %>%
st_sfc(crs = sf::st_crs(4326)) %>%
st_transform(3857) %>%
st_coordinates() %>%
as.double() + c(550000, 920000)
}
),
map2(
lat_dec, long_dec,
~ {
c(.x, .y) %>%
st_point() %>%
st_sfc(crs = sf::st_crs(4326)) %>%
st_transform(3857) %>%
st_coordinates() %>%
as.double()
}
)),
shifted_coords = map(shifted_coords, ~ {
st_point(.x) %>%
st_sfc(crs = sf::st_crs(3857)) %>%
st_transform(4326) %>%
st_coordinates() %>%
as.double()
})) %>%
unnest_wider(shifted_coords, names_sep = "_") %>%
rename(lat_dec_moveCAN = shifted_coords_1,
long_dec_moveCAN = shifted_coords_2)
Basically in the beginning it's all wrapped in an if_else()
. If the station's in the islands, I convert the coordinates with st_point()
, set the reference CRS, transform them to the 3857 reference, and add the offset. If the station's not in the islands I apply the same functions but without the offset. After the if_else()
I just convert the coordinates back to the decimal system and format them into individual columns again (unnest_wider()
) so I can plot them with geom_point()
.
However, this is the plot that I get after all of this!
But if use the values I got with the hacky approach I mentioned in my earlier comment, the locations are more correct:
All of this is to say that I solved my initial problem (which, I want to emphasize, is not at all a problem related to the package, which by the way is amazing; it has the best ggplot2 map of Spain I've seen to date), but I'm just curious about how to solve it using the same approach you used in the original code. If you have time in the future to look into it that'd be cool, but I don't want you to waste any more of your time on this so please feel free to close the issue!
from mapspain.
Hi, for completion I applied the same code of the package to your dataset (thanks for providing sample data btw), I think it works fine:
library(mapSpain)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
# Points as sf
points <- read_csv("https://gist.githubusercontent.com/pedrotercero3/7d789d11c5b22374c5ad13a47d828526/raw/53bf0bed9ef3decc997a95ffc8178f6ac8b806f1/solar_rad_stations_esp.csv") %>%
st_as_sf(
coords = c("long_dec", "lat_dec"),
crs = 4326
)
#> Rows: 26 Columns: 3
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): nombre_estacion
#> dbl (2): lat_dec, long_dec
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Map
provs <- esp_get_prov(epsg = 4326)
ggplot(provs) +
geom_sf() +
geom_sf(data = points)
# ---- Move points on Canary Islands ----
# Split dataset
data_sf <- st_transform(points, 3857)
penin <- data_sf %>% filter(!nombre_estacion %in% c(
"Sta. Cruz de Tenerife",
"Izaña", "Maspalomas"
))
can <- data_sf %>% filter(nombre_estacion %in% c(
"Sta. Cruz de Tenerife",
"Izaña", "Maspalomas"
))
# Move Canary points
offset <- c(550000, 920000)
can <- st_sf(
st_drop_geometry(can),
geometry = st_geometry(can) + offset,
crs = st_crs(can)
)
# Regenerate and project back to the initial CRS
data_sf <- rbind(penin, can) %>%
st_transform(st_crs(points))
# ---- End---- #
ggplot(provs) +
geom_sf() +
geom_sf(data = data_sf)
Created on 2022-08-21 with reprex v2.0.2
from mapspain.
Thank you very much! My problem was knowing the order in which to apply the necessary st
functions.
from mapspain.
Hi,
Just to let you know that in mapspain v0.9.0 I introduced a new function esp_move_can()
that displaces arbitray objects with the same offset than moveCAN = TRUE
.
Now it is as easy as:
library(mapSpain)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
# Points as sf
points <- read_csv("https://gist.githubusercontent.com/pedrotercero3/7d789d11c5b22374c5ad13a47d828526/raw/53bf0bed9ef3decc997a95ffc8178f6ac8b806f1/solar_rad_stations_esp.csv",
show_col_types = FALSE
) %>%
st_as_sf(
coords = c("long_dec", "lat_dec"),
crs = 4326
)
# Split points
penin <- points %>% filter(!nombre_estacion %in% c(
"Sta. Cruz de Tenerife",
"Izaña", "Maspalomas"
))
# Move this points with esp_move_can()
can <- points %>% filter(nombre_estacion %in% c(
"Sta. Cruz de Tenerife",
"Izaña", "Maspalomas"
)) %>%
# Use esp_move_can
esp_move_can()
# Regenrate but canary points moved
points_disp <- penin %>% bind_rows(can)
# Map
provs <- esp_get_prov(epsg = 4326)
bb <- esp_get_can_box()
ggplot(provs) +
geom_sf() +
geom_sf(data = bb) +
geom_sf(data = points_disp)
Created on 2024-01-30 with reprex v2.1.0
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.3.2 (2023-10-31 ucrt)
#> os Windows 11 x64 (build 22631)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate Spanish_Spain.utf8
#> ctype Spanish_Spain.utf8
#> tz Europe/Madrid
#> date 2024-01-30
#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> bit 4.0.5 2022-11-15 [1] CRAN (R 4.3.0)
#> bit64 4.0.5 2020-08-30 [1] CRAN (R 4.3.0)
#> class 7.3-22 2023-05-03 [1] CRAN (R 4.3.1)
#> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.1)
#> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.2)
#> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0)
#> countrycode 1.5.0 2023-05-30 [1] CRAN (R 4.3.0)
#> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0)
#> curl 5.2.0 2023-12-08 [1] CRAN (R 4.3.2)
#> DBI 1.2.1 2024-01-12 [1] CRAN (R 4.3.2)
#> digest 0.6.34 2024-01-11 [1] CRAN (R 4.3.2)
#> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.2)
#> e1071 1.7-14 2023-12-06 [1] CRAN (R 4.3.2)
#> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.2)
#> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.2)
#> farver 2.1.1 2022-07-06 [1] CRAN (R 4.3.0)
#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0)
#> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0)
#> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1)
#> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0)
#> ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.3.2)
#> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2)
#> gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.1)
#> highr 0.10 2022-12-22 [1] CRAN (R 4.3.0)
#> hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0)
#> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.2)
#> KernSmooth 2.23-22 2023-07-10 [1] CRAN (R 4.3.1)
#> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.2)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2)
#> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0)
#> mapSpain * 0.9.0 2024-01-23 [1] CRAN (R 4.3.2)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.0)
#> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0)
#> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0)
#> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.1)
#> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.0)
#> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0)
#> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2)
#> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.2)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0)
#> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.2)
#> readr * 2.1.5 2024-01-10 [1] CRAN (R 4.3.2)
#> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.2)
#> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.2)
#> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1)
#> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1)
#> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.2)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0)
#> sf * 1.0-15 2023-12-18 [1] CRAN (R 4.3.2)
#> stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.2)
#> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.2)
#> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.1)
#> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0)
#> tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.3.2)
#> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0)
#> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0)
#> timechange 0.3.0 2024-01-18 [1] CRAN (R 4.3.2)
#> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0)
#> units 0.8-5 2023-11-28 [1] CRAN (R 4.3.2)
#> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.2)
#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2)
#> vroom 1.6.5 2023-12-05 [1] CRAN (R 4.3.2)
#> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.2)
#> xfun 0.41 2023-11-01 [1] CRAN (R 4.3.2)
#> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.3.2)
#> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2)
#>
#> [1] C:/Users/XXX/AppData/Local/R/win-library/4.3
#> [2] C:/Program Files/R/R-4.3.2/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
from mapspain.
Related Issues (20)
- New feature: function for displacing external objects located on Canary Islands
- Release mapSpain 0.9.0
- Add national grid data HOT 5
- Revisar traducciones en gallego, euskera y catalán HOT 1
- Release mapSpain 0.5.0
- Añadir secciones censales HOT 1
- Release mapSpain 0.6.0
- Release mapSpain 0.6.1
- Añadir mapas del INE
- Release mapSpain 0.6.2
- Problem installing mapSpain in R version 4.2.1 (2022-06-23) HOT 2
- Update leaflet providers to v1.3.1
- Does Leaflet-providersESP work with mapview? HOT 2
- More WMS? HOT 3
- Release mapSpain 0.7.0
- URGENT! Failed CRAN Checks: 2023-02-13
- Release mapSpain 0.8.0
- Echo de menos 3 municipios en el resultado de esp_get_munic_siane HOT 1
- pkgcheck results - main HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapspain.