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
do
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
done
Hi,
I think I've solved this. I can compute i,j with:
On 2016-02-04 at 16:48, Ken Mankoff <mankoff@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
-k.
Le Thu, 04 Feb 2016 19:55:13 -0500,
Ken Mankoff <mankoff@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@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
values.
Moritz