|
@@ -32,13 +32,11 @@ Usage:
|
|
|
... map2d_1.write(mapname="map2d_1", overwrite=True)
|
|
|
0
|
|
|
>>>
|
|
|
->>> # We create a new array and read map2d_1 to modify it
|
|
|
-... map2d_2 = garray.array()
|
|
|
+>>> # We create a new array from raster map2d_1 to modify it
|
|
|
+... map2d_2 = garray.array(mapname="map2d_1")
|
|
|
>>> # Don't do map2d_2 = map2d_1 % 3
|
|
|
... # because: this will overwrite the internal temporary filename
|
|
|
-... map2d_2.read("map2d_1")
|
|
|
-0
|
|
|
->>> map2d_2 %= 3
|
|
|
+... map2d_2 %= 3
|
|
|
>>> # Show the result
|
|
|
... print(map2d_2)
|
|
|
[[ 0. 1. 2. 0. 1. 2.]
|
|
@@ -80,13 +78,11 @@ Usage:
|
|
|
... # with name map3d_1
|
|
|
... map3d_1.write(mapname="map3d_1", overwrite=True)
|
|
|
0
|
|
|
->>> # We create a new 3D array and read map3d_1 to modify it
|
|
|
-... map3d_2 = garray.array3d()
|
|
|
+>>> # We create a new 3D array from 3D raster map3d_1 to modify it
|
|
|
+... map3d_2 = garray.array3d(mapname="map3d_1")
|
|
|
>>> # Don't do map3d_2 = map3d_1 % 3
|
|
|
... # because: this will overwrite the internal temporary filename
|
|
|
-... map3d_2.read("map3d_1")
|
|
|
-0
|
|
|
->>> map3d_2 %= 3
|
|
|
+... map3d_2 %= 3
|
|
|
>>> # Show the result
|
|
|
... print(map3d_2)
|
|
|
[[[ 0. 1. 2. 0. 1. 2.]
|
|
@@ -114,7 +110,9 @@ for details.
|
|
|
|
|
|
.. sectionauthor:: Glynn Clements
|
|
|
"""
|
|
|
+
|
|
|
from __future__ import absolute_import
|
|
|
+import sys
|
|
|
|
|
|
import numpy
|
|
|
|
|
@@ -135,7 +133,7 @@ class _tempfile(object):
|
|
|
###############################################################################
|
|
|
|
|
|
class array(numpy.memmap):
|
|
|
- def __new__(cls, dtype=numpy.double):
|
|
|
+ def __new__(cls, mapname=None, null=None, dtype=numpy.double):
|
|
|
"""Define new numpy array
|
|
|
|
|
|
:param cls:
|
|
@@ -147,12 +145,35 @@ class array(numpy.memmap):
|
|
|
shape = (r, c)
|
|
|
|
|
|
tempfile = _tempfile()
|
|
|
+ if mapname:
|
|
|
+ kind = numpy.dtype(dtype).kind
|
|
|
+ size = numpy.dtype(dtype).itemsize
|
|
|
+
|
|
|
+ if kind == 'f':
|
|
|
+ flags = 'f'
|
|
|
+ elif kind in 'biu':
|
|
|
+ flags = 'i'
|
|
|
+ else:
|
|
|
+ raise ValueError(_('Invalid kind <%s>') % kind)
|
|
|
+
|
|
|
+ if size not in [1, 2, 4, 8]:
|
|
|
+ raise ValueError(_('Invalid size <%d>') % size)
|
|
|
+
|
|
|
+ gcore.run_command(
|
|
|
+ 'r.out.bin',
|
|
|
+ flags=flags,
|
|
|
+ input=mapname,
|
|
|
+ output=tempfile.filename,
|
|
|
+ bytes=size,
|
|
|
+ null=null,
|
|
|
+ quiet=True,
|
|
|
+ overwrite=True)
|
|
|
|
|
|
self = numpy.memmap.__new__(
|
|
|
cls,
|
|
|
filename=tempfile.filename,
|
|
|
dtype=dtype,
|
|
|
- mode='w+',
|
|
|
+ mode='r+',
|
|
|
shape=shape)
|
|
|
|
|
|
self.tempfile = tempfile
|
|
@@ -167,7 +188,14 @@ class array(numpy.memmap):
|
|
|
|
|
|
:return: 0 on success
|
|
|
:return: non-zero code on failure
|
|
|
+
|
|
|
+ .. deprecated:: 7.1
|
|
|
+ Instead reading the map after creating the array,
|
|
|
+ pass the map name in the array constructor.
|
|
|
"""
|
|
|
+ if sys.platform == 'win32':
|
|
|
+ gcore.warning(_("grass.script.array.read is deprecated and does not"
|
|
|
+ " work on MS Windows, pass raster name in the constructor"))
|
|
|
kind = self.dtype.kind
|
|
|
size = self.dtype.itemsize
|
|
|
|
|
@@ -252,7 +280,7 @@ class array(numpy.memmap):
|
|
|
|
|
|
|
|
|
class array3d(numpy.memmap):
|
|
|
- def __new__(cls, dtype=numpy.double):
|
|
|
+ def __new__(cls, mapname=None, null=None, dtype=numpy.double):
|
|
|
"""Define new 3d numpy array
|
|
|
|
|
|
:param cls:
|
|
@@ -265,12 +293,35 @@ class array3d(numpy.memmap):
|
|
|
shape = (d, r, c)
|
|
|
|
|
|
tempfile = _tempfile()
|
|
|
+ if mapname:
|
|
|
+ kind = numpy.dtype(dtype).kind
|
|
|
+ size = numpy.dtype(dtype).itemsize
|
|
|
+
|
|
|
+ if kind == 'f':
|
|
|
+ flags = None # default is double
|
|
|
+ elif kind in 'biu':
|
|
|
+ flags = 'i'
|
|
|
+ else:
|
|
|
+ raise ValueError(_('Invalid kind <%s>') % kind)
|
|
|
+
|
|
|
+ if size not in [1, 2, 4, 8]:
|
|
|
+ raise ValueError(_('Invalid size <%d>') % size)
|
|
|
+
|
|
|
+ gcore.run_command(
|
|
|
+ 'r3.out.bin',
|
|
|
+ flags=flags,
|
|
|
+ input=mapname,
|
|
|
+ output=tempfile.filename,
|
|
|
+ bytes=size,
|
|
|
+ null=null,
|
|
|
+ quiet=True,
|
|
|
+ overwrite=True)
|
|
|
|
|
|
self = numpy.memmap.__new__(
|
|
|
cls,
|
|
|
filename=tempfile.filename,
|
|
|
dtype=dtype,
|
|
|
- mode='w+',
|
|
|
+ mode='r+',
|
|
|
shape=shape)
|
|
|
|
|
|
self.tempfile = tempfile
|
|
@@ -286,7 +337,14 @@ class array3d(numpy.memmap):
|
|
|
|
|
|
:return: 0 on success
|
|
|
:return: non-zero code on failure
|
|
|
+
|
|
|
+ .. deprecated:: 7.1
|
|
|
+ Instead reading the map after creating the array,
|
|
|
+ pass the map name in the array constructor.
|
|
|
"""
|
|
|
+ if sys.platform == 'win32':
|
|
|
+ gcore.warning(_("grass.script.array3d.read is deprecated and does not"
|
|
|
+ " work on MS Windows, pass 3D raster name in the constructor"))
|
|
|
kind = self.dtype.kind
|
|
|
size = self.dtype.itemsize
|
|
|
|