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 > > o...@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 o...@coords directly. This produces no errors but also doesn't do the job as you can see below. It does round the o...@coords part but this doesn't have any effect on coordinates(obj) or on bbox(obj). > ## Second suggestion > roundcoordinates = function(obj, r = 3) { + o...@coords = round(o...@coords, r) + obj + } > > obj <- roundcoordinates(obj) > > o...@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 o...@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.cliff...@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@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo