Author: Roger Bivand

# Testing packages using CRS objects

#### Roger Bivand

## Introduction

As has been explained in https://CRAN.R-project.org/package=rgdal/vignettes/CRS_projections_transformations.html, coordinate reference systems are represented differently when PROJ < 6 and PROJ >= 6.

To alert interactive users to the possibility that their workflows might be degraded unless they take the representational changes into account, the sp `proj4string()` method issues warnings if a `"CRS"` object does not have an accompanying `WKT2 2019` representation (stored in a `comment` attached to the `"CRS"` object). This is because `proj4string()` only returns the PROJ 4 representation, not the accompanying `WKT2 2019` representation if any, so may constitute a loss of information. See also https://stat.ethz.ch/pipermail/r-sig-geo/2020-May/028125.html for further discussion.

In general, it is good practice to replace all use in packages of `proj4string()` with `slot(, "proj4string")` and its assignment form by the assignment form of `slot()`; when `sp::proj4string()` is not called, many of the warnings are suppressed anyway.

On attaching rgdal:

    library(rgdal)

    ## Loading required package: sp

    ## Please note that rgdal will be retired by the end of 2023,
    ## plan transition to sf/stars/terra functions using GDAL and PROJ
    ## at your earliest convenience.
    ##
    ## rgdal: version: 1.5-28, (SVN revision 1158)
    ## Geospatial Data Abstraction Library extensions to R successfully loaded
    ## Loaded GDAL runtime: GDAL 3.4.0, released 2021/11/04
    ## Path to GDAL shared files: /usr/share/gdal
    ## GDAL binary built with GEOS: TRUE
    ## Loaded PROJ runtime: Rel. 8.2.0, November 1st, 2021, [PJ_VERSION: 820]
    ## Path to PROJ shared files: /home/edzer/.local/share/proj:/usr/share/proj
    ## PROJ CDN enabled: FALSE
    ## Linking to sp version:1.4-6
    ## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
    ## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.

a package startup message is displayed if PROJ >= 6 and GDAL >= 3, including the important information that:

> To mute warnings of possible GDAL/OSR exportToProj4() degradation, use options(“rgdal_show_exportToProj4_warnings”=“none”) before loading rgdal.

In sp 1.4 and rgdal 1.5, the default option value is `"all"`, in sp >= 1.5 and rgdal >= 1.6, the default will be flipped to `"none"`.

## Testing

Both CRAN checks and use of checks by end users may trigger spurious errors, if the versions of PROJ and GDAL differ from those on the development platform. Since the `WKT2 2019` representation is only generated on systems with PROJ >= 6 (and GDAL >= 3), warnings should not be expected when the same code is run on platforms with older versions of PROJ and GDAL. There are many such platforms, including RHEL and CentOS systems, as well as Ubuntu 18 and others. Think of a large lab with a cluster runnning such a system version, and that those installing software may wish to run `R CMD check` or just your test suite to confirm that things are as they should be. They will be dismayed to find that tests fail, but will not be able to find out whether this matters for them.

Consequently, it is best either not to check warnings that perform differently when run under different versions of upstream external software (PROJ and GDAL), or to condition on the software versions with different expectations depending on the version.

To mute warnings in all settings, add the option above early in any relevant test file.

Fine-grained control may be obtained using `rgdal::rgdal_extSoftVersion()`:

    rgdal::rgdal_extSoftVersion()

    ##           GDAL GDAL_with_GEOS           PROJ             sp           EPSG
    ##        "3.4.0"         "TRUE"        "8.2.0"        "1.4-6"      "v10.038"

and its shorter version, testing only PROJ >= 6 (and GDAL >= 3):

    rgdal::new_proj_and_gdal()

    ## [1] TRUE

Using this test, a warning might be expected if recent PROJ and GDAL are used, and before the warning default is flipped in sp > 1.4 and rgdal > 1.5.

In sf, use `sf::sf_extSoftVersion()` to determine versions if necessary.