[R-sig-Geo] projection() and proj4string()

Roger Bivand Roger.Bivand at nhh.no
Sat May 29 13:09:36 CEST 2010


On Sat, 29 May 2010, Agustin Lobo wrote:

> Why do I have to use proj4string() instead of projection() in this example?

You are getting mixed up between packages. projection() is a function in 
the raster package, while proj4string() is a function in sp. Use 
projection() for classes defined or extended in raster, proj4string() for 
classes defined in sp. Critically, projection<-() tries to assign to a 
slot called "crs", which is not present in Spatial* objects.

My feeling is that the sp nomenclature is clearer but clunkier, because it 
specifies the representation of the projection as a PROJ.4 string, while 
the name of the function "projection" could admit arbitrary 
representations, but in fact uses the same representation and mechanisms 
as sp (with verification through the PROJ.4 library if rgdal is loaded).

Probably projection<-() needs the same if() statement as that used in 
projection(), to distinguish extensions of "BasicRaster" from those of 
"Spatial", in which case projection<-() and proj4string<-() will both work 
on objects extending "Spatial" too.

Roger


>
>> class(Montseny20090409sh)
> [1] "SpatialPointsDataFrame"
> attr(,"package")
> [1] "sp"
>
>> projection(Montseny20090409sh)
> [1] "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"
>
>> a<- SpatialPoints(coordinates(Montseny20090409sh))
>> class(a)
> [1] "SpatialPoints"
> attr(,"package")
> [1] "sp"
>
>> projection(a) <- projection(Montseny20090409sh)
> Error in checkSlotAssignment(object, name, value) :
>  "crs" is not a slot in class "SpatialPoints"
> Calls: projection<- -> @<- -> slot<- -> checkSlotAssignment
>
>> proj4string(a) <- projection(Montseny20090409sh)
>> geogWGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
>> aGW <- spTransform(a, geogWGS84)
>> Montseny20090409sh2 <- Montseny20090409sh
>> Montseny20090409sh2 at data <- cbind(Montseny20090409sh2 at data,coordinates(aGW))
>
> Thanks for any clarification,
>
> Agus
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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