[GRASS-dev] [bug #4742] (grass) i.fusion.brovey, i.in.spotvgt, v.rast.stats: don't use g.region

Glynn Clements glynn at gclements.plus.com
Tue Jun 27 12:00:02 EDT 2006


Hamish wrote:

> > > > this bug's URL: http://intevation.de/rt/webrt?serial_num=4742
> > > > -----------------------------------------------------------------
> > > > 
> > > > Subject: i.fusion.brovey, i.in.spotvgt, v.rast.stats: don't use
> > > > g.region
> > > > 
> > > > Glynn wrote on the grass-dev:
> > > > 
> > > > > scripts shouldn't modify the WIND file unless persistently
> > > > > changing the current region is the script's intended function.
> > > > 
> > > > > use either WIND_OVERRIDE or GRASS_REGION (the latter is cleaner,
> > > > > but the former is probably easier).
> 
> in practice WIND_OVERRIDE is actually more work? see below.
> 
> 
> Markus:
> > i.in.spotvgt: it is an import script. The GRASS import commands are
> >  expected to import a full map at original resolution. The internal
> >  bit shuffling must therefore be done at the original map region
> >  and resolution. The script saves the current user region and restores
> >  it back.
> 
> (guessed) current region usage:
> 
> g.region save=region.save
> g.region rast=$input
> do_stuff()
> g.region region.save
> g.remove region=region.save
> 
> same thing, using WIND_OVERRIDE:
> 
> g.region save=region.save
> g.region rast=$input
> g.region save=region.tmp
> g.region region.save
> WIND_OVERRIDE=region.tmp
> export WIND_OVERRIDE
> do_stuff()
> unset WIND_OVERRIDE   # unneeded if script exits, but good hygiene
> g.remove region=region.save,region.tmp

You don't need to touch the current region at all. Just:

	g.region rast=$input save=region.tmp.$$
	WIND_OVERRIDE=region.tmp.$$
	export WIND_OVERRIDE
	do_stuff()
	g.remove region=region.tmp.$$

If do_stuff() is a single command, you could use a temporary
environment binding, i.e.:

	g.region rast=$input save=region.tmp.$$
	WIND_OVERRIDE=region.tmp.$$ do_stuff()
	g.remove region=region.tmp.$$

> For C (and SWIG) modules, I think we need a lib fn like
> 
> char * G_window_string(cellhd); 
> 
> (any better G_() name?)
> 
> that will form a GRASS_REGION string from a cell-head structure.
> maybe check that it is valid also (rows*nsres=ns_extent etc)??
> 
> with scripts I guess you could do
> 
> eval `g.region -g`
> GRASS_REGION="north:$n;south:$s;..."
>  or
> 
> ORIGINAL_REGION=`g.region -p | tr '\n' ';' | sed -e 's/ //g' -e 's/;$//'`
> RES=50
> GRASS_REGION=`echo "$ORIGINAL_REGION" | sed -e "s/nsres:[^;]/nsres:$RES/"`
> 
> in any case I think (with error checking) we should favour GRASS_REGION
> over WIND_OVERRIDE as a cleaner solution. With a C lib fn it shouldn't
> be any more work to create.

GRASS_REGION is cleaner in that you don't have to worry about:

a) choosing a region name which doesn't conflict with any existing
region, or
b) the saved region being left behind if the script terminates
prematurely.

It's currently more effort to use GRASS_REGION from within a shell
script; extending g.region to allow output in GRASS_REGION format
would eliminate that issue; you could just do e.g.:

	GRASS_REGION=`g.region -e rast=$input`

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list