[GRASS-dev] how to do g.region n=n+(defined by user input option) in a python script?

hi,

I'm trying to understand how to use g.region n=n+x s=s-x e=e+x w=w-x in a
python script, where x is a user option.

some code snippets:

#%option
#% key: region_extension
#% type: double
#% key_desc: float
#% description: region extension
#% required : no
#% answer: 5000
#%end

[...]

    regext = options['region_extension']

[...]

    grass.run_command('g.region', n = n+regext,
                                     s = s-regext,
                                     e = e+regext,
                                     w = w-regext)

doesn't work with following error:

Traceback (most recent call last):
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 202, in <module>
    sys.exit(main())
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 159, in main
    grass.run_command('g.region', n = n+regext,
NameError: global name 'n' is not defined

with
    current_region = grass.region()
n = current_region["n"]
s = current_region["s"]
e = current_region["e"]
w = current_region["w"]

it could be defined, but then it says

Traceback (most recent call last):
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 202, in <module>
    sys.exit(main())
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 159, in main
    grass.run_command('g.region', n = n+regext,
TypeError: unsupported operand type(s) for +: 'float' and
'str'

any ideas/pointers?

thanks.

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/how-to-do-g-region-n-n-defined-by-user-input-option-in-a-python-script-tp5139216.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

Hi,

2014-05-08 13:39 GMT+02:00 Helmut Kudrnovsky <hellik@web.de>:

hi,

I'm trying to understand how to use g.region n=n+x s=s-x e=e+x w=w-x in a
python script, where x is a user option.

some code snippets:

#%option
#% key: region_extension
#% type: double
#% key_desc: float
#% description: region extension
#% required : no
#% answer: 5000
#%end

this would be useful option for g.region (already thinking about
that), unfortunately this modules has so much flags and options...

[...]

    regext = options['region_extension']

[...]

    grass.run_command('g.region', n = n+regext,
                                     s = s-regext,
                                     e = e+regext,
                                     w = w-regext)

n = 'n+%f' % regext

Martin

Helmut Kudrnovsky wrote:

   regext = options['region_extension']

with
    current_region = grass.region()
n = current_region["n"]
s = current_region["s"]
e = current_region["e"]
w = current_region["w"]

it could be defined, but then it says

Traceback (most recent call last):
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 202, in <module>
    sys.exit(main())
  File "C:\Users\myricaria\AppData\Roaming\GRASS7\addons\scr
ipts\v.habitat.dem.py", line 159, in main
    grass.run_command('g.region', n = n+regext,
TypeError: unsupported operand type(s) for +: 'float' and
'str'

"regext" will be a string; you need to use e.g.

  regext = float(regext)

before you can do arithmetic with it.

Also, unless the purpose of the script is to modify the region for
subsequent commands, either use use_temp_region() to localise any
changes to the script.

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

Hi,

please consider to add best practice to

http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Parsing_the_options_and_flags

thanks
Markus

"regext" will be a string; you need to use e.g.

       regext = float(regext)

before you can do arithmetic with it.

ah, I see it now.

Also, unless the purpose of the script is to modify the region for
subsequent commands, either use use_temp_region() to localise any
changes to the script.

I'll have a look at this option. thanks.

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/how-to-do-g-region-n-n-defined-by-user-input-option-in-a-python-script-tp5139216p5139310.html
Sent from the Grass - Dev mailing list archive at Nabble.com.