int main(int argc, char **argv) { struct GModule *module; struct Option *opt1, *opt2; struct Flag *mapcoords; int R, G, B, color = 0; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("cartography")); module->description = _("Program for generating and displaying simple graphics on the " "display monitor."); opt1 = G_define_option(); opt1->key = "input"; opt1->type = TYPE_STRING; opt1->required = NO; opt1->description = _("Name of file containing graphics commands, " "if not given reads from standard input"); opt1->gisprompt = "old_file,file,input"; opt2 = G_define_option(); opt2->key = "color"; opt2->type = TYPE_STRING; opt2->required = NO; opt2->description = _("Color to draw with, either a standard GRASS color " "or R:G:B triplet"); opt2->answer = DEFAULT_FG_COLOR; opt2->gisprompt = "old_color,color,color"; mapcoords = G_define_flag(); mapcoords->key = 'm'; mapcoords->description = _("Coordinates are given in map units"); /* Check command line */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* default font scaling: 5% of active frame */ hsize = vsize = 5.; if (opt1->answer != NULL) { if ((infile = fopen(opt1->answer, "r")) == NULL) G_fatal_error(_("Graph file <%s> not found"), opt1->answer); } else infile = stdin; /* open graphics window */ if (D_open_driver() != 0) G_fatal_error(_("No graphics device selected. " "Use d.mon to select graphics device.")); /* Parse and select color */ if (opt2->answer != NULL) { color = G_str_to_color(opt2->answer, &R, &G, &B); if (color == 0) G_fatal_error(_("[%s]: No such color"), opt2->answer); if (color == 1) { D_RGB_color(R, G, B); set_last_color(R, G, B, RGBA_COLOR_OPAQUE); } else /* (color==2) is "none" */ set_last_color(0, 0, 0, RGBA_COLOR_NONE); } if (mapcoords->answer) { mapunits = TRUE; D_setup(0); } else { D_setup2(0, 0, 100, 0, 0, 100); mapunits = FALSE; } /* Do the graphics */ set_graph_stuff(); set_text_size(); graphics(infile); D_save_command(G_recreate_command()); D_close_driver(); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { struct GModule *module; struct Option *map, *profile; struct Flag *stored; struct Cell_head window; struct point *points = NULL; int num_points, max_points = 0; double length; double t, b, l, r; int fd; int i; double sx; int last; /* Initialize the GIS calls */ G_gisinit(argv[0]); /* Set description */ module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("profile")); G_add_keyword(_("raster")); module->description = _("Plots profile of a transect."); /* set up command line */ map = G_define_standard_option(G_OPT_R_INPUT); map->description = _("Raster map to be profiled"); profile = G_define_option(); profile->key = "profile"; profile->type = TYPE_DOUBLE; profile->required = YES; profile->multiple = YES; profile->key_desc = "east,north"; profile->description = _("Profile coordinate pairs"); stored = G_define_flag(); stored->key = 'r'; stored->description = _("Use map's range recorded range"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); mapname = map->answer; fd = Rast_open_old(mapname, ""); if (stored->answer) get_map_range(); else get_region_range(fd); G_get_window(&window); num_points = 0; length = 0; for (i = 0; profile->answers[i]; i += 2) { struct point *p; double x, y; if (num_points >= max_points) { max_points = num_points + 100; points = G_realloc(points, max_points * sizeof(struct point)); } p = &points[num_points]; G_scan_easting( profile->answers[i+0], &x, G_projection()); G_scan_northing(profile->answers[i+1], &y, G_projection()); p->x = Rast_easting_to_col (x, &window); p->y = Rast_northing_to_row(y, &window); if (num_points > 0) { const struct point *prev = &points[num_points-1]; double dx = fabs(p->x - prev->x); double dy = fabs(p->y - prev->y); double d = sqrt(dx * dx + dy * dy); length += d; p->d = length; } num_points++; } points[0].d = 0; if (num_points < 2) G_fatal_error(_("At least two points are required")); /* establish connection with graphics driver */ if (D_open_driver() != 0) G_fatal_error(_("No graphics device selected. " "Use d.mon to select graphics device.")); D_setup2(1, 0, 1.05, -0.05, -0.15, 1.05); plot_axes(); D_use_color(D_translate_color(DEFAULT_FG_COLOR)); D_get_src(&t, &b, &l, &r); t -= 0.1 * (t - b); b += 0.1 * (t - b); l += 0.1 * (r - l); r -= 0.1 * (r - l); D_begin(); i = 0; last = 0; for (sx = 0; sx < 1; sx += D_get_d_to_u_xconv()) { double d = length * (sx - l); const struct point *p, *next; double k, sy, x, y; DCELL v; for (;;) { p = &points[i]; next = &points[i + 1]; k = (d - p->d) / (next->d - p->d); if (k < 1) break; i++; } x = p->x * (1 - k) + next->x * k; y = p->y * (1 - k) + next->y * k; if (!get_cell(&v, fd, x, y)) { last = 0; continue; } sy = (v - min) / (max - min); if (last) D_cont_abs(sx, sy); else D_move_abs(sx, sy); last = 1; } D_end(); D_stroke(); D_close_driver(); exit(EXIT_SUCCESS); }