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