[R-sig-Geo] spTransform changes the bounding box

Roger Bivand Roger.Bivand at nhh.no
Sun Jun 24 15:03:28 CEST 2012


On Sat, 23 Jun 2012, PCB wrote:

> Dear All,
>
> I have been trying to use spTransform to convert some lon-lat data
> (SpatialPointsDataFrame) onto Equal Area projection.
>
> However, it seems that spTransform changes the bounding box of the data to
> contain exclusively the data points, and this crashes my code.
>
> I have been looking around, but could not find out what I am doing wrong.
> Is there a way to tell spTransform not to change the bounding box?
>
> Some reproducible example:
> bbox1 <- matrix(c(10, 20, -25, -3), , ncol=2, byrow = T)
> dimnames(bbox1) <- list(c("lon", "lat"), c("min", "max"))
> CRSWG84 <- CRS(" +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
> +towgs84=0,0,0")
>
> #Two points inside the bounding box
> pts1 <- SpatialPoints(coords = matrix(c(12, 15, -20, - 10), nrow=2, byrow=F)
> 		, bbox=bbox1, proj4string = CRSWG84)
> #Check the bounding box
> bbox(pts1)
>
> #I get
> #	min max
> #lon  10  20
> #lat -25  -3

This should arguably never have worked; expect changes in sp to prevent 
users from creating objects with bounding boxes that are not the strict 
envelopes of their geometries (grid bounding boxes respect the geometry 
representation of the full cell size anchored at the cell centre). When a 
Spatial* object is modified using sp and similar methods and functions, 
such as spTransform(), the bbox slot is updated to match, and by defintion 
is the strict envelope (enclosing rectangle) of the geometries.

I assume that you are using bbox because of its effects on the plot() 
method for Spatial objects - use the xlim= and ylim= arguments instead.

Roger

>
> ##Project into Lambert Azimuthal Equal Area
> CRSEqA <- CRS(" +proj=laea +ellps=WGS84")
> pts1Laea <- spTransform(pts1, CRSEqA)
>
> #Reproject back
> pts1LonLat <- spTransform(pts1Laea, CRSWG84)
> #Check the bounding box
> bbox(pts1LonLat)
>
> #I get
> #	 min max
> #lon  12  15
> #lat -20 -10
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/spTransform-changes-the-bounding-box-tp7580319.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
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