[R-sig-Geo] Raster package: colFromX behaviour

Jonathan j2kennel at gmail.com
Mon Apr 15 06:16:51 CEST 2013


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.



More information about the R-sig-Geo mailing list