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

David.Clifford at csiro.au David.Clifford at csiro.au
Fri Oct 29 01:30:46 CEST 2010


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.



More information about the R-sig-Geo mailing list