[GRASS-user] python parse_command

Anna Petrášová kratochanna at gmail.com
Mon Feb 19 06:02:22 PST 2018


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


More information about the grass-user mailing list