瀏覽代碼

pythonlib/array: fix reading raster on Windows, make read method deprecated

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67710 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 9 年之前
父節點
當前提交
c13a7a44f1
共有 1 個文件被更改,包括 72 次插入14 次删除
  1. 72 14
      lib/python/script/array.py

+ 72 - 14
lib/python/script/array.py

@@ -32,13 +32,11 @@ Usage:
 ... map2d_1.write(mapname="map2d_1", overwrite=True)
 ... map2d_1.write(mapname="map2d_1", overwrite=True)
 0
 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
 >>> # Don't do map2d_2 = map2d_1 % 3
 ... # because: this will overwrite the internal temporary filename
 ... # because: this will overwrite the internal temporary filename
-... map2d_2.read("map2d_1")
-0
->>> map2d_2 %= 3
+... map2d_2 %= 3
 >>> # Show the result
 >>> # Show the result
 ... print(map2d_2)
 ... print(map2d_2)
 [[ 0.  1.  2.  0.  1.  2.]
 [[ 0.  1.  2.  0.  1.  2.]
@@ -80,13 +78,11 @@ Usage:
 ... # with name map3d_1
 ... # with name map3d_1
 ... map3d_1.write(mapname="map3d_1", overwrite=True)
 ... map3d_1.write(mapname="map3d_1", overwrite=True)
 0
 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
 >>> # Don't do map3d_2 = map3d_1 % 3
 ... # because: this will overwrite the internal temporary filename
 ... # because: this will overwrite the internal temporary filename
-... map3d_2.read("map3d_1")
-0
->>> map3d_2 %= 3
+... map3d_2 %= 3
 >>> # Show the result
 >>> # Show the result
 ... print(map3d_2)
 ... print(map3d_2)
 [[[ 0.  1.  2.  0.  1.  2.]
 [[[ 0.  1.  2.  0.  1.  2.]
@@ -114,7 +110,9 @@ for details.
 
 
 .. sectionauthor:: Glynn Clements
 .. sectionauthor:: Glynn Clements
 """
 """
+
 from __future__ import absolute_import
 from __future__ import absolute_import
+import sys
 
 
 import numpy
 import numpy
 
 
@@ -135,7 +133,7 @@ class _tempfile(object):
 ###############################################################################
 ###############################################################################
 
 
 class array(numpy.memmap):
 class array(numpy.memmap):
-    def __new__(cls, dtype=numpy.double):
+    def __new__(cls, mapname=None, null=None, dtype=numpy.double):
         """Define new numpy array
         """Define new numpy array
 
 
         :param cls:
         :param cls:
@@ -147,12 +145,35 @@ class array(numpy.memmap):
         shape = (r, c)
         shape = (r, c)
 
 
         tempfile = _tempfile()
         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__(
         self = numpy.memmap.__new__(
             cls,
             cls,
             filename=tempfile.filename,
             filename=tempfile.filename,
             dtype=dtype,
             dtype=dtype,
-            mode='w+',
+            mode='r+',
             shape=shape)
             shape=shape)
 
 
         self.tempfile = tempfile
         self.tempfile = tempfile
@@ -167,7 +188,14 @@ class array(numpy.memmap):
 
 
         :return: 0 on success
         :return: 0 on success
         :return: non-zero code on failure
         :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
         kind = self.dtype.kind
         size = self.dtype.itemsize
         size = self.dtype.itemsize
 
 
@@ -252,7 +280,7 @@ class array(numpy.memmap):
 
 
 
 
 class array3d(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
         """Define new 3d numpy array
 
 
         :param cls:
         :param cls:
@@ -265,12 +293,35 @@ class array3d(numpy.memmap):
         shape = (d, r, c)
         shape = (d, r, c)
 
 
         tempfile = _tempfile()
         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__(
         self = numpy.memmap.__new__(
             cls,
             cls,
             filename=tempfile.filename,
             filename=tempfile.filename,
             dtype=dtype,
             dtype=dtype,
-            mode='w+',
+            mode='r+',
             shape=shape)
             shape=shape)
 
 
         self.tempfile = tempfile
         self.tempfile = tempfile
@@ -286,7 +337,14 @@ class array3d(numpy.memmap):
 
 
         :return: 0 on success
         :return: 0 on success
         :return: non-zero code on failure
         :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
         kind = self.dtype.kind
         size = self.dtype.itemsize
         size = self.dtype.itemsize