[R-sig-Geo] Changing the coordinates of a SpatialGridDataFrame

Michael Sumner mdsumner at gmail.com
Fri Oct 29 01:54:11 CEST 2010


Probably the best way to do this is to modify the GridTopology and
recreate the grid.

The coordinates of a SpatialGridDataFrame are calculated from the 6
values of the grid topology, only SpatialPixelsDataFrame stores every
cell coordinate (possibly sparsely).

It's best to recreate the object, otherwise you might miss some of the
properties that the constructors generate.


## using your offset from the coords, but assuming equal cellsize and
smallish dims (so my max coordinates differ)
g0 <- GridTopology(cellcentre.offset = c(112.0013, -43.999004),
cellsize = c(0.025, 0.025), cells.dim = c(50, 100))

obj <- SpatialGridDataFrame(g0, data.frame(dat = 1:(50*100)))


## check the min coordinates match yours
 bbox(obj)
##               min      max
##coords.x1 111.9888 113.2388
##coords.x2 -44.0115 -41.5115


g1 <- GridTopology(cellcentre.offset = round(c(112.0013, -43.999004),
3), cellsize = c(0.025, 0.025), cells.dim = c(50, 100))

obj1 <- SpatialGridDataFrame(g1, data.frame(dat = 1:(50*100)))

Another brute force way to do this (brute as it generates every
coordinate, then detects the underlying pattern:

g1 <- points2grid(SpatialPoints(round(coordinates(obj), 3)))

I think that achieves what you were attempting in a literal sense, but
whether it is sensible is another question.

By rounding the "coordinates" directly you may break the regularity of
the grid, though you could work with the coordinates independently of
the grid object itself, as a single matrix or data.frame.

HTH

On Fri, Oct 29, 2010 at 10:30 AM,  <David.Clifford at csiro.au> wrote:
> Hi there,
>
> I have a SpatialGridDataFrame object and I wish to round the coordinates associated with the grid to three decimal places in order to match points in this object with points on another spatial object where the coordinates are recorded to three decimal places.
>
> Some information about the obj. It is quite a large grid that covers Australia with over 2 million points in it.
>
>> coords1 <- coordinates(obj)
>> dim(coords1)
> [1] 2287841       2
>> head(coords1)
>            x         y
> [1,] 112.0013 -9.999004
> [2,] 112.0263 -9.999004
> [3,] 112.0513 -9.999004
> [4,] 112.0763 -9.999004
> [5,] 112.1013 -9.999004
> [6,] 112.1263 -9.999004
>>
>> obj at coords
>            x          y
> [1,] 112.0013 -43.999004
> [2,] 154.0013  -9.999004
>> bbox(obj)
>       min        max
> x 111.9888 154.013789
> y -44.0115  -9.986504
>>
>
> My first attempt to do the rounding was based on the coordinates function. When I read the help file it  indicates that one can set the coordinates by passing a matrix here. I get an error as listed below.
>
>> ## First suggestion
>> coordinates(obj) <- round(coordinates(obj),3)
> Error in `coordinates<-`(`*tmp*`, value = c(112.001, 112.026, 112.051,  :
>  setting coordinates cannot be done on Spatial objects, where they have already been set
>
> A second way was suggested to me that involved rounding obj at coords directly. This produces no errors but also doesn't do the job as you can see below. It does round the obj at coords part but this doesn't have any effect on coordinates(obj) or on bbox(obj).
>
>> ## Second suggestion
>> roundcoordinates = function(obj, r = 3) {
> +   obj at coords = round(obj at coords, r)
> +   obj
> + }
>>
>> obj <- roundcoordinates(obj)
>>
>> obj at coords
>           x       y
> [1,] 112.001 -43.999
> [2,] 154.001  -9.999
>> bbox(obj)
>       min        max
> x 111.9888 154.013789
> y -44.0115  -9.986504
>>
>> coords2 <- coordinates(obj)
>> head(coords2)
>            x         y
> [1,] 112.0013 -9.999004
> [2,] 112.0263 -9.999004
> [3,] 112.0513 -9.999004
> [4,] 112.0763 -9.999004
> [5,] 112.1013 -9.999004
> [6,] 112.1263 -9.999004
>>
>
> Does anyone know how to go about this in a way that will also not cause other potential problems - ie changing obj at coords doesn't automatically update coordinates(obj) or bbox(obj).
>
> Thanks in advance,
>
> David
>
> Dr David Clifford
> Research Scientist
> CSIRO Division of Mathematics, Informatics and Statistics
> Phone: +61 7 3214 2816 | Fax: +61 7 3214 2855
> David.Clifford at csiro.au | www.csiro.au | www.csiro.au/people/David.Clifford.html
> Address: CSIRO Long Pocket Laboratories, 120 Meiers Road, INDOOROOPILLY QLD 4068
> New Postal Address from Nov 2010: CSIRO, EcoSciences Precinct, PO Box 2583, Brisbane, QLD 4001
> New Street Address from Nov 2010: CSIRO, EcoSciences Precinct, 41 Boggo Road, Annerley, QLD 4103
> New Phone from Nov 2010: +61 7 3833 5532
>
> PLEASE NOTE
> The information contained in this email may be confidential or privileged. Any unauthorised use or disclosure is prohibited. If you have received this email in error, please delete it immediately and notify the sender by return email. Thank you. To the extent permitted by law, CSIRO does not represent, warrant and/or guarantee that the integrity of this communication has been maintained or that the communication is free of errors, virus, interception or interference.
> Please consider the environment before printing this email.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list