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

Ken Mankoff mankoff at gmail.com
Thu Feb 4 13:48:02 PST 2016

Hi list,

I'd like to do additional processing elsewhere (Python), outside of grass, based on the index (idx_X, idx_Y) 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?

An alternative is to re-create the full projection setup in Python and then access things there based on lon,lat or x,y, but I'd rather just use i,j indices that I calculate in grass, if this is easily doable.

I've created the projection and loaded the data with the following commands:

grass71 -c -e EPSG:3413 ~/data/grass/my_proj
grass71 -text ~/data/grass/my_proj/PERMANENT
r.in.gdal -o -e input=NETCDF:file.nc:surf output=surf
g.region -s rast=surf
g.remove surf
g.region -p

Now i'm looping over each element in surf (just to get the coordinates of each point) with this:

r.out.xyz input=surf output=- fs=, | cut -d"," -f1-2 | while read -r line
  x=$(echo $line | cut -f1 -d,)
  y=$(echo $line | cut -f2 -d,)
  r.drain -n input=bar output=tmp coordinate=$x,$y --q
  # SOMETHING WITH $x and $y goes here to make i,j
  # Save tmp w/ unique filename based on i,j

More information about the grass-user mailing list