瀏覽代碼

r.mapcalc: Introduce new variables to access current region dimensions in number of cells

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67664 15284696-431f-4ddb-bdfa-cd5b030d7da7
Maris Nartiss 9 年之前
父節點
當前提交
894ca95dbf

+ 3 - 0
raster/r.mapcalc/func_proto.h

@@ -4,6 +4,9 @@
 extern func_t f_col;
 extern func_t f_row;
 extern func_t f_depth;
+extern func_t f_nrows;
+extern func_t f_ncols;
+extern func_t f_ndepths;
 
 extern func_t f_x;
 extern func_t f_y;

+ 3 - 0
raster/r.mapcalc/function.c

@@ -11,6 +11,9 @@ func_desc local_func_descs[] = {
     {"col", c_int0, f_col},
     {"row", c_int0, f_row},
     {"depth", c_int0, f_depth},
+    {"ncols", c_int0, f_ncols},
+    {"nrows", c_int0, f_nrows},
+    {"ndepths", c_int0, f_ndepths},
 
     {"x", c_double0, f_x},
     {"y", c_double0, f_y},

+ 8 - 6
raster/r.mapcalc/r.mapcalc.html

@@ -332,12 +332,14 @@ xor(x,y)                exclusive-or (XOR) of x and y                   I
 
 <div class="code"><pre>
 Internal variables:
- row()                  current row of moving window
- col()                  current col of moving window
- x()                    current x-coordinate of moving window
- y()                    current y-coordinate of moving window
- ewres()                current east-west resolution
- nsres()                current north-south resolution
+ row()                  current row of moving window                    I
+ col()                  current col of moving window                    I
+ nrows()                number of rows in computation region            I
+ ncols()                number of columns in computation region         I
+ x()                    current x-coordinate of moving window           F
+ y()                    current y-coordinate of moving window           F
+ ewres()                current east-west resolution                    F
+ nsres()                current north-south resolution                  F
  null()                 NULL value
 </pre></div>
 Note, that the row() and col() indexing starts with 1.

+ 12 - 9
raster/r.mapcalc/r3.mapcalc.html

@@ -240,15 +240,18 @@ xor(x,y)                exclusive-or (XOR) of x and y                   I
 
 <div class="code"><pre>
 Internal variables:
- row()                  current row of moving window
- col()                  current col of moving window
- depth()                return current depth
- x()                    current x-coordinate of moving window
- y()                    current y-coordinate of moving window
- z()                    return current z value
- ewres()                current east-west resolution
- nsres()                current north-south resolution
- tbres()                current top-bottom resolution
+ row()                  current row of moving window                    I
+ col()                  current col of moving window                    I
+ depth()                return current depth                            I
+ nrows()                number of rows in computation region            I
+ ncols()                number of columns in computation region         I
+ ndepths()              number of depth levels in computation region    I
+ x()                    current x-coordinate of moving window           F
+ y()                    current y-coordinate of moving window           F
+ z()                    return current z value                          F
+ ewres()                current east-west resolution                    F
+ nsres()                current north-south resolution                  F
+ tbres()                current top-bottom resolution                   F
  null()                 NULL value
 </pre></div>
 Note, that the row(), col() and depth() indexing starts with 1. 

+ 8 - 0
raster/r.mapcalc/testsuite/test_r3_mapcalc.py

@@ -51,6 +51,14 @@ class TestBasicOperations(TestCase):
             expression='diff_e_e = 3 * x() * y() * z() - 3 * x() * y() * z()')
         self.to_remove.append('diff_e_e')
         self.assertRaster3dMinMax('diff_e_e', refmin=0, refmax=0)
+    
+    def test_nrows_ncols_ndepths_sum(self):
+        """Test if sum of nrows, ncols and ndepths matches one
+        expected from current region settigs"""
+        self.assertModule('r3.mapcalc',
+            expression='nrows_ncols_ndepths_sum = nrows() + ncols() + ndepths()')
+        self.to_remove.append('nrows_ncols_ndepths_sum')
+        self.assertRasterMinMax('nrows_ncols_ndepths_sum', refmin=2160, refmax=2160)
 
 
 if __name__ == '__main__':

+ 8 - 0
raster/r.mapcalc/testsuite/test_r_mapcalc.py

@@ -218,6 +218,14 @@ class TestBasicOperations(TestCase):
             expression='diff_e_e = 3 * x() * y() - 3 * x() * y()')
         self.to_remove.append('diff_e_e')
         self.assertRasterMinMax('diff_e_e', refmin=0, refmax=0)
+    
+    def test_nrows_ncols_sum(self):
+        """Test if sum of nrows and ncols matches one
+        expected from current region settigs"""
+        self.assertModule('r.mapcalc',
+            expression='nrows_ncols_sum = nrows() + ncols()')
+        self.to_remove.append('nrows_ncols_sum')
+        self.assertRasterMinMax('nrows_ncols_sum', refmin=20, refmax=20)
 
 
 if __name__ == '__main__':

+ 54 - 0
raster/r.mapcalc/xrowcol.c

@@ -9,6 +9,9 @@
 col() column number
 row() row number
 depth() depth number
+ncols() number of columns
+nrows() number of rows
+ndepths() number of depths
 **********************************************************************/
 
 int f_col(int argc, const int *argt, void **args)
@@ -63,3 +66,54 @@ int f_depth(int argc, const int *argt, void **args)
 
     return 0;
 }
+
+int f_nrows(int argc, const int *argt, void **args)
+{
+    CELL *res = args[0];
+    int i;
+
+    if (argc > 0)
+	return E_ARG_HI;
+
+    if (argt[0] != CELL_TYPE)
+	return E_RES_TYPE;
+
+    for (i = 0; i < columns; i++)
+	res[i] = rows;
+
+    return 0;
+}
+
+int f_ncols(int argc, const int *argt, void **args)
+{
+    CELL *res = args[0];
+    int i;
+
+    if (argc > 0)
+	return E_ARG_HI;
+
+    if (argt[0] != CELL_TYPE)
+	return E_RES_TYPE;
+
+    for (i = 0; i < columns; i++)
+	res[i] = columns;
+
+    return 0;
+}
+
+int f_ndepths(int argc, const int *argt, void **args)
+{
+    CELL *res = args[0];
+    int i;
+
+    if (argc > 0)
+	return E_ARG_HI;
+
+    if (argt[0] != CELL_TYPE)
+	return E_RES_TYPE;
+
+    for (i = 0; i < columns; i++)
+	res[i] = depths;
+
+    return 0;
+}