On 2/3/18 1:33 PM, Stefan Blumentrath wrote:
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).
Implemented your patch, thanks. Dinarzarde, can you test?
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
<https://trac.osgeo.org/grass/wiki/Submitting/Python> or here:
https://grasswiki.osgeo.org/wiki/GRASS_and_Python
<https://grasswiki.osgeo.org/wiki/GRASS_and_Python> ?
Cheers
Stefan
*From:*grass-user [mailto:grass-user-boun...@lists.osgeo.org] *On
Behalf Of *Paulo van Breugel
*Sent:* lørdag 3. februar 2018 09.14
*To:* Helmut Kudrnovsky <hel...@web.de>; grass-user@lists.osgeo.org
*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 <hel...@web.de
<mailto:hel...@web.de>> 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
> grass-user@lists.osgeo.org <mailto:grass-user@lists.osgeo.org>
> https://lists.osgeo.org/mailman/listinfo/grass-user
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user