[GRASS-dev] How to read a raster cell value?

Colin Nielsen colin.nielsen at gmail.com
Wed Jun 25 09:37:01 EDT 2008


Hello again,
I hope this is an easy question:
I took this chunk of r.drain which reads single int CELL values given
a row and col:


    CELL direction;
    CELL *dir;

    dir = G_allocate_c_raster_buf();
    next_row = list->row;
    next_col = list->col;
...
	lseek(fd, (off_t) list->row * ncol * sizeof(CELL), SEEK_SET);
	read(fd, dir, ncol * sizeof(CELL));
	direction = *(dir + list->col);
	G_message(_("row:%i, col:%i, direction:%i"), next_row, next_col, direction);


And tried to replicate it for use with a double DCELL like this:

	int row, col, next_row, next_col;
	DCELL direction;
	DCELL *dir_buf;
	dir_buf = G_allocate_d_raster_buf();
	
	next_row = list->row;
	next_col = list->col;
	lseek(dir_fd, (off_t) list->row * ncol * sizeof(DCELL), SEEK_SET);
	read(dir_fd, dir_buf, ncol * sizeof(DCELL));
	direction = *(dir_buf + list->col);
	G_message(_("row:%i, col:%i, direction:%lf"), next_row, next_col, direction);


Somehow the top int section functions properly, while my modified
section for doubles does not (ie. it returns the wrong "direction"
value). I suspect it has something to do with the offset in lseek, but
I can't figure it out. Does it matter that the row and col are int,
while the value is double?

Is there an easier way to read a single cell value given that you know
the row and col?

Thanks,
-Colin


More information about the grass-dev mailing list