Skip to content

Commit d58d787

Browse files
authored
Merge pull request #197 from inbo/update_crs
Updating CRS specifications in existing tutorials
2 parents 5138300 + 3d6fcef commit d58d787

File tree

19 files changed

+454
-180
lines changed

19 files changed

+454
-180
lines changed
-8.4 KB
Binary file not shown.

content/tutorials/r_inla/spatial/index.Rmd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ output:
1616
library(knitr)
1717
opts_chunk$set(
1818
message = FALSE,
19+
warning = FALSE, # suppress rgdal CRS warnings here and there
1920
dev = "cairo_pdf",
2021
dpi = 300
2122
)
@@ -329,8 +330,8 @@ PRborder %>%
329330
list() %>%
330331
Polygons(ID = 1) %>%
331332
list() %>%
332-
SpatialPolygons(proj4string = CRS("+init=epsg:4326")) %>%
333-
spTransform(CRS("+init=epsg:5880")) -> boundary
333+
SpatialPolygons(proj4string = CRS(SRS_string = "EPSG:4326")) %>%
334+
spTransform(CRS(SRS_string = "EPSG:5880")) -> boundary
334335
```
335336

336337
```{r mesh-rainfall-1}
@@ -577,7 +578,7 @@ si <- inla.stack.index(stack_all, "grid")$data
577578
grid_data %>%
578579
bind_cols(model_grid$summary.fitted.values[si, ]) %>%
579580
`coordinates<-`(~X + Y) %>%
580-
`proj4string<-`(CRS("+init=epsg:5880")) -> gd
581+
`proj4string<-`(CRS(SRS_string = "EPSG:5880")) -> gd
581582
gd[!is.na(over(gd, boundary)), ] %>%
582583
as.data.frame() %>%
583584
ggplot() + geom_tile(aes(x = X, y = Y, fill = mean)) + coord_fixed()

content/tutorials/spatial_create_leaflet_map/index.Rmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ tags: ["gis", "r", "maps"]
88
output:
99
md_document:
1010
preserve_yaml: true
11-
variant: markdown_github
11+
variant: gfm
1212
---
1313

14+
_General note: migration to the more actively developed `sf` package is currently advised by the `sp` maintainer._
15+
_The `sp` package, used in this tutorial, is still maintained in order to support the newest versions of the GDAL and PROJ backends._
16+
1417
```{r setup, include=FALSE}
1518
knitr::opts_chunk$set(echo = TRUE)
1619
```
@@ -46,7 +49,7 @@ plot(data$lon, data$lat)
4649
We need to convert the `data.frame` to a `SpatialPointsDataFrame`:
4750

4851
```{r}
49-
crs_wgs84 <- CRS("+init=epsg:4326")
52+
crs_wgs84 <- CRS(SRS_string = "EPSG:4326")
5053
pts <- SpatialPointsDataFrame(data[c("lon","lat")],
5154
data[!(names(data) %in% c("lon","lat"))],
5255
proj4string = crs_wgs84)
@@ -69,4 +72,4 @@ leaf_map
6972

7073
**Nice, no?!**
7174

72-
More information is provided [at the leaflet information website](https://rstudio.github.io/leaflet/)!
75+
More information is provided [at the leaflet information website](https://rstudio.github.io/leaflet/)!

content/tutorials/spatial_create_leaflet_map/index.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ tags: ["gis", "r", "maps"]
88
output:
99
md_document:
1010
preserve_yaml: true
11-
variant: markdown_github
11+
variant: gfm
1212
---
1313

14-
Introduction
15-
------------
14+
*General note: migration to the more actively developed `sf` package is
15+
currently advised by the `sp` maintainer.* *The `sp` package, used in
16+
this tutorial, is still maintained in order to support the newest
17+
versions of the GDAL and PROJ backends.*
18+
19+
## Introduction
1620

1721
The required packages are **leaflet** and **sp**.
1822

@@ -21,10 +25,10 @@ library(leaflet)
2125
library(sp)
2226
```
2327

24-
Dummy data
25-
----------
28+
## Dummy data
2629

27-
Let's create a dumy `data.frame` to play around, i.e. the three locations of INBO:
30+
Let’s create a dumy `data.frame` to play around, i.e. the three
31+
locations of INBO:
2832

2933
``` r
3034
names <- c("VAC HT","Geraardsbergen","Linkebeek")
@@ -39,15 +43,14 @@ We created three points:
3943
plot(data$lon, data$lat)
4044
```
4145

42-
![](index_files/figure-markdown_github/unnamed-chunk-3-1.png)
46+
![](index_files/figure-gfm/unnamed-chunk-3-1.png)<!-- -->
4347

44-
Creating a map
45-
--------------
48+
## Creating a map
4649

4750
We need to convert the `data.frame` to a `SpatialPointsDataFrame`:
4851

4952
``` r
50-
crs_wgs84 <- CRS("+init=epsg:4326")
53+
crs_wgs84 <- CRS(SRS_string = "EPSG:4326")
5154
pts <- SpatialPointsDataFrame(data[c("lon","lat")],
5255
data[!(names(data) %in% c("lon","lat"))],
5356
proj4string = crs_wgs84)
@@ -66,8 +69,9 @@ leaf_map <- leaflet(pts) %>%
6669
leaf_map
6770
```
6871

69-
![](index_files/figure-markdown_github/unnamed-chunk-6-1.png)
72+
![](index_files/figure-gfm/unnamed-chunk-6-1.png)<!-- -->
7073

71-
**Nice, no?!**
74+
**Nice, no?\!**
7275

73-
More information is provided [at the leaflet information website](https://rstudio.github.io/leaflet/)!
76+
More information is provided [at the leaflet information
77+
website](https://rstudio.github.io/leaflet/)\!
7.07 KB
Loading
691 KB
Loading

content/tutorials/spatial_point_in_polygon/index.Rmd

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags: ["gis", "r", "maps"]
88
output:
99
md_document:
1010
preserve_yaml: true
11-
variant: markdown_github
11+
variant: gfm
1212
---
1313

1414
```{r include = FALSE}
@@ -128,12 +128,15 @@ invasive_be10grid_sf %>%
128128

129129
## Point in polygon with the `sp` package
130130

131+
_General note: migration to the more actively developed `sf` package is currently advised by the `sp` maintainer._
132+
_The `sp` package is still maintained in order to support the newest versions of the GDAL and PROJ backends._
133+
131134
Instead of `sf` objects (= `data.frames` or `tibbles` with a geometry list-column), the `sp` package works with `Spatial` spatial data classes (which has many derived spatial data classes for points, polygons, ...).
132135

133136
First, we need to convert the `data.frame` with point locations to a `SpatialPointsDataFrame`. We also need to ensure that the coordinate reference system (CRS) for both the point locations and the grid is the same. The data from GBIF are in WGS84 format.
134137

135138
```{r}
136-
crs_wgs84 <- CRS("+init=epsg:4326")
139+
crs_wgs84 <- CRS(SRS_string = "EPSG:4326")
137140
coord <- invasive_species %>%
138141
select(decimalLongitude, decimalLatitude)
139142
invasive_spatial <- SpatialPointsDataFrame(coord,
@@ -148,6 +151,19 @@ be10grid <- readOGR(dsn = file.path(tempdir(), "Belgium.sqlite"),
148151
layer = "be_10km")
149152
```
150153

154+
Note the warning: it is because some PROJ.4 information, i.e. the string to represent the coordinate reference system, is not supported anymore in the current geospatial GDAL and PROJ libraries (the background workhorses for spatial R packages).
155+
The spatialite database from the EEA website (with the 10 km x 10 km grid) still uses the older PROJ.4 string .
156+
Because the `rgdal` package is still backwards compatible, we should not (yet) worry about this: `rgdal` does the translation for the newer GDAL 3 and PROJ >= 6.
157+
Do know that, instead of _PROJ.4_ strings, the _WKT2_ string is now used in R to better represent coordinate reference systems (so it would best be incorporated in the EEA data source).
158+
Just compare these:
159+
160+
```{r}
161+
# PROJ.4 string = old; used by PROJ 4
162+
proj4string(be10grid) # or: be10grid@proj4string
163+
# WKT2 string = new and much better; used by PROJ >= 6
164+
wkt(be10grid) %>% cat # or: be10grid@proj4string %>% comment %>% cat
165+
```
166+
151167

152168
We transform the 10 km x 10 km grid to the same CRS system:
153169

0 commit comments

Comments
 (0)