[R-sig-Geo] Apparent rotation when plotting a SpatialGridDataFrame?

Roger Bivand Roger.Bivand at nhh.no
Mon Oct 1 20:45:17 CEST 2012


On Mon, 1 Oct 2012, MacQueen, Don wrote:

>
> I have simple data coming to me from an outside source.
> x,y are coordinates in a UTM projection, and z is a
> measured value at each location.
>
> I make a SpatialPointsDataFrame from it, plot it with spplot(),
> and it appears as expected.
>
> I convert to a SpatialGridDataFrame, plot using spplot(),
> it appears to have been rotated 90 degrees.
>
> See example code to illustrate below.
>
> Am I doing something wrong?
>

No, this is a consequence of code cleanup, revision 1140 in R-forge SVN:

https://r-forge.r-project.org/scm/viewvc.php/pkg/sp/R/Class-SpatialGrid.R?root=rspatial&r1=1140&r2=1204

removing "coords" and "grid.index" slots from the definition of 
the SpatialGrid class. This split SpatialPixels and SpatialGrid cleanly, 
but meant that you can't smuggle the data slot across unpunished. This 
works:

slot(tstsp, "data")$z
tstpxdf <- as(tstsp, "SpatialPixelsDataFrame")
slot(tstpxdf, "data")$z
tstgrdf <- as(tstpxdf, "SpatialGridDataFrame")
slot(tstgrdf, "data")$z # showing the re-ordering
tmppl4 <- spplot(tstgrdf)
print(tmppl4)


Hope this helps,

Roger

>
> I used the same sequence of conversions about 7 months ago on
> a different set of data and did not see this problem. Inspecting
> the two versions of the data does not suggest a problem with
> the conversion process. The bounding box is consistent before
> and after the conversion.
>
> Here is a simple example to illustrate, followed by sessionInfo()
> It should be possible to just source the example code into an
> R session.
>
> ##
> ## simple example to illustrate the question
> ##
> require(sp)
>
> ## example data
> tstdf <- expand.grid(x=1:3, y=1:3)
> tstdf$z <- 5
> tstdf$z[tstdf$x==tstdf$y] <- 10
>
> ## move points to somewhere actually in the zone
> tstdf$x <- tstdf$x +  608000
> tstdf$y <- tstdf$y + 4167900
>
> ## convert to spatial points data frame; UTM projection
> tstsp <- tstdf
> coordinates(tstsp) <- c('x','y')
> proj4string(tstsp) <- CRS('+init=epsg:32610')
> print(bbox(tstsp))
>
> ## plot
> tmppl1 <- spplot(tstsp)
> print(tmppl1)
>
>
> ## convert to spatial grid
> tstgr <- points2grid(tstsp)
> tstsgr <- SpatialGrid(tstgr,
>             proj4string=CRS('+init=epsg:32610'))
> print(bbox(tstsgr))
> tstsgrd <- SpatialGridDataFrame(tstgr,
>                data=tstsp at data,
>                proj4string=CRS('+init=epsg:32610'))
> print(bbox(tstsgrd))
>
> ## plot -- compare with first plot
> tmppl2 <- spplot(tstsgrd)
> print(tmppl2)
>
> ##
> ## sessionInfo()
> ##
> R version 2.15.1 (2012-06-22)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>
> locale:
> [1] C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] sp_1.0-0
>
> loaded via a namespace (and not attached):
> [1] grid_2.15.1    lattice_0.20-6
>
>
> -------------------------------
> By the way, the reason for converting to a spatial grid is
> so that I can then convert it to a SpatialPolygonsDataFrame,
> spTransform() to long/lat and export to KML,
> then display in GoogleEarth with a raster-like appearance.
>
>
> There are no doubt other and likely better ways to accomplish
> the same end goal, a raster-like display in GoogleEarth,
> but this is a way I found.
> --------------------------------
>
>

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