Hello,
I'm trying to use GRASS via Python and import some rasters into numpy. I'm using garray.read(raster) and the result is always 0. When I inspect the raster in GRASS (via bash) it is not zero. I can re-create this with the following MWE. Can someone suggest what I might be doing wrong or if this is a bug?
Thank you,
-k.
import numpy as np
import shutil
import os
from grass_session import Session
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules.shortcuts import raster as r
import grass.script.array as garray
if os.path.exists("G_test"): # reset everything
shutil.rmtree("G_test", ignore_errors=False, onerror=None)
W = Session()
W.open(gisdb='.', location='G_test', create_opts="EPSG:3413")
g.region(w=0, s=0, n=100, e=100)
r.mapcalc("foo = 42.24")
foo = garray.array().read("foo", null=np.nan)
W.close()
print(np.max(foo))
# 0.0 is
Hi Ken,
I have no experience with garray, but raster2numpy function in pygrass worked quite well for me:
https://grass.osgeo.org/grass78/manuals/libpython/pygrass.raster.html#pygrass.raster.raster2numpy
Cheers
Stefan
-----Original Message-----
From: grass-user <grass-user-bounces@lists.osgeo.org> On Behalf Of Ken Mankoff
Sent: søndag 15. mars 2020 16:36
To: GRASS user list <grass-user@lists.osgeo.org>
Subject: [GRASS-user] garray.read() returns 0-filled array
Hello,
I'm trying to use GRASS via Python and import some rasters into numpy. I'm using garray.read(raster) and the result is always 0. When I inspect the raster in GRASS (via bash) it is not zero. I can re-create this with the following MWE. Can someone suggest what I might be doing wrong or if this is a bug?
Thank you,
-k.
import numpy as np
import shutil
import os
from grass_session import Session
from grass.pygrass.modules.shortcuts import general as g from grass.pygrass.modules.shortcuts import raster as r import grass.script.array as garray
if os.path.exists("G_test"): # reset everything
shutil.rmtree("G_test", ignore_errors=False, onerror=None)
W = Session()
W.open(gisdb='.', location='G_test', create_opts="EPSG:3413") g.region(w=0, s=0, n=100, e=100) r.mapcalc("foo = 42.24") foo = garray.array().read("foo", null=np.nan)
W.close()
print(np.max(foo))
# 0.0 is
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user
On 2020-03-15 at 15:11 -07, Stefan Blumentrath <Stefan.Blumentrath@nina.no> wrote...
I have no experience with garray, but raster2numpy function in pygrass
worked quite well for me:
https://grass.osgeo.org/grass78/manuals/libpython/pygrass.raster.html#pygrass.raster.raster2numpy
Thank you for this suggestion. This works but raises new issues.
1) Fully external-to-GRASS interactions works better for me. This solution requires running Python from within GRASS, which adds an extra layer, which makes it harder to get to the same place interactively in Python and debug.
2) Worse, it seems I need to run the $GRASS_PYTHON version which is /usr/bin/python2.7, so I no longer have access to my scientific Python stack.
The 1st issue can be worked around. The 2nd not so much. I hope I'm mis-using the library and can do this from within my Anaconda Python.
-k.
Hei Ken,
With this PR:
https://github.com/zarch/grass-session/pull/14
applied, also pygrass modules should work as usual with grass-session.
I just updated it, so it needs testing...
If you define the GRASS_PYTHON environment variable, it should be possible to use any Python installation. If these are compatible is a different question...
GRASS GIS 7.8 should use Python 3, so I am surprised to see that your $GRASS_PYTHON version is /usr/bin/python2.7 ... That is possibly also a source for your trouble with garray...
Cheers
Stefan
-----Original Message-----
From: Ken Mankoff <mankoff@gmail.com>
Sent: mandag 16. mars 2020 01:14
To: Stefan Blumentrath <Stefan.Blumentrath@nina.no>
Cc: GRASS user list <grass-user@lists.osgeo.org>
Subject: Re: [GRASS-user] garray.read() returns 0-filled array
On 2020-03-15 at 15:11 -07, Stefan Blumentrath <Stefan.Blumentrath@nina.no> wrote...
I have no experience with garray, but raster2numpy function in pygrass
worked quite well for me:
https://grass.osgeo.org/grass78/manuals/libpython/pygrass.raster.html#
pygrass.raster.raster2numpy
Thank you for this suggestion. This works but raises new issues.
1) Fully external-to-GRASS interactions works better for me. This solution requires running Python from within GRASS, which adds an extra layer, which makes it harder to get to the same place interactively in Python and debug.
2) Worse, it seems I need to run the $GRASS_PYTHON version which is /usr/bin/python2.7, so I no longer have access to my scientific Python stack.
The 1st issue can be worked around. The 2nd not so much. I hope I'm mis-using the library and can do this from within my Anaconda Python.
-k.
Hi Stefan,
On 2020-03-16 at 07:01 -07, Stefan Blumentrath <Stefan.Blumentrath@nina.no> wrote...
Hei Ken,
With this PR:
https://github.com/zarch/grass-session/pull/14
applied, also pygrass modules should work as usual with grass-session.
Thank you for submitting that. It'll take me a while to get up and running with the git version as opposed to the pip version, but I will test it.
GRASS GIS 7.8 should use Python 3, so I am surprised to see that your
$GRASS_PYTHON version is /usr/bin/python2.7 ... That is possibly also
a source for your trouble with garray...
I'm on an LTS OS - Ubuntu 18.04 and the default GRASS that comes with that: 7.4. I'm looking forward to 7.8 but haven't upgraded yet.
-k.
Hi Ken,
Thanks for volunteering to test.
This is how you can install grass-session from (my) git branch:
pip3 install git+https://github.com/ninsbl/grass-session.git@grass_versions
If you are allowed to add a PPA to your system you could get GRASS 7.8 from UbuntuGIS PPA (even in the stable one):
https://launchpad.net/~ubuntugis/+archive/ubuntu/ppa
Hilsen,
Stefan
-----Original Message-----
From: Ken Mankoff <mankoff@gmail.com>
Sent: tirsdag 17. mars 2020 02:02
To: Stefan Blumentrath <Stefan.Blumentrath@nina.no>
Cc: GRASS user list <grass-user@lists.osgeo.org>
Subject: Re: [GRASS-user] garray.read() returns 0-filled array
Hi Stefan,
On 2020-03-16 at 07:01 -07, Stefan Blumentrath <Stefan.Blumentrath@nina.no> wrote...
Hei Ken,
With this PR:
https://github.com/zarch/grass-session/pull/14
applied, also pygrass modules should work as usual with grass-session.
Thank you for submitting that. It'll take me a while to get up and running with the git version as opposed to the pip version, but I will test it.
GRASS GIS 7.8 should use Python 3, so I am surprised to see that your
$GRASS_PYTHON version is /usr/bin/python2.7 ... That is possibly also
a source for your trouble with garray...
I'm on an LTS OS - Ubuntu 18.04 and the default GRASS that comes with that: 7.4. I'm looking forward to 7.8 but haven't upgraded yet.
-k.
Hello,
Replying to my earlier issues from one month ago:
On 2020-03-15 at 08:36 -07, Ken Mankoff <mankoff@gmail.com> wrote...
foo = garray.array().read("foo", null=np.nan)
I have found I can read the GRASS array into Python with this syntax:
foo = garray.array(mapname="foo", null=np.nan)
I'm still waiting to upgrade GRASS to test Stefan's suggestion (later/elsewhere in this thread).
-k.