[GRASS-SVN] r58811 - grass/trunk/raster/r.category

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 30 10:18:13 PST 2014


Author: annakrat
Date: 2014-01-30 10:18:11 -0800 (Thu, 30 Jan 2014)
New Revision: 58811

Modified:
   grass/trunk/raster/r.category/main.c
   grass/trunk/raster/r.category/r.category.html
   grass/trunk/raster/r.category/test_rcategory_doctest.txt
Log:
r.category: apply separator also for input, more robust input checking, update manual page, doctest (co-author wenzeslaus)

Modified: grass/trunk/raster/r.category/main.c
===================================================================
--- grass/trunk/raster/r.category/main.c	2014-01-30 17:37:59 UTC (rev 58810)
+++ grass/trunk/raster/r.category/main.c	2014-01-30 18:18:11 UTC (rev 58811)
@@ -156,9 +156,12 @@
 	}
 
 	/* load cats from rules file */
-	/*  TODO: respect fs= */
 	if (parm.file->answer) {
 	    FILE *fp;
+	    char **tokens;
+	    int ntokens;
+	    char *e1;
+	    char *e2;
 
 	    if (strcmp("-", parm.file->answer) == 0) {
 		from_stdin = TRUE;
@@ -174,18 +177,41 @@
 	    Rast_init_cats("", &cats);
 
 	    for (;;) {
-		char buf[1024], label[1024];
+		char buf[1024];
 		DCELL d1, d2;
+		int parse_error = 0;
 
 		if (!G_getl2(buf, sizeof(buf), fp))
 		    break;
 
-		if (sscanf(buf, "%lf:%lf:%[^\n]", &d1, &d2, label) == 3)
-		    Rast_set_d_cat(&d1, &d2, label, &cats);
-		else if (sscanf(buf, "%lf:%[^\n]", &d1, label) == 2)
-		    Rast_set_d_cat(&d1, &d1, label, &cats);
+		tokens = G_tokenize(buf, fs);
+		ntokens = G_number_of_tokens(tokens);
+
+		if (ntokens == 3) {
+		    d1 = strtod(tokens[0], &e1);
+		    d2 = strtod(tokens[1], &e2);
+		    if (*e1 == 0 && *e2 == 0)
+			Rast_set_d_cat(&d1, &d2, tokens[2], &cats);
+		    else
+			parse_error = 1;
+		}
+		else if (ntokens == 2) {
+		    d1 = strtod(tokens[0], &e1);
+		    if (*e1 == 0)
+			Rast_set_d_cat(&d1, &d1, tokens[1], &cats);
+		    else
+			parse_error = 1;
+		}
+		else if (!strlen(buf))
+		    continue;
+		else
+		    parse_error = 1;
+
+		if (parse_error)
+		    G_fatal_error(_("Incorrect format of input rules. "
+				    "Check separators. Invalid line is:\n%s"), buf);
 	    }
-
+	    G_free_tokens(tokens);
 	    Rast_write_cats(name, &cats);
 
 	    if (!from_stdin)

Modified: grass/trunk/raster/r.category/r.category.html
===================================================================
--- grass/trunk/raster/r.category/r.category.html	2014-01-30 17:37:59 UTC (rev 58810)
+++ grass/trunk/raster/r.category/r.category.html	2014-01-30 18:18:11 UTC (rev 58811)
@@ -39,7 +39,7 @@
 The <b>rules</b> option allows the user to assign category labels from values
 found in a file. The label can refer to a single category, range of
 categories, floating point value, or a range of floating point values.
-The format is given as follows.
+The format is given as follows (when separator is set to colon).
 <div class="code"><pre>
 cat:Label
 val1:val2:Label
@@ -141,7 +141,7 @@
 <dl>
 <dt>Example defining category labels:
 <dd><div class="code"><pre>
-r.category diseasemap rules=- << EOF
+r.category diseasemap separator=: rules=- << EOF
 1:potential absence
 2:potential presence
 EOF
@@ -149,11 +149,7 @@
 <p>sets the categoy values 1 and 2 to respective text labels.
 </dl>
 
-<h2>TODO</h2>
 
-Respect the <b>separator=</b> field separator setting for input rules.
-
-
 <h2>SEE ALSO</h2>
 
 UNIX Manual entries for <i>awk</i> and <i>sort</i>

Modified: grass/trunk/raster/r.category/test_rcategory_doctest.txt
===================================================================
--- grass/trunk/raster/r.category/test_rcategory_doctest.txt	2014-01-30 17:37:59 UTC (rev 58810)
+++ grass/trunk/raster/r.category/test_rcategory_doctest.txt	2014-01-30 18:18:11 UTC (rev 58811)
@@ -31,7 +31,7 @@
 Basic tnput and output
 ======================
 
->>> write_command('r.category', map='test', rules='-', stdin="""
+>>> write_command('r.category', map='test', rules='-', separator=':', stdin="""
 ... 1:trees
 ... 2:water
 ... """)
@@ -44,8 +44,8 @@
 =======================================
 
 >>> write_command('r.category', map='test', rules='-', stdin="""
-... 1:trees
-... 2:water
+... 1\ttrees
+... 2\twater
 ... """)
 0
 >>> read_command('r.category', map='test', separator='tab')
@@ -57,7 +57,7 @@
 Category range
 ==============
 
->>> write_command('r.category', map='test_14', rules='-', stdin="""
+>>> write_command('r.category', map='test_14', separator=':', rules='-', stdin="""
 ... 1:trees
 ... 2:4:buildings
 ... """)
@@ -76,7 +76,7 @@
 Floating point maps
 ===================
 
->>> write_command('r.category', map='test_d', rules='-', stdin="""
+>>> write_command('r.category', map='test_d', separator=':', rules='-', stdin="""
 ... 0:1.5:trees
 ... 1.5:3:buildings
 ... """)



More information about the grass-commit mailing list