Debian Codegraph @

inst/doc/retiring_rgdal_geos_1.Rmd

raw
1 ---
2 title: "sp evolution status: examples of migration from retiring packages"
3 author: "Roger Bivand, Edzer Pebesma"
4 output:
5 html_document:
6 toc: true
7 toc_float:
8 collapsed: false
9 smooth_scroll: false
10 toc_depth: 2
11 vignette: >
12 %\VignetteIndexEntry{sp evolution status: examples of migration from retiring packages}
13 %\VignetteEngine{knitr::rmarkdown}
14 %\VignetteEncoding{UTF-8}
15 ---
16
17 ```{r echo=FALSE}
18 knitr::opts_chunk$set(comment = "")
19 ```
20
21 ```{r setup, include=FALSE}
22 knitr::opts_chunk$set(echo = TRUE, paged.print=FALSE)
23 ```
24
25 TOC
26
27 [DOWNLOADHERE]
28
29 **Summary**:
30
31 This is the fourth report on the R-spatial evolution project and is addressed to maintainers of packages and workflows using `sp` classes, methods and functions. The project involves the retirement (archiving) of `rgdal`, `rgeos` and `maptools` during 2023. The [first report](https://r-spatial.org/r/2022/04/12/evolution.html) set out the main goals of the project. The [second report](https://r-spatial.org/r/2022/12/14/evolution2.html) covered progress so far, steps already taken, and those remaining to be accomplished. The [third](https://r-spatial.org/r/2023/04/10/evolution3.html) gave detailed guidance for maintainers of packages using the retiring packages.
32
33 From June 2023 (`sp` version 2.0-0), the internal evolution status setting of `sp` will be changed from "business as usual" to "use `sf` instead of `rgdal` and `rgeos`". Packages depending on `sp` may need to add `sf` to their weak dependencies, and to monitor any changes in output. From this version 2.1-0, use of `rgdal` has been dropped, so setting the internal evolution status has been removed.
34
35 The final step occurred during October 2023, `rgdal`, `rgeos` and `maptools` were archived on CRAN, and packages with strong dependencies on the retiring packages must be either upgraded to use `sf`, `terra` or other alternatives or work-arounds by or before that time.
36
37 [This spreadsheet](https://github.com/r-spatial/evolution/blob/main/pkgapi_230502.csv) lists methods and functions in retiring packages and `sp` found by `pkgapi`. They are listed by function name as used by packages, and the analogous list by package may be found of functions is [here](https://github.com/r-spatial/evolution/blob/main/pkgapi_by_pkg_230502.csv).
38
39 # `sp` evolution status
40
41 Repeating from the second and third blogs:
42
43 As mentioned in our first report, `sp` on CRAN has been provided with conditional code that prevents `sp` calling most code in `rgdal` or `rgeos`. This can be enabled *before* loading `sp` by setting e.g.:
44 ```
45 options("sp_evolution_status"=2)
46 library(sp)
47 ```
48 for checking packages under status
49
50 * `0`: business as usual,
51 * `1`: stop if `rgdal` or `rgeos` are absent, or
52 * `2`: use `sf` instead of `rgdal` (currently no use of `rgeos` is considered)
53
54 or alternatively can be set as an environment variable read when `sp` is loaded, e.g. when running checks from the command line by
55 ```
56 _SP_EVOLUTION_STATUS_=2 R CMD check
57 ```
58 This construction should permit maintainers to detect potential problems in code. `devtools::check()` provides the `env_vars=` argument, which may be used for the same purpose.
59
60 From `sp 1.6.0` published on CRAN 2023-01-19, these status settings may also be changed when `sp` is loaded, using `sp::get_evolution_status()` returning the current value, and `sp::set_evolution_status(value)`, where value can take the integer values `0L`, `1L` and `2L`.
61
62 ## Use of `sp` affected functions and methods
63
64 Under evolution status `2`, which will become the default in June 2023, no use is made of `rgdal` in `sp::CRS`, `sp::is.projected`, and `sp::spTransform`, rather using `sf` to access `PROJ` through `GDAL`. A warning is given if `sf` is not available. This is similar to behaviour under legacy `sp` and evolution status `0` with regard to the use of `rgdal` to access `PROJ`.
65
66 `sp::CRS` is used by the following packages (`pkgapi` May 2, 2023):
67
68 ```
69 adehabitatHR adehabitatHS adehabitatLT adehabitatMA AFM AGPRIS angstroms
70 animaltracker anipaths antaresViz aqp AquaBEHER atakrig ausplotsR
71
72 bamlss bfsMaps biogeo bioRad biosurvey birdring birdscanR bivariatemaps
73 bRacatus briskaR
74
75 canadianmaps changeRangeR chronosphere cleangeo cmsafops cmsafvis
76 ConR CoordinateCleaner cruts ctmm
77
78 dartR DEPONS2R dggridR dismo dispeRse DRHotNet dtwSat dynamAedes dynamicSDM
79
80 ecospat elevatr EMbC epitweetr
81
82 fasterize FIESTAutils FLightR FRK
83
84 GapAnalysis gDefrag gdistance GeNetIt GeoAdjust GeodesiCL geodiv geofacet
85 geogrid geojsonio geomerge geoviz gfcanalysis ggOceanMaps gmGeostats
86 googletraffic GPSeqClus grainscape graticule gwer gwfa
87
88 hero Hmsc hydroTSM
89
90 iccTraj inlabru INLAspacetime intamap intSDM IsoriX itcSegment
91
92 kehra
93
94 lakemorpho leaflet letsR lgcp loa
95
96 macroBiome mapedit MapGAM mapmisc mapview MEDITS meteo meteoForecast
97 meteoland micromapST MinBAR momentuHMM morphomap move
98
99 nodiv
100
101 oceanic OpenStreetMap OSMscale
102
103 phyloregion places plotKML prevR PWFSLSmoke
104
105 QRAGadget quadmesh quickmapr quickPlot
106
107 RAC raster rasterVis rbgm rcanvec rCAT RCGLS RchivalTag rcrimeanalysis
108 ReadDIM red RgoogleMaps riverdist rleafmap rosm rpostgis RSIP rtop RWmisc
109
110 sdm sdStaf SeerMapper seg shadow SimSurvey soilassessment SongEvo spacetime
111 SpaDES.tools SpatialEpi spatialfusion SpatialGraph spatsoc spatsurv spbabel
112 spdep sperich spex spgwr stppSim stxplore SUNGEO SurfaceTortoise SWTools
113
114 tiler track2KBA TrajDataMining trajectories trip
115
116 uavRmp
117
118 vec2dtransf
119
120 waver WEGE wildlifeDI wkb wux
121
122 zoon
123 ```
124
125 `sp::is.projected` is used by:
126
127 ```
128 GapAnalysis GeNetIt intkrige ptools riverdist surveillance track2KBA trip
129 ```
130
131 and `sp::spTransform` by:
132
133 ```
134 AGPRIS antaresViz
135
136 biosurvey bivariatemaps
137
138 canadianmaps chronosphere CoordinateCleaner ctmm
139
140 elevatr epitweetr
141
142 fastmaRching FLightR
143
144 GeoAdjust GeodesiCL geoviz ggOceanMaps
145
146 icosa inlabru INLAspacetime
147
148 loa
149
150 macroBiome mapedit mapview micromapST MinBAR momentuHMM mregions
151
152 quickmapr
153
154 raster rcanvec rcrimeanalysis reproducible RgoogleMaps riverdist rosm rpostgis
155
156 SeerMapper shadow SUNGEO
157
158 track2KBA
159
160 uavRmp
161 ```
162 Reverse dependency checks of May 4, 2023 showed only 2 packages failing `CMD check` under `_SP_EVOLUTION_STATUS_=2`, and their difficulties are mentioned below (github issues have been raised offering maintainers mitigations). The salient reasons are given below so that others facing similar apparent intractible problems in workflows can perhaps benefit. Since other packages passed `CMD check`, there are no strong reasons not to flip `sp`'s evolution status in June 2023 from `rgdal` to `sf`.
163
164
165 ## Possible difficulties
166
167 One possible source of difference is that the strings (WKT2_2019, Proj4) returned from `rgdal` came straight from `PROJ`, but `sf` accesses `PROJ` through `GDAL`.
168
169 Another source of difference is that `rgdal` used `sp` S4 classes with inheritance, but `sf` uses S3 classes which do not (seem to) treat inheritance in the same way. So workflows using `sp` and classes, so coercion to `sp` classes before `sp::spTransform` is required, and reconstruction of the classes extending `sp` classes after return is probably required.
170
171 A further potential cause of confusion is that round-tripping by coercion to `sf` perhaps applying an operation, and returning to `sp` may break rarely used `sp` methods, such as:
172
173 ```{r}
174 library(sp)
175 getMethod("$", "SpatialPoints")
176 ```
177
178 which accesses columns by name no longer in the `data.frame` in the data slot through the column names of the matrix of points:
179
180 ```{r}
181 data(meuse)
182 names(meuse)
183 ```
184
185
186 ```{r}
187 coordinates(meuse) <- ~ x + y
188 names(meuse)
189 ```
190 ```{r}
191 colnames(slot(meuse, "coords"))
192 ```
193
194 ```{r}
195 str(meuse$x)
196 ```
197 So far so good, but round-tripping if `sf` is available changes the column names of the matrix of points:
198
199 ```{r}
200 run <- requireNamespace("sf", quietly = TRUE)
201 ```
202
203
204 ```{r, eval=run}
205 meuse_rt <- as(sf::st_as_sf(meuse), "Spatial")
206 names(meuse_rt)
207 ```
208 ```{r, eval=run}
209 colnames(slot(meuse_rt, "coords"))
210 ```
211
212 Consequently, code using this legacy short cut may be a source of difficulty.
213
214 ## Why not `terra`?
215
216 The reason for choosing to replace `rgdal` by `sf` for these three methods has been the absence of an independent object for coordinate reference systems matching `"CRS"` in `sp` and `"crs"` in `sf`.
217
218 It would be possible to create an empty `"SpatVector"` object and assign a coordinate reference system, coerce to `sp`, and extract the `"CRS"` object:
219
220 ```{r, echo=FALSE}
221 run <- requireNamespace("terra", quietly=TRUE)
222 run <- run && require("raster", quietly=TRUE)
223 ```
224
225
226 ```{r, eval=run}
227 v <- terra::vect(matrix(c(0, 0), nrow=1), crs="OGC:CRS84")
228 ```
229
230 ```{r, eval=run}
231 library(raster)
232 slot(as(v, "Spatial"), "proj4string")
233 ```
234
235 but this seems somewhat forced. If there is substantial demand for an alternative mechanism using `terra` and `raster` in place of `sf`, then a well-tested pull request would be considered. It is understandable that Windows or macOS users who otherwise would not need to install `sf`, and who are already using `terra` might prefer to stay with `terra` inside `sp`, but input to the evolution project would be needed to take any action. It may rather be simpler for users with such workflows to migrate to `terra` instead of sustaining a mixed `sp`, `raster` vector combination when `terra` can probably cover all needs.