[GRASS-user] [GRASSLIST:1135] Qn. on extracting from multiple rasters, data corresponding to vector polygons

Hi all,

I have a vector polygon shapefile which contains 8 polygons (each watershed is a polygon). I also have some 100 GRASS raster files of environmental data (rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding average data from each raster file and put it in a text file?

I’ve looked at v.what.rast but it seems to be quite cumbersome for this application. I’m using Grass 6.0.1 on Ubuntu linux.

cheers,
vishal

Hallo,

On Tue, Jun 27, 2006 at 04:16:38PM +0530, Vishal Mehta wrote:

Hi all,

I have a vector polygon shapefile which contains 8 polygons (each watershed
is a polygon). I also have some 100 GRASS raster files of environmental data
(rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding average
data from each raster file and put it in a text file?

I've looked at v.what.rast but it seems to be quite cumbersome for this
application. I'm using Grass 6.0.1 on Ubuntu linux.

cheers,
vishal

v.type in=polygons out=polygons2 type=centroid,point
v.to.rast in=polygons2 out=wathershed use=cat
r.average base=wathershed cover=rainfalls out=rainfalls.avg
echo "ALTER TABLE polygons2 ADD COLUMN rainfalls double"|db.execute
v.what.vect vect=polygons2 rast=rainfalls.avg coulmn=rainfalls
...

v.type in=polygons2 out=polygons3 type=point,centroid
d.vect polygons3
d.what.vect polygons3

in the batch:

#----
# convert centorids to points
v.type in=polygons out=polygons2 type=centroid,point
v.to.rast in=polygons2 out=wathershed use=cat

# make 100 rasters
for raster in $( g.mlist rast ); do
    echo "Working on: $raster"
    r.average base=wathershed cover=$raster out=$raster.avg
    echo "ALTER TABLE polygons2 ADD COLUMN $raster double"|db.execute
    v.what.vect vect=polygons2 rast=$raster.avg coulmn=$raster

# convert points to centroids
v.type in=polygons2 out=polygons3 type=point,centroid
d.vect polygons3
d.what.vect polygons3
#----

copy & paste

jachym

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
GDF-Hannover
Mengendamm 16d
30177 Hannover
Germany
e-mail: cepicky@gdf-hannover.de
URL: http://gdf-hannover.de
Tel.: +49 511-39088507

eehm:

#----
# convert centorids to points
v.type in=polygons out=polygons2 type=centroid,point
v.to.rast in=polygons2 out=wathershed use=cat

# make 100 rasters
for raster in $( g.mlist rast ); do
    echo "Working on: $raster"
    r.average base=wathershed cover=$raster out=$raster.avg
    echo "ALTER TABLE polygons2 ADD COLUMN $raster double"|db.execute
    v.what.vect vect=polygons2 rast=$raster.avg coulmn=$raster

      done
# ----^^^^----

# convert points to centroids
v.type in=polygons2 out=polygons3 type=point,centroid
d.vect polygons3
d.what.vect polygons3
#----

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
GDF-Hannover
Mengendamm 16d
30177 Hannover
Germany
e-mail: cepicky@gdf-hannover.de
URL: http://gdf-hannover.de
Tel.: +49 511-39088507

On Tuesday 27 June 2006 03:46, Vishal Mehta wrote:

Hi all,

I have a vector polygon shapefile which contains 8 polygons (each watershed
is a polygon). I also have some 100 GRASS raster files of environmental
data (rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding average
data from each raster file and put it in a text file?

I've looked at v.what.rast but it seems to be quite cumbersome for this
application. I'm using Grass 6.0.1 on Ubuntu linux.

cheers,
vishal

if you have the ability to compile from source code, try starspan.

http://starspan.casil.ucdavis.edu/?StarSpan

Matt Perry has a nice demo here:

http://www.perrygeo.net/wordpress/?p=30

Cheers,

--
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341

Vishal:

            We designed starspan to do exactly this - one of these days
we'll mod it so it works from within grass: starspan.casil.ucdavis.edu .

--j

--

Jonathan A. Greenberg, PhD
NRC Research Associate
NASA Ames Research Center
MS 242-4
Moffett Field, CA 94035-1000
Phone: 415-794-5043
AIM: jgrn3007
MSN: jgrn3007@hotmail.com

  _____

From: owner-GRASSLIST@baylor.edu [mailto:owner-GRASSLIST@baylor.edu] On
Behalf Of Vishal Mehta
Sent: Tuesday, June 27, 2006 3:47 AM
To: GRASS Users list
Subject: [GRASSLIST:1135] Qn. on extracting from multiple rasters, data
corresponding to vector polygons

Hi all,

I have a vector polygon shapefile which contains 8 polygons (each watershed
is a polygon). I also have some 100 GRASS raster files of environmental data
(rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding average
data from each raster file and put it in a text file?

I've looked at v.what.rast but it seems to be quite cumbersome for this
application. I'm using Grass 6.0.1 on Ubuntu linux.

cheers,
vishal

Thanks all (esp. Jachym, Dylan, Jonathan)

Dylan/Jonathan, I havent tried Starspan, but it
looks good. i’ll get into it if i find i have to. I cant compile from sourcecode since i’m not that Linux savvy.

Jachym, I tried your approach on a small set of 12 rasters. v.what.vect is not there in my Grass 6.0.1; but i was able to do the same thing with v.what.rast
Basically values from each raster were added on as a column in the vector attribute table. Thanks Jachym!

However, even with just 12 rasters, it took a bit of time (~10 seconds). With some 100 rasters to process at a time, I have the following doubts:

  1. Can the vector attribute table handle 100 additional tables, or is there a limit?

  2. My rasters are large, covering south asia (60E-100E, 5N-45N; 0.5 degree resolution). My vectors are very small (covering small part of south-west India). Perhaps this effects the processing time…should i mask the rasters and then do the adding of columns?

  3. Since my watershed are small, most fall within one pixel. When they do cross 2 pixels, the v.to.rast clips the raster watershed to one pixel. Any suggestion on perhaps better ways of handling this?

Thanks very much!
cheers,
vishal

On 6/27/06, Jachym Cepicky <jachym.cepicky@centrum.cz> wrote:

Hallo,

On Tue, Jun 27, 2006 at 04:16:38PM +0530, Vishal Mehta wrote:

Hi all,

I have a vector polygon shapefile which contains 8 polygons (each watershed
is a polygon). I also have some 100 GRASS raster files of environmental data
(rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding average
data from each raster file and put it in a text file?

I’ve looked at v.what.rast but it seems to be quite cumbersome for this
application. I’m using Grass 6.0.1 on Ubuntu linux.

cheers,
vishal

v.type in=polygons out=polygons2 type=centroid,point
v.to.rast in=polygons2 out=wathershed use=cat
r.average base=wathershed cover=rainfalls out=rainfalls.avg
echo “ALTER TABLE polygons2 ADD COLUMN rainfalls double”|db.execute
v.what.vect vect=polygons2 rast=rainfalls.avg coulmn=rainfalls

v.type in=polygons2 out=polygons3 type=point,centroid
d.vect polygons3
d.what.vect polygons3

in the batch:

#----

convert centorids to points

v.type in=polygons out=polygons2 type=centroid,point
v.to.rast in=polygons2 out=wathershed use=cat

make 100 rasters

for raster in $( g.mlist rast ); do
echo “Working on: $raster”
r.average base=wathershed cover=$raster out=$raster.avg
echo “ALTER TABLE polygons2 ADD COLUMN $raster double”|db.execute
v.what.vect vect=polygons2 rast=$raster.avg coulmn=$raster

convert points to centroids

v.type in=polygons2 out=polygons3 type=point,centroid
d.vect polygons3
d.what.vect polygons3
#----

copy & paste

jachym


Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc

OFFICE:
GDF-Hannover
Mengendamm 16d
30177 Hannover
Germany
e-mail: cepicky@gdf-hannover.de
URL: http://gdf-hannover.de
Tel.: +49 511-39088507

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEoSC6yKt0uAjU4I8RAs7bAJ0VbEJZ7763uMKCL7HPRQvCk7XztQCfarwE
yKmI8KxGdTk0qskBiEFSE2E=
=CJ47
-----END PGP SIGNATURE-----

Vishal Mehta wrote:

I have a vector polygon shapefile which contains 8 polygons (each
watershed is a polygon). I also have some 100 GRASS raster files of
environmental data (rainfall, temperature etc).

Whats the best way to extract for each watershed, the corresponding
average data from each raster file and put it in a text file?

I've looked at v.what.rast but it seems to be quite cumbersome for
this application. I'm using Grass 6.0.1 on Ubuntu linux.

I think this can be easily done.

why not just

g.region rast=biggest_map
v.to.rast in=vectmap out=watersheds use=cat # or attr if ID column exists
r.mapcalc "MASK=if(watersheds == 1, 1, null() )"
r.univar rainfall

putting that in a loop:

g.region rast=typical_map
v.to.rast in=vectmap out=watersheds use=cat

r.info -r watersheds
min=1
max=8
eval `r.info -r watersheds`

#if IDs are not sequential use
# for WATERSHED in `r.stats -qn watersheds` ; do

# this is untested, but the general method works

echo "watershed|map|average_val" > results.txt

for WATERSHED in `seq $min $max` ; do
  g.remove MASK
  unset MEAN

  echo "Watershed: $WATERSHED Map: $MAP"
  r.mapcalc "MASK=if(watersheds == $WATERSHED, 1, null() )"

  for MAP in `g.mlist type=rast pat=* fs=space` ; do
     #g.region rast=$MAP # adjust to match res of each map
     MEAN=`r.univar -g $MAP | grep '^mean=' | cut -f2 -d=`
     echo "$WATERSHED|$MAP|$MEAN" >> results.txt
  done

done
g.remove MASK

if maps are all different sizes and resolutions do the v.to.rast
step at the finest resolution to avoid aliasing problems.

good luck,
Hamish

Vishal Mehta wrote:

2. My rasters are large, covering south asia (60E-100E, 5N-45N; 0.5
degree resolution). My vectors are very small (covering small part of
south-west India). Perhaps this effects the processing time..should i
mask the rasters and then do the adding of columns?

add in a step after MASK creation:

g.region zoom=MASK res=0.5 -a

leave out the g.mlist sep=space, it uses the whole word!
`g.mlist pat='*'` is enough.

Hamish

ps - new mailing list address: grassuser at grass.itc.it

Hi all,
I had, with your help, added 500 columns of rain data from 500 corresponding rasters, t0 my vector map of 8 watersheds.

Now i want to add another 500 columns to the same vector map, but from different 100 rasters, namely derived reference ET maps i have created.

My qna are:

  1. Should i use the same table but first connect to a different empty ‘layer’ first using v.db.connect?

  2. If i do the above, will i be able to extract individual layers of the table in openoffice calc or Excel?

  3. i was going to just copy my vector map and then delete the original 500 rain columns from the table, and then add the 500 ET columns- but i was not able to do it. i tried commands like
    echo “DELETE COLUMN ‘Rain1’ FROM watershed8c” | db.execute

and
echo “DELETE FROM watershed8c COLUMN LIKE ‘rain’” | db.execute
but they did’nt work.

How do i delete say all columns that start with the text ‘rain’ from a table?

thanks much,
vishal

On 6/28/06, Hamish <hamish_nospam@yahoo.com> wrote:

Vishal Mehta wrote:

  1. My rasters are large, covering south asia (60E-100E, 5N-45N; 0.5
    degree resolution). My vectors are very small (covering small part of
    south-west India). Perhaps this effects the processing time…should i
    mask the rasters and then do the adding of columns?

add in a step after MASK creation:

g.region zoom=MASK res=0.5 -a

leave out the g.mlist sep=space, it uses the whole word!
g.mlist pat='*' is enough.

Hamish

ps - new mailing list address: grassuser at grass.itc.it

Vishal Mehta wrote:

3. i was going to just copy my vector map and then delete the original
500 rain columns from the table, and then add the 500 ET columns- but
i was not able to do it. i tried commands like
echo "DELETE COLUMN 'Rain1' FROM watershed8c" | db.execute

and
echo "DELETE FROM watershed8c COLUMN LIKE 'rain'" | db.execute
but they did'nt work.

How do i delete say all columns that start with the text 'rain' from a
table?

I'm not sure, but the DBF driver may not support "DELETE" ?
Make a new table, copy columns you want to keep, v.db.droptable the old
table? or use another db?

Hamish

Hamish wrote:

Vishal Mehta wrote:

echo "DELETE FROM watershed8c COLUMN LIKE 'rain'" | db.execute
but they did'nt work.

How do i delete say all columns that start with the text 'rain' from a
table?

I'm not sure, but the DBF driver may not support "DELETE" ?

Correct. Neither DROP COLUMN will work. Neither in SQLITE doesn't. A
workaround: http://grass.gdf-hannover.de/wiki/Sqlite_Drop_Column

Make a new table, copy columns you want to keep, v.db.droptable the old
table? or use another db?

Postgres and Mysql support DROP COLUMN.

Maciek