[GRASS-user] Can a table imported in the data base (sqlite.db) used to assign new raster category labels non-interactively?

Dear All,

I am having a hard time to figure out how I could avoid to enter
interactively new labels for a raster map. I've been reading the last 2
hours in the list, in GRASS-Book.

Any directions highly appreciated.

* A raster map (Global Land Cover 2000 over Europe) looks like this:

r.report glc2000_eu -h
100%
+-----------------------------------------------------------------------------+
| Category Information
|
| #|description
|
|-----------------------------------------------------------------------------|
| 2|
|
| 4|
|
| 6|
|
|11|
|
|12|
|
|16|
|
|20|
|
|22|
|
|23|
|
+-----------------------------------------------------------------------------+

* Importing the file "Global_Legend.dbf" which holds category labels in
the data base:
db.in.ogr dsn=Global_Legend.dbf out=glc2000_legend

*Looking at the imported table
db.select -c glc2000_legend
1|Tree Cover, broadleaved, evergreen
2|Tree Cover, broadleaved, deciduous, closed
3|Tree Cover, broadleaved, deciduous, open
4|Tree Cover, needle-leaved, evergreen
5|Tree Cover, needle-leaved, deciduous
6|Tree Cover, mixed leaf type
7|Tree Cover, regularly flooded, fresh water
8|Tree Cover, regularly flooded, saline water
9|Mosaic: Tree Cover / Other natural vegetation
10|Tree Cover, burnt
11|Shrub Cover, closed-open, evergreen
12|Shrub Cover, closed-open, deciduous
13|Herbaceous Cover, closed-open
14|Sparse herbaceous or sparse shrub cover
15|Regularly flooded shrub and/or herbaceous cover
16|Cultivated and managed areas
17|Mosaic: Cropland / Tree Cover / Other natural vegetation
18|Mosaic: Cropland / Shrub and/or grass cover
19|Bare Areas
20|Water Bodies
21|Snow and Ice
22|Artificial surfaces and associated areas
23|Irrigated Agriculture

Question: IS there a way to add these labels to the raster map
non-interactively?

Nikos Alexandris wrote:

I am having a hard time to figure out how I could avoid to enter
interactively new labels for a raster map. I've been reading the last 2
hours in the list, in GRASS-Book.

Any directions highly appreciated.

....

Question: IS there a way to add these labels to the raster map
non-interactively?

In GRASS 6.3 use 'r.category rules=filename'. See the help page.
(new option added in October; may not have filtered into all the docs
yet)

In GRASS 6.2 you can put a similar ASCII text file in the $MAPSET/cats/
dir, see the Programmers' manual or another map to get the format.

*Looking at the imported table
db.select -c glc2000_legend
1|Tree Cover, broadleaved, evergreen
2|Tree Cover, broadleaved, deciduous, closed
3|Tree Cover, broadleaved, deciduous, open
4|Tree Cover, needle-leaved, evergreen
5|Tree Cover, needle-leaved, deciduous
6|Tree Cover, mixed leaf type
7|Tree Cover, regularly flooded, fresh water
8|Tree Cover, regularly flooded, saline water
9|Mosaic: Tree Cover / Other natural vegetation
10|Tree Cover, burnt
11|Shrub Cover, closed-open, evergreen
12|Shrub Cover, closed-open, deciduous
13|Herbaceous Cover, closed-open
14|Sparse herbaceous or sparse shrub cover
15|Regularly flooded shrub and/or herbaceous cover
16|Cultivated and managed areas
17|Mosaic: Cropland / Tree Cover / Other natural vegetation
18|Mosaic: Cropland / Shrub and/or grass cover
19|Bare Areas
20|Water Bodies
21|Snow and Ice
22|Artificial surfaces and associated areas
23|Irrigated Agriculture

it is very close, just `tr '|' ':'` or `sed -e 's/|/:/'`.
Hopefully ":" in the label names won't cause a problem.

("TODO: Respect the fs= field separator setting for input rules.")

Also for GRASS 7 it would be nice to use a consistent fs for all modules,
instead of sometimes |,:,space, ...

Hamish

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

On Mon, 2008-03-17 at 22:42 -0700, Hamish wrote:

Nikos Alexandris wrote:
> I am having a hard time to figure out how I could avoid to enter
> interactively new labels for a raster map. I've been reading the last 2
> hours in the list, in GRASS-Book.
>
> Any directions highly appreciated.
....
> Question: IS there a way to add these labels to the raster map
> non-interactively?

In GRASS 6.3 use 'r.category rules=filename'. See the help page.
(new option added in October; may not have filtered into all the docs
yet)

Yep, this is what I was trying. A fragment from my history (yesterday
before posting) confirms my attempts - I really tried hard... :stuck_out_tongue_winking_eye:
history | grep r.cat
   95 r.cats help
   96 r.category help
   97 r.category
   98 r.category map=glc2000_eu cats=glc2000_legend fs=|
   99 r.category map=glc2000_eu cats=glc2000_legend fs="|"
  100 r.category map=glc2000_eu cats=glc2000_legend
  286 r.category
  290 r.cats map=glc2000_eu rules=`db.select glc2000_legend`
  294 r.cats map=glc2000_eu rules=`db.select -c glc2000_legend`
  296 r.cats map=glc2000_eu fs=| rules=`db.select -c glc2000_legend`
  302 r.cats map=glc2000_eu fs=| rules=`db.select -c glc2000_legend |
tr "|" ":"`
  304 r.cats map=glc2000_eu rules=`db.select -c glc2000_legend | tr "|"
":"`
  305 r.cats map=glc2000_eu rules=`db.select -c glc2000_legend`
  306 r.cats map=glc2000_eu fs="|" rules=`db.select -c glc2000_legend`
  307 r.cats map=glc2000_eu fs=| rules=`db.select -c glc2000_legend`
  308 r.cats map=glc2000_eu fs=| rules=`db.select -c glc2000_legend`
--v
  309 r.cats map=glc2000_eu fs=| rules=`db.select -c glc2000_legend`
--V
  310 r.cats --v map=glc2000_eu fs=| rules=`db.select -c
glc2000_legend`
  316 r.cats glc2000_eu

In GRASS 6.2 you can put a similar ASCII text file in the $MAPSET/cats/
dir, see the Programmers' manual or another map to get the format.

> *Looking at the imported table
> db.select -c glc2000_legend
> 1|Tree Cover, broadleaved, evergreen
> 2|Tree Cover, broadleaved, deciduous, closed
> 3|Tree Cover, broadleaved, deciduous, open
> 4|Tree Cover, needle-leaved, evergreen
> 5|Tree Cover, needle-leaved, deciduous
> 6|Tree Cover, mixed leaf type
> 7|Tree Cover, regularly flooded, fresh water
> 8|Tree Cover, regularly flooded, saline water
> 9|Mosaic: Tree Cover / Other natural vegetation
> 10|Tree Cover, burnt
> 11|Shrub Cover, closed-open, evergreen
> 12|Shrub Cover, closed-open, deciduous
> 13|Herbaceous Cover, closed-open
> 14|Sparse herbaceous or sparse shrub cover
> 15|Regularly flooded shrub and/or herbaceous cover
> 16|Cultivated and managed areas
> 17|Mosaic: Cropland / Tree Cover / Other natural vegetation
> 18|Mosaic: Cropland / Shrub and/or grass cover
> 19|Bare Areas
> 20|Water Bodies
> 21|Snow and Ice
> 22|Artificial surfaces and associated areas
> 23|Irrigated Agriculture

it is very close, just `tr '|' ':'` or `sed -e 's/|/:/'`.
Hopefully ":" in the label names won't cause a problem.

The result from both is the same:
r.category map=glc2000_eu rules=`db.select -c glc2000_legend | tr '|'
':'`

or

r.category map=glc2000_eu rules=`db.select -c glc2000_legend | sed -e
's/|/:/'`

output:
Sorry <Cover,> is not a valid option
Sorry <broadleaved,> is not a valid option
Sorry <evergreen> is not a valid option
Sorry <2:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <broadleaved,> is not a valid option
Sorry <deciduous,> is not a valid option
Sorry <closed> is not a valid option
Sorry <3:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <broadleaved,> is not a valid option
Sorry <deciduous,> is not a valid option
Sorry <open> is not a valid option
Sorry <4:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <needle-leaved,> is not a valid option
Sorry <evergreen> is not a valid option
Sorry <5:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <needle-leaved,> is not a valid option
Sorry <deciduous> is not a valid option
Sorry <6:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <mixed> is not a valid option
Sorry <leaf> is not a valid option
Sorry <type> is not a valid option
Sorry <7:Tree> is not a valid option
Sorry <Cover,> is not a valid option
Sorry <regularly> is not a valid option
Sorry <flooded,> is not a valid option
Sorry <fresh> is not a valid option
Sorry <water> is not a valid option
Sorry <8:Tree> is not a valid option

[...]

Finally it worked like this:

1. db.select -c glc2000_legend | tr '|' ':' > glc2000_legend.csv

2. r.category map=glc2000_eu rules=glc2000_legend.csv

("TODO: Respect the fs= field separator setting for input rules.")

Also for GRASS 7 it would be nice to use a consistent fs for all modules,
instead of sometimes |,:,space, ...

Why not directly from the db.table(s)

Given that the category numbers and labels are there as they should be
(in a table imported in the data base), wouldn't it be nice to enable
attachment of such tables in a raster map?

Hamish

Cheers and THANKS!!!

Nikos

> Nikos Alexandris wrote:
> > I am having a hard time to figure out how I could avoid to enter
> > interactively new labels for a raster map.
> > ....
> > Question: IS there a way to add these labels to the raster map
> > non-interactively?
>

Hamish:

> In GRASS 6.3 use 'r.category rules=filename'. See the help page.
> (new option added in October; may not have filtered into all the docs
> yet)

N:

> > *Looking at the imported table
> > db.select -c glc2000_legend
> > 1|Tree Cover, broadleaved, evergreen
> > 2|Tree Cover, broadleaved, deciduous, closed
> > 3|Tree Cover, broadleaved, deciduous, open

[...]
H:

> it is very close, just `tr '|' ':'` or `sed -e 's/|/:/'`.
> Hopefully ":" in the label names won't cause a problem.

The result from both is the same:
r.category map=glc2000_eu rules=`db.select -c glc2000_legend | tr '|'
':'`
or
r.category map=glc2000_eu rules=`db.select -c glc2000_legend | sed -e
's/|/:/'`

output:
Sorry <Cover,> is not a valid option
Sorry <broadleaved,> is not a valid option
Sorry <evergreen> is not a valid option
Sorry <2:Tree> is not a valid option

[...]

Finally it worked like this:

1. db.select -c glc2000_legend | tr '|' ':' > glc2000_legend.csv

2. r.category map=glc2000_eu rules=glc2000_legend.csv

Right, it wants a filename or "-" for the rules= option:
   rules File containing category label rules (or "-" to read from
stdin)

it was thinking your DB dump was a very large filename....

using stdin you could have done like:
  db.select -c glc2000_legend fs=: | r.category map=glc2000_eu rules=-

> ("TODO: Respect the fs= field separator setting for input rules.")
>
> Also for GRASS 7 it would be nice to use a consistent fs for all
> modules, instead of sometimes |,:,space, ...

Why not directly from the db.table(s)

Given that the category numbers and labels are there as they should be
(in a table imported in the data base), wouldn't it be nice to enable
attachment of such tables in a raster map?

see
http://grass.gdf-hannover.de/wiki/GRASS_SoC_Ideas_2008#Raster_Database

Cheers and THANKS!!!

no worries, this new r.cats label management option isn't well tested
yet, I'm glad someone is trying it out. In the past you had to use
r.support or hack the file by hand.

Hamish

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

On Tue, 2008-03-18 at 05:46 -0700, Hamish wrote:

> Finally it worked like this:
>
> 1. db.select -c glc2000_legend | tr '|' ':' > glc2000_legend.csv
>
> 2. r.category map=glc2000_eu rules=glc2000_legend.csv

Right, it wants a filename or "-" for the rules= option:
   rules File containing category label rules (or "-" to read from
stdin)

it was thinking your DB dump was a very large filename....

using stdin you could have done like:
  db.select -c glc2000_legend fs=: | r.category map=glc2000_eu rules=-

Great! I didn't really understand the use of "-" before.

> > ("TODO: Respect the fs= field separator setting for input rules.")
> >
> > Also for GRASS 7 it would be nice to use a consistent fs for all
> > modules, instead of sometimes |,:,space, ...
>
> Why not directly from the db.table(s)
>
> Given that the category numbers and labels are there as they should
be
> (in a table imported in the data base), wouldn't it be nice to
enable
> attachment of such tables in a raster map?

see

http://grass.gdf-hannover.de/wiki/GRASS_SoC_Ideas_2008#Raster_Database

Got it! Unfortunately my know-how stops here ;-(