<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Ok, now I understood the problem, thanks a lot Anna<br>
    </p>
    <p>But I'm still struggling with the parse_command and r.in.xyz</p>
    <p>code-example:<br>
    </p>
    <blockquote>
      <p>  compregion = grass.parse_command("r.in.xyz",input="tmp.xyz",
        separator="space", flags="sg", output="bbox",
        parse=(grass.parse_key_val,<br>
                                                    {'sep': '='}))<br>
          print(compregion)<br>
          g.region(compregion,res="1", flags="p")</p>
    </blockquote>
    <p>output:</p>
    <blockquote>
      <p>{u'n': u'5611999 s=5610000 e=32361999 w=32360000 b=159.04
        t=206.46'}<br>
        Traceback (most recent call last):<br>
          File "grass_scripts/rinopennrw.py", line 67, in <module><br>
            g.region(compregion,res="1", flags="p")<br>
          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__<br>
            param.value = arg<br>
          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<br>
            self._value, self._rawvalue = _check_value(self, value)<br>
          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<br>
            check_string(value)<br>
          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<br>
            raise ValueError(msg % (param.name, type(value), value))<br>
        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'}</p>
    </blockquote>
    <p>##########<br>
    </p>
    <p>The computional region is not correctly parsed. This part seems
      wrong: ...u'n': u'5611999...</p>
    <p>I do not know where the < u' > is coming from and why it
      behaves like that.</p>
    <p>I did the same command in GRASS itself and it looked like this:<br>
    </p>
    <p>>>> r.in.xyz -sg
      input=dgm1_05314000_Bonn_EPSG4647_XYZ/tmp.xyz output=bla sep=space<br>
      n=5611999 s=5610000 e=32361999 w=32360000 b=159.04 t=206.46</p>
    <p>Manually changing the compregion to the GRASS region i got the
      output like this:</p>
    <blockquote>
      <p>WARNING: Illegal filename <n=5611999 s=5610000 e=32361999
        w=32360000<br>
                 b=159.04 t=206.46>. Character <=> not allowed.<br>
        ERROR: Region <n=5611999 s=5610000 e=32361999 w=32360000
        b=159.04 t=206.46><br>
               not found<br>
        Traceback (most recent call last):<br>
          File "grass_scripts/rinopennrw.py", line 68, in <module><br>
            g.region(compregion,res="1", flags="p")<br>
          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__<br>
            return self.run()<br>
          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<br>
            self.wait()<br>
          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<br>
            module=self.name, errors=stderr)<br>
        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<br>
        Process ended with non-zero return code 1. See errors in the
        (error) output.</p>
    </blockquote>
    <p>#############</p>
    <p>This leads to the question, how the input for g.region should
      look like in python?<br>
    </p>
    <p>Again, any help is appreciated :)</p>
    <p>best regards<br>
    </p>
    <p><br>
    </p>
    <div class="moz-cite-prefix">Am 19.02.2018 um 15:02 schrieb Anna
      Petrášová:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAE0EDEoOzh2K6vOKTGHd=dL9hx13Dw_zc643By_o7_pS-ZKZMw@mail.gmail.com">
      <pre wrap="">On Mon, Feb 19, 2018 at 4:36 AM, Jonathan Reith <a class="moz-txt-link-rfc2396E" href="mailto:reith@mundialis.de"><reith@mundialis.de></a> wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">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')
##################
</pre>
      </blockquote>
      <pre wrap="">

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

</pre>
      <blockquote type="cite">
        <pre wrap="">
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
<a class="moz-txt-link-abbreviated" href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/grass-user">https://lists.osgeo.org/mailman/listinfo/grass-user</a>
</pre>
      </blockquote>
    </blockquote>
  </body>
</html>