Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing test failure for sf_transform_xy() "no arguments in initialization list" #5201

Open
MichaelChirico opened this issue Feb 22, 2023 · 6 comments

Comments

@MichaelChirico
Copy link
Contributor

MichaelChirico commented Feb 22, 2023

We are observing this test failure:

── Error ('test-coord_sf.R:290'): sf_transform_xy() works ──────────────────────
Error in `proj_from_crs(from)`: no arguments in initialization list: GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    USAGE[
        SCOPE["unknown"],
        AREA["World"],
        BBOX[-90,-180,90,180]],
    ID["EPSG",4326]]
Backtrace:
    ▆
 1. └─ggplot2::sf_transform_xy(data, 3347, 4326) at test-coord_sf.R:290:2
 2.   └─sf::sf_project(...)
 3.     └─sf (local) proj_from_crs(from)

Probably this is related to missing certain GDAL drivers. But I hope you agree the error message is not very helpful. I'm not sure if this is something on the ggplot2 or the sf side; I know sf offers some things we could use to escape here e.g. sf_extSoftVersion() or is_driver_available():

https://github.com/r-spatial/sf/blob/b798413d4cd681cd173f9bdc110f2a014555ee09/tests/testthat/test_read.R#L176

https://github.com/r-spatial/sf/blob/b798413d4cd681cd173f9bdc110f2a014555ee09/tests/testthat/test_read.R#L121

Happy to offer more details as needed but I'm not sure what would help.


FWIW, the issue also causes some examples to fail:

ggplot2/man/geom_map.Rd

Lines 125 to 162 in 519bc83

if (require(maps)) {
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
# Equivalent to crimes \%>\% tidyr::pivot_longer(Murder:Rape)
vars <- lapply(names(crimes)[-1], function(j) {
data.frame(state = crimes$state, variable = j, value = crimes[[j]])
})
crimes_long <- do.call("rbind", vars)
states_map <- map_data("state")
# without geospatial coordinate system, the resulting plot
# looks weird
ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat)
# in combination with `coord_sf()` we get an appropriate result
ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
# crs = 5070 is a Conus Albers projection for North America,
# see: https://epsg.io/5070
# default_crs = 4326 tells coord_sf() that the input map data
# are in longitude-latitude format
coord_sf(
crs = 5070, default_crs = 4326,
xlim = c(-125, -70), ylim = c(25, 52)
)
ggplot(crimes_long, aes(map_id = state)) +
geom_map(aes(fill = value), map = states_map) +
coord_sf(
crs = 5070, default_crs = 4326,
xlim = c(-125, -70), ylim = c(25, 52)
) +
facet_wrap(~variable)
}

\examples{
if (requireNamespace("sf", quietly = TRUE)) {
# location of cities in NC by long (x) and lat (y)
data <- data.frame(
city = c("Charlotte", "Raleigh", "Greensboro"),
x = c(-80.843, -78.639, -79.792),
y = c(35.227, 35.772, 36.073)
)
# transform to projected coordinates
data_proj <- sf_transform_xy(data, 3347, 4326)
data_proj
# transform back
sf_transform_xy(data_proj, 4326, 3347)
}
}

@teunbrand
Copy link
Collaborator

teunbrand commented Mar 28, 2024

I'm relatively sure ggplot2 assumes that when {sf} is installed, the GDAL drivers work as intended.

I agree that the error message isn't helpful, but it is thrown from an {sf} function so there is little control that ggplot2 has over the message other than wrapping every piece of {sf} related code in tryCatch() blocks.

Other than maybe putting the geom_map() examples that use coord_sf() in if (require("sf")) blocks, I'm not sure what ggplot2 can do about this issue.

@MichaelChirico
Copy link
Contributor Author

{sf} owns the error message, but {ggplot2} owns the map data & I would guess could be responsible for knowing where it comes from & thus what drivers are needed to process it?

@edzer WDYT? Is there anything reasonably straightforward {ggplot2} and/or {sf} could do to help with the error messaging here?

@edzer
Copy link
Contributor

edzer commented Mar 29, 2024

@MichaelChirico I'm not sure; can you provide a reprex that triggers this, or point to where it happens? I've never seen the no arguments in initialization list error, I believe.

@MichaelChirico
Copy link
Contributor Author

I can point to the line causing it:

out <- sf_transform_xy(data, 3347, 4326)

But it will be hard to offer a reprex I'm afraid, sorry :\

@edzer
Copy link
Contributor

edzer commented Mar 29, 2024

I can only find the error message here, but that should not be callable (it's part of a standalone app, not the PROJ library we use). What does sf::sf_extSoftVersion() give on your system?

@MichaelChirico
Copy link
Contributor Author

I'll have to return to this when I'm off leave in May

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants