Esempio n. 1
0
int main(int argc, char *argv[])
{
    struct file_info Current, Trans, Coord;

    struct GModule *module;

    struct Option *vold, *vnew, *pointsfile, *xshift, *yshift, *zshift,
	*xscale, *yscale, *zscale, *zrot, *columns, *table, *field;
    struct Flag *quiet_flag, *tozero_flag, *shift_flag, *print_mat_flag;

    char *mapset, mon[4], date[40], buf[1000];
    struct Map_info Old, New;
    int ifield;
    int day, yr;
    BOUND_BOX box;

    double ztozero;
    double trans_params[7];	/* xshift, ..., xscale, ..., zrot */

    /* columns */
    unsigned int i;
    int idx, out3d;
    char **tokens;
    char *columns_name[7];	/* xshift, yshift, zshift, xscale, yscale, zscale, zrot */

    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("vector, transformation");
    module->description =
	_("Performs an affine transformation (shift, scale and rotate, "
	  "or GPCs) on vector map.");

    /* remove in GRASS7 */
    quiet_flag = G_define_flag();
    quiet_flag->key = 'q';
    quiet_flag->description =
	_("Suppress display of residuals or other information");

    tozero_flag = G_define_flag();
    tozero_flag->key = 't';
    tozero_flag->description = _("Shift all z values to bottom=0");
    tozero_flag->guisection = _("Custom");

    print_mat_flag = G_define_flag();
    print_mat_flag->key = 'm';
    print_mat_flag->description =
	_("Print the transformation matrix to stdout");
    
    shift_flag = G_define_flag();
    shift_flag->key = 's';
    shift_flag->description =
	_("Instead of points use transformation parameters "
	  "(xshift, yshift, zshift, xscale, yscale, zscale, zrot)");
    shift_flag->guisection = _("Custom");
	
    vold = G_define_standard_option(G_OPT_V_INPUT);

    field = G_define_standard_option(G_OPT_V_FIELD);
    field->answer = "-1";
    
    vnew = G_define_standard_option(G_OPT_V_OUTPUT);

    pointsfile = G_define_standard_option(G_OPT_F_INPUT);
    pointsfile->key = "pointsfile";
    pointsfile->required = NO;
    pointsfile->label = _("ASCII file holding transform coordinates");
    pointsfile->description = _("If not given, transformation parameters "
				"(xshift, yshift, zshift, xscale, yscale, zscale, zrot) are used instead");

    pointsfile->gisprompt = "old_file,file,points";
    pointsfile->guisection = _("Points");
    
    xshift = G_define_option();
    xshift->key = "xshift";
    xshift->type = TYPE_DOUBLE;
    xshift->required = NO;
    xshift->multiple = NO;
    xshift->description = _("Shifting value for x coordinates");
    xshift->answer = "0.0";
    xshift->guisection = _("Custom");

    yshift = G_define_option();
    yshift->key = "yshift";
    yshift->type = TYPE_DOUBLE;
    yshift->required = NO;
    yshift->multiple = NO;
    yshift->description = _("Shifting value for y coordinates");
    yshift->answer = "0.0";
    yshift->guisection = _("Custom");

    zshift = G_define_option();
    zshift->key = "zshift";
    zshift->type = TYPE_DOUBLE;
    zshift->required = NO;
    zshift->multiple = NO;
    zshift->description = _("Shifting value for z coordinates");
    zshift->answer = "0.0";
    zshift->guisection = _("Custom");

    xscale = G_define_option();
    xscale->key = "xscale";
    xscale->type = TYPE_DOUBLE;
    xscale->required = NO;
    xscale->multiple = NO;
    xscale->description = _("Scaling factor for x coordinates");
    xscale->answer = "1.0";
    xscale->guisection = _("Custom");

    yscale = G_define_option();
    yscale->key = "yscale";
    yscale->type = TYPE_DOUBLE;
    yscale->required = NO;
    yscale->multiple = NO;
    yscale->description = _("Scaling factor for y coordinates");
    yscale->answer = "1.0";
    yscale->guisection = _("Custom");

    zscale = G_define_option();
    zscale->key = "zscale";
    zscale->type = TYPE_DOUBLE;
    zscale->required = NO;
    zscale->multiple = NO;
    zscale->description = _("Scaling factor for z coordinates");
    zscale->answer = "1.0";
    zscale->guisection = _("Custom");

    zrot = G_define_option();
    zrot->key = "zrot";
    zrot->type = TYPE_DOUBLE;
    zrot->required = NO;
    zrot->multiple = NO;
    zrot->description =
	_("Rotation around z axis in degrees counterclockwise");
    zrot->answer = "0.0";
    zrot->guisection = _("Custom");

    table = G_define_standard_option(G_OPT_TABLE);
    table->description =
	_("Name of table containing transformation parameters");
    table->guisection = _("Attributes");

    columns = G_define_option();
    columns->key = "columns";
    columns->type = TYPE_STRING;
    columns->required = NO;
    columns->multiple = NO;
    columns->label =
	_("Name of attribute column(s) used as transformation parameters");
    columns->description =
	_("Format: parameter:column, e.g. xshift:xs,yshift:ys,zrot:zr");
    columns->guisection = _("Attributes");

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    G_strcpy(Current.name, vold->answer);
    G_strcpy(Trans.name, vnew->answer);

    Vect_check_input_output_name(vold->answer, vnew->answer, GV_FATAL_EXIT);
    
    out3d = WITHOUT_Z;
    
    ifield = atoi(field->answer);

    if (shift_flag->answer)
	G_warning(_("The '%c' flag is deprecated and will be removed in future. "
		   "Transformation parameters are used automatically when no pointsfile is given."),
		  shift_flag->key);

    /* please remove in GRASS7 */
    if (quiet_flag->answer) {
	G_warning(_("The '%c' flag is deprecated and will be removed in future. "
		   "Please use '--quiet' instead."), quiet_flag->key);
	G_putenv("GRASS_VERBOSE", "0");
    }

    /* if a table is specified, require columns and layer */
    /* if columns are specified, but no table, require layer > 0 and use 
     * the table attached to that layer */
    if (table->answer && !columns->answer) {
	G_fatal_error(_("Column names are not defined. Please use '%s' parameter."),
		      columns->key);
    }

    if ((columns->answer || table->answer) && ifield < 1) {
	G_fatal_error(_("Please specify a valid layer with '%s' parameter."),
		      field->key);
    }

    if (table->answer && strcmp(vnew->answer, table->answer) == 0) {
	G_fatal_error(_("Name of table and name for output vector map must be different. "
		       "Otherwise the table is overwritten."));
    }

    if (!columns->answer && !table->answer)
	ifield = -1;

    if (pointsfile->answer != NULL && !shift_flag->answer) {
	G_strcpy(Coord.name, pointsfile->answer);
    }
    else {
	Coord.name[0] = '\0';
    }

    /* open coord file */
    if (Coord.name[0] != '\0') {
	if ((Coord.fp = fopen(Coord.name, "r")) == NULL)
	    G_fatal_error(_("Unable to open file with coordinates <%s>"),
			  Coord.name);
    }

    /* tokenize columns names */
    for (i = 0; i <= IDX_ZROT; i++) {
	columns_name[i] = NULL;
    }
    i = 0;
    if (columns->answer) {
	while (columns->answers[i]) {
	    tokens = G_tokenize(columns->answers[i], ":");
	    if (G_number_of_tokens(tokens) == 2) {
		if (strcmp(tokens[0], xshift->key) == 0)
		    idx = IDX_XSHIFT;
		else if (strcmp(tokens[0], yshift->key) == 0)
		    idx = IDX_YSHIFT;
		else if (strcmp(tokens[0], zshift->key) == 0)
		    idx = IDX_ZSHIFT;
		else if (strcmp(tokens[0], xscale->key) == 0)
		    idx = IDX_XSCALE;
		else if (strcmp(tokens[0], yscale->key) == 0)
		    idx = IDX_YSCALE;
		else if (strcmp(tokens[0], zscale->key) == 0)
		    idx = IDX_ZSCALE;
		else if (strcmp(tokens[0], zrot->key) == 0)
		    idx = IDX_ZROT;
		else
		    idx = -1;

		if (idx != -1)
		    columns_name[idx] = G_store(tokens[1]);

		G_free_tokens(tokens);
	    }
	    else {
		G_fatal_error(_("Unable to tokenize column string: [%s]"),
			      columns->answers[i]);
	    }
	    i++;
	}
    }

    /* determine transformation parameters */
    trans_params[IDX_XSHIFT] = atof(xshift->answer);
    trans_params[IDX_YSHIFT] = atof(yshift->answer);
    trans_params[IDX_ZSHIFT] = atof(zshift->answer);
    trans_params[IDX_XSCALE] = atof(xscale->answer);
    trans_params[IDX_YSCALE] = atof(yscale->answer);
    trans_params[IDX_ZSCALE] = atof(zscale->answer);
    trans_params[IDX_ZROT] = atof(zrot->answer);

    /* open vector maps */
    if ((mapset = G_find_vector2(vold->answer, "")) == NULL)
	G_fatal_error(_("Vector map <%s> not found"), vold->answer);

    Vect_open_old(&Old, vold->answer, mapset);
    
    /* should output be 3D ? 
     * note that z-scale and ztozero have no effect with input 2D */
    if (Vect_is_3d(&Old) || trans_params[IDX_ZSHIFT] != 0. ||
	columns_name[IDX_ZSHIFT])
	out3d = WITH_Z;

    Vect_open_new(&New, vnew->answer, out3d);
    
    /* copy and set header */
    Vect_copy_head_data(&Old, &New);

    Vect_hist_copy(&Old, &New);
    Vect_hist_command(&New);

    sprintf(date, "%s", G_date());
    sscanf(date, "%*s%s%d%*s%d", mon, &day, &yr);
    sprintf(date, "%s %d %d", mon, day, yr);
    Vect_set_date(&New, date);

    Vect_set_person(&New, G_whoami());

    sprintf(buf, "transformed from %s", vold->answer);
    Vect_set_map_name(&New, buf);

    Vect_set_scale(&New, 1);
    Vect_set_zone(&New, 0);
    Vect_set_thresh(&New, 0.0);

    /* points file */
    if (Coord.name[0]) {
	create_transform_from_file(&Coord, quiet_flag->answer);

	if (Coord.name[0] != '\0')
	    fclose(Coord.fp);
    }

    Vect_get_map_box(&Old, &box);

    /* z to zero */
    if (tozero_flag->answer)
	ztozero = 0 - box.B;
    else
	ztozero = 0;

    /* do the transformation */
    transform_digit_file(&Old, &New, Coord.name[0] ? 1 : 0,
			 ztozero, trans_params,
			 table->answer, columns_name, ifield);

    if (Vect_copy_tables(&Old, &New, 0))
        G_warning(_("Failed to copy attribute table to output map"));
    Vect_close(&Old);
    Vect_build(&New);

    if (!quiet_flag->answer) {
	Vect_get_map_box(&New, &box);
	G_message(_("\nNew vector map <%s> boundary coordinates:"),
		  vnew->answer);
	G_message(_(" N: %-10.3f    S: %-10.3f"), box.N, box.S);
	G_message(_(" E: %-10.3f    W: %-10.3f"), box.E, box.W);
	G_message(_(" B: %6.3f    T: %6.3f"), box.B, box.T);

	/* print the transformation matrix if requested */
	if (print_mat_flag->answer)
	    print_transform_matrix();
    }

    Vect_close(&New);

    G_done_msg(" ");

    exit(EXIT_SUCCESS);
}
Esempio n. 2
0
int read_text(char *east, char *north, char *text)
{
    PSCOLOR color, hcolor, background, border;
    int r, g, b;
    int ret;
    int xoffset;
    int yoffset;
    float size;
    int fontsize;
    double width;
    double hwidth;
    double rotate;
    int xref, yref;
    int opaque;
    char t1[128];
    char buf[1024];
    char *key, *data;
    FILE *fd;
    char fontname[128];

    set_color(&color, 0, 0, 0);	/* black */
    unset_color(&hcolor);
    unset_color(&background);
    unset_color(&border);
    opaque = TRUE;
    size = 0.0;
    fontsize = 0;
    xoffset = 0;
    yoffset = 0;
    width = 1.;
    hwidth = 0.;
    rotate = 0.0;
    xref = CENTER;
    yref = CENTER;
    G_strcpy(fontname, "Helvetica");

    while (*text == ' ' || *text == '\t')
	text++;
    if (*text == '\\')
	text++;
    if (*text == 0) {
	error("text", "", "no text given");
	gobble_input();
	return 0;
    }

    while (input(2, buf, help)) {
	if (!key_data(buf, &key, &data))
	    continue;

	if (KEY("font")) {
	    get_font(data);
	    strcpy(fontname, data);
	    continue;
	}

	if (KEY("color")) {
	    ret = G_str_to_color(data, &r, &g, &b);
	    if (ret == 1)
		set_color(&color, r, g, b);
	    else if (ret == 2)
		error(key, data, "primary color cannot be \"none\"");
	    else
		error(key, data, "illegal color request");

	    continue;
	}

	if (KEY("hcolor")) {
	    ret = G_str_to_color(data, &r, &g, &b);
	    if (ret == 1)
		set_color(&hcolor, r, g, b);
	    else if (ret == 2)
		unset_color(&hcolor);
	    else
		error(key, data, "illegal hcolor request");

	    if (color_none(&hcolor) || hwidth <= 0.)
		hwidth = 0.;
	    continue;
	}

	if (KEY("background")) {
	    ret = G_str_to_color(data, &r, &g, &b);
	    if (ret == 1)
		set_color(&background, r, g, b);
	    else if (ret == 2) {
		unset_color(&background);
		opaque = FALSE;
	    }
	    else
		error(key, data, "illegal background color request");

	    continue;
	}

	if (KEY("border")) {
	    ret = G_str_to_color(data, &r, &g, &b);
	    if (ret == 1)
		set_color(&border, r, g, b);
	    else if (ret == 2)
		unset_color(&border);
	    else
		error(key, data, "illegal border color request");

	    continue;
	}

	if (KEY("opaque")) {
	    opaque = yesno(key, data);
	    continue;
	}

	if (KEY("width")) {
	    width = -1.;
	    *t1 = 0;
	    if (sscanf(data, "%lf%1s", &width, t1) < 1 || width < 0.) {
		width = 1.;
		error(key, data, "illegal width request");
	    }
	    if (t1 && t1[0] == 'i')
		width = width / 72.0;
	    continue;
	}

	if (KEY("hwidth")) {
	    hwidth = -1.;
	    *t1 = 0;
	    if (sscanf(data, "%lf%1s", &hwidth, t1) < 1 || hwidth < 0.) {
		hwidth = 0.;
		error(key, data, "illegal width request");
	    }
	    if (t1 && t1[0] == 'i')
		hwidth = hwidth / 72.0;
	    continue;
	}

	if (KEY("size")) {
	    double x;

	    if (!scan_resolution(data, &x)) {
		size = 0.0;
		error(key, data, "illegal size request");
	    }
	    else
		size = x;
	    continue;
	}

	if (KEY("fontsize")) {
	    if (sscanf(data, "%d", &fontsize) != 1 || fontsize <= 0) {
		error(key, data, "illegal fontsize request");
	    }
	    else
		continue;
	}

	if (KEY("xoffset")) {
	    *t1 = 0;
	    if (sscanf(data, "%d%1s", &xoffset, t1) != 1 || *t1) {
		xoffset = 0;
		error(key, data, "illegal request (text)");
	    }
	    continue;
	}

	if (KEY("yoffset")) {
	    *t1 = 0;
	    if (sscanf(data, "%d%1s", &yoffset, t1) != 1 || *t1) {
		yoffset = 0;
		error(key, data, "illegal request (text)");
	    }
	    continue;
	}

	if (KEY("rotate")) {
	    if (sscanf(data, "%lf", &rotate) != 1) {
		rotate = 0.0;
		error(key, data, "illegal rotate request");
	    }
	    continue;
	}

	if (KEY("ref")) {
	    if (!scan_ref(data, &xref, &yref)) {
		xref = CENTER;
		yref = CENTER;
		error(key, data, "illegal ref request");
	    }
	    continue;
	}

	error(key, data, "illegal request (text)");
    }

    /* if file doesn't exist create it and close it */
    if (labels.other == NULL) {
	labels.other = G_tempfile();
	if ((fd = fopen(labels.other, "w")) != NULL)
	    fclose(fd);
    }

    /* open file in append mode */
    fd = fopen(labels.other, "a");
    if (fd == NULL) {
	error("misc labels file", "", "can't open");
	return 1;
    }

    /* write the file */
    fprintf(fd, "font: %s\n", fontname);
    fprintf(fd, "east: %s\n", east);
    fprintf(fd, "north: %s\n", north);
    fprintf(fd, "xoffset: %d\n", xoffset);
    fprintf(fd, "yoffset: %d\n", yoffset);
    fprintf(fd, "width: %f\n", width);
    fprintf(fd, "hwidth: %f\n", hwidth);
    fprintf(fd, "size: %f\n", size);
    fprintf(fd, "fontsize: %d\n", fontsize);
    fprintf(fd, "opaque: %s\n", opaque ? "yes" : "no");
    if (rotate != 0)
	fprintf(fd, "rotate: %f\n", rotate);

    fprintf(fd, "color: ");
    if (!color_none(&color))
	fprintf(fd, "%d:%d:%d\n", color.r, color.g, color.b);
    else
	fprintf(fd, "black\n");

    fprintf(fd, "hcolor: ");
    if (!color_none(&hcolor))
	fprintf(fd, "%d:%d:%d\n", hcolor.r, hcolor.g, hcolor.b);
    else
	fprintf(fd, "none\n");

    fprintf(fd, "background: ");
    if (!color_none(&background))
	fprintf(fd, "%d:%d:%d\n", background.r, background.g, background.b);
    else
	fprintf(fd, "none\n");


    fprintf(fd, "border: ");
    if (!color_none(&border))
	fprintf(fd, "%d:%d:%d\n", border.r, border.g, border.b);
    else
	fprintf(fd, "none\n");

    fprintf(fd, "ref: ");
    switch (yref) {
    case UPPER:
	fprintf(fd, "upper");
	break;
    case LOWER:
	fprintf(fd, "lower");
	break;
    case CENTER:
	fprintf(fd, "center");
	break;
    }
    switch (xref) {
    case LEFT:
	fprintf(fd, " left");
	break;
    case RIGHT:
	fprintf(fd, " right");
	break;
    case CENTER:
	fprintf(fd, "%s", (xref == CENTER) ? "" : " center");
	break;
    }
    fprintf(fd, "\n");
    fprintf(fd, "text:%s\n\n", text);
    fclose(fd);

    return 0;
}
Esempio n. 3
0
int
main (int argc, char *argv[]) {
	struct GModule *module;
	struct Option *map; /* raster map to work on */
	struct Option *sites; /* output map to write */
	struct Option *mode; /* Categorisation mode */
	struct Option *min; /* range of values to categorise ... */
	struct Option *max; /*   .. anything outside will be NULL in output */
	struct Option *logfile; /* log output to this file instead of stdout */
	struct Option *precision; /* decimal digits precision for log file */
	struct Option *cachesize;
	struct Flag *null; /* also count NULL values? */
	struct Flag *all; /* disregard region when reporting total percentages */			
	struct Flag *zeroskip;	/* also show categories with 0 count? */
	struct Flag *uncat; /* also show cells outside category range? */	
	struct Flag *background; /* show background distribution? */
	struct Flag *gain; /* calculate Kvamme's gain for each category? */
	struct Flag *quiet; /* no status display on screen */				
	
	char *mapset;
	int show_progress = 1; /* enable progress display by default */
	/* these vars are used to store information about the input map */
	int cats; /* number of categories in input map */
	long null_count; /* number of NULL cells */
	long nocat_count; /* number of cells that do not fall into the category range [0 .. n] */	
	/* use to create command line for r.categorize call */
	char *sysstr, *tmpfile, *input_map;	
	int error;
	
	sysstr = NULL;
	tmpfile = NULL;
	
	/* setup some basic GIS stuff */
	G_gisinit (argv[0]);
	module = G_define_module ();
	module->description = "Analyses distribution of vector sample over a raster map";
	/* do not pause after a warning message was displayed */
	G_sleep_on_error (0);
		
	/* DEFINE OPTIONS AND FLAGS */
	/* raster map to sample */
	map = G_define_standard_option (G_OPT_R_INPUT);
	map->key = "raster";
	map->type = TYPE_STRING;
	map->required = YES;
	map->description = "Raster map to sample";

	/* site map for sampling positions */
	sites = G_define_standard_option (G_OPT_V_INPUT);
	sites->key = "positions";
	sites->type = TYPE_STRING;
	sites->required = YES;
	sites->description = "Vector map with sampling positions";
		
	/* Categorisation mode */
	mode = G_define_option ();
	mode->key = "mode";
	mode->type = TYPE_STRING;
	mode->required = NO;
	mode->description = "Categorisation mode (see manual)";

	/* min raster value to categorise */
	min = G_define_option ();
	min->key = "min";
	min->type = TYPE_DOUBLE;
	min->required = NO;
	min->description = "Minimum value to include in categorisation";

	/* max raster value to categorise */
	max = G_define_option ();
	max->key = "max";
	max->type = TYPE_DOUBLE;
	max->required = NO;
	max->description = "Maximum value to include in categorisation.";

	/* optional name of logfile */
	logfile = G_define_option ();
	logfile->key = "logfile";
	logfile->type = TYPE_DOUBLE;
	logfile->required = NO;
	logfile->description = "Name of ASCII logfile, if desired.";
	
	/* optional number of decimal digitis to store in logfile/show on screen */
	precision = G_define_option ();
	precision->key = "precision";
	precision->type = TYPE_DOUBLE;
	precision->required = NO;
	precision->answer = "2";
	precision->description = "Number of decimal digits for statistics/logfile";		
	
	/* number of lines to store in cache */
	cachesize = G_define_option ();
	cachesize->key = "cachesize";
	cachesize->type = TYPE_INTEGER;
	cachesize->answer = "-1";
	cachesize->required = NO;
	cachesize->description = "Number of raster rows to store in cache (-1 for auto)";	
		
	null = G_define_flag ();
	null->key = 'n';
	null->description = "Include NULL cells in report.";
	
	uncat = G_define_flag ();
	uncat->key = 'u';
	uncat->description = "Include uncategorised cells in statistics.";
	
	all = G_define_flag ();
	all->key = 'a';
	all->description = "Disregard region setting for counts.";	
	
	zeroskip = G_define_flag ();
	zeroskip->key = 'z';
	zeroskip->description = "Also show categories with zero count/percentage.";

	background = G_define_flag ();
	background->key = 'b';
	background->description = "Show background distribution of categories in raster input map.";

	gain = G_define_flag ();
	gain->key = 'g';
	gain->description = "Calculate Kvamme's Gain Factor for each category.";

	quiet = G_define_flag ();
	quiet->key = 'q';
	quiet->description = "Quiet operation: do not show progress display.";	

	
	/* parse command line */
	if (G_parser (argc, argv))
	{
		exit (-1);
	}

	/* check for 'quiet' flag */
	if ( quiet->answer ) {
		show_progress = 0;
	}
	
	PROGNAME = argv[0];		

	/* copy the 'map' option string to another buffer and use that */
	/* from now on. We do this because we might want to manipulate */
	/* that string and that is dangerous with GRASS Option->answer strings! */
	input_map = G_calloc (255, sizeof (char));
	G_strcpy (input_map, map->answer);
	
	mapset = G_calloc (255, sizeof (char));
	/* check if input map is a fully categorised raster map */
	mapset = G_find_cell (input_map,"");
	if ( mapset == NULL) {
		G_fatal_error ("The input map does not exist in the current database.");
	}

	if ( mode->answer == NULL ) {		
		cats = GT_get_stats (input_map,mapset,&null_count, &nocat_count, show_progress);
		if ( cats < 0 ) {
			G_fatal_error ("Could not stat input map. Do you have read access?");
		}
		if ( ((cats == 0) && (mode->answer==NULL)) ) {
			G_fatal_error ("No categories defined in input map.\nPlease specify a mode to create a fully classified map.");
		}
	}

	/* a classification mode was given: call r.categorize and save output
	   in a tmp file */
	if ( mode->answer != NULL ) {
		srand ((unsigned long) time(NULL));
		tmpfile = G_calloc (255, sizeof(char));
		sysstr = G_calloc (255, sizeof(char));
		/* create tmp file name from current PID and system time */		
		sprintf (tmpfile,"tmp.%i.%i", getpid(),(rand())/10000);
		if ( getenv("GISBASE") == NULL ) {
			G_fatal_error ("GISBASE not set. Unable to call GRASS module 'r.categorise'.");
		}		
		if ( quiet->answer ) {
			sprintf (sysstr,"%s/bin/r.categorize input=%s@%s output=%s mode=%s min=%s max=%s -q", getenv("GISBASE"), map->answer, 
					mapset, tmpfile, mode->answer, min->answer, max->answer);
		} else {
			sprintf (sysstr,"%s/bin/r.categorize input=%s@%s output=%s mode=%s min=%s max=%s", getenv("GISBASE"), map->answer, 
					mapset, tmpfile, mode->answer, min->answer, max->answer);
		}
		/* now create fully classified map using r.categorize and store */
		/* it in a temporary raster map */
		error = system (sysstr);
		if ( error < 0 ) {
			G_fatal_error ("Calling r.categorize has failed. Check your installation.");
		}
				
		/* store name of temporary file as new input map */
		G_strcpy (input_map, tmpfile);
		
		/* check categories of tmpfile */
		cats = GT_get_stats (input_map,mapset,&null_count, &nocat_count, show_progress);
		if (cats < 1) {
			G_fatal_error ("Could not create a fully categorised temporary file.\nTry another categorisation mode.");
		}
	}
	
	/* initialise cache structure */
	if ( input_map != NULL ) {
		/* set cachesize */
		CACHESIZE = atoi (cachesize->answer);
		if ( (CACHESIZE<-1) || (CACHESIZE > G_window_rows ()) ) {
			/* if cache size is invalid, just set to auto-mode (-1) */
			G_warning ("Invalid cache size requested (must be between 0 and %i or -1).\n", G_window_rows());
			CACHESIZE = -1;
		}
	}			
	
	if ( G_raster_map_is_fp (input_map, mapset))  {
		do_report_DCELL (input_map, mapset, sites->answer,
				atoi (precision->answer),null->answer, uncat->answer,
	           	all->answer, quiet->answer, zeroskip->answer,
			   	logfile->answer, background->answer, gain->answer, show_progress);
	} else {
		do_report_CELL (input_map, mapset, sites->answer,
				atoi (precision->answer),null->answer, uncat->answer,
	           	all->answer, quiet->answer, zeroskip->answer,
			   	logfile->answer, background->answer, gain->answer, show_progress);		
	}

		
	/* delete temporary file, if needed */
	if ( mode->answer != NULL ) {
		delete_tmpfile (tmpfile);
		G_free (tmpfile);
	}
	
	if ( sysstr != NULL ) {
		G_free (sysstr);
	}
	G_free (input_map);

	return (EXIT_SUCCESS);
}
Esempio n. 4
0
void user_input(int argc, char **argv)
{
    int i;

    /* setup the GRASS parsing routine
       structures to be used to read 
       in the user's parameter choices */
    struct Flag *units;
    struct Flag *zscore;
    struct Flag *edgemap;
    struct Option *name;
    struct Option *sampling_method;
    struct Option *region;
    struct Option *att;
    struct Option *diversity;
    struct Option *measure_code;
    struct Option *method_code;
    struct Option *juxtaposition;
    struct Option *edge;


    /* use the GRASS parsing routines to read in the user's parameter choices */

    edgemap = G_define_flag();
    edgemap->key = 'e';
    edgemap->description =
	"Output map 'edge' of edges given a '1' in r.le.para/edge file";

    units = G_define_flag();
    units->key = 'u';
    units->description =
	"Output maps 'units_x' with sampling units for each scale x ";

    zscore = G_define_flag();
    zscore->key = 'z';
    zscore->description = "Output map 'zscores' with standardized scores";

    name = G_define_option();
    name->key = "map";
    name->description = "Raster map to be analyzed";
    name->type = TYPE_STRING;
    name->gisprompt = "old,cell,raster";
    name->required = YES;

    sampling_method = G_define_option();
    sampling_method->answer = "w";
    sampling_method->key = "sam";
    sampling_method->description = "Sampling method (choose only 1 method):\n"
	"\tw = whole map      u = units        m = moving window   r = regions";
    sampling_method->type = TYPE_STRING;
    sampling_method->multiple = NO;
    sampling_method->required = NO;

    region = G_define_option();
    region->key = "reg";
    region->description =
	"Name of regions map, only when sam = r; omit otherwise";
    region->type = TYPE_STRING;
    region->gisprompt = "old,cell,raster";
    region->required = NO;

    att = G_define_option();
    att->key = "att";
    att->description =
	"b1 = mn. pixel att.                 b2 = s.d. pixel att.\n"
	"\tb3 = min. pixel att.                b4 = max. pixel att.";
    att->options = "b1,b2,b3,b4";
    att->type = TYPE_STRING;
    att->multiple = YES;
    att->required = NO;

    diversity = G_define_option();
    diversity->key = "div";
    diversity->description =
	"d1 = richness      d2 = Shannon     d3 = dominance     d4 = inv. Simpson";
    diversity->options = "d1,d2,d3,d4";
    diversity->type = TYPE_STRING;
    diversity->multiple = YES;
    diversity->required = NO;

    method_code = G_define_option();
    method_code->key = "te1";
    method_code->description = "Texture method (choose only 1 method):\n"
	"\tm1 = 2N-H          m2 = 2N-45       m3 = 2N-V          m4 = 2N-135\n"
	"\tm5 = 4N-HV         m6 = 4N-DIAG     m7 = 8N";
    method_code->options = "m1,m2,m3,m4,m5,m6,m7";
    method_code->type = TYPE_STRING;
    method_code->multiple = NO;
    method_code->required = NO;

    measure_code = G_define_option();
    measure_code->key = "te2";
    measure_code->description =
	"Texture measures (required if te1 was specified):\n"
	"\tt1 = contagion           t2 = ang. sec. mom.     t3 = inv. diff. mom.\n"
	"\tt4 = entropy             t5 = contrast";
    measure_code->options = "t1,t2,t3,t4,t5";
    measure_code->type = TYPE_STRING;
    measure_code->multiple = YES;
    measure_code->required = NO;

    juxtaposition = G_define_option();
    juxtaposition->key = "jux";
    juxtaposition->description =
	"Juxtaposition measures (weight file in r.le.para needed):\n"
	"\tj1 = mn. juxtaposition              j2 = s.d. juxtaposition";
    juxtaposition->options = "j1,j2";
    juxtaposition->type = TYPE_STRING;
    juxtaposition->multiple = YES;
    juxtaposition->required = NO;

    edge = G_define_option();
    edge->key = "edg";
    edge->description =
	"e1 = sum of edges  e2 = sum of edges by type (need edge file: r.le.para)";
    edge->options = "e1,e2";
    edge->type = TYPE_STRING;
    edge->multiple = YES;
    edge->required = NO;


    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    /* record the user inputs for map,
       sam, run, and out parameters */

    G_strcpy(choice->fn, name->answer);

    choice->wrum = sampling_method->answer[0];


    /* check for unacceptable values for
       input parameters */

    if (strcmp(sampling_method->answer, "w") &&
	strcmp(sampling_method->answer, "u") &&
	strcmp(sampling_method->answer, "m") &&
	strcmp(sampling_method->answer, "r")) {
	fprintf(stdout, "\n");
	fprintf(stdout,
		"   ***************************************************\n");
	fprintf(stdout,
		"    You input an unacceptable value for parameter sam \n");
	fprintf(stdout,
		"   ***************************************************\n");
	exit(EXIT_FAILURE);
    }

    /* check for multiple values for te1 */

    if (method_code->answer)
	if (method_code->answers[1]) {
	    fprintf(stdout, "\n");
	    fprintf(stdout,
		    "   **********************************************\n");
	    fprintf(stdout,
		    "    You input multiple values for parameter te1, \n");
	    fprintf(stdout,
		    "    but only one is allowed                      \n");
	    fprintf(stdout,
		    "   **********************************************\n");
	    exit(EXIT_FAILURE);
	}


    /* if the -u flag is specified, then
       set the choice->units flag to 1 */

    choice->units = 0;
    if (!strcmp(sampling_method->answer, "u") && units->answer)
	choice->units = 1;
    else if (strcmp(sampling_method->answer, "u") && units->answer) {
	fprintf(stdout, "\n");
	fprintf(stdout,
		"   ***************************************************\n");
	fprintf(stdout,
		"    You requested output of map 'units' with sampling \n");
	fprintf(stdout,
		"    units, by using flag -u, but this option is only  \n");
	fprintf(stdout,
		"    available when sam=u                              \n");
	fprintf(stdout,
		"   ***************************************************\n");
	exit(EXIT_FAILURE);
    }

    /* if sampling_method is by REGION
       get region file name.  Check to see
       that the name was input */

    if (!strcmp(sampling_method->answer, "r")) {
	if (region->answer)
	    G_strcpy(choice->reg, region->answer);
	else {
	    fprintf(stdout, "\n");
	    fprintf(stdout,
		    "   ***********************************************\n");
	    fprintf(stdout,
		    "    You requested sampling by region, but did not \n");
	    fprintf(stdout,
		    "    input the name of the region using the reg=   \n");
	    fprintf(stdout,
		    "    parameter                                     \n");
	    fprintf(stdout,
		    "   ***********************************************\n");
	    exit(EXIT_FAILURE);
	}
    }

    if (region->answer)
	if (strcmp(sampling_method->answer, "r")) {
	    fprintf(stdout, "\n");
	    fprintf(stdout,
		    "   ***********************************************\n");
	    fprintf(stdout,
		    "    You requested sampling by region, by using    \n");
	    fprintf(stdout,
		    "    the reg= parameter, but did not input the     \n");
	    fprintf(stdout,
		    "    sam=r parameter                               \n");
	    fprintf(stdout,
		    "   ***********************************************\n");
	    exit(EXIT_FAILURE);
	}

    /* initialize flag arrays in choice
       data structure. */

    for (i = 0; i < 5; i++)
	choice->att[i] = 0;
    for (i = 0; i < 5; i++)
	choice->div[i] = 0;
    for (i = 0; i < 6; i++)
	choice->te2[i] = 0;
    for (i = 0; i < 3; i++)
	choice->jux[i] = 0;
    for (i = 0; i < 3; i++)
	choice->edg[i] = 0;

    /* fill measure_code and method
       code arrays */


    if (att->answer) {
	choice->att[0] = 1;
	for (i = 0; att->answers[i] != NULL; i++) {
	    if (!strcmp(att->answers[i], "b1"))
		choice->att[1] = 1;
	    else if (!strcmp(att->answers[i], "b2"))
		choice->att[2] = 1;
	    else if (!strcmp(att->answers[i], "b3"))
		choice->att[3] = 1;
	    else if (!strcmp(att->answers[i], "b4"))
		choice->att[4] = 1;
	}
    }

    if (edgemap->answer && choice->wrum == 'w') {
	choice->edgemap = edgemap->answer;
	choice->edg[0] = 1;
	choice->edg[2] = 1;
    }
    else if (edgemap->answer && choice->wrum != 'w') {
	fprintf(stdout, "\n");
	fprintf(stdout,
		"   ****************************************************\n");
	fprintf(stdout,
		"    An edge map (flag is -e) is not available unless   \n");
	fprintf(stdout,
		"    sam=w                                              \n");
	fprintf(stdout,
		"   ****************************************************\n");
	exit(EXIT_FAILURE);
    }

    if (zscore->answer && choice->wrum == 'w') {
	choice->z = zscore->answer;
	choice->att[0] = 1;
	choice->att[1] = 1;
	choice->att[2] = 1;
    }
    else if (zscore->answer && choice->wrum != 'w') {
	fprintf(stdout, "\n");
	fprintf(stdout,
		"   ****************************************************\n");
	fprintf(stdout,
		"    A zscores map (flag is -z) is not available unless \n");
	fprintf(stdout,
		"    sam=w                                              \n");
	fprintf(stdout,
		"   ****************************************************\n");
	exit(EXIT_FAILURE);
    }

    if (diversity->answer) {
	choice->div[0] = 1;
	for (i = 0; diversity->answers[i] != NULL; i++) {
	    if (!strcmp(diversity->answers[i], "d1"))
		choice->div[1] = 1;
	    else if (!strcmp(diversity->answers[i], "d2"))
		choice->div[2] = 1;
	    else if (!strcmp(diversity->answers[i], "d3"))
		choice->div[3] = 1;
	    else if (!strcmp(diversity->answers[i], "d4"))
		choice->div[4] = 1;
	}
    }

    choice->tex = 0;
    if (measure_code->answer || method_code->answer) {
	if (measure_code->answer && method_code->answer) {
	    choice->te2[0] = 1;
	    for (i = 0; measure_code->answers[i] != NULL; i++) {
		if (!strcmp(measure_code->answers[i], "t1"))
		    choice->te2[1] = 1;
		else if (!strcmp(measure_code->answers[i], "t2"))
		    choice->te2[2] = 1;
		else if (!strcmp(measure_code->answers[i], "t3"))
		    choice->te2[3] = 1;
		else if (!strcmp(measure_code->answers[i], "t4"))
		    choice->te2[4] = 1;
		else if (!strcmp(measure_code->answers[i], "t5"))
		    choice->te2[5] = 1;
	    }
	    if (!strcmp(method_code->answer, "m1"))
		choice->tex = 1;
	    else if (!strcmp(method_code->answer, "m2"))
		choice->tex = 2;
	    else if (!strcmp(method_code->answer, "m3"))
		choice->tex = 3;
	    else if (!strcmp(method_code->answer, "m4"))
		choice->tex = 4;
	    else if (!strcmp(method_code->answer, "m5"))
		choice->tex = 5;
	    else if (!strcmp(method_code->answer, "m6"))
		choice->tex = 6;
	    else if (!strcmp(method_code->answer, "m7"))
		choice->tex = 7;
	}
	else {
	    fprintf(stdout, "\n");
	    fprintf(stdout,
		    "   ************************************************\n");
	    fprintf(stdout,
		    "    You requested texture measurement, but did not \n");
	    fprintf(stdout,
		    "    input both parameter te1 and te2               \n");
	    fprintf(stdout,
		    "   ************************************************\n");
	    exit(EXIT_FAILURE);
	}
    }

    if (juxtaposition->answer) {
	choice->jux[0] = 1;
	for (i = 0; juxtaposition->answers[i] != NULL; i++) {
	    if (!strcmp(juxtaposition->answers[i], "j1"))
		choice->jux[1] = 1;
	    else if (!strcmp(juxtaposition->answers[i], "j2"))
		choice->jux[2] = 1;
	}
    }

    if (edge->answer) {
	choice->edg[0] = 1;
	for (i = 0; edge->answers[i] != NULL; i++) {
	    if (!strcmp(edge->answers[i], "e1"))
		choice->edg[1] = 1;
	    else if (!strcmp(edge->answers[i], "e2"))
		choice->edg[2] = 1;
	}
    }

    if (!att->answer && !diversity->answer && !measure_code->answer &&
	!juxtaposition->answer && !edge->answer && !zscore->answer &&
	!edgemap->answer) {
	fprintf(stdout, "\n");
	fprintf(stdout,
		"   **************************************************\n");
	fprintf(stdout,
		"    You did not select any measures to be calculated \n");
	fprintf(stdout,
		"   **************************************************\n");
	exit(EXIT_FAILURE);
    }

    return;
}
Esempio n. 5
0
int open_machine_layer(char * name,Pointer  g_db) {
 char  complete_name[400];
   G_strcpy(complete_name,DirOrigin);
   G_strcat(complete_name,name);
	return msg_id(sqlite3_open(complete_name,(sqlite3 **)g_db));}
Esempio n. 6
0
char *G_site_format(const Site * s, const char *fs, int id)
/* sprintf analog to G_site_put with the addition of a field separator fs 
   and option of printing site attribute identifiers
 */
{
    char ebuf[MAX_SITE_STRING], nbuf[MAX_SITE_STRING];
    char xbuf[MAX_SITE_STRING];
    const char *nfs;
    char *buf;
    int fmt, i, j, k;

    buf = (char *)G_malloc(MAX_SITE_LEN * sizeof(char));

    fmt = G_projection();

    G_format_northing(s->north, nbuf, fmt);
    G_format_easting(s->east, ebuf, fmt);

    nfs = (char *)((fs == (char *)NULL) ? "|" : fs);

    sprintf(buf, "%s%s%s", ebuf, nfs, nbuf);

    for (i = 0; i < s->dim_alloc; ++i) {
	format_double(s->dim[i], nbuf);
	sprintf(xbuf, "%s%s", nfs, nbuf);
	G_strcat(buf, xbuf);
    }

    nfs = (fs == NULL) ? " " : fs;

    switch (s->cattype) {
    case CELL_TYPE:
	sprintf(xbuf, "%s%s%d ", nfs, ((id == 0) ? "" : "#"), (int)s->ccat);
	G_strcat(buf, xbuf);
	break;
    case FCELL_TYPE:
    case DCELL_TYPE:
	sprintf(xbuf, "%s%s%g ", nfs, ((id == 0) ? "" : "#"), (float)s->fcat);
	G_strcat(buf, xbuf);
	break;
    }

    for (i = 0; i < s->dbl_alloc; ++i) {
	format_double(s->dbl_att[i], nbuf);
	sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "%"), nbuf);
	G_strcat(buf, xbuf);
    }

    for (i = 0; i < s->str_alloc; ++i) {
	if (strlen(s->str_att[i]) != 0) {
	    /* escape double quotes */
	    j = k = 0;

	    /* do not uncomment this code because sites file was created
	     * as we want. So it's enough to print them out as it is.
	     *
	     if (G_index (s->str_att[i], DQUOTE) != (char *) NULL)
	     {
	     while (!isnull(s->str_att[i][j]))
	     {
	     if (isquote(s->str_att[i][j]))
	     {
	     xbuf[k++] = BSLASH;
	     xbuf[k++] = DQUOTE;
	     }
	     else
	     xbuf[k++] = s->str_att[i][j];
	     j++;
	     }
	     xbuf[k] = (char) NULL;
	     }
	     else
	     */

	    G_strcpy(xbuf, s->str_att[i]);

	    G_strcpy(s->str_att[i], xbuf);

	    if (G_index(s->str_att[i], SPACE) != (char *)NULL)
		sprintf(xbuf, "%s%s\"%s\"", nfs, ((id == 0) ? "" : "@"),
			s->str_att[i]);
	    else
		sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "@"),
			s->str_att[i]);
	    G_strcat(buf, xbuf);
	}
    }
    return buf;
}
Esempio n. 7
0
int main(int argc, char **argv)
{
    char *mapset;
    int ret, level;
    int i, stat = 0, type, display;
    int chcat = 0;
    int r, g, b;
    int has_color, has_fcolor;
    struct color_rgb color, fcolor;
    double size;
    int default_width;
    double width_scale;
    int verbose = FALSE;
    double minreg, maxreg, reg;
    char map_name[128];
    struct GModule *module;
    struct Option *map_opt;
    struct Option *color_opt, *fcolor_opt, *rgbcol_opt, *zcol_opt;
    struct Option *type_opt, *display_opt;
    struct Option *icon_opt, *size_opt, *sizecolumn_opt, *rotcolumn_opt;
    struct Option *where_opt;
    struct Option *field_opt, *cat_opt, *lfield_opt;
    struct Option *lcolor_opt, *bgcolor_opt, *bcolor_opt;
    struct Option *lsize_opt, *font_opt, *xref_opt, *yref_opt;
    struct Option *attrcol_opt, *maxreg_opt, *minreg_opt;
    struct Option *width_opt, *wcolumn_opt, *wscale_opt;
    struct Option *render_opt;
    struct Flag *verbose_flag;	/* please remove before GRASS 7 released */
    struct Flag *id_flag, *table_acolors_flag, *cats_acolors_flag, *x_flag,
	*zcol_flag;
    struct cat_list *Clist;
    int *cats, ncat;
    LATTR lattr;
    struct Map_info Map;
    struct field_info *fi;
    dbDriver *driver;
    dbHandle handle;
    struct Cell_head window;
    BOUND_BOX box;
    double overlap;

    /* Initialize the GIS calls */
    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("display, vector");
    module->description = _("Displays user-specified vector map "
			    "in the active graphics frame.");
    
    map_opt = G_define_standard_option(G_OPT_V_MAP);

    display_opt = G_define_option();
    display_opt->key = "display";
    display_opt->type = TYPE_STRING;
    display_opt->required = YES;
    display_opt->multiple = YES;
    display_opt->answer = "shape";
    display_opt->options = "shape,cat,topo,dir,attr,zcoor";
    display_opt->description = _("Display");
    display_opt->descriptions = _("shape;Display geometry of features;"
				  "cat;Display category numbers of features;"
				  "topo;Display topology information (nodes, edges);"
				  "dir;Display direction of linear features;"
				  "attr;Display selected attribute based on 'attrcol';"
				  "zcoor;Display z-coordinate of features (only for 3D vector maps)");
    
    /* Query */
    type_opt = G_define_standard_option(G_OPT_V_TYPE);
    type_opt->answer = "point,line,boundary,centroid,area,face";
    type_opt->options = "point,line,boundary,centroid,area,face";
    type_opt->guisection = _("Selection");

    field_opt = G_define_standard_option(G_OPT_V_FIELD);
    field_opt->label =
	_("Layer number (if -1, all layers are displayed)");
    field_opt->gisprompt = "old_layer,layer,layer_all";
    field_opt->guisection = _("Selection");

    cat_opt = G_define_standard_option(G_OPT_V_CATS);
    cat_opt->guisection = _("Selection");

    where_opt = G_define_standard_option(G_OPT_WHERE);
    where_opt->guisection = _("Selection");

    /* Colors */
    color_opt = G_define_option();
    color_opt->key = "color";
    color_opt->type = TYPE_STRING;
    color_opt->answer = DEFAULT_FG_COLOR;
    color_opt->label = _("Feature color");
    color_opt->guisection = _("Colors");
    color_opt->gisprompt = "old_color,color,color_none";
    color_opt->description =
	_("Either a standard GRASS color, R:G:B triplet, or \"none\"");

    fcolor_opt = G_define_option();
    fcolor_opt->key = "fcolor";
    fcolor_opt->type = TYPE_STRING;
    fcolor_opt->answer = "200:200:200";
    fcolor_opt->label = _("Area fill color");
    fcolor_opt->guisection = _("Colors");
    fcolor_opt->gisprompt = "old_color,color,color_none";
    fcolor_opt->description =
	_("Either a standard GRASS color, R:G:B triplet, or \"none\"");

    rgbcol_opt = G_define_standard_option(G_OPT_COLUMN);
    rgbcol_opt->key = "rgb_column";
    rgbcol_opt->guisection = _("Colors");
    rgbcol_opt->description = _("Name of color definition column (for use with -a flag)");
    rgbcol_opt->answer = "GRASSRGB";

    zcol_opt = G_define_option();
    zcol_opt->key = "zcolor";
    zcol_opt->key_desc = "style";
    zcol_opt->type = TYPE_STRING;
    zcol_opt->required = NO;
    zcol_opt->description = _("Type of color table (for use with -z flag)");
    zcol_opt->answer = "terrain";
    zcol_opt->guisection = _("Colors");

    /* Lines */
    width_opt = G_define_option();
    width_opt->key = "width";
    width_opt->type = TYPE_INTEGER;
    width_opt->answer = "0";
    width_opt->guisection = _("Lines");
    width_opt->description = _("Line width");

    wcolumn_opt = G_define_standard_option(G_OPT_COLUMN);
    wcolumn_opt->key = "wcolumn";
    wcolumn_opt->guisection = _("Lines");
    wcolumn_opt->description =
	_("Name of column for line widths (these values will be scaled by wscale)");

    wscale_opt = G_define_option();
    wscale_opt->key = "wscale";
    wscale_opt->type = TYPE_DOUBLE;
    wscale_opt->answer = "1";
    wscale_opt->guisection = _("Lines");
    wscale_opt->description = _("Scale factor for wcolumn");

    /* Symbols */
    icon_opt = G_define_option();
    icon_opt->key = "icon";
    icon_opt->type = TYPE_STRING;
    icon_opt->required = NO;
    icon_opt->multiple = NO;
    icon_opt->guisection = _("Symbols");
    icon_opt->answer = "basic/x";
    /* This could also use ->gisprompt = "old,symbol,symbol" instead of ->options */
    icon_opt->options = icon_files();
    icon_opt->description = _("Point and centroid symbol");

    size_opt = G_define_option();
    size_opt->key = "size";
    size_opt->type = TYPE_DOUBLE;
    size_opt->answer = "5";
    size_opt->guisection = _("Symbols");
    size_opt->label = _("Symbol size");
    size_opt->description =
	_("When used with the size_column option this becomes the scale factor");

    sizecolumn_opt = G_define_standard_option(G_OPT_COLUMN);
    sizecolumn_opt->key = "size_column";
    sizecolumn_opt->guisection = _("Symbols");
    sizecolumn_opt->description =
	_("Name of numeric column containing symbol size");

    rotcolumn_opt = G_define_standard_option(G_OPT_COLUMN);
    rotcolumn_opt->key = "rot_column";
    rotcolumn_opt->guisection = _("Symbols");
    rotcolumn_opt->label =
	_("Name of numeric column containing symbol rotation angle");
    rotcolumn_opt->description =
	_("Measured in degrees CCW from east");

    /* Labels */
    lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
    lfield_opt->key = "llayer";
    lfield_opt->guisection = _("Labels");
    lfield_opt->description =
	_("Layer number for labels (default: the given layer number)");

    attrcol_opt = G_define_standard_option(G_OPT_COLUMN);
    attrcol_opt->key = "attrcol";
    attrcol_opt->multiple = NO;	/* or fix attr.c, around line 102 */
    attrcol_opt->guisection = _("Labels");
    attrcol_opt->description = _("Name of column to be displayed");

    lcolor_opt = G_define_option();
    lcolor_opt->key = "lcolor";
    lcolor_opt->type = TYPE_STRING;
    lcolor_opt->answer = "red";
    lcolor_opt->label = _("Label color");
    lcolor_opt->guisection = _("Labels");
    lcolor_opt->gisprompt = "old_color,color,color";
    lcolor_opt->description = _("Either a standard color name or R:G:B triplet");

    bgcolor_opt = G_define_option();
    bgcolor_opt->key = "bgcolor";
    bgcolor_opt->type = TYPE_STRING;
    bgcolor_opt->answer = "none";
    bgcolor_opt->guisection = _("Labels");
    bgcolor_opt->label = _("Label background color");
    bgcolor_opt->gisprompt = "old_color,color,color_none";
    bgcolor_opt->description =
	_("Either a standard GRASS color, R:G:B triplet, or \"none\"");

    bcolor_opt = G_define_option();
    bcolor_opt->key = "bcolor";
    bcolor_opt->type = TYPE_STRING;
    bcolor_opt->answer = "none";
    bcolor_opt->guisection = _("Labels");
    bcolor_opt->label = _("Label border color");
    bcolor_opt->gisprompt = "old_color,color,color_none";
    bcolor_opt->description =
	_("Either a standard GRASS color, R:G:B triplet, or \"none\"");

    lsize_opt = G_define_option();
    lsize_opt->key = "lsize";
    lsize_opt->type = TYPE_INTEGER;
    lsize_opt->answer = "8";
    lsize_opt->guisection = _("Labels");
    lsize_opt->description = _("Label size (pixels)");

    font_opt = G_define_option();
    font_opt->key = "font";
    font_opt->type = TYPE_STRING;
    font_opt->guisection = _("Labels");
    font_opt->description = _("Font name");

    xref_opt = G_define_option();
    xref_opt->key = "xref";
    xref_opt->type = TYPE_STRING;
    xref_opt->guisection = _("Labels");
    xref_opt->answer = "left";
    xref_opt->options = "left,center,right";
    xref_opt->description = _("Label horizontal justification");

    yref_opt = G_define_option();
    yref_opt->key = "yref";
    yref_opt->type = TYPE_STRING;
    yref_opt->guisection = _("Labels");
    yref_opt->answer = "center";
    yref_opt->options = "top,center,bottom";
    yref_opt->description = _("Label vertical justification");

    minreg_opt = G_define_option();
    minreg_opt->key = "minreg";
    minreg_opt->type = TYPE_DOUBLE;
    minreg_opt->required = NO;
    minreg_opt->description =
	_("Minimum region size (average from height and width) "
	  "when map is displayed");

    maxreg_opt = G_define_option();
    maxreg_opt->key = "maxreg";
    maxreg_opt->type = TYPE_DOUBLE;
    maxreg_opt->required = NO;
    maxreg_opt->description =
	_("Maximum region size (average from height and width) "
	  "when map is displayed");

    render_opt = G_define_option();
    render_opt->key = "render";
    render_opt->type = TYPE_STRING;
    render_opt->required = NO;
    render_opt->multiple = NO;
    render_opt->answer = "c";
    render_opt->options = "g,r,d,c,l";
    render_opt->description = _("Rendering method for filled polygons");
    render_opt->descriptions =
	_("g;use the libgis render functions (features: clipping);"
	  "r;use the raster graphics library functions (features: polylines);"
	  "d;use the display library basic functions (features: polylines);"
	  "c;use the display library clipping functions (features: clipping);"
	  "l;use the display library culling functions (features: culling, polylines)");

    /* please remove before GRASS 7 released */
    verbose_flag = G_define_flag();
    verbose_flag->key = 'v';
    verbose_flag->description = _("Run verbosely");

    /* Colors */
    table_acolors_flag = G_define_flag();
    table_acolors_flag->key = 'a';
    table_acolors_flag->guisection = _("Colors");
    table_acolors_flag->description =
	_("Get colors from map table column (of form RRR:GGG:BBB)");

    cats_acolors_flag = G_define_flag();
    cats_acolors_flag->key = 'c';
    cats_acolors_flag->guisection = _("Colors");
    cats_acolors_flag->description =
	_("Random colors according to category number "
	  "(or layer number if 'layer=-1' is given)");

    /* Query */
    id_flag = G_define_flag();
    id_flag->key = 'i';
    id_flag->guisection = _("Selection");
    id_flag->description = _("Use values from 'cats' option as feature id");

    x_flag = G_define_flag();
    x_flag->key = 'x';
    x_flag->description =
	_("Don't add to list of vectors and commands in monitor "
	  "(it won't be drawn if the monitor is refreshed)");

    zcol_flag = G_define_flag();
    zcol_flag->key = 'z';
    zcol_flag->description = _("Colorize polygons according to z height");
    zcol_flag->guisection = _("Colors");

    /* Check command line */
    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    if (G_strcasecmp(render_opt->answer, "g") == 0)
	render = RENDER_GPP;
    else if (G_strcasecmp(render_opt->answer, "r") == 0)
	render = RENDER_RPA;
    else if (G_strcasecmp(render_opt->answer, "d") == 0)
	render = RENDER_DP;
    else if (G_strcasecmp(render_opt->answer, "c") == 0)
	render = RENDER_DPC;
    else if (G_strcasecmp(render_opt->answer, "l") == 0)
	render = RENDER_DPL;
    else
	render = RENDER_GPP;

    /* please remove -v flag before GRASS 7 released */
    if (verbose_flag->answer) {
	G_putenv("GRASS_VERBOSE", "3");
	G_warning(_("The '-v' flag is superseded and will be removed "
		    "in future. Please use '--verbose' instead."));
    }
    /* but keep this */
    if (G_verbose() > G_verbose_std())
	verbose = TRUE;

    G_get_set_window(&window);

    if (R_open_driver() != 0)
	G_fatal_error(_("No graphics device selected"));

    /* Read map options */

    /* Check min/max region */
    reg = ((window.east - window.west) + (window.north - window.south)) / 2;
    if (minreg_opt->answer) {
	minreg = atof(minreg_opt->answer);

	if (reg < minreg) {
	    G_message(_("Region size is lower than minreg, nothing displayed."));
	    D_add_to_list(G_recreate_command());
	    exit(EXIT_SUCCESS);
	}
    }
    if (maxreg_opt->answer) {
	maxreg = atof(maxreg_opt->answer);

	if (reg > maxreg) {
	    G_message(_("Region size is greater than maxreg, nothing displayed."));
	    D_add_to_list(G_recreate_command());
	    exit(EXIT_SUCCESS);
	}
    }

    G_strcpy(map_name, map_opt->answer);

    default_width = atoi(width_opt->answer);
    if (default_width < 0)
	default_width = 0;
    width_scale = atof(wscale_opt->answer);

    if (table_acolors_flag->answer && cats_acolors_flag->answer) {
	cats_acolors_flag->answer = '\0';
	G_warning(_("The '-c' and '-a' flags cannot be used together, "
		    "the '-c' flag will be ignored!"));
    }

    color = G_standard_color_rgb(WHITE);
    ret = G_str_to_color(color_opt->answer, &r, &g, &b);
    if (ret == 1) {
	has_color = 1;
	color.r = r;
	color.g = g;
	color.b = b;
    }
    else if (ret == 2) {	/* none */
	has_color = 0;
    }
    else if (ret == 0) {	/* error */
	G_fatal_error(_("Unknown color: [%s]"), color_opt->answer);
    }

    fcolor = G_standard_color_rgb(WHITE);
    ret = G_str_to_color(fcolor_opt->answer, &r, &g, &b);
    if (ret == 1) {
	has_fcolor = 1;
	fcolor.r = r;
	fcolor.g = g;
	fcolor.b = b;
    }
    else if (ret == 2) {	/* none */
	has_fcolor = 0;
    }
    else if (ret == 0) {	/* error */
	G_fatal_error(_("Unknown color: '%s'"), fcolor_opt->answer);
    }

    size = atof(size_opt->answer);

    /* Make sure map is available */
    mapset = G_find_vector2(map_name, "");

    if (mapset == NULL)
	G_fatal_error(_("Vector map <%s> not found"), map_name);

    /* if where_opt was specified select categories from db 
     * otherwise parse cat_opt */
    Clist = Vect_new_cat_list();
    Clist->field = atoi(field_opt->answer);

    /* open vector */
    level = Vect_open_old(&Map, map_name, mapset);

    if (where_opt->answer) {
	if (Clist->field < 1)
	    G_fatal_error(_("'layer' must be > 0 for 'where'."));
	chcat = 1;
	if ((fi = Vect_get_field(&Map, Clist->field)) == NULL)
	    G_fatal_error(_("Database connection not defined"));
	if (fi != NULL) {
	    driver = db_start_driver(fi->driver);
	    if (driver == NULL)
		G_fatal_error(_("Unable to start driver <%s>"), fi->driver);

	    db_init_handle(&handle);
	    db_set_handle(&handle, fi->database, NULL);
	    if (db_open_database(driver, &handle) != DB_OK)
		G_fatal_error(_("Unable to open database <%s>"),
			      fi->database);

	    ncat =
		db_select_int(driver, fi->table, fi->key, where_opt->answer,
			      &cats);

	    db_close_database(driver);
	    db_shutdown_driver(driver);

	    Vect_array_to_cat_list(cats, ncat, Clist);
	}
    }
    else if (cat_opt->answer) {
	if (Clist->field < 1)
	    G_fatal_error(_("'layer' must be > 0 for 'cats'."));
	chcat = 1;
	ret = Vect_str_to_cat_list(cat_opt->answer, Clist);
	if (ret > 0)
	    G_warning(_("%d errors in cat option"), ret);
    }

    type = Vect_option_to_types(type_opt); 

    i = 0;
    display = 0;
    while (display_opt->answers[i]) {
	switch (display_opt->answers[i][0]) {
	case 's':
	    display |= DISP_SHAPE;
	    break;
	case 'c':
	    display |= DISP_CAT;
	    break;
	case 't':
	    display |= DISP_TOPO;
	    break;
	case 'd':
	    display |= DISP_DIR;
	    break;
	case 'a':
	    display |= DISP_ATTR;
	    break;
	case 'z':
	    display |= DISP_ZCOOR;
	    break;
	}
	i++;
    }

    /* Read label options */
    if (lfield_opt->answer != NULL)
	lattr.field = atoi(lfield_opt->answer);
    else
	lattr.field = Clist->field;

    lattr.color.R = lattr.color.G = lattr.color.B = 255;
    if (G_str_to_color(lcolor_opt->answer, &r, &g, &b)) {
	lattr.color.R = r;
	lattr.color.G = g;
	lattr.color.B = b;
    }
    lattr.has_bgcolor = 0;
    if (G_str_to_color(bgcolor_opt->answer, &r, &g, &b) == 1) {
	lattr.has_bgcolor = 1;
	lattr.bgcolor.R = r;
	lattr.bgcolor.G = g;
	lattr.bgcolor.B = b;
    }
    lattr.has_bcolor = 0;
    if (G_str_to_color(bcolor_opt->answer, &r, &g, &b) == 1) {
	lattr.has_bcolor = 1;
	lattr.bcolor.R = r;
	lattr.bcolor.G = g;
	lattr.bcolor.B = b;
    }

    lattr.size = atoi(lsize_opt->answer);
    lattr.font = font_opt->answer;
    switch (xref_opt->answer[0]) {
    case 'l':
	lattr.xref = LLEFT;
	break;
    case 'c':
	lattr.xref = LCENTER;
	break;
    case 'r':
	lattr.xref = LRIGHT;
	break;
    }
    switch (yref_opt->answer[0]) {
    case 't':
	lattr.yref = LTOP;
	break;
    case 'c':
	lattr.yref = LCENTER;
	break;
    case 'b':
	lattr.yref = LBOTTOM;
	break;
    }

    D_setup(0);

    G_setup_plot(D_get_d_north(), D_get_d_south(),
		 D_get_d_west(), D_get_d_east(), D_move_abs, D_cont_abs);

    if (verbose)
	G_message(_("Plotting ..."));

    if (level >= 2)
	Vect_get_map_box(&Map, &box);

    if (level >= 2 && (window.north < box.S || window.south > box.N ||
		       window.east < box.W ||
		       window.west > G_adjust_easting(box.E, &window))) {
	G_message(_("The bounding box of the map is outside the current region, "
		   "nothing drawn."));
	stat = 0;
    }
    else {
	overlap =
	    G_window_percentage_overlap(&window, box.N, box.S, box.E, box.W);
	G_debug(1, "overlap = %f \n", overlap);
	if (overlap < 1)
	    Vect_set_constraint_region(&Map, window.north, window.south,
				       window.east, window.west,
				       PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);

	/* default line width */
	if (!wcolumn_opt->answer)
	    D_line_width(default_width);

	if (type & GV_AREA) {
	    if (level >= 2) {
		if (display & DISP_SHAPE) {
		    stat = darea(&Map, Clist,
				 has_color ? &color : NULL,
				 has_fcolor ? &fcolor : NULL, chcat,
				 (int)id_flag->answer,
				 table_acolors_flag->answer,
				 cats_acolors_flag->answer, &window,
				 rgbcol_opt->answer, default_width,
				 wcolumn_opt->answer, width_scale,
				 zcol_flag->answer, zcol_opt->answer);
		}
		if (wcolumn_opt->answer)
		    D_line_width(default_width);
	    }
	    else
		G_warning(_("Unable to display areas, topology not available"));
	}

	if (display & DISP_SHAPE) {
	    if (id_flag->answer && level < 2) {
		G_warning(_("Unable to display lines by id, topology not available"));
	    }
	    else {
		stat = plot1(&Map, type, Clist,
			     has_color ? &color : NULL,
			     has_fcolor ? &fcolor : NULL, chcat, icon_opt->answer,
			     size, sizecolumn_opt->answer, rotcolumn_opt->answer,
			     (int)id_flag->answer, table_acolors_flag->answer,
			     cats_acolors_flag->answer, rgbcol_opt->answer,
			     default_width, wcolumn_opt->answer, width_scale,
			     zcol_flag->answer, zcol_opt->answer);
		if (wcolumn_opt->answer)
		    D_line_width(default_width);
	    }
	}

	if (has_color) {
	    R_RGB_color(color.r, color.g, color.b);
	    if (display & DISP_DIR)
                stat = dir(&Map, type, Clist, chcat, size);
	}

	/* reset line width: Do we need to get line width from display
	 * driver (not implemented)?  It will help restore previous line
	 * width (not just 0) determined by another module (e.g.,
	 * d.linewidth). */
	if (!wcolumn_opt->answer)
	    R_line_width(0);

	if (display & DISP_CAT)
	    stat = label(&Map, type, Clist, &lattr, chcat);

	if (display & DISP_ATTR)
	    stat =
		attr(&Map, type, attrcol_opt->answer, Clist, &lattr, chcat);

	if (display & DISP_ZCOOR)
	    stat = zcoor(&Map, type, &lattr);

	if (display & DISP_TOPO) {
	    if (level >= 2)
		stat = topo(&Map, type, &lattr);
	    else
		G_warning(_("Unable to display topology, not available"));
	}
    }

    if (!x_flag->answer) {
	D_add_to_list(G_recreate_command());

	D_set_dig_name(G_fully_qualified_name(map_name, mapset));
	D_add_to_dig_list(G_fully_qualified_name(map_name, mapset));
    }

    R_close_driver();

    if (verbose)
	G_done_msg(" ");

    Vect_close(&Map);
    Vect_destroy_cat_list(Clist);

    exit(stat);
}