[GRASS-user] How to list all unique pixel values (of a raster) covered by polygons (areas) for each polygon separately(?)

I tried to make it clear in the title... but I am not sure
that it's easy to understand.

Using v.rast.stats I get uni-variate statistics from a
raster (in my case a MODIS image with 250m pixel size)
based on polygons I digitised manually (areas).

How can I get listed all unique pixel values enclosed in
each of the polygons I digitised (and for which the univ.
stats are calculated)?

Need them for further statistical analysis...

There must be a way to do this instead of creating unique
raster(s) for each polygon and using r.stats... !
           

On Jan 17, 2008 4:30 PM, <nikos.alexandris@felis.uni-freiburg.de> wrote:

How can I get listed all unique pixel values enclosed in
each of the polygons I digitised (and for which the univ.
stats are calculated)?

Can’t you just rasterise your polygons, and use that as a mask with r.mapcalc? You can loop around in your shell. I tend to do that with python, but it’s similar in GRASS. You can also try starspan. Or… <http://grass.gdf-hannover.de/wiki/Aggregate_Values >

Cheers,
Jose


Centre for Terrestrial Carbon Dynamics
Department of Geography, University College London
Gower Street, London WC1E 6BT, UK

Salut Jose!

This is what I am doing... and I only have 7 polygons - so
it is ok.

But if I have 50 polygons... I am not so sure how to script
it to get (finally) a table with all pixels for each unique
polygon for 2 different images of the same location (for
raster MODIS year 2006 and then another of the same area
MODIS 2007!).

I want to run a regression with the pixel values... (pairs
of 2006 and 2007 values).

Thank you,

Nikos.

On Thu, 17 Jan 2008 16:42:31 +0000
"Jose Gomez-Dans" <jgomezdans@gmail.com> wrote:

On Jan 17, 2008 4:30 PM,
<nikos.alexandris@felis.uni-freiburg.de> wrote:

> How can I get listed all unique pixel values enclosed
in
> each of the polygons I digitised (and for which the
univ.
> stats are calculated)?
>

Can't you just rasterise your polygons, and use that as a
mask with
r.mapcalc? You can loop around in your shell. I tend to
do that with python,
but it's similar in GRASS. You can also try starspan.
Or.... <
http://grass.gdf-hannover.de/wiki/Aggregate_Values&gt;

Cheers,
Jose

--
Centre for Terrestrial Carbon Dynamics
Department of Geography, University College London
Gower Street, London WC1E 6BT, UK

                        .
         Nikos Alexandris - Ph.D. Candidate
                        .
            Department of Remote Sensing
                       and
            Landscape Information Systems
                     (FeLIS)
                        .
      Faculty of Forestry and Natural Environment
          Albert-Ludwigs-University Freiburg
                        .
            Tel. +49 (0) 761 203 3697
            Fax. +49 (0) 761 203 3701
             Skype: Nikos.Alexandris
                        .
           Address: Tennenbacher str. 4
              D-79106 Freiburg i. Br.
                     Germany
                        .

Just a rapid idea.
You could make just one rasterized map of your polygons and then
insert in a loop this, giving each pixel a unique category value:

r.mapcalc "MASK = if(raster=$i,1,null())"
with $1 the cat value (deriving from you original polygon)

and then run r.stats, which will work only for the un-masked pixels.

I don't have time to try this method, hope it helps.
GIovanni

2008/1/17, nikos.alexandris@felis.uni-freiburg.de
<nikos.alexandris@felis.uni-freiburg.de>:

Salut Jose!

This is what I am doing... and I only have 7 polygons - so
it is ok.

But if I have 50 polygons... I am not so sure how to script
it to get (finally) a table with all pixels for each unique
polygon for 2 different images of the same location (for
raster MODIS year 2006 and then another of the same area
MODIS 2007!).

I want to run a regression with the pixel values... (pairs
of 2006 and 2007 values).

Thank you,

Nikos.

On Thu, 17 Jan 2008 16:42:31 +0000
"Jose Gomez-Dans" <jgomezdans@gmail.com> wrote:
> On Jan 17, 2008 4:30 PM,
> <nikos.alexandris@felis.uni-freiburg.de> wrote:
>
> > How can I get listed all unique pixel values enclosed
> in
> > each of the polygons I digitised (and for which the
> univ.
> > stats are calculated)?
> >
>
> Can't you just rasterise your polygons, and use that as a
> mask with
> r.mapcalc? You can loop around in your shell. I tend to
> do that with python,
> but it's similar in GRASS. You can also try starspan.
> Or.... <
> http://grass.gdf-hannover.de/wiki/Aggregate_Values&gt;
>
> Cheers,
> Jose
>
>
>
> --
> Centre for Terrestrial Carbon Dynamics
> Department of Geography, University College London
> Gower Street, London WC1E 6BT, UK

                        .
         Nikos Alexandris - Ph.D. Candidate
                        .
            Department of Remote Sensing
                       and
            Landscape Information Systems
                     (FeLIS)
                        .
      Faculty of Forestry and Natural Environment
          Albert-Ludwigs-University Freiburg
                        .
            Tel. +49 (0) 761 203 3697
            Fax. +49 (0) 761 203 3701
             Skype: Nikos.Alexandris
                        .
           Address: Tennenbacher str. 4
              D-79106 Freiburg i. Br.
                     Germany
                        .
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Giovanni,

thank you for your proposal.

The link to the FAQ (the one that Jose provided) looks like a solution.

But your idea is clever as well and I had a similar way in my mind.

If I got it correct you suggest:

1. VectorTargetAreas ~~~v.to.rast (based on unique attribute like cat
or FID~~~> RasterTargetAreas (all pixels of a "TargetArea" have now the
same value

2.MASK out everything besides the "TargetAreas" ---looping to create as
many MASKS as the the number of the "TargetAreas"

3. For each MASK run r.stats

4 (I would also like to have) add the output list(s) as a new table
(column pixval) into the initial VectorTargetAreas (with db.connect,
db.addcol, etc). Let's say for a polygons which covers 20 pixels it
would be like:

FID - cat - pixval
1 - 1 - 200
2 - 1 - 198
3 - 1 - 234
.. - .. - .....
20 - 1 - 765

Is it possible to put all this together?

r.stats and output into a new column (pixval)
On Thu, 2008-01-17 at 18:25 +0100, G. Allegri wrote:

Just a rapid idea.
You could make just one rasterized map of your polygons and then
insert in a loop this, giving each pixel a unique category value:

r.mapcalc "MASK = if(raster=$i,1,null())"
with $1 the cat value (deriving from you original polygon)

and then run r.stats, which will work only for the un-masked pixels.

I don't have time to try this method, hope it helps.
GIovanni

2008/1/17, nikos.alexandris@felis.uni-freiburg.de
<nikos.alexandris@felis.uni-freiburg.de>:
> Salut Jose!
>
> This is what I am doing... and I only have 7 polygons - so
> it is ok.
>
> But if I have 50 polygons... I am not so sure how to script
> it to get (finally) a table with all pixels for each unique
> polygon for 2 different images of the same location (for
> raster MODIS year 2006 and then another of the same area
> MODIS 2007!).
>
> I want to run a regression with the pixel values... (pairs
> of 2006 and 2007 values).
>
> Thank you,
>
> Nikos.
>
>
>
> On Thu, 17 Jan 2008 16:42:31 +0000
> "Jose Gomez-Dans" <jgomezdans@gmail.com> wrote:
> > On Jan 17, 2008 4:30 PM,
> > <nikos.alexandris@felis.uni-freiburg.de> wrote:
> >
> > > How can I get listed all unique pixel values enclosed
> > in
> > > each of the polygons I digitised (and for which the
> > univ.
> > > stats are calculated)?
> > >
> >
> > Can't you just rasterise your polygons, and use that as a
> > mask with
> > r.mapcalc? You can loop around in your shell. I tend to
> > do that with python,
> > but it's similar in GRASS. You can also try starspan.
> > Or.... <
> > http://grass.gdf-hannover.de/wiki/Aggregate_Values&gt;
> >
> > Cheers,
> > Jose
> >
> >
> >
> > --
> > Centre for Terrestrial Carbon Dynamics
> > Department of Geography, University College London
> > Gower Street, London WC1E 6BT, UK
>
>
> .
> Nikos Alexandris - Ph.D. Candidate
> .
> Department of Remote Sensing
> and
> Landscape Information Systems
> (FeLIS)
> .
> Faculty of Forestry and Natural Environment
> Albert-Ludwigs-University Freiburg
> .
> Tel. +49 (0) 761 203 3697
> Fax. +49 (0) 761 203 3701
> Skype: Nikos.Alexandris
> .
> Address: Tennenbacher str. 4
> D-79106 Freiburg i. Br.
> Germany
> .
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>

--
Nikos Alexandris
.
Department of Remote Sensing & Landscape Information Systems
Faculty of Forestry & Environmental Sciences, Albert-Ludwigs-University Freiburg
.
Tel. +49 (0) 761 203 3697 / Fax. +49 (0) 761 203 3701 / Skype: Nikos.Alexandris
.
Address: Tennenbacher str. 4, D-79106 Freiburg i. Br., Germany

About point 4), you can do it simply adding (v.db.addcol) at the
beginning of your job, then insert something like this into the loop:

echo "UPDATE (your_table) SET pixval=(what_you_want_to_upload) WHERE
FID=$i" | db.execute

Inside "what_you_want_to_upload" you can grab what you want from the
r.stats output (i.e. the values range).

The pixval column type depends on what kind of information you want to
insert. If a value range you set it as string type, or whatelse...

2008/1/18, Nikos Alexandris <nikos.alexandris@felis.uni-freiburg.de>:

Giovanni,

thank you for your proposal.

The link to the FAQ (the one that Jose provided) looks like a solution.

But your idea is clever as well and I had a similar way in my mind.

If I got it correct you suggest:

1. VectorTargetAreas ~~~v.to.rast (based on unique attribute like cat
or FID~~~> RasterTargetAreas (all pixels of a "TargetArea" have now the
same value

2.MASK out everything besides the "TargetAreas" ---looping to create as
many MASKS as the the number of the "TargetAreas"

3. For each MASK run r.stats

4 (I would also like to have) add the output list(s) as a new table
(column pixval) into the initial VectorTargetAreas (with db.connect,
db.addcol, etc). Let's say for a polygons which covers 20 pixels it
would be like:

FID - cat - pixval
1 - 1 - 200
2 - 1 - 198
3 - 1 - 234
.. - .. - .....
20 - 1 - 765

Is it possible to put all this together?

r.stats and output into a new column (pixval)
On Thu, 2008-01-17 at 18:25 +0100, G. Allegri wrote:
> Just a rapid idea.
> You could make just one rasterized map of your polygons and then
> insert in a loop this, giving each pixel a unique category value:
>
> r.mapcalc "MASK = if(raster=$i,1,null())"
> with $1 the cat value (deriving from you original polygon)
>
> and then run r.stats, which will work only for the un-masked pixels.
>
> I don't have time to try this method, hope it helps.
> GIovanni
>
> 2008/1/17, nikos.alexandris@felis.uni-freiburg.de
> <nikos.alexandris@felis.uni-freiburg.de>:
> > Salut Jose!
> >
> > This is what I am doing... and I only have 7 polygons - so
> > it is ok.
> >
> > But if I have 50 polygons... I am not so sure how to script
> > it to get (finally) a table with all pixels for each unique
> > polygon for 2 different images of the same location (for
> > raster MODIS year 2006 and then another of the same area
> > MODIS 2007!).
> >
> > I want to run a regression with the pixel values... (pairs
> > of 2006 and 2007 values).
> >
> > Thank you,
> >
> > Nikos.
> >
> >
> >
> > On Thu, 17 Jan 2008 16:42:31 +0000
> > "Jose Gomez-Dans" <jgomezdans@gmail.com> wrote:
> > > On Jan 17, 2008 4:30 PM,
> > > <nikos.alexandris@felis.uni-freiburg.de> wrote:
> > >
> > > > How can I get listed all unique pixel values enclosed
> > > in
> > > > each of the polygons I digitised (and for which the
> > > univ.
> > > > stats are calculated)?
> > > >
> > >
> > > Can't you just rasterise your polygons, and use that as a
> > > mask with
> > > r.mapcalc? You can loop around in your shell. I tend to
> > > do that with python,
> > > but it's similar in GRASS. You can also try starspan.
> > > Or.... <
> > > http://grass.gdf-hannover.de/wiki/Aggregate_Values&gt;
> > >
> > > Cheers,
> > > Jose
> > >
> > >
> > >
> > > --
> > > Centre for Terrestrial Carbon Dynamics
> > > Department of Geography, University College London
> > > Gower Street, London WC1E 6BT, UK
> >
> >
> > .
> > Nikos Alexandris - Ph.D. Candidate
> > .
> > Department of Remote Sensing
> > and
> > Landscape Information Systems
> > (FeLIS)
> > .
> > Faculty of Forestry and Natural Environment
> > Albert-Ludwigs-University Freiburg
> > .
> > Tel. +49 (0) 761 203 3697
> > Fax. +49 (0) 761 203 3701
> > Skype: Nikos.Alexandris
> > .
> > Address: Tennenbacher str. 4
> > D-79106 Freiburg i. Br.
> > Germany
> > .
> > _______________________________________________
> > grass-user mailing list
> > grass-user@lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/grass-user
> >
--
Nikos Alexandris
.
Department of Remote Sensing & Landscape Information Systems
Faculty of Forestry & Environmental Sciences, Albert-Ludwigs-University Freiburg
.
Tel. +49 (0) 761 203 3697 / Fax. +49 (0) 761 203 3701 / Skype: Nikos.Alexandris
.
Address: Tennenbacher str. 4, D-79106 Freiburg i. Br., Germany

G. Allegri wrote:

About point 4), you can do it simply adding (v.db.addcol) at the
beginning of your job, then insert something like this into the loop:

echo "UPDATE (your_table) SET pixval=(what_you_want_to_upload) WHERE
FID=$i" | db.execute

fyi there is v.db.update which may be easier to use than raw
db.execute.

Hamish

      ____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping

Here it goes... :

It's my first attempt to write a functional script
(attached below), so please, if you have the time, correct
my mistakes.

So far:

1. I managed to "grab" pixel values from rasters covered by
vector areas and export them in .txt (or .csv) file(s).

2. If I get the "upload to vector table" task working
(point 3.), I would like to make the script more flexible
so it can automatically run the process for more raster(s).

I mean: grab pix values from all files named after pattern
"MOD200???" -- e.g. MOD2006NIR, MOD2006R, MOD2006Blue,
MOD2007NIR, etc. --

3. I am stuck with how to get these on a table (other than
the existing one for my source vector polygons)

I want to create a new table where I upload the extracted
pixel values.

[ I think I got the meaning of layers (tables connected to
on e vector) but still, I can't:

a. update in a new "layer2", a column "areacat" with the
"cat" values from "layer1" (?)

b. (if "a" successful then put in the loop a v.db.update
command)

About "a" following command: > v.to.db map=$AREASVECT
type=boundary layer=2 qlayer=1 option=query
column='areacat' qcolumn='cat'

gives:

Reading data from the map...
100%
Querying database...
100%
Updating database...
100%
1 categories read from map
0 records selected from table
0 categories read from map exist in selection from table
0 categories read from map don't exist in selection from
table
0 records updated/inserted
0 update/insert errors ]

*****************************

My (first!) script:

#!/bin/sh
# List pixel values covered by areas of interest for each
area separately
# First attempt, Nikos Alexandris

if [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program."

&2

    exit 1
fi

PROG="$0"

echo "Script to list all unique pixel values in raster(s)
covered by vector areas of interest"

echo "\nExplanations:"
echo "a. Repeated value(s) listed as many times as present"
echo "b. Areas derived from vector polygons"
echo "c. Assuming first column is "cat""
sleep 2

if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" -o "$1" =
"-help" ] ;
then
echo "\n* Please provide vector area(s) and raster(s)
source(s) of interest!"
echo "Usage:"
echo " $PROG [Vector areas of interest] [Raster
source 1] [Raster source 2]\n"
exit 1
fi

# Source for vector area(s) of interest
AREASVECT=$1

# Raster(s) from which pixel values will be extracted
RASTERSRC1=$2
RASTERSRC2=$3

# Define the number of areas based on "cat" column of
AREASVECT
CATi="`db.select table=$AREASVECT | cut -d"|" -f1 `"
for i in $CATi; do
  CATMAX=$(echo $i);
done
echo "Table [$AREASVECT] contains $CATMAX areas."

echo "Creating new table to upload pixel values... "

v.db.addtable map=$AREASVECT layer=2 'columns=areacat
integer, pixval integer'
v.db.update map=$AREASVECT layer=2 column=areacat
qcolumn="cat"

# Step 1. Rasterize vector polygons and assign cat values
(one unique value) to each raster area
echo "Rasterising vector polygons"
v.to.rast input=$AREASVECT output=AREASRAST use=cat
type=area layer=1

# March region to AREASRAST
echo "Prepare region for analysis..."
g.remove MASK
g.region vect=$AREASVECT -p && echo "
                              ...matched to "$AREASVECT""

# Step 2. MASK-ing based on raster area(s) of interest and
grabbing pixel values through loop
echo "\nLooping over all areas of interest to grab pixel
values:"
# where "i" the "cat" value derived from VECTOR1
for i in $CATi; do
  echo "\nProcessing Area [$i] out of [$CATMAX]"
  # Step 2(a). Masking areas of interest
  echo "*Masking..."
  r.mapcalc MASK="if(AREASRAST==$i, 1, null())"
  # Step 2(b). Extract all unique pixel values for above
created MASK along with their x,y and grid coordinates
  echo "*Grabbing pixel values... (exporting in
Pixel_Values_"$RASTERSRC1"_$i.csv)"
  r.stats -nxg input=$RASTERSRC1,$RASTERSRC2 fs="|"
output=Pixel_Values_$i.csv
  g.remove MASK && echo "otherwise r.mapcalc will work on
it!"

done

echo "\nDone!"
echo "Order of output: grid coordinates, pixel column and
row, pixel value $RASTERSRC1, pixel value $RASTERSRC2"

*******************************************8

Is there maybe the interest to integrate this to
v.rast.stats ?

On Sun, 20 Jan 2008 21:43:01 -0800 (PST)
Hamish <hamish_b@yahoo.com> wrote:

G. Allegri wrote:
> About point 4), you can do it simply adding
(v.db.addcol) at the
> beginning of your job, then insert something like this
into the loop:
>
> echo "UPDATE (your_table) SET
pixval=(what_you_want_to_upload) WHERE
> FID=$i" | db.execute

fyi there is v.db.update which may be easier to use than
raw
db.execute.

Hamish