Re: Issue with addon r.vif in MS Windows GUI installations (stand-alone v. 7.2.2 and 7.0.5)

Stefan Blumentrath <[email protected]>
Newsgroups gmane.comp.gis.grass.user
Message-ID <[email protected]>
Hi Paulo and Helmut,

Did you consider using cStringIO, if you need a file-object?
That would avoid the temporary file and thus should be more efficient too. See attached diff (it is probably not necessary to remove the last line break though).

Just a suggestion.
Yet, there might be even more efficient solutions. For example raster2numpy from pygrass...
https://grass.osgeo.org/grass74/manuals/libpython/pygrass.raster.html#pygrass.raster.raster2numpy
Here however, you would have to mask NULL and I am not sure how to (platform-independent) determine values that represent NULL in a raster map. This gives pointers but there are again issues with Windows it seems: https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Interfacing_with_NumPy
And it leaves the question how to find out if a raster is Integer or not...

With pygrass you could do:
from grass.pygrass import raster as r
p = np.ma.masked_equal(r.raster2numpy(raster), np.nan)) # For floating point rasters on Linux
or
p = np.ma.masked_equal(r.raster2numpy(raster), -2147483648)) # For integer rasters

Hints from experienced Python/Numpy developers would be appreciated...

That makes me wonder if it would be worth to add/collect recommendations for how to solve such common operations (like e.g. parsing table output from grass commands) either e.g. here: https://trac.osgeo.org/grass/wiki/Submitting/Python or here: https://grasswiki.osgeo.org/wiki/GRASS_and_Python ?

Cheers
Stefan


From: grass-user [mailto:[email protected]] On Behalf Of Paulo van Breugel
Sent: lørdag 3. februar 2018 09.14
To: Helmut Kudrnovsky <[email protected]>; [email protected]
Subject: Re: [GRASS-user] Issue with addon r.vif in MS Windows GUI installations (stand-alone v. 7.2.2 and 7.0.5)


On February 2, 2018 8:52:03 PM Helmut Kudrnovsky <[email protected]<mailto:[email protected]>> wrote:

>>Any idea how I can do that in such a way that it works on both Linux and
> Windows?
>
> in r.vif script I've changed the relevant lines:
>
> <----------------------------------------
>     # Get the raster values at sample points
>     fd, tmpcov = tempfile.mkstemp()
>     with open(tmpcov, "w") as text_file:
>         text_file.write(
>                 gs.read_command("r.stats", flags="1n", input=raster,
>                                 quiet=True, separator="comma"))
>     p = np.loadtxt(tmpcov, skiprows=0, delimiter=",")
>
>     # Clean up
>     text_file.close()
>     os.close(fd)
>     os.remove(tmpcov)
>     if not n == "100%":
>         gs.run_command("r.mask", flags="r", quiet=True)
>         if exist_mask['fullname']:
>             gs.run_command("g.rename", raster=[mask_backup, "MASK"],
>                            quiet=True)
>     return(p)
> ---------------------------------------->
>
> and tested the script in winGRASS; it seems to work in windows; not tested
> on linux and mac.
>
>
Ok, thanks Helmut, I'll see if that works in Linux too
>
> -----
> best regards
> Helmut
> --
> Sent from: http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html
> _______________________________________________
> grass-user mailing list
> [email protected]<mailto:[email protected]>
> https://lists.osgeo.org/mailman/listinfo/grass-user

_______________________________________________
grass-user mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/grass-user
r.vif.diff (application/octet-stream, 1.1 KB)
Index: grass7/raster/r.vif/r.vif.py
===================================================================
--- grass7/raster/r.vif/r.vif.py	(revision 72204)
+++ grass7/raster/r.vif/r.vif.py	(working copy)
@@ -91,6 +91,7 @@
 import sys
 import math
 import numpy as np
+from cStringIO import StringIO
 import uuid
 import tempfile
 import atexit
@@ -145,15 +146,10 @@
         gs.run_command("r.mask", raster=new_mask, quiet=True)
 
     # Get the raster values at sample points
-    tmpcov = tempfile.mkstemp()[1]
-    with open(tmpcov, "w") as text_file:
-        text_file.write(
-                gs.read_command("r.stats", flags="1n", input=raster,
-                                quiet=True, separator="comma"))
+    tmpcov = StringIO(gs.read_command("r.stats", flags="1n", input=raster,
+                                quiet=True, separator="comma").rstrip('\n'))
     p = np.loadtxt(tmpcov, skiprows=0, delimiter=",")
 
-    # Clean up
-    os.remove(tmpcov)
     if not n == "100%":
         gs.run_command("r.mask", flags="r", quiet=True)
         if exist_mask['fullname']:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.