[GRASS-user] python parse_command

Dear list,
I am struggling with my python code.
I am trying to get the computational region of a xyz-file. If I use
r.in.xyz in GRASS itself, it works all just fine, but using it in python
I always get an error.
Here is my code:

    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

    with Session(gisdb="/home/jreith/grassdata",
    location="nrw",mapset="elevation", create_opts="EPSG:4326"):
    compregion = gcore.parse_command('r.in_xyz',
    input="/geodaten/dgm1.xyz", flags='s',
    output="new_file",separator='space')

The error:

    Traceback (most recent call last):
     File "grass_scripts/grass_dem.py", line 32, in <module>
     compregion = gcore.parse_command('r.in_xyz',
    input="/geodaten/dgm1.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 appreciate any help and hope I made myself clear.

best regards
Jonathan

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:

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

Isn’t the problem that you have there r.in_xyz instead of r.in.xyz?

Anna

···

On Feb 12, 2018 9:35 AM, “Pietro” <peter.zamb@gmail.com> wrote:

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:

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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

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’)

···

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:

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

On Mon, Feb 19, 2018 at 4:36 AM, Jonathan Reith <reith@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 :slight_smile:

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:

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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

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:

···

Am 19.02.2018 um 15:02 schrieb Anna Petrášová:

On Mon, Feb 19, 2018 at 4:36 AM, Jonathan Reith [<reith@mundialis.de>](mailto:reith@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@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)

Thank you Anna I did not noticed the underscor…

you are hassling the dictionary to a region, but you have to unpack the dictionary just write:

g.region(res=“1”, flags=“p”, **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

···

On 19 Feb 2018 4:41 p.m., “Jonathan Reith” <reith@mundialis.de> wrote:

print(compregion)
g.region(compregion,res=“1”, flags=“p”)

On Mon, Feb 19, 2018 at 6:49 PM, Pietro <peter.zamb@gmail.com> wrote:

On 19 Feb 2018 4:41 p.m., "Jonathan Reith" <reith@mundialis.de> wrote:

  print(compregion)
  g.region(compregion,res="1", flags="p")

you are hassling the dictionary to a region, but you have to unpack the
dictionary just write:

...

g.region(res="1", flags="p", **compregion)

the two * unpack the dictionary.

Thanks for the hint: I have turned it into a Wiki snippet at
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Example_for_parsing_the_region_output_of_a_module

Please update directly there if needed.

Markus

Ok. So now my code is:

compregion = grass.parse_command(“r.in.xyz”, input=xyz, separator=“space”, flags=“sg”, output=“bbox”,parse=(grass.parse_key_val, {‘sep’: ‘=’}))
print(compregion)
g.region(res=“1”, flags=“p”, **compregion)

and the output:

{u’n’: u’5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46’}
ERROR: Invalid input <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
t=206.46>
Traceback (most recent call last):
File “grass_scripts/error_script.py”, line 40, in
main()
File “grass_scripts/error_script.py”, line 38, in main
g.region(res=“1”, flags=“p”, **compregion)
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 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.

···

Am 19.02.2018 um 18:49 schrieb Pietro:

Thank you Anna I did not noticed the underscor…

On 19 Feb 2018 4:41 p.m., “Jonathan Reith” <reith@mundialis.de> wrote:

print(compregion)
g.region(compregion,res=“1”, flags=“p”)

you are hassling the dictionary to a region, but you have to unpack the dictionary just write:

g.region(res=“1”, flags=“p”, **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

-- 
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: [www.mundialis.de](http://www.mundialis.de)

Would

parse=(grass.parse_key_val, {'sep': '=', 'vsep': ' '}))

help?

On Mon, Feb 26, 2018 at 4:53 AM, Jonathan Reith <reith@mundialis.de> wrote:

Ok. So now my code is:

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

and the output:

{u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}
ERROR: Invalid input <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
       t=206.46>
Traceback (most recent call last):
  File "grass_scripts/error_script.py", line 40, in <module>
    main()
  File "grass_scripts/error_script.py", line 38, in main
    g.region(res="1", flags="p", **compregion)
  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 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.
#########################

It seems like my last error disappeared, but now I have a new one.

running < g.region n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
t=206.46 res=1 -p > from command line in GRASS works just fine.

Any help appreciated

Jonathan

Am 19.02.2018 um 18:49 schrieb Pietro:

Thank you Anna I did not noticed the underscor...

On 19 Feb 2018 4:41 p.m., "Jonathan Reith" <reith@mundialis.de> wrote:

  print(compregion)
  g.region(compregion,res="1", flags="p")

you are hassling the dictionary to a region, but you have to unpack the
dictionary just write:

...

g.region(res="1", flags="p", **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

Nope, still the same error

Am 26.02.2018 um 15:35 schrieb Anna Petrášová:

Would

parse=(grass.parse_key_val, {'sep': '=', 'vsep': ' '}))

help?

On Mon, Feb 26, 2018 at 4:53 AM, Jonathan Reith <reith@mundialis.de> wrote:

Ok. So now my code is:

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

and the output:

{u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}
ERROR: Invalid input <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
       t=206.46>
Traceback (most recent call last):
  File "grass_scripts/error_script.py", line 40, in <module>
    main()
  File "grass_scripts/error_script.py", line 38, in main
    g.region(res="1", flags="p", **compregion)
  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 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.
#########################

It seems like my last error disappeared, but now I have a new one.

running < g.region n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
t=206.46 res=1 -p > from command line in GRASS works just fine.

Any help appreciated

Jonathan

Am 19.02.2018 um 18:49 schrieb Pietro:

Thank you Anna I did not noticed the underscor...

On 19 Feb 2018 4:41 p.m., "Jonathan Reith" <reith@mundialis.de> wrote:

  print(compregion)
  g.region(compregion,res="1", flags="p")

you are hassling the dictionary to a region, but you have to unpack the
dictionary just write:

...

g.region(res="1", flags="p", **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

When I try this:

grass.parse_key_val('n=676 s=887 e=88 w=4', sep='=')

I get:

{u'n': u'676 s=887 e=88 w=4'}

which is what you are getting. But with adding space as vsep, I get
correct results, so it should work. Are you sure you used space there?

grass.parse_key_val('n=676 s=887 e=88 w=4', sep='=', vsep=' ')

{u's': u'887', u'e': u'88', u'w': u'4', u'n': u'676'}

On Mon, Feb 26, 2018 at 9:39 AM, Jonathan Reith <reith@mundialis.de> wrote:

Nope, still the same error

Am 26.02.2018 um 15:35 schrieb Anna Petrášová:

Would

parse=(grass.parse_key_val, {'sep': '=', 'vsep': ' '}))

help?

On Mon, Feb 26, 2018 at 4:53 AM, Jonathan Reith <reith@mundialis.de> wrote:

Ok. So now my code is:

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

and the output:

{u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}
ERROR: Invalid input <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
       t=206.46>
Traceback (most recent call last):
  File "grass_scripts/error_script.py", line 40, in <module>
    main()
  File "grass_scripts/error_script.py", line 38, in main
    g.region(res="1", flags="p", **compregion)
  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 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.
#########################

It seems like my last error disappeared, but now I have a new one.

running < g.region n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
t=206.46 res=1 -p > from command line in GRASS works just fine.

Any help appreciated

Jonathan

Am 19.02.2018 um 18:49 schrieb Pietro:

Thank you Anna I did not noticed the underscor...

On 19 Feb 2018 4:41 p.m., "Jonathan Reith" <reith@mundialis.de> wrote:

  print(compregion)
  g.region(compregion,res="1", flags="p")

you are hassling the dictionary to a region, but you have to unpack the
dictionary just write:

...

g.region(res="1", flags="p", **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

Ok, now it works. Thanks a lot. My test-file made some problems, but now
it finally works :slight_smile:

Am 26.02.2018 um 15:42 schrieb Anna Petrášová:

When I try this:

grass.parse_key_val('n=676 s=887 e=88 w=4', sep='=')

I get:

{u'n': u'676 s=887 e=88 w=4'}

which is what you are getting. But with adding space as vsep, I get
correct results, so it should work. Are you sure you used space there?

grass.parse_key_val('n=676 s=887 e=88 w=4', sep='=', vsep=' ')

{u's': u'887', u'e': u'88', u'w': u'4', u'n': u'676'}

On Mon, Feb 26, 2018 at 9:39 AM, Jonathan Reith <reith@mundialis.de> wrote:

Nope, still the same error

Am 26.02.2018 um 15:35 schrieb Anna Petrášová:

Would

parse=(grass.parse_key_val, {'sep': '=', 'vsep': ' '}))

help?

On Mon, Feb 26, 2018 at 4:53 AM, Jonathan Reith <reith@mundialis.de> wrote:

Ok. So now my code is:

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

and the output:

{u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46'}
ERROR: Invalid input <n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
       t=206.46>
Traceback (most recent call last):
  File "grass_scripts/error_script.py", line 40, in <module>
    main()
  File "grass_scripts/error_script.py", line 38, in main
    g.region(res="1", flags="p", **compregion)
  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 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.
#########################

It seems like my last error disappeared, but now I have a new one.

running < g.region n=5611999 s=5610000 e=32361999 w=32360000 b=159.04
t=206.46 res=1 -p > from command line in GRASS works just fine.

Any help appreciated

Jonathan

Am 19.02.2018 um 18:49 schrieb Pietro:

Thank you Anna I did not noticed the underscor...

On 19 Feb 2018 4:41 p.m., "Jonathan Reith" <reith@mundialis.de> wrote:

  print(compregion)
  g.region(compregion,res="1", flags="p")

you are hassling the dictionary to a region, but you have to unpack the
dictionary just write:

...

g.region(res="1", flags="p", **compregion)

the two * unpack the dictionary.

Have fun!

Pietro

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

--
Studentische Hilfskraft
mundialis GmbH & Co. KG
Firmensitz: Kölnstraße 99, 53111 Bonn
Registergericht: Amtsgericht Bonn, HRA 8528

vertreten durch: mundialis Verwaltungsgesellschaft mbH
Geschäftsführer:
Dr. rer. nat. Markus Neteler
Dipl.-Geogr. Hinrich Paulsen
Dipl.-Geogr. Till Adams

Tel. +49 (0)228 - 38 75 80 - 80
Fax. +49 (0)228 - 96 28 99 57
info [at] mundialis [dot] de

Internet: www.mundialis.de

On Mon, Feb 26, 2018 at 3:49 PM, Jonathan Reith <reith@mundialis.de> wrote:

Ok, now it works. Thanks a lot. My test-file made some problems, but now
it finally works :slight_smile:

Great!

I have updated the Wiki code snippet to a working example
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Example_for_parsing_the_region_output_of_a_module

Thanks for the support to Pietro and Anna!

Markus