[R-sig-Geo] flip SpatialGridDataFrame across axis

Michael Sumner mdsumner at gmail.com
Mon Nov 2 21:40:59 CET 2009


Hi Sebastian, I think the "north-south" note is referring to
possibly-rotated grids (using the transform values supported by many
formats) - not to "north vs. south" in orientation.

You can easily flip a grid by reverting it (one band at a time) to an
xyz image and using indexing. I find this approach the least confusing
and easily repeatable.

library(rgdal)
data(meuse.grid)
coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
image(meuse.grid[3])

## note this method only works on single bands at once
x1 <- as.image.SpatialGridDataFrame(meuse.grid[3])

## reverse the Y by indexing in to the (image) xyz list
x1$z <- x1$z[,ncol(x1$z):1]

## convert to SGDF
flipY <- image2Grid(x1, p4 = as.character(proj4string(meuse.grid)))
contour(flipY, add = TRUE)

It would not be difficult to adapt this to flip each band
sequentially, and use the internal arrangement of the
SpatialGrid/Pixels classes directly - but this is enough for me.

Regards, Mike.

On Tue, Nov 3, 2009 at 7:05 AM, Sebastian P. Luque <spluque at gmail.com> wrote:
> Hi,
>
> After importing a grid with readGDAL(), a `SpatialGridDataFrame' object
> is produced that looks correct, except the y-axis is flipped upside
> down, despite the note in the "Value" section in ?readGDAL.  So I need
> to flip the grid across the horizontal axis, and searching the archives
> points to an old reference to elide() in maptools.  However, this
> doesn't take a `SpatialGridDataFrame', so I'd appreciate any suggestions
> for better alternatives.  Thanks.
>
> Cheers,
>
> --
> Seb
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list