Пример #1
0
/*!
  \brief Get units code by name

  Units codes (gis.h):
   - U_METERS
   - U_KILOMETERS
   - U_ACRES
   - U_HECTARES
   - U_MILES
   - U_FEET
   - U_USFEET
   - ...
   - U_YEARS
   - ...

  \param units_name units name (singular or plural form)

  \return units code
  \return U_UNKNOWN if not found
*/
int G_units(const char *units_name)
{
    if (units_name == NULL) {
	return G_units(G_database_unit_name(1));
    }
	
    if (strcasecmp(units_name, "meter") == 0 ||
	strcasecmp(units_name, "meters") == 0)
	return U_METERS;
    else if (strcasecmp(units_name, "kilometer") == 0 ||
	     strcasecmp(units_name, "kilometers") == 0)
	return U_KILOMETERS;
    else if (strcasecmp(units_name, "acre") == 0 ||
	     strcasecmp(units_name, "acres") == 0)
	return U_ACRES;
    else if (strcasecmp(units_name, "hectare") == 0 ||
	     strcasecmp(units_name, "hectares") == 0)
	return U_HECTARES;
    else if (strcasecmp(units_name, "mile") == 0 ||
	     strcasecmp(units_name, "miles") == 0)
	return U_MILES;
    else if (strcasecmp(units_name, "foot") == 0 ||
	     strcasecmp(units_name, "feet") == 0)
	return U_FEET;
    else if (strcasecmp(units_name, "foot_us") == 0 ||
	     strcasecmp(units_name, "foot_uss") == 0)
	return U_USFEET;
    else if (strcasecmp(units_name, "degree") == 0 ||
	     strcasecmp(units_name, "degrees") == 0)
	return U_DEGREES;
    else if (strcasecmp(units_name, "year") == 0 ||
	     strcasecmp(units_name, "years") == 0)
	return U_YEARS;
    else if (strcasecmp(units_name, "month") == 0 ||
	     strcasecmp(units_name, "months") == 0)
	return U_MONTHS;
    else if (strcasecmp(units_name, "day") == 0 ||
	     strcasecmp(units_name, "days") == 0)
	return U_DAYS;
    else if (strcasecmp(units_name, "hour") == 0 ||
	     strcasecmp(units_name, "hours") == 0)
	return U_HOURS;
    else if (strcasecmp(units_name, "minute") == 0 ||
	     strcasecmp(units_name, "minutes") == 0)
	return U_MINUTES;
    else if (strcasecmp(units_name, "secons") == 0 ||
	     strcasecmp(units_name, "seconds") == 0)
	return U_SECONDS;

    return U_UNKNOWN;
}
Пример #2
0
Файл: main.c Проект: caomw/grass
int main(int argc, char *argv[])
{
    char *name, *outfile;
    const char *unit;
    int unit_id;
    double factor;
    int fd, projection;
    FILE *fp, *coor_fp;
    double res;
    char *null_string;
    char ebuf[256], nbuf[256], label[512], formatbuff[256];
    char b1[100], b2[100];
    int n;
    int havefirst = FALSE;
    int coords = 0, i, k = -1;
    double e1, e2, n1, n2;
    RASTER_MAP_TYPE data_type;
    struct Cell_head window;
    struct
    {
	struct Option *opt1, *profile, *res, *output, *null_str, *coord_file, *units;
	struct Flag *g, *c, *m;
    }
    parm;
    struct GModule *module;

    G_gisinit(argv[0]);

    /* Set description */
    module = G_define_module();
    G_add_keyword(_("raster"));
    G_add_keyword(_("profile"));
    module->description =
	_("Outputs the raster map layer values lying on user-defined line(s).");

    parm.opt1 = G_define_standard_option(G_OPT_R_INPUT);

    parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
    parm.output->required = NO;
    parm.output->answer = "-";
    parm.output->description =
	_("Name of file for output (use output=- for stdout)");

    parm.profile = G_define_standard_option(G_OPT_M_COORDS);
    parm.profile->required = NO;
    parm.profile->multiple = YES;
    parm.profile->description = _("Profile coordinate pairs");

    parm.coord_file = G_define_standard_option(G_OPT_F_INPUT);
    parm.coord_file->key = "file";
    parm.coord_file->required = NO;
    parm.coord_file->label =
	_("Name of input file containing coordinate pairs");
    parm.coord_file->description =
	_("Use instead of the 'coordinates' option. "
	  "\"-\" reads from stdin.");

    parm.res = G_define_option();
    parm.res->key = "resolution";
    parm.res->type = TYPE_DOUBLE;
    parm.res->required = NO;
    parm.res->description =
	_("Resolution along profile (default = current region resolution)");

    parm.null_str = G_define_option();
    parm.null_str->key = "null";
    parm.null_str->type = TYPE_STRING;
    parm.null_str->required = NO;
    parm.null_str->answer = "*";
    parm.null_str->description = _("Character to represent no data cell");

    parm.g = G_define_flag();
    parm.g->key = 'g';
    parm.g->description =
	_("Output easting and northing in first two columns of four column output");

    parm.c = G_define_flag();
    parm.c->key = 'c';
    parm.c->description =
	_("Output RRR:GGG:BBB color values for each profile point");

    parm.units = G_define_standard_option(G_OPT_M_UNITS);
    parm.units->options = "meters,kilometers,feet,miles";
    parm.units->label = parm.units->description;
    parm.units->description = _("If units are not specified, current location units are used. "
                                "Meters are used by default in geographic (latlon) locations.");

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

    clr = 0;
    if (parm.c->answer)
	clr = 1;		/* color output */

    null_string = parm.null_str->answer;

    if ((parm.profile->answer && parm.coord_file->answer) ||
	(!parm.profile->answer && !parm.coord_file->answer))
	G_fatal_error(_("Either use profile option or coordinate_file "
			" option, but not both"));

    G_get_window(&window);
    projection = G_projection();

    /* get conversion factor and units name */
    if (parm.units->answer) {
        unit_id = G_units(parm.units->answer);
        factor = 1. / G_meters_to_units_factor(unit_id);
        unit = G_get_units_name(unit_id, 1, 0);
    }
    /* keep meters in case of latlon */
    else if (projection == PROJECTION_LL) {
        factor = 1;
        unit = "meters";
    } 
    else {
        /* get conversion factor to current units */
        unit = G_database_unit_name(1);
        factor = G_database_units_to_meters_factor();
    }

    if (parm.res->answer) {
	res = atof(parm.res->answer);
	/* Catch bad resolution ? */
	if (res <= 0)
	    G_fatal_error(_("Illegal resolution %g [%s]"), res / factor, unit);
    }
    else {
	/* Do average of EW and NS res */
	res = (window.ew_res + window.ns_res) / 2;
    }

    G_message(_("Using resolution: %g [%s]"), res / factor, unit);

    G_begin_distance_calculations();

    /* Open Input File for reading */
    /* Get Input Name */
    name = parm.opt1->answer;
    if (parm.g->answer)
	coords = 1;

    /* Open Raster File */
    fd = Rast_open_old(name, "");

    /* initialize color structure */
    if (clr)
	Rast_read_colors(name, "", &colors);

    /* Open ASCII file for output or stdout */
    outfile = parm.output->answer;

    if ((strcmp("-", outfile)) == 0) {
	fp = stdout;
    }
    else if (NULL == (fp = fopen(outfile, "w")))
	G_fatal_error(_("Unable to open file <%s>"), outfile);

    /* Get Raster Type */
    data_type = Rast_get_map_type(fd);
    /* Done with file */

    /* Show message giving output format */
    G_message(_("Output columns:"));
    if (coords == 1)
	sprintf(formatbuff,
		_("Easting, Northing, Along track dist. [%s], Elevation"), unit);
    else
	sprintf(formatbuff, _("Along track dist. [%s], Elevation"), unit);
    if (clr)
	strcat(formatbuff, _(" RGB color"));
    G_message(formatbuff);

    /* Get Profile Start Coords */
    if (parm.coord_file->answer) {
	if (strcmp("-", parm.coord_file->answer) == 0)
	    coor_fp = stdin;
	else
	    coor_fp = fopen(parm.coord_file->answer, "r");

	if (coor_fp == NULL)
	    G_fatal_error(_("Could not open <%s>"), parm.coord_file->answer);


	for (n = 1; input(b1, ebuf, b2, nbuf, label, coor_fp); n++) {
	    G_debug(4, "stdin line %d: ebuf=[%s]  nbuf=[%s]", n, ebuf, nbuf);
	    if (!G_scan_easting(ebuf, &e2, G_projection()) ||
		!G_scan_northing(nbuf, &n2, G_projection()))
		G_fatal_error(_("Invalid coordinates %s %s"), ebuf, nbuf);

	    if (havefirst)
		do_profile(e1, e2, n1, n2, coords, res, fd, data_type,
			   fp, null_string, unit, factor);
	    e1 = e2;
	    n1 = n2;
	    havefirst = TRUE;
	}

	if (coor_fp != stdin)
	    fclose(coor_fp);
    }
    else {
	/* Coords given on the Command Line using the profile= option */
	for (i = 0; parm.profile->answers[i]; i += 2) {
	    /* Test for number coordinate pairs */
	    k = i;
	}

	if (k == 0) {
	    /* Only one coordinate pair supplied */
	    G_scan_easting(parm.profile->answers[0], &e1, G_projection());
	    G_scan_northing(parm.profile->answers[1], &n1, G_projection());
	    e2 = e1;
	    n2 = n1;

	    /* Get profile info */
	    do_profile(e1, e2, n1, n2, coords, res, fd, data_type, fp,
		       null_string, unit, factor);
	}
	else {
	    for (i = 0; i <= k - 2; i += 2) {
		G_scan_easting(parm.profile->answers[i], &e1, G_projection());
		G_scan_northing(parm.profile->answers[i + 1], &n1,
				G_projection());
		G_scan_easting(parm.profile->answers[i + 2], &e2,
			       G_projection());
		G_scan_northing(parm.profile->answers[i + 3], &n2,
				G_projection());

		/* Get profile info */
		do_profile(e1, e2, n1, n2, coords, res, fd, data_type,
			   fp, null_string, unit, factor);

	    }
	}
    }

    Rast_close(fd);
    fclose(fp);

    if (clr)
	Rast_free_colors(&colors);

    exit(EXIT_SUCCESS);
}				/* Done with main */
Пример #3
0
/*!
  \brief Get localized units name

  Units codes (gis.h):
   - U_METERS
   - U_KILOMETERS
   - U_ACRES
   - U_HECTARES
   - U_MILES
   - U_FEET
   - U_USFEET

  \param units units code
  \param plural plural form if true
  \param square area units if true

  \return units name
  \return NULL if units not found
*/
const char *G_get_units_name(int units, int plural, int square)
{
    switch (units) {
    case U_UNKNOWN:
	if (square)
	    return plural ? _("square units") : _("square unit");
	else
	    return plural ? _("units") : _("unit");
	break;
	  
    case U_METERS:
	if (square) 
	    return plural ? _("square meters") : _("square meter");
	else
	    return plural ? _("meters") : _("meter");
	break;
	
    case U_KILOMETERS:
	if (square)
	    return plural ? _("square kilometers") : _("square kilometer");
	else
	    return plural ? _("kilometers") : _("kilometer");
	break;
	
    case U_ACRES:
	if (square)
	    return plural ? _("acres") : _("acre");
	else
	    return G_get_units_name(G_units(G_database_unit_name(1)),
				    plural, square);
	break;
	
    case U_HECTARES:
	if (square)
	    return plural ? _("hectares") : _("hectare");
	else
	    return G_get_units_name(G_units(G_database_unit_name(1)),
				    plural, square);
	break;
	
    case U_MILES:
	if (square)
	    return plural ? _("square miles") : _("square mile");
	else
	    return plural ? _("miles") : _("mile");
	break;
	
    case U_FEET:
	if (square)
	    return plural ? _("square feet") : _("square foot");
	else
	    return plural ? _("feet") : _("foot");
	break;

    case U_USFEET:
	if (square)
	    return plural ? _("square US feet") : _("square US foot");
	else
	    return plural ? _("US feet") : _("US foot");
	break;

    case U_DEGREES:
	if (square)
	    return plural ? _("square degrees") : _("square degree");
	else
	    return plural ? _("degrees") : _("degree");
	break;  
        
    case U_YEARS:
	return plural ? _("years") : _("year");
	break;
	
    case U_MONTHS:
	return plural ? _("months") : _("month");
	break;
	
    case U_DAYS:
	return plural ? _("days") : _("day");
	break;
	
    case U_HOURS:
	return plural ? _("hours") : _("hour");
	break;
	
    case U_MINUTES:
	return plural ? _("minutes") : _("minute");
	break;
	
    case U_SECONDS:
	return plural ? _("seconds") : _("second");
	break;
    }
    
    return NULL;
}
Пример #4
0
int main(int argc, char **argv)
{
    struct GModule *module;
    struct Option *bg_color_opt, *fg_color_opt, *coords, *fsize, *barstyle,
            *text_placement, *length_opt, *segm_opt, *units_opt, *label_opt,
            *width_scale_opt;
    struct Flag *feet, *no_text, *n_symbol;
    struct Cell_head W;
    double east, north;
    double fontsize;
    int bar_style, text_position, units;
    double length;
    int segm;
    char *label;
    double width_scale;

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

    module = G_define_module();
    G_add_keyword(_("display"));
    G_add_keyword(_("cartography"));
    module->description = _("Displays a barscale on the graphics monitor.");

    feet = G_define_flag();
    feet->key = 'f';
    feet->description = _("Use feet/miles instead of meters");

    no_text = G_define_flag();
    no_text->key = 't';
    no_text->description = _("Draw the scale bar without text");
    no_text->guisection = _("Text");

    n_symbol = G_define_flag();
    n_symbol->key = 'n';
    n_symbol->description = _("Display north-arrow symbol.");
    n_symbol->guisection = _("Style");

    barstyle = G_define_option();
    barstyle->key = "style";
    barstyle->description = _("Type of barscale to draw");
    barstyle->options =
        "classic,line,solid,hollow,full_checker,part_checker,mixed_checker,tail_checker,up_ticks,down_ticks,both_ticks,arrow_ends";
    barstyle->answer = "classic";
    barstyle->gisprompt = "old,barscale,barscale";
    barstyle->guisection = _("Style");
    G_asprintf((char **)&(barstyle->descriptions),
               "classic;%s;"
               "line;%s;"
               "solid;%s;"
               "hollow;%s;"
               "full_checker;%s;"
               "part_checker;%s;"
               "mixed_checker;%s;"
               "tail_checker;%s;"
               "up_ticks;%s;"
               "down_ticks;%s;"
               "both_ticks;%s;"
               "arrow_ends;%s",
               _("Classic style"),
               _("Line style"),
               _("Solid style"),
               _("Hollow style"),
               _("Full checker style"),
               _("Part checker style"),
               _("Mixed checker style"),
               _("Tail checker style"),
               _("Up ticks style"),
               _("Down ticks style"),
               _("Both ticks style"), _("Arrow ends style"));

    coords = G_define_option();
    coords->key = "at";
    coords->key_desc = "x,y";
    coords->type = TYPE_DOUBLE;
    coords->answer = "0.0,10.0";
    coords->options = "0-100";
    coords->label =
        _("Screen coordinates of the rectangle's top-left corner");
    coords->description = _("(0,0) is lower-left of the display frame");

    length_opt = G_define_option();
    length_opt->key = "length";
    length_opt->key_desc = "integer";
    length_opt->type = TYPE_INTEGER;
    length_opt->answer = "0";
    length_opt->options = "0-";
    length_opt->label = _("Length of barscale in map units");

    units_opt = G_define_option();
    units_opt->key = "units";
    units_opt->description = _("Barscale units to display");
    units_opt->options = "meters, kilometers, feet, miles";

    label_opt = G_define_option();
    label_opt->key = "label";
    label_opt->description = _("Custom label of unit");
    label_opt->type = TYPE_STRING;
    label_opt->guisection = _("Text");

    segm_opt = G_define_option();
    segm_opt->key = "segment";
    segm_opt->type = TYPE_INTEGER;
    segm_opt->answer = "10";
    segm_opt->options = "1-100";
    segm_opt->label = _("Number of segments");
    segm_opt->guisection = _("Style");

    fg_color_opt = G_define_standard_option(G_OPT_C);
    fg_color_opt->label = _("Bar scale and text color");
    fg_color_opt->guisection = _("Colors");

    bg_color_opt = G_define_standard_option(G_OPT_CN);
    bg_color_opt->key = "bgcolor";
    bg_color_opt->answer = "white";
    bg_color_opt->label = _("Background color (drawn behind the bar)");
    bg_color_opt->guisection = _("Colors");

    text_placement = G_define_option();
    text_placement->key = "text_position";
    text_placement->description = _("Text position");
    text_placement->options = "under,over,left,right";
    text_placement->answer = "right";
    text_placement->guisection = _("Text");
    
    width_scale_opt = G_define_option();
    width_scale_opt->key = "width_scale";
    width_scale_opt->type = TYPE_DOUBLE;
    width_scale_opt->required = NO;
    width_scale_opt->answer = "1";
    width_scale_opt->options = "0.5-100";
    width_scale_opt->description = _("Scale factor to change bar width");

    fsize = G_define_option();
    fsize->key = "fontsize";
    fsize->type = TYPE_DOUBLE;
    fsize->required = NO;
    fsize->answer = "12";
    fsize->options = "1-360";
    fsize->description = _("Font size");
    fsize->guisection = _("Text");

     G_option_exclusive(feet, units_opt, NULL);

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


    G_get_window(&W);
    if (W.proj == PROJECTION_LL)
        G_fatal_error(_("%s does not work with a latitude-longitude location"),
                      argv[0]);


    north_arrow = n_symbol->answer ? TRUE : FALSE;

    switch (barstyle->answer[0]) {
    case 'c':
        bar_style = STYLE_CLASSIC_BAR;
        break;
    case 'p':
        bar_style = STYLE_PART_CHECKER;
        break;
    case 'f':
        bar_style = STYLE_FULL_CHECKER;
        break;
    case 'm':
        bar_style = STYLE_MIXED_CHECKER;
        break;
    case 't':
        bar_style = STYLE_TAIL_CHECKER;
        break;
    case 'l':
        bar_style = STYLE_THIN_WITH_ENDS;
        break;
    case 's':
        bar_style = STYLE_SOLID_BAR;
        break;
    case 'h':
        bar_style = STYLE_HOLLOW_BAR;
        break;
    case 'u':
        bar_style = STYLE_TICKS_UP;
        break;
    case 'd':
        bar_style = STYLE_TICKS_DOWN;
        break;
    case 'b':
        bar_style = STYLE_TICKS_BOTH;
        break;
    case 'a':
        bar_style = STYLE_ARROW_ENDS;
        break;
    default:
        G_fatal_error(_("Programmer error"));
    }

    switch (text_placement->answer[0]) {
    case 'u':
        text_position = TEXT_UNDER;
        break;
    case 'o':
        text_position = TEXT_OVER;
        break;
    case 'l':
        text_position = TEXT_LEFT;
        break;
    case 'r':
        text_position = TEXT_RIGHT;
        break;
    default:
        G_fatal_error(_("Programmer error"));
    }

    sscanf(coords->answers[0], "%lf", &east);
    sscanf(coords->answers[1], "%lf", &north);


    length = atof(length_opt->answer);
    sscanf(segm_opt->answer, "%d", &segm);

    if (feet->answer == 1){
        use_feet = 1;
        units = U_FEET;
        label = "ft";
    }
    else {
        if (!units_opt->answer)
            units = G_database_unit();
        else
            units = G_units(units_opt->answer);
        switch (units) {
        case U_METERS:
            label = "m";
            break;
        case U_KILOMETERS:
            label = "km";
            break;
        case U_FEET:
            use_feet = 1;
            label = "ft";
            break;
        case U_USFEET:
            use_feet = 1;
            label = "ft";
            break;
        case U_MILES:
            use_feet = 1;
            label = "mi";
            break;
        default:
            units = U_METERS;
            label = "m";
        }
    }

    if (label_opt->answer){
        label = label_opt->answer;
    }

    fontsize = atof(fsize->answer);
    if (no_text->answer)
        fontsize = -1;

    width_scale = atof(width_scale_opt->answer);

    /* Parse and select foreground color */
    fg_color = D_parse_color(fg_color_opt->answer, 0);

    /* Parse and select background color */
    bg_color = D_parse_color(bg_color_opt->answer, 1);
    if (bg_color == 0)
        do_background = FALSE;


    D_open_driver();

    D_setup(0);

    draw_scale(east, north, length, segm, units, label, bar_style, text_position, width_scale, fontsize);

    D_save_command(G_recreate_command());
    D_close_driver();

    exit(EXIT_SUCCESS);
}