Hello group, I am trying to get the column from point that exists the interfaces of two cells and find that the results I'm getting are not consistent. In the below example I am getting the higher boundary for the cell for 0.1, 0.2, and 0.4. However, I get the lower boundary for 0.3. My current solution is to add a very small number to the coordinate if the value falls on the interface but does anyone know a better workaround? library(raster) H <- raster(matrix(sample.int(100), ncol=10)) colFromX(H, 0.1) # 2 colFromX(H, 0.2) # 3 colFromX(H, 0.3) # 3 colFromX(H, 0.4) # 5 colFromX(H, 0.30000000000000001) #3 colFromX(H, 0.3000000000000001) #4 Cheers, -Jonathan Session Info R version 3.0.0 (2013-04-03) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] raster_2.1-16 sp_1.0-8 loaded via a namespace (and not attached): [1] grid_3.0.0 lattice_0.20-15 tools_3.0.0 -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Raster-package-colFromX-behaviour-tp7583304.html Sent from the R-sig-geo mailing list archive at Nabble.com.
Raster package: colFromX behaviour
3 messages · Robert J. Hijmans, Jonathan
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130414/a9d09c88/attachment.pl>
Thank you Robert for your response,
I see it is a floating point issue so I've now come up with a solution
(modified your version slightly) that is functional for my use. Basically
it rounds the calculated ratio to a certain number of digits and then
truncates the value. I've attached it in case it is of use to someone. I
am tracking points across the cells and needed values to be consistently on
one side or the other or else my points can become trapped in a particular
cell when an edge is reached.
colFromX <- function ( object, x, digits=10) {
if (inherits(x, 'Spatial')) {
x <- x at coords[,1]
}
if (rotated(object)) {
stop('this function is not supported for rotated rasters')
}
colnr <- trunc(round((x - xmin(object)) / xres(object), digits)) + 1
colnr[ x == xmax(object) ] <- object at ncols
colnr[ x < xmin(object) | x > xmax(object) ] <- NA
return(as.vector(colnr))
}
Thank you again,
Jonathan
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Raster-package-colFromX-behaviour-tp7583304p7583314.html
Sent from the R-sig-geo mailing list archive at Nabble.com.