[R-sig-Geo] proj4string-issue - leading spaces

Jon Olav Skoien jon.skoien at jrc.ec.europa.eu
Fri Jan 11 12:10:05 CET 2013


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

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

-- 
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Environment and Sustainability (IES)
Land Resource Management Unit

Via Fermi 2749, TP 440,  I-21027 Ispra (VA), ITALY

jon.skoien at jrc.ec.europa.eu
Tel:  +39 0332 789206

Disclaimer: Views expressed in this email are those of the individual and do not necessarily represent official views of the European Commission.



More information about the R-sig-Geo mailing list