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

st_transform (and coord_sf) don't respect proj4 +over #511

Closed
econandrew opened this issue Oct 10, 2017 · 2 comments
Closed

st_transform (and coord_sf) don't respect proj4 +over #511

econandrew opened this issue Oct 10, 2017 · 2 comments

Comments

@econandrew
Copy link

Quoting proj4:

By default PROJ.4 wraps output longitudes in the range -180 to 180. The +over switch can be used to disable the default wrapping which is done at a low level - in pj_inv().

e.g. compare the following in rgdal, which correctly applies +over

# The +over paramater should stop projected coordinates from wrapping
rgdal::project(t(as.matrix(c(190, 0))), "+proj=robin")
rgdal::project(t(as.matrix(c(190, 0))), "+proj=robin +over")

This is very useful, for example, in ensuring that Russia appears as a contiguous block where otherwise it would wrap around 180 degrees. In fact maps::map("world") seems to be built that way.

st_transform doesn't appear to support this flag. One consequence is that ggplot2 will happily plot untransformed data outside the "usual" range for a projection, UNLESS you add a coord_sf() at which point it will fail.

library(sf)
library(ggplot2)

# PROBLEM
world <- maps::map("world", plot = FALSE, fill = TRUE)
sf.world <- st_as_sf(world)
sf.world.robin <- st_transform(sf.world, crs = "+proj=robin +over")

ggplot(sf.world) + geom_sf() # Correct with Siberia extended beyond 180 deg E
ggplot(sf.world.robin) + geom_sf() # Inverted polys
ggplot(sf.world) + geom_sf() + coord_sf(crs="+proj=robin +over") # Inverted polys

# WORKAROUND via sp
sp.world <- as(sf.world, "Spatial")
sp.world.robin <- sp::spTransform(sp.world, sp::CRS("+proj=robin +over"))
sf.world.robin2 <- st_as_sf(sp.world.robin)
ggplot(sf.world.robin2) + geom_sf()
@edzer
Copy link
Member

edzer commented Oct 15, 2017

I refer to #509, and close here because both issues refer to the fact that sf uses the GDAL interface to PROJ.4, rather than doing projections directly through the PROJ.4 API, as rgdal does.

@edzer edzer closed this as completed Oct 15, 2017
edzer added a commit that referenced this issue Oct 19, 2017
st_transform gains a parameter, use_gdal, which if FALSE takes a route through lwgeom_transform completely ignoreing GDAL. This allows using +over and +proj=wintri, proj.4 options not honoured/accepted by GDAL.
@edzer
Copy link
Member

edzer commented Dec 11, 2017

This is now found in (moved to) lwgeom::st_transform_proj; see https://github.com/r-spatial/lwgeom

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

No branches or pull requests

2 participants