[GRASS-user] find (x,y) index of data

Moritz Lennert mlennert at club.worldonline.be
Sun Feb 7 00:16:37 PST 2016

Le Thu, 04 Feb 2016 19:55:13 -0500,
Ken Mankoff <mankoff at gmail.com> a écrit :

> Hi,
> I think I've solved this. I can compute i,j with:
> On 2016-02-04 at 16:48, Ken Mankoff <mankoff at gmail.com> wrote:
> > I'd like to do additional processing elsewhere (Python), outside of
> > grass, based on the index (i,j) of each point in a raster. Is
> > it possible to loop through the lon,lat or x,y coordinates in grass,
> > perform a calculation, and then also generate the i,j grid index
> > values for each point?
> >  
> eval `g.region -pg`
> r.out.xyz input=surf output=- fs=, | cut -d"," -f1-2 | while read -r
> line do
>   x=$(echo $line | cut -f1 -d,)
>   y=$(echo $line | cut -f2 -d,)
>   i=`echo "(($x)-($w))/$ewres"|bc`
>   j=`echo "(($y)-($s))/$nsres"|bc`
>   echo $x   $y   $i   $j
> done

You can also do this directly in GRASS using
r.mapcalc. Something like this:

g.region rast=YourMap
r.mapcalc "row = row()"
r.mapcalc "col = col()"
r.stats -g in=row,col output=results.csv

By adding YourMap (or others) to the in= parameter of r.stats you can
get the x,y and i,j coordinates directly with the respective pixel


More information about the grass-user mailing list