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

Roger Bivand Roger.Bivand at nhh.no
Fri Jan 11 14:42:19 CET 2013


On Fri, 11 Jan 2013, Jon Olav Skoien wrote:

> On 11-Jan-13 12:42, Roger Bivand wrote:
>> On Fri, 11 Jan 2013, Edzer Pebesma wrote:
>> 
>>> 
>>> 
>>> On 01/11/2013 12:10 PM, Jon Olav Skoien wrote:
>>>> 
>>>> 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)))
>>>> }
>>> 
>>> Great suggestion, Jon; I added this to sp on r-forge.
>> 
>> Not so great, it doesn't address the problem that users with different 
>> versions of rgdal (and/or with stored objects from older versions of rgdal) 
>> may have extraneous prepended spaces. The function nees to test for NA or 
>> not, then length, if lengths equal, OK, test, if differ by one check 
>> whether space and if so trim, ...
>> 
> You mean that the suggestion does not solve the problem when using CRS from 
> sp instead of rgdal as the sp-version does not trim leading spaces?

There is only one user-facing CRS(), but it behaves differently when rgdal 
is loaded (checking against projlib), or not available (no check against 
projlib). The problem arose because (lazy programming in projlib?) a space 
gets prepended to the string when it passes through projlib (and we 
certainly want to use the output from projlib, because it completes tags 
while going through - it parses the tags as input, and re-assembles them 
on the way back out).

Roger

> Then I think an alternative sp version could use gsub without being too 
> complicated. Leading spaces can be removed from the proj4string if they 
> exists, NAs are not touched:
>
> ch = "+init=epsg:4326"
> gsub("^ .", "", ch)
> ch = " +init=epsg:4326"
> gsub("^ .", "", ch)
> ch = NA
> gsub("^ .", "", ch)
>
> and the function could be something like:
>
> identicalCRS = function(x,y) {
>  if (inherits(x, "Spatial")) x = proj4string(x)
>  if (inherits(y, "Spatial")) y = proj4string(y)
>  x = gsub("^ .", "", y)
>  y = gsub("^ .", "", y)
>  identical(x,y)
> }
>
> Two NA-proj4strings will be treated as identical, same as they are now. 
> However, it will not catch the similarity between "+init=epsg:4326" and 
> "+init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs 
> +towgs84=0,0,0" such as a possible rgdal-version (first suggestion above) can 
> do.
>
> Cheers,
> 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



More information about the R-sig-Geo mailing list