[GRASS-user] python parse_command

Jonathan Reith reith at mundialis.de
Mon Feb 19 07:41:21 PST 2018


Ok, now I understood the problem, thanks a lot Anna

But I'm still struggling with the parse_command and r.in.xyz

code-example:

      compregion = grass.parse_command("r.in.xyz",input="tmp.xyz",
    separator="space", flags="sg", output="bbox",
    parse=(grass.parse_key_val,
                                                {'sep': '='}))
      print(compregion)
      g.region(compregion,res="1", flags="p")

output:

    {u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}
    Traceback (most recent call last):
      File "grass_scripts/rinopennrw.py", line 67, in <module>
        g.region(compregion,res="1", flags="p")
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
    line 626, in __call__
        param.value = arg
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/parameter.py",
    line 184, in _set_value
        self._value, self._rawvalue = _check_value(self, value)
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/parameter.py",
    line 72, in _check_value
        check_string(value)
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/parameter.py",
    line 42, in check_string
        raise ValueError(msg % (param.name, type(value), value))
    ValueError: The Parameter <region> require a string, <class
    'grass.script.utils.KeyValue'> instead is provided: {u'n': u'5611999
    s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}

##########

The computional region is not correctly parsed. This part seems wrong:
...u'n': u'5611999...

I do not know where the < u' > is coming from and why it behaves like that.

I did the same command in GRASS itself and it looked like this:

>>> r.in.xyz -sg input=dgm1_05314000_Bonn_EPSG4647_XYZ/tmp.xyz
output=bla sep=space
n=5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46

Manually changing the compregion to the GRASS region i got the output
like this:

    WARNING: Illegal filename <n=5611999 s=5610000 e=32361999 w=32360000
             b=159.04 t=206.46>. Character <=> not allowed.
    ERROR: Region <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
    t=206.46>
           not found
    Traceback (most recent call last):
      File "grass_scripts/rinopennrw.py", line 68, in <module>
        g.region(compregion,res="1", flags="p")
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
    line 648, in __call__
        return self.run()
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
    line 769, in run
        self.wait()
      File
    "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
    line 790, in wait
        module=self.name, errors=stderr)
    grass.exceptions.CalledModuleError: Module run g.region g.region
    region=n=5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46
    res=1 -p ended with error
    Process ended with non-zero return code 1. See errors in the (error)
    output.

#############

This leads to the question, how the input for g.region should look like
in python?

Again, any help is appreciated :)

best regards


Am 19.02.2018 um 15:02 schrieb Anna Petrášová:
> On Mon, Feb 19, 2018 at 4:36 AM, Jonathan Reith <reith at mundialis.de> wrote:
>> thanks for the answers, but this did not work.
>>
>> Again, here is my code, this time not just the minimum version:
>>
>> #!/usr/bin/env python
>> import os
>> from grass_session import Session
>> from grass.script import core as gcore
>>
>> from grass.pygrass.modules.shortcuts import general as g
>> from grass.pygrass.modules.shortcuts import raster as r
>>
>> home= "/home/jreith/grassdata/nrw"
>> gisdb= "/home/jreith/grassdata"
>> loc= "nrw"
>> maps= "elevation"
>> epsg= "EPSG:4647"
>> xyz= "/home/jreith/geodaten/dgm1_05314000_Bonn_EPSG4647_XYZ/tmp.xyz"
>>
>> if not os.path.exists(gisdb):
>>     os.makedirs(gisdb)
>>
>> if not os.path.exists(os.path.join(gisdb, loc)):
>>     with Session(gisdb=gisdb, location=loc, create_opts=epsg):
>>         print("Created a new location!")
>> else:
>>     print("Location already exist!")
>>
>> with open(xyz, "r") as t:
>>     print(t.read())
>>
>> with Session(gisdb=gisdb, location=loc, mapset="elevation", create_opts=""):
>>     print("r.in_xyz works:")
>>     r.in_xyz(input=xyz, output="new_file", flags="s",separator="space")
>>     gisenvironment = gcore.parse_command("g.gisenv", flags="s")
>>     print(gisenvironment)
>>     compregion = gcore.parse_command('r.in_xyz', input=xyz, flags='s',
>> output="new_file",separator='space')
>> ##################
>
> You have to use r.in.xyz with gcore.parse_command as I suggested
> earlier. You are now using 2 different APIs, PyGRASS (where you use
> r.in_xyz) and Python Scripting Library (where you must use the real
> name of the command).
>
> Anna
>
>> This is my output:
>>
>> GRASSBIN: grass74
>> GISBASE: /home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu
>> Location already exist!
>> 32360000.00 5610000.00 170.55
>> 32360000.00 5610001.00 170.56
>> 32360000.00 5610002.00 170.58
>> 32360000.00 5610003.00 170.55
>> 32360000.00 5610004.00 170.58
>>
>>
>>
>> r.in_xyz works:
>> Range:     min         max
>> x:    32360000    32360000
>> y:     5610000     5610004
>> z:      170.55      170.58
>> {u'MAPSET': u"'elevation';", u'GISDBASE': u"'/home/jreith/grassdata';",
>> u'LOCATION_NAME': u"'nrw';"}
>> Traceback (most recent call last):
>>   File "grass_scripts/error_script.py", line 33, in <module>
>>     compregion = gcore.parse_command('r.in_xyz', input=xyz, flags='s',
>> output="new_file",separator='space')
>>   File
>> "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
>> line 516, in parse_command
>>     res = read_command(*args, **kwargs)
>>   File
>> "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
>> line 471, in read_command
>>     process = pipe_command(*args, **kwargs)
>>   File
>> "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
>> line 444, in pipe_command
>>     return start_command(*args, **kwargs)
>>   File
>> "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
>> line 380, in start_command
>>     return Popen(args, **popts)
>>   File
>> "/home/jreith/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
>> line 74, in __init__
>>     subprocess.Popen.__init__(self, args, **kwargs)
>>   File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
>>     errread, errwrite)
>>   File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
>>     raise child_exception
>> OSError: [Errno 2] No such file or directory
>> ###########################
>>
>> I used a minimum example for the xyz-file. As you can see, there is no
>> problem with the location or database.
>>
>> r.in_xyz is correct and also works itself. I also can parse the g.gisenv to
>> a variable and print it.
>>
>> The problem is, that I cannot parse r.in_xyz to a new variable.
>>
>> Maybe you have some hints for me :)
>>
>> best regards
>>
>> Jonathan
>>
>>
>> Am 12.02.2018 um 15:35 schrieb Pietro:
>>
>> Dear Jonathan,
>>
>> the error is due to grass_session that is not creating the location if
>> missing.
>> I don't have time in this day to fix this issue in grass_session, so the
>> fastest fsolution at the momenth is to check and create what is needed step
>> by step.
>>
>> I did not have xyz file to test so I've only execute g.gisenv and it works,
>> let me know if it works also with r.inxyz:
>>
>> ```python
>> from __future__ import print_function
>> import os
>> from grass_session import Session
>> from grass.script import core as gcore
>>
>> GISDBASE = "/tmp/grassdata"
>> LOCATION = "nrw"
>> EPSG = "EPSG:4326"
>>
>>
>> if not os.path.exists(GISDBASE):
>>     os.makedirs(GISDBASE)
>>
>> if not os.path.exists(os.path.join(GISDBASE, LOCATION)):
>>     with Session(gisdb=GISDBASE, location=LOCATION,
>>                  create_opts=EPSG):
>>         print("Created a new location!")
>> else:
>>     print("Location already exist!")
>>
>>
>> with Session(gisdb=GISDBASE, location=LOCATION, mapset="elevation",
>>              create_opts=""):
>>     gcore.run_command("g.gisenv")
>> ```
>>
>> Best regards
>>
>> Pietro
>>
>>
>>
>> _______________________________________________
>> grass-user mailing list
>> grass-user at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/grass-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20180219/51319643/attachment-0001.html>


More information about the grass-user mailing list