[GRASS-SVN] r32576 - grass/trunk/raster/r.quant
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Aug 6 13:01:08 EDT 2008
Author: glynn
Date: 2008-08-06 13:01:08 -0400 (Wed, 06 Aug 2008)
New Revision: 32576
Modified:
grass/trunk/raster/r.quant/global.h
grass/trunk/raster/r.quant/main.c
grass/trunk/raster/r.quant/read_rules.c
Log:
Add rules= option
Fix breakage arising from removing interactive functions
Modified: grass/trunk/raster/r.quant/global.h
===================================================================
--- grass/trunk/raster/r.quant/global.h 2008-08-06 13:55:31 UTC (rev 32575)
+++ grass/trunk/raster/r.quant/global.h 2008-08-06 17:01:08 UTC (rev 32576)
@@ -1,20 +1,13 @@
#include <grass/gis.h>
-#ifdef MAIN
-struct Quant quant_struct;
-CELL old_min, old_max;
-DCELL old_dmin, old_dmax;
-char *name[GNAME_MAX]; /* input map names */
-char *mapset[GMAPSET_MAX]; /* input mapsets */
-int noi;
-#else
+
extern struct Quant quant_struct;
extern CELL old_min, old_max;
extern DCELL old_dmin, old_dmax;
extern char *name[GNAME_MAX]; /* input map names */
extern char *mapset[GMAPSET_MAX]; /* input mapsets */
extern int noi;
-#endif
+
/* read_rules.c */
int read_range(void);
int report_range(void);
-int read_rules(void);
+int read_rules(const char *);
Modified: grass/trunk/raster/r.quant/main.c
===================================================================
--- grass/trunk/raster/r.quant/main.c 2008-08-06 13:55:31 UTC (rev 32575)
+++ grass/trunk/raster/r.quant/main.c 2008-08-06 17:01:08 UTC (rev 32576)
@@ -14,7 +14,7 @@
* for details.
*
*****************************************************************************/
-#define MAIN
+
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
@@ -22,11 +22,17 @@
#include "global.h"
#include <grass/glocale.h>
+struct Quant quant_struct;
+CELL old_min, old_max;
+DCELL old_dmin, old_dmax;
+char *name[GNAME_MAX]; /* input map names */
+char *mapset[GMAPSET_MAX]; /* input mapsets */
+int noi;
+
int main(int argc, char *argv[])
{
- char buf[1024];
struct GModule *module;
- struct Option *input, *basemap, *fprange, *range;
+ struct Option *input, *basemap, *fprange, *range, *rules;
struct Flag *trunc, *rnd;
int truncate;
int round;
@@ -42,14 +48,6 @@
module->description =
_("Produces the quantization file for a floating-point map.");
- basemap = G_define_option();
- basemap->key = "basemap";
- basemap->required = NO;
- basemap->type = TYPE_STRING;
- basemap->answer = "NONE";
- basemap->gisprompt = "old,cell,raster";
- basemap->description = _("Base map to take quant rules from");
-
input = G_define_option();
input->key = "input";
input->required = YES;
@@ -58,21 +56,28 @@
input->gisprompt = "old,cell,raster";
input->description = _("Raster map(s) to be quantized");
+ rules = G_define_standard_option(G_OPT_F_INPUT);
+ rules->key = "rules";
+ rules->required = NO;
+ rules->description = _("Path to rules file (\"-\" to read from stdin)");
+
+ basemap = G_define_option();
+ basemap->key = "basemap";
+ basemap->type = TYPE_STRING;
+ basemap->gisprompt = "old,cell,raster";
+ basemap->description = _("Base map to take quant rules from");
+
fprange = G_define_option();
fprange->key = "fprange";
fprange->key_desc = "dmin,dmax";
fprange->description = _("Floating point range: dmin,dmax");
fprange->type = TYPE_STRING;
- fprange->answer = "";
- fprange->required = YES;
range = G_define_option();
range->key = "range";
range->key_desc = "min,max";
range->description = _("Integer range: min,max");
range->type = TYPE_STRING;
- range->answer = "1,255";
- range->required = YES;
trunc = G_define_flag();
trunc->key = 't';
@@ -84,27 +89,46 @@
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+
truncate = trunc->answer;
round = rnd->answer;
- G_quant_init(&quant_struct);
+ basename = basemap->answer;
+ i = 0;
+ if (truncate)
+ i++;
+ if (round)
+ i++;
+ if (basename)
+ i++;
+ if (fprange->answer || range->answer)
+ i++;
+ if (rules->answer)
+ i++;
+ if (i > 1)
+ G_fatal_error(_("-t, -r, rules=, basemap= and fprange= are mutually exclusive"));
+
+ i = 0;
+ if (fprange->answer)
+ i++;
+ if (range->answer)
+ i++;
+ if (i == 1)
+ G_fatal_error(_("fprange= and range= must be used together"));
+
/* read and check inputs */
for (noi = 0; input->answers[noi]; noi++) {
name[noi] = G_store(input->answers[noi]);
mapset[noi] = G_find_cell2(name[noi], "");
- if (mapset[noi] == NULL) {
- sprintf(buf, "%s - not found", name[noi]);
- G_fatal_error(buf);
- }
+ if (!mapset[noi])
+ G_fatal_error(_("%s - not found"), name[noi]);
- if (G_raster_map_type(name[noi], mapset[noi]) == CELL_TYPE) {
- sprintf(buf, "%s is integer map, it can't be quantized",
- name[noi]);
- G_fatal_error(buf);
- }
+ if (G_raster_map_type(name[noi], mapset[noi]) == CELL_TYPE)
+ G_fatal_error(_("%s is integer map, it can't be quantized"),
+ name[noi]);
}
- basename = basemap->answer;
+ G_quant_init(&quant_struct);
/* now figure out what new quant rules to write */
if (truncate) {
@@ -117,50 +141,47 @@
G_quant_round(&quant_struct);
}
- else if (strncmp(basename, "NONE", 4) != 0)
+ else if (basename)
/* set the quant to that of basemap */
{
basemapset = G_find_cell2(basename, "");
- if (basemapset == NULL) {
- sprintf(buf, "%s - not found", basename);
- G_fatal_error(buf);
- }
+ if (!basemapset)
+ G_fatal_error(_("%s - not found"), basename);
- if (G_raster_map_type(basename, basemapset) == CELL_TYPE) {
- sprintf(buf, "%s is integer map, it can't be used as basemap",
- basename);
- G_fatal_error(buf);
- }
+ if (G_raster_map_type(basename, basemapset) == CELL_TYPE)
+ G_fatal_error(_("%s is integer map, it can't be used as basemap"),
+ basename);
- if (G_read_quant(basename, basemapset, &quant_struct) <= 0) {
- sprintf(buf, "Can't read quant rules for basemap %s! Exiting.",
- basename);
- G_fatal_error(buf);
- }
+ if (G_read_quant(basename, basemapset, &quant_struct) <= 0)
+ G_fatal_error(_("unable to read quant rules for basemap <%s>"),
+ basename);
}
- else if ((sscanf(fprange->answer, "%lf,%lf", &new_dmin, &new_dmax) == 2)
- && (sscanf(range->answer, "%d,%d", &new_min, &new_max) == 2)) {
- G_message(_
- ("Setting quant rules for input map(s) to (%f %f) -> (%d,%d)"),
+ else if (fprange->answer)
+ {
+ if (sscanf(fprange->answer, "%lf,%lf", &new_dmin, &new_dmax) != 2)
+ G_fatal_error(_("invalid value for fprange= <%s>"), fprange->answer);
+ if (sscanf(range->answer, "%d,%d", &new_min, &new_max) != 2)
+ G_fatal_error(_("invalid value for range= <%s>"), range->answer);
+ G_message(_("Setting quant rules for input map(s) to (%f,%f) -> (%d,%d)"),
new_dmin, new_dmax, new_min, new_max);
G_quant_add_rule(&quant_struct, new_dmin, new_dmax, new_min, new_max);
}
+ else if (rules->answer) {
+ if (!read_rules(rules->answer))
+ G_fatal_error("No rules specified");
+ }
+
else { /* ask user for quant rules */
-
-
- if (!read_rules()) {
+ if (!read_rules("-")) {
if (isatty(0))
- G_message(_
- ("No rules specified. Quant table(s) not changed."));
+ G_message(_("No rules specified. Quant table(s) not changed."));
else
- G_fatal_error("No rules specified");
+ G_fatal_error(_("No rules specified"));
}
-
} /* use rules */
-
for (i = 0; i < noi; i++) {
if (G_write_quant(name[i], mapset[i], &quant_struct) < 0)
G_message(_("Quant table not changed for %s"), name[i]);
@@ -168,5 +189,5 @@
G_message(_("New quant table created for %s"), name[i]);
}
- exit(EXIT_FAILURE);
+ exit(EXIT_SUCCESS);
}
Modified: grass/trunk/raster/r.quant/read_rules.c
===================================================================
--- grass/trunk/raster/r.quant/read_rules.c 2008-08-06 13:55:31 UTC (rev 32575)
+++ grass/trunk/raster/r.quant/read_rules.c 2008-08-06 17:01:08 UTC (rev 32576)
@@ -63,7 +63,7 @@
return 0;
}
-int read_rules(void)
+int read_rules(const char *filename)
{
char buf[256];
DCELL dLow, dHigh;
@@ -72,18 +72,26 @@
int first = 1;
DCELL dmin, dmax;
CELL cmin, cmax;
- char *prompt = "Add more quant rules?";
+ FILE *fp;
+ if (strcmp(filename, "-") == 0)
+ fp = stdin;
+ else {
+ fp = fopen(filename, "r");
+ if (!fp)
+ G_fatal_error(_("unable to open input file <%s>"), filename);
+ }
+
read_range();
report_range();
- fprintf(stderr,
- "\nEnter the rule or 'help' for the format description or 'end' to exit:\n");
+ if (isatty(fileno(fp)))
+ fprintf(stderr, _("\nEnter the rule or 'help' for the format description or 'end' to exit:\n"));
G_quant_init(&quant_struct);
for (line = 1;; line++) {
- if (isatty(0))
- fprintf(stdout, "> ");
- if (!fgets(buf, 1024, stdin))
- return 0;
+ if (isatty(fileno(fp)))
+ fprintf(stderr, "> ");
+ if (!G_getl2(buf, sizeof(buf), fp))
+ break;
for (n = 0; buf[n]; n++)
if (buf[n] == ',')
buf[n] = ' ';
@@ -99,23 +107,17 @@
/* give warning when quant rules do not cover the whole range of map */
G_quant_get_limits(&quant_struct, &dmin, &dmax, &cmin, &cmax);
- if ((dmin > old_dmin || dmax < old_dmax) && !first) {
- fprintf(stdout,
- "Warning: quant rules do not cover the whole range map\n");
- if (G_yes(prompt, 1))
- continue;
- }
+ if ((dmin > old_dmin || dmax < old_dmax) && !first)
+ G_warning(_("quant rules do not cover the whole range map"));
break;
}
if (strcmp(buf, "help") == 0) {
- fprintf(stdout, "Enter a rule in one of these formats:\n");
- fprintf(stdout, "float_low:float_high:int_low:int_high\n");
- fprintf(stdout,
- "float_low:float_high:int_val (i.e. int_high == int_low)\n");
- fprintf(stdout,
- "*:float_val:int_val (interval [inf, float_val])\n");
- fprintf(stdout,
+ fprintf(stderr,
+ "Enter a rule in one of these formats:\n"
+ "float_low:float_high:int_low:int_high\n"
+ "float_low:float_high:int_val (i.e. int_high == int_low)\n"
+ "*:float_val:int_val (interval [inf, float_val])\n"
"float_val:*:int_val (interval [float_val, inf])\n");
}
@@ -152,10 +154,13 @@
break;
else
- fprintf(stderr, "%s is not a valid rule\n", buf);
+ G_warning(_("%s is not a valid rule"), buf);
break;
} /* switch */
} /* loop */
+ if (fp != stdin)
+ fclose(fp);
+
return nrules;
}
More information about the grass-commit
mailing list