[GRASS-SVN] r63663 - grass/branches/releasebranch_7_0/raster/r.in.poly

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 21 19:18:53 PST 2014


Author: annakrat
Date: 2014-12-21 19:18:53 -0800 (Sun, 21 Dec 2014)
New Revision: 63663

Modified:
   grass/branches/releasebranch_7_0/raster/r.in.poly/format.h
   grass/branches/releasebranch_7_0/raster/r.in.poly/get_item.c
   grass/branches/releasebranch_7_0/raster/r.in.poly/getformat.c
   grass/branches/releasebranch_7_0/raster/r.in.poly/local_proto.h
   grass/branches/releasebranch_7_0/raster/r.in.poly/main.c
   grass/branches/releasebranch_7_0/raster/r.in.poly/poly2rast.c
   grass/branches/releasebranch_7_0/raster/r.in.poly/raster.c
Log:
r.in.poly: sync with trunk r62809, r62818

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/format.h
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/format.h	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/format.h	2014-12-22 03:18:53 UTC (rev 63663)
@@ -2,3 +2,5 @@
 #define USE_UCHAR 2
 #define USE_SHORT 3
 #define USE_CELL 4
+#define USE_FCELL 5
+#define USE_DCELL 6

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/get_item.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/get_item.c	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/get_item.c	2014-12-22 03:18:53 UTC (rev 63663)
@@ -1,8 +1,9 @@
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/glocale.h>
+#include "format.h"
 
-int get_item(FILE * fd, int *type, long *cat, double **x, double **y,
+int get_item(FILE * fd, int format, int *type, int *cat_int, double *cat_double, double **x, double **y,
 	     int *count, struct Categories *labels)
 {
     static double *X = NULL;
@@ -13,8 +14,10 @@
     char east[256], north[256];
     double e, n;
     long offset;
+    FCELL cat_float_tmp;
 
-    *cat = 0;
+    *cat_int = 0;
+    *cat_double = 0;
     *count = 0;
     *type = 0;
 
@@ -63,14 +66,32 @@
 
 	/* if we found a cat (and optionally a label), read them and continue to scan */
 	if (*buf == '=') {
-	    if (sscanf(buf + 1, "%ld", cat) != 1)
+	    if (format == USE_FCELL || format == USE_DCELL) {
+		if (sscanf(buf + 1, "%lf", cat_double) != 1)
+		    continue;
+		/* probably change this as G_getl2() doesn't store the new line (?) */
+		if (sscanf(buf + 1, "%lf%[^\n]", cat_double, lbl) == 2) {
+		    G_strip(lbl);
+		    if (format == USE_FCELL) {
+			cat_float_tmp = (FCELL) *cat_double;
+			Rast_set_f_cat(&cat_float_tmp, &cat_float_tmp, lbl, labels);
+		    }
+		    else {
+			Rast_set_d_cat((DCELL*) cat_double, (DCELL *) cat_double, lbl, labels);
+		    }
+		}
 		continue;
-	    /* probably change this as G_getl2() doesn't store the new line (?) */
-	    if (sscanf(buf + 1, "%ld%[^\n]", cat, lbl) == 2) {
-		G_strip(lbl);
-		Rast_set_c_cat((CELL*) cat, (CELL *) cat, lbl, labels);
 	    }
-	    continue;
+	    else {
+		if (sscanf(buf + 1, "%d", cat_int) != 1)
+		    continue;
+		/* probably change this as G_getl2() doesn't store the new line (?) */
+		if (sscanf(buf + 1, "%d%[^\n]", cat_int, lbl) == 2) {
+		    G_strip(lbl);
+		    Rast_set_c_cat((CELL*) cat_int, (CELL *) cat_int, lbl, labels);
+		}
+		continue;
+	    }
 	}
 	if (sscanf(buf, "%s %s", east, north) != 2) {
 	    G_warning(_("Illegal coordinate <%s, %s>, skipping."), east, north);

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/getformat.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/getformat.c	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/getformat.c	2014-12-22 03:18:53 UTC (rev 63663)
@@ -2,7 +2,7 @@
 #include <grass/raster.h>
 #include "format.h"
 
-int getformat(FILE * fd)
+int getformat(FILE * fd, int raster_type, int *null)
 {
     char buf[1024];
     long x;
@@ -12,6 +12,14 @@
     short Smin, Smax;
     int first;
 
+    if (raster_type == FCELL_TYPE)
+        return USE_FCELL;
+    if (raster_type == DCELL_TYPE)
+        return USE_DCELL;
+
+    if (null)
+        return USE_CELL;
+
     max = min = 0;
     first = 1;
     G_fseek(fd, 0L, 0);
@@ -22,6 +30,9 @@
 	if (sscanf(buf + 1, "%ld", &x) != 1)
 	    continue;
 	cat = (CELL) x;
+	/* if we want to write zeros, we must use CELL */
+	if (cat == 0)
+	    return USE_CELL;
 	if (first) {
 	    first = 0;
 	    max = cat;

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/local_proto.h
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/local_proto.h	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/local_proto.h	2014-12-22 03:18:53 UTC (rev 63663)
@@ -1,17 +1,18 @@
 #include <grass/raster.h>
 
 /* get_item.c */
-int get_item(FILE *, int *, long *, double **, double **, int *,
+int get_item(FILE *, int, int *, int *, double *, double **, double **, int *,
 	     struct Categories *);
 /* getformat.c */
-int getformat(FILE *);
+int getformat(FILE *, int, int *);
 
 /* poly2rast.c */
-int poly_to_rast(char *, char *, char *, int);
+int poly_to_rast(char *, char *, char *, int, int, int *);
 
 /* raster.c */
 int begin_rasterization(int, int);
 int configure_plot(void);
-int output_raster(int);
-int set_cat(long);
+int output_raster(int, int *);
+int set_cat_int(int);
+int set_cat_double(double);
 int raster_dot(int, int);

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/main.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/main.c	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/main.c	2014-12-22 03:18:53 UTC (rev 63663)
@@ -12,6 +12,7 @@
  *
  *****************************************************************************/
 #include <stdlib.h>
+#include <string.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
 #include "local_proto.h"
@@ -20,8 +21,12 @@
 int main(int argc, char *argv[])
 {
     struct GModule *module;
-    struct Option *input, *output, *title, *rows;
+    struct Option *input, *output, *title, *rows, *nulls, *type;
     int n;
+    int raster_type;
+    int null_value;
+    int *null;
+    null = &null_value;
 
     G_gisinit(argv[0]);
 
@@ -44,6 +49,20 @@
     title->required = NO;
     title->description = _("Title for resultant raster map");
 
+    type = G_define_option();
+    type->key = "type";
+    type->type = TYPE_STRING;
+    type->required = NO;
+    type->description = _("Output raster type");
+    type->options = "CELL,FCELL,DCELL";
+    type->answer = "CELL";
+
+    nulls = G_define_option();
+    nulls->key = "null";
+    nulls->type = TYPE_INTEGER;
+    nulls->required = NO;
+    nulls->description = _("Integer representing NULL value data cell");
+
     rows = G_define_option();
     rows->key = "rows";
     rows->type = TYPE_INTEGER;
@@ -54,10 +73,23 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-
     sscanf(rows->answer, "%d", &n);
     if (n < 1)
 	G_fatal_error(_("Minimum number of rows to hold in memory is 1"));
 
-    exit(poly_to_rast(input->answer, output->answer, title->answer, n));
+    if (strcmp(type->answer, "CELL") == 0)
+        raster_type = CELL_TYPE;
+    else if (strcmp(type->answer, "FCELL") == 0)
+        raster_type = FCELL_TYPE;
+    else if (strcmp(type->answer, "DCELL") == 0)
+        raster_type = DCELL_TYPE;
+    else
+        G_fatal_error(_("Type doesn't exist"));
+
+    if (nulls->answer)
+        *null = atoi(nulls->answer);
+    else
+        null = NULL;
+
+    exit(poly_to_rast(input->answer, output->answer, title->answer, n, raster_type, null));
 }

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/poly2rast.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/poly2rast.c	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/poly2rast.c	2014-12-22 03:18:53 UTC (rev 63663)
@@ -6,11 +6,12 @@
 #include "format.h"
 #include "local_proto.h"
 
-int poly_to_rast(char *input_file, char *raster_map, char *title, int nrows)
+int poly_to_rast(char *input_file, char *raster_map, char *title, int nrows, int raster_type, int *null)
 {
     double *x, *y;
     int count;
-    long cat;
+    int cat_int;
+    double cat_double;
     int type;
     struct Categories labels;
     FILE *ifd;			/* for input file */
@@ -32,7 +33,7 @@
 	exit(EXIT_FAILURE);
     }
 
-    rfd = Rast_open_c_new(raster_map);
+    rfd = Rast_open_new(raster_map, raster_type);
 
     if (title == NULL)
 	title = "";
@@ -40,7 +41,7 @@
 
     Rast_init_cats(title, &labels);
 
-    format = getformat(ifd);
+    format = getformat(ifd, raster_type, null);
     
     /* ?? otherwise get complaints about window changes */
     G_suppress_warnings(TRUE);
@@ -55,8 +56,11 @@
 	    G_message(_("Pass #%d (of %d) ..."), pass, npasses);
 
 	G_fseek(ifd, 0L, 0);
-	while (get_item(ifd, &type, &cat, &x, &y, &count, &labels)) {
-	    set_cat(cat);
+	while (get_item(ifd, format, &type, &cat_int, &cat_double, &x, &y, &count, &labels)) {
+	    if (format == USE_FCELL || format == USE_DCELL)
+		set_cat_double(cat_double);
+	    else
+		set_cat_int(cat_int);
 	    switch (type) {
 	    case 'A':
 		G_plot_polygon(x, y, count);
@@ -76,7 +80,7 @@
 
 	G_message(_("Writing raster map..."));
 
-	stat = output_raster(rfd);
+	stat = output_raster(rfd, null);
     } while (stat == 0);
     /* stat: 0 means repeat
      *       1 means done

Modified: grass/branches/releasebranch_7_0/raster/r.in.poly/raster.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.in.poly/raster.c	2014-12-22 02:59:19 UTC (rev 63662)
+++ grass/branches/releasebranch_7_0/raster/r.in.poly/raster.c	2014-12-22 03:18:53 UTC (rev 63663)
@@ -1,3 +1,4 @@
+#include <math.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include "format.h"
@@ -11,16 +12,21 @@
     unsigned char **u;
     short **s;
     CELL **cell;
+    FCELL **fcell;
+    DCELL **dcell;
 } raster;
 static int max_rows;
 static int at_row;
-static long cat;
+static int cat_int;
+static double cat_double;
 static int cur_x, cur_y;
 static int format;
 static CELL *cell;
 
 static int (*dot) ();
 static int cell_dot(int, int);
+static int fcell_dot(int, int);
+static int dcell_dot(int, int);
 static int uchar_dot(int, int);
 static int char_dot(int, int);
 static int short_dot(int, int);
@@ -80,8 +86,24 @@
 	    raster.cell[i] = raster.cell[i - 1] + region.cols;
 	dot = cell_dot;
 	break;
+
+    case USE_FCELL:
+	raster.fcell = (FCELL **) G_calloc(max_rows, sizeof(FCELL *));
+	raster.fcell[0] = (FCELL *) G_calloc(size, sizeof(FCELL));
+	for (i = 1; i < max_rows; i++)
+	    raster.fcell[i] = raster.fcell[i - 1] + region.cols;
+	dot = fcell_dot;
+	break;
+ 
+    case USE_DCELL:
+	raster.dcell = (DCELL **) G_calloc(max_rows, sizeof(DCELL *));
+	raster.dcell[0] = (DCELL *) G_calloc(size, sizeof(DCELL));
+	for (i = 1; i < max_rows; i++)
+	    raster.dcell[i] = raster.dcell[i - 1] + region.cols;
+	dot = dcell_dot;
+	break;
     }
-    if (format != USE_CELL)
+    if (format != USE_CELL && format != USE_FCELL && format != USE_DCELL)
 	cell = Rast_allocate_c_buf();
 
     at_row = 0;
@@ -131,9 +153,16 @@
 
     case USE_CELL:
 	for (i = 0; i < nrows; i++)
-	    for (j = 0; j < ncols; j++)
-		raster.cell[i][j] = 0;
+	    Rast_set_c_null_value(raster.cell[i], ncols);
 	break;
+    case USE_FCELL:
+	for (i = 0; i < nrows; i++)
+	    Rast_set_f_null_value(raster.fcell[i], ncols);
+	break;
+    case USE_DCELL:
+	for (i = 0; i < nrows; i++)
+	    Rast_set_d_null_value(raster.dcell[i], ncols);
+	break;
     }
 
     /* change the region */
@@ -148,58 +177,103 @@
     return AGAIN;
 }
 
-int output_raster(int fd)
+int  output_raster(int fd, int *null)
 {
     int i, j;
+    FCELL *fcell;
+    DCELL *dcell;
 
     for (i = 0; i < page.rows; i++, at_row++) {
-	switch (format) {
+        switch (format) {
+        
+        case USE_CHAR:
+            for (j = 0; j < page.cols; j++) {
+                cell[j] = (CELL) raster.c[i][j];
+                if (cell[j] == 0)
+                    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
+            }
+            break;
+            
+        case USE_UCHAR:
+            for (j = 0; j < page.cols; j++) {
+                cell[j] = (CELL) raster.u[i][j];
+                if (cell[j] == 0)
+                    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
+            }
+            break;
+            
+        case USE_SHORT:
+            for (j = 0; j < page.cols; j++) {
+                cell[j] = (CELL) raster.s[i][j];
+                if (cell[j] == 0)
+                    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
+            }
+            break;
 
-	case USE_CHAR:
-	    for (j = 0; j < page.cols; j++) {
-		cell[j] = (CELL) raster.c[i][j];
-		if (cell[j] == 0)
-		    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
-	    }
-	    break;
+        case USE_CELL:
+            cell = raster.cell[i];
+            if (!null)
+                break;
+            for (j = 0; j < page.cols; j++) {
+                if (cell[j] == *null)
+                    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
+            }
+            break;
 
-	case USE_UCHAR:
-	    for (j = 0; j < page.cols; j++) {
-		cell[j] = (CELL) raster.u[i][j];
-		if (cell[j] == 0)
-		    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
-	    }
-	    break;
-
-	case USE_SHORT:
-	    for (j = 0; j < page.cols; j++) {
-		cell[j] = (CELL) raster.s[i][j];
-		if (cell[j] == 0)
-		    Rast_set_null_value(&cell[j], 1, CELL_TYPE);
-	    }
-	    break;
-
-	case USE_CELL:
-	    cell = raster.cell[i];
-	    if (cell == 0)
-		Rast_set_null_value(&cell, 1, CELL_TYPE);
-	    break;
-	}
-
-	G_percent(i, page.rows, 2);
-	Rast_put_row(fd, cell, CELL_TYPE);
+        case USE_FCELL:
+            fcell = raster.fcell[i];
+            if (!null)
+                break;
+            for (j = 0; j < page.cols; j++) {
+                if (fabs(fcell[j] - (FCELL) *null) < 1e-6)
+                    Rast_set_null_value(&fcell[j], 1, FCELL_TYPE);
+            }
+            break;
+            
+        case USE_DCELL:
+            dcell = raster.dcell[i];
+            if (!null)
+                break;
+            for (j = 0; j < page.cols; j++) {
+                if (fabs(dcell[j] - (DCELL) *null) < 1e-6)
+                    Rast_set_null_value(&dcell[j], 1, DCELL_TYPE);
+            }
+            break;
+        }
+        switch (format) {
+        case USE_CHAR:
+        case USE_UCHAR:
+        case USE_SHORT:
+        case USE_CELL:
+            Rast_put_row(fd, cell, CELL_TYPE);
+            break;
+        case USE_FCELL:
+            Rast_put_row(fd, fcell, FCELL_TYPE);
+            break;
+        case USE_DCELL:
+            Rast_put_row(fd, dcell, DCELL_TYPE);
+            break;
+        }
+        G_percent(i, page.rows, 2);
     }
     G_percent(i, page.rows, 2);
     return configure_plot();
 }
 
-int set_cat(long x)
+int set_cat_int(int x)
 {
-    cat = x;
+    cat_int = x;
 
     return 0;
 }
 
+int set_cat_double(double x)
+{
+    cat_double = x;
+
+    return 0;
+}
+
 int raster_dot(int x, int y)
 {
     dot(x, y);
@@ -237,15 +311,31 @@
 static int cell_dot(int x, int y)
 {
     if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
-	raster.cell[y][x] = cat;
+	raster.cell[y][x] = (CELL) cat_int;
 
     return 0;
 }
 
+static int fcell_dot(int x, int y)
+{
+    if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
+	raster.fcell[y][x] = (FCELL) cat_double;
+
+    return 0;
+}
+
+static int dcell_dot(int x, int y)
+{
+    if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
+	raster.dcell[y][x] = (DCELL) cat_double;
+
+    return 0;
+}
+
 static int uchar_dot(int x, int y)
 {
     if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
-	raster.u[y][x] = cat;
+	raster.u[y][x] = cat_int;
 
     return 0;
 }
@@ -253,7 +343,7 @@
 static int char_dot(int x, int y)
 {
     if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
-	raster.c[y][x] = cat;
+	raster.c[y][x] = cat_int;
 
     return 0;
 }
@@ -261,7 +351,7 @@
 static int short_dot(int x, int y)
 {
     if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
-	raster.s[y][x] = cat;
+	raster.s[y][x] = cat_int;
 
     return 0;
 }



More information about the grass-commit mailing list