proj4string-issue - leading spaces
On Fri, 11 Jan 2013, Jon Olav Skoien wrote:
Dear list, Leading spaces seems to be a tricky thing in proj4strings, I think the issue below is related to the following post where leading spaces where removed: http://r-sig-geo.2731867.n2.nabble.com/a-minor-bug-in-rgdal-tt7581906.html#none
Well, preferably: https://stat.ethz.ch/pipermail/r-sig-geo/2012-December/016913.html but also see: http://cran.r-project.org/web/packages/rgdal/ChangeLog or library(rgdal) file.show(system.file("ChangeLog", package="rgdal")) See also: https://r-forge.r-project.org/scm/viewvc.php/pkg/R/Class-CRSx.R?root=rgdal&r1=378&r2=401 https://r-forge.r-project.org/scm/viewvc.php/pkg/src/projectit.cpp?root=rgdal&r1=361&r2=401 The underlying problem is the very widespread use of identical() in all kinds of code in lots of places. Since the rgdal release in December, entry into libproj is not going to prepend spaces anymore. So over time, workarounds to add spaces will cease to be needed. In the mean time, all use of identical() for comparing these character strings remains problematic. Extending to a dependency from sp to the compare package seems unnecessary, but the sp package could offer a function to compare CRS directly - contributions welcome! Roger
It seems the stripping of leading spaces has created another issue in my
case, when overlaying objects with (from outside R) and without (created in
R) these spaces, as over calls identical to check if the proj4strings are
equal. Below is an example where I add a space to the proj4string, similar
to the object I actually get from outside R:
# (R 2.15-1, sp_1.0-5, rgdal_0.8-01)
library(rgdal)
spdf = SpatialPixelsDataFrame(matrix(c(0,0,1,1,0,1,1,0), ncol = 2),
proj4string = CRS("+init=epsg:4326"), data = data.frame(dat = 1:4))
P1 = Polygon(cbind(c(0,0,1,1,0),c(0,1,1,0,0)))
P2 = Polygons(list(P1), "p1")
SpP = SpatialPolygons(list(P2), proj4string = CRS(" +init=epsg:4326"))
over(SpP, spdf)
# Up to here it works fine, as the leading space in SpP was stripped by the
CRS function. Then I add this space directly to projargs, as the objects I am
loading:
spdf at proj4string@projargs = SpP at proj4string@projargs = paste("
",proj4string(spdf), sep = "")
proj4string(spdf)
over(SpP, spdf)
#However, when setting the proj4string with CRS on one of them and doing the
overlay:
proj4string(spdf) = CRS(proj4string(spdf))
over(SpP, spdf)
# I get an error because of the missing space in one of the objects. In my
case this error comes when spdf is transformed to a different projection and
back again:
spdfb = spTransform(spdf, CRS("+proj=moll +lon_0+x_0+y_0") )
# do something with spdfb
spdfc = spTransform(spdfb, CRS(proj4string(spdf)))
over(SpP, spdfc) # will then fail in the same way as above.
Is there a simple way to solve this, not having check and modify the
proj4string of all objects? Or could the call to identical in functions like
over be replaced by an identicalCRS()-function?
identicalCRS = function(x,y) {
identical(CRS(proj4string(x)), CRS(proj4string(y)))
}
Thanks,
Jon
Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no