Prechádzať zdrojové kódy

v.colors: Implement rules from stdin (#1389)

* v.colors: Implement rules from stdin

* Update vector/v.colors/v.colors.html

Co-authored-by: Markus Neteler <neteler@osgeo.org>

Co-authored-by: Markus Neteler <neteler@osgeo.org>
Huidae Cho 4 rokov pred
rodič
commit
981a6db51e

+ 2 - 10
vector/v.colors/main.c

@@ -7,7 +7,7 @@
  *               
  * PURPOSE:      Manage color tables for vector maps
  *               
- * COPYRIGHT:    (C) 2011-2014 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2011-2021 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
@@ -243,8 +243,6 @@ int main(int argc, char *argv[])
 		      opt.rgbcol->key, flag.c->key);
 
     is_from_stdin = rules && strcmp(rules, "-") == 0;
-    if (is_from_stdin)
-        G_fatal_error(_("Reading rules from standard input is not implemented yet, please provide path to rules file instead."));
 
     mapset = G_find_vector(name, "");
     if (!mapset)
@@ -291,13 +289,7 @@ int main(int argc, char *argv[])
     }
 
     Rast_init_colors(&colors);
-    if (is_from_stdin) {
-        G_fatal_error(_("Reading color rules from standard input is currently not supported"));
-	/*
-        if (!read_color_rules(stdin, &colors, min, max, fp))
-            exit(EXIT_FAILURE);
-	*/
-    } else if (style || rules) {	
+    if (style || rules) {	
 	if (style && !G_find_color_rule(style))
 	    G_fatal_error(_("Color table <%s> not found"), style);
 	

+ 4 - 2
vector/v.colors/make_colors.c

@@ -41,8 +41,10 @@ void make_colors(struct Colors *colors, const char *style, DCELL min, DCELL max,
 void load_colors(struct Colors *colors, const char *rules, DCELL min, DCELL max, int is_fp)
 {
     int ret;
-    
-    if (is_fp)
+
+    if (rules[0] == '-' && rules[1] == 0)
+	ret = Rast_read_color_rules(colors, min, max, Rast_read_color_rule, stdin);
+    else if (is_fp)
 	ret = Rast_load_fp_colors(colors, rules, (DCELL) min, (DCELL) max);
     else
 	ret = Rast_load_colors(colors, rules, (CELL) min, (CELL) max);

+ 8 - 7
vector/v.colors/scan_z.c

@@ -8,9 +8,9 @@ void scan_z(struct Map_info *Map, int field,
             const char *style, const char *rules,
             const struct FPRange *range, struct Colors *colors)
 {
-    int ltype, line, cat, i;
+    int ltype, line, cat, i, found;
     int items_alloc;
-    double zmin, zmax;
+    double zmin = 0, zmax = 0;
     struct line_pnts *Points;
     struct line_cats *Cats;
     
@@ -29,7 +29,7 @@ void scan_z(struct Map_info *Map, int field,
     Vect_set_constraint_type(Map, GV_POINTS); /* points, centroids or kernels only) */
         
     G_message(_("Reading features..."));
-    line = i = 0;
+    line = i = found = 0;
     while(TRUE) {
 	ltype = Vect_read_next_line(Map, Points, Cats);
 	if (ltype == -1)
@@ -51,10 +51,11 @@ void scan_z(struct Map_info *Map, int field,
         cvarr.value[i].cat = cat;
         cvarr.value[i++].val.d = Points->z[0];
 
-	if (line == 1 || Points->z[0] < zmin)
+	if (!found || Points->z[0] < zmin)
 	    zmin = Points->z[0];
-	if (line == 1 || Points->z[0] > zmax)
+	if (!found || Points->z[0] > zmax)
 	    zmax = Points->z[0];
+	found = 1;
     }
     G_progress(1, 1);
     
@@ -62,13 +63,13 @@ void scan_z(struct Map_info *Map, int field,
     db_CatValArray_sort_by_value(&cvarr);
     
     if (range) {
-	if (range->min >= zmin && range->min <= zmax)
+	if (!found || (range->min >= zmin && range->min <= zmax))
 	    zmin = range->min;
 	else
 	    G_warning(_("Min value (%f) is out of range %f,%f"),
 		      range->min, zmin, zmax);
 	
-	if (range->max <= zmax && range->max >= zmin)
+	if (!found || (range->max <= zmax && range->max >= zmin))
 	    zmax = range->max;
 	else
 	    G_warning(_("Max value (%f) is out of range %f,%f"),

+ 13 - 1
vector/v.colors/v.colors.html

@@ -65,7 +65,7 @@ instead of creating color table.
 v.colors map=soils_general layer=1 color=wave use=attr column=AREA rgb_column=GRASSRGB
 
 # See some GRASSRGB values:
-v.db.select map=soils_general where="cat < 4"
+v.db.select map=soils_general where="cat &lt; 4"
 cat|OBJECTID|AREA|PERIMETER|GSLNC250_|GSLNC250_I|GSL_NAME|GRASSRGB
 1|1|0|164616.125|2|1|NC113|212:42:127
 2|2|0|30785.529297|3|2|NC096|212:42:127
@@ -86,6 +86,18 @@ by <em><a href="v.colors.out.html">v.colors.out</a></em>) together
 with GRASSRGB attribute column. Also note that color table is preferred
 over RGB values stored in attribute table.
 
+<h3>Transfer raster colors to vector</h3>
+
+<div class="code"><pre>
+# create an example raster from census blocks (10m pixel resolution)
+g.region vector=censusblk_swwake res=10 -ap
+v.to.rast input=censusblk_swwake use=attr attribute_column=TOTAL_POP output=censusblk_swwake_total_pop
+r.colors -e map=censusblk_swwake_total_pop color=blues
+
+# transfer raster colors to vector using an attribute column
+r.colors.out map=censusblk_swwake_total_pop rules=- | v.colors map=censusblk_swwake use=attr column=TOTAL_POP rules=-
+</pre></div>
+
 <h3>Remove existing color table</h3>
 
 <p>