int quit(int n) { char command[1024]; End_curses(); R_close_driver(); if (use_digitizer) { sprintf(command, "%s/etc/geo.unlock %s", G_gisbase(), digit_points); system(command); } unlink(tempfile1); unlink(tempfile2); unlink(cell_list); unlink(group_list); unlink(vect_list); unlink(digit_points); unlink(digit_results); system("d.frame -e"); exit(n); }
/* CALCULATE THE COORDINATES OF THE TOP LEFT CORNER OF THE SAMPLING UNITS */ static int calc_unit_loc(double radius, int top, int bot, int left, int right, double ratio, int u_w, int u_l, int method, double intv, int num, int h_d, int v_d, double *ux, double *uy, int *sites, double startx, int starty, int fmask, double nx, double x, double y) { char *sites_mapset, sites_file_name[GNAME_MAX], *cmd; struct Map_info Map; struct Cell_head region; double D_u_to_a_col(), D_u_to_a_row(); int i, j, k, cnt = 0, w_w = right - left, w_l = bot - top, exp1, exp2, dx = w_w, dy = w_l, l, t, left1 = left, top1 = top, n, tmp, ulrow, ulcol, *row_buf, lap = 0; static struct line_pnts *Points; struct line_cats *Cats; int ltype; /* VARIABLES: UNITS FOR ALL DIMENSION VARIABLES IN THIS ROUTINE ARE IN PIXELS top = sampling frame top row in pixels bot = sampling frame bottom row in pixels left = sampling frame left in pixels right = sampling frame right in pixels left1 = col of left side of sampling frame or each stratum top1 = row of top side of sampling frame or each stratum l = random # cols to be added to left1 r = random # rows to be added to top1 ratio = u_w = sampling unit width in pixels u_l = sampling unit length in pixels method = method of sampling unit distribution (1-5) intv = interval between sampling units when method=3 num = number of sampling units h_d = number of horizontal strata v_d = number of vertical strata ux = uy = sites = startx = col of UL corner starting pt for strata starty = row of UL corner starting pt for strata dx = number of cols per stratum dy = number of rows per stratum w_w = width of sampling frame in cols w_l = length of sampling frame in rows */ /* if user hits Ctrl+C, abort this calculation */ setjmp(jmp); if (tag) { tag = 0; return 0; } /* for syst. noncontig. distribution */ if (method == 3) { u_w += intv; u_l += intv; } /* for stratified random distribution */ if (method == 4) { dx = (int)((double)(w_w - startx) / (double)(h_d)); dy = (int)((double)(w_l - starty) / (double)(v_d)); } /* for syst. contig. and noncontig. distribution */ else if (method == 2 || method == 3) { if (nx >= num) dx = (w_w - startx) - (num - 1) * u_w; else { dx = (w_w - startx) - (nx - 1) * u_w; dy = (w_l - starty) - (num / nx - 1) * u_l; } } if (10 > (exp1 = (int)pow(10.0, ceil(log10((double)(dx - u_w + 10)))))) exp1 = 10; if (10 > (exp2 = (int)pow(10.0, ceil(log10((double)(dy - u_l + 10)))))) exp2 = 10; /* for random nonoverlapping and stratified random */ if (method == 1 || method == 4) { fprintf(stderr, "\n 'Ctrl+C' and choose fewer units if the requested number"); fprintf(stderr, " is not reached\n"); for (i = 0; i < num; i++) { /* if Cntl+C */ if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, f); /* for stratified random distribution */ if (method == 4) { j = 0; if (n = i % h_d) left1 += dx; else { left1 = left + startx; if (i < h_d) top1 = top + starty; else top1 += dy; } get_rd(exp1, exp2, dx, dy, u_w, u_l, &l, &t); } /* for random nonoverlapping distribution */ if (method == 1) { /* get random numbers */ back: get_rd(exp1, exp2, dx, dy, u_w, u_l, &l, &t); if (left1 + l + u_w > right || top1 + t + u_l > bot || left1 + l < left || top1 + t < top) goto back; /* if there is a mask, check to see that the unit will be within the mask area */ if (fmask > 0) { row_buf = Rast_allocate_c_buf(); Rast_get_c_row_nomask(fmask, row_buf, t + top1); if (! (*(row_buf + l + left1) && *(row_buf + l + left1 + u_w - 1))) goto back; Rast_zero_c_buf(row_buf); Rast_get_c_row_nomask(fmask, row_buf, t + top1 + u_l - 1); if (! (*(row_buf + l + left1) && *(row_buf + l + left1 + u_w - 1))) goto back; G_free(row_buf); } /* check for sampling unit overlap */ lap = 0; for (j = 0; j < cnt; j++) { if (overlap (l + left1, t + top1, (int)ux[j], (int)uy[j], u_w, u_l)) lap = 1; } if (lap) goto back; cnt++; } /* fill the array of upper left coordinates for the sampling units */ *(ux + i) = l + left1; *(uy + i) = t + top1; /* draw the sampling units on the screen */ R_open_driver(); R_standard_color(D_translate_color("red")); if (radius) draw_circle((int)((double)(ux[i]) / x), (int)((double)(uy[i]) / y), (int)((double)(ux[i] + u_w) / x), (int)((double)(uy[i] + u_l) / y), 3); else draw_box((int)((double)(ux[i]) / x), (int)((double)(uy[i]) / y), (int)((double)(ux[i] + u_w) / x), (int)((double)(uy[i] + u_l) / y), 1); R_close_driver(); fprintf(stderr, " Distributed unit %4d of %4d requested\r", i + 1, num); } } /* for syst. contig. & syst. noncontig. */ else if (method == 2 || method == 3) { for (i = 0; i < num; i++) { *(ux + i) = left + startx + u_w * (i - nx * floor((double)i / nx)); *(uy + i) = top + starty + u_l * floor((double)i / nx); } } /* for centered over sites */ else if (method == 5) { sites_mapset = G_ask_vector_old(" Enter name of vector points map", sites_file_name); if (sites_mapset == NULL) { G_system("d.frame -e"); exit(0); } if (Vect_open_old(&Map, sites_file_name, sites_mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), sites_file_name); /* fprintf(stderr, "\n Can't open vector points file %s\n", sites_file_name); */ *sites = 0; i = 0; n = 0; Points = Vect_new_line_struct(); /* init line_pnts struct */ Cats = Vect_new_cats_struct(); while (1) { ltype = Vect_read_next_line(&Map, Points, Cats); if (ltype == -1) G_fatal_error(_("Cannot read vector")); if (ltype == -2) break; /* EOF */ /* point features only. (GV_POINTS is pts AND centroids, GV_POINT is just pts) */ if (!(ltype & GV_POINT)) continue; ulcol = ((int)(D_u_to_a_col(Points->x[0]))) + 1 - u_w / 2; ulrow = ((int)(D_u_to_a_row(Points->y[0]))) + 1 - u_l / 2; if (ulcol <= left || ulrow <= top || ulcol + u_w - 1 > right || ulrow + u_l - 1 > bot) { fprintf(stderr, " No sampling unit over site %d at east=%8.1f north=%8.1f\n", n + 1, Points->x[0], Points->y[0]); fprintf(stderr, " as it would extend outside the map\n"); } else { *(ux + i) = ulcol - 1; *(uy + i) = ulrow - 1; i++; } n++; if (n > 250) G_fatal_error ("There are more than the maximum of 250 sites\n"); } fprintf(stderr, " Total sites with sampling units = %d\n", i); *sites = i; cmd = G_malloc(100); sprintf(cmd, "d.vect %s color=black", sites_file_name); G_system(cmd); G_free(cmd); Vect_close(&Map); G_free(Points); G_free(Cats); } return 1; }
/* FOR STRATIFIED RANDOM DISTRIBUTION, DRAW THE STRATA ON THE SCREEN */ static void draw_grid(int l, int t, int w_w, int w_l, int h_d, int v_d, int starty, int startx, double colratio, double rowratio) { int j, k, l0, t0, itmp, dx, dy, initl, tmp; /* VARIABLES: k = the remainder after dividing the screen width/height by the number of strata dx = how many screen cols per stratum dy = how many screen rows per stratum l0 = left side of screen + dx t0 = top of screen + dy w_w = width of screen w_l = height of screen h_d = number of horizontal strata v_d = number of vertical strata */ R_open_driver(); R_standard_color(D_translate_color("orange")); if (startx > 0) { dx = (int)((double)((int)(colratio * ((int) ((double)(w_w - startx) / (double)(h_d)))) / colratio + 0.5)); l0 = l + startx; } else { dx = (int)((double)((int)(colratio * ((int)((double)(w_w) / (double)(h_d)))) / colratio + 0.5)); l0 = l; } if (starty > 0) { dy = (int)((double)((int)(rowratio * ((int) ((double)(w_l - starty) / (double)(v_d)))) / rowratio + 0.5)); t0 = t + starty; } else { dy = (int)((double)((int)(rowratio * ((int)((double)(w_l) / (double)(v_d)))) / rowratio + 0.5)); t0 = t; } initl = l0; /* draw the vertical strata */ for (j = 1; j <= h_d - 1; j++) { l0 += dx; R_move_abs(l0, t0); R_cont_rel(0, w_l - starty); } /* draw the horizontal strata */ for (j = 1; j <= v_d - 1; j++) { t0 += dy; R_move_abs(initl, t0); R_cont_rel(w_w - startx, 0); } R_close_driver(); return; }
/* DEFINE SAMPLING UNITS MANUALLY */ static void man_unit(int t, int b, int l, int r, char *n1, char *n2, char *n3, double *mx, int fmask) { int i, j, dx, dy, w_w, w_l, u_w, u_l, method, l0, t0, randflag = 0, unit_num, num = 0, scales, h_d = 1, v_d = 1, itmp, thick, sites, *row_buf, fr, k, count = 0, maxsize = 0, nx = 0, ny = 0, numx = 0, numy = 0, al = 0, ar = 0, at = 0, ab = 0, au_w = 0, au_l = 0; double *ux, *uy; FILE *fp; double dtmp, ratio, size, intv = 0.0, start[2], cnt = 0, radius = 0.0; char *sites_mapset; struct Cell_head wind; /* VARIABLES: COORDINATES IN THIS ROUTINE ARE IN CELLS t = top row of sampling frame b = bottom row of sampling frame l = left col of sampling frame r = right col of sampling frame n1 = n2 = n3 = start[0]= row of UL corner of starting pt for strata start[1]= col of UL corner of starting pt for strata mx[0] = cols of region/width of screen mx[1] = rows of region/height of screen */ start[0] = 0.0; start[1] = 0.0; l = (int)((double)(l * mx[0]) + 0.5); r = (int)((double)(r * mx[0]) + 0.5); t = (int)((double)(t * mx[1]) + 0.5); b = (int)((double)(b * mx[1]) + 0.5); w_w = r - l; w_l = b - t; /* draw the sampling frame */ R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1); R_close_driver(); /* open the units file for output */ fp = fopen0("r.le.para/units", "w"); G_sleep_on_error(0); /* get the number of scales */ do { fprintf(stderr, "\n How many different SCALES do you want (1-15)? "); numtrap(1, &dtmp); if (dtmp > 15 || dtmp < 1) { fprintf(stderr, "\n Too many (>15) or too few scales; try again"); } } while (dtmp < 1 || dtmp > 15); fprintf(fp, "%10d # of scales\n", (scales = (int)dtmp)); /* for each scale */ for (i = 0; i < scales; i++) { for (;;) { G_system("clear"); radius = 0.0; fprintf(stderr, "\n\n TYPE IN PARAMETERS FOR SCALE %d:\n", i + 1); /* get the distribution method */ fprintf(stderr, "\n Choose method of sampling unit DISTRIBUTION \n"); fprintf(stderr, " Random nonoverlapping 1\n"); fprintf(stderr, " Systematic contiguous 2\n"); fprintf(stderr, " Systematic noncontiguous 3\n"); fprintf(stderr, " Stratified random 4\n"); fprintf(stderr, " Centered over sites 5\n"); fprintf(stderr, " Exit to setup option menu 6\n\n"); do { fprintf(stderr, " Which Number? "); numtrap(1, &dtmp); if ((method = fabs(dtmp)) > 6 || method < 1) { fprintf(stderr, "\n Choice must between 1-5; try again"); } } while (method > 6 || method < 1); if (method == 6) return; /* for stratified random distribution, determine the number of strata */ if (method == 4) { getstrata: fprintf(stderr, "\n Number of strata along the x-axis? (1-60) "); numtrap(1, &dtmp); h_d = fabs(dtmp); fprintf(stderr, "\n Number of strata along the y-axis? (1-60) "); numtrap(1, &dtmp); v_d = fabs(dtmp); if (h_d < 1 || v_d < 1 || h_d > 60 || v_d > 60) { fprintf(stderr, "\n Number must be between 1-60; try again."); goto getstrata; } } /* for methods with strata */ if (method == 2 || method == 3 || method == 4) { strata: fprintf(stderr, "\n Sampling frame row & col for upper left corner of"); fprintf(stderr, " the strata?\n Rows are numbered down and columns"); fprintf(stderr, " are numbered to the right\n Enter 1 1 to start in"); fprintf(stderr, " upper left corner of sampling frame: "); numtrap(2, start); start[0] = start[0] - 1.0; start[1] = start[1] - 1.0; if (start[0] > w_l || start[0] < 0 || start[1] > w_w || start[1] < 0) { fprintf(stderr, "\n The starting row and col you entered are outside"); fprintf(stderr, " the sampling frame\n Try again\n"); goto strata; } } if (method == 4) { /* call draw_grid with the left, top, width, length, the number of horizontal and vertical strata, and the starting row and col for the strata */ draw_grid((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(w_w / mx[0] + 0.5), (int)(w_l / mx[1] + 0.5), h_d, v_d, (int)(start[0] / mx[1] + 0.5), (int)(start[1] / mx[0] + 0.5), mx[0], mx[1]); if (!G_yes(" Are these strata OK? ", 1)) { if (G_yes("\n\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1); R_close_driver(); } goto getstrata; } } /* if sampling using circles */ fprintf(stderr, "\n Do you want to sample using rectangles"); if (!G_yes ("\n (including squares) (y) or circles (n)? ", 1)) { getradius: fprintf(stderr, "\n What radius do you want for the circles? Radius"); fprintf(stderr, "\n is in pixels; add 0.5 pixels, for the center"); fprintf(stderr, "\n pixel, to the number of pixels outside the"); fprintf(stderr, "\n center pixel. Type a real number with one"); fprintf(stderr, "\n decimal place ending in .5 (e.g., 4.5): "); numtrap(1, &radius); if (radius > 100.0) { fprintf(stderr, "\n Are you sure that you want such a large"); if (!G_yes("\n radius (> 100 pixels)? ", 1)) goto getradius; } ratio = 1.0; u_w = (int)(2 * radius); u_l = (int)(2 * radius); if (fmask > 0) { count = 0; row_buf = Rast_allocate_buf(CELL_TYPE); fr = Rast_open_old(n1, G_mapset()); for (j = t; j < b; j++) { Rast_zero_buf(row_buf, CELL_TYPE); Rast_get_row(fr, row_buf, j, CELL_TYPE); for (k = l; k < r; k++) { if (*(row_buf + k)) count++; } } G_free(row_buf); Rast_close(fr); cnt = (double)(count); if (cnt) cnt = sqrt(cnt); else cnt = 0; } else { count = (w_l - (int)(start[0])) * (w_w - (int)(start[1])); } } /* if sampling using rectangles/squares */ else { /* get the width/length ratio */ getratio: fprintf(stderr, "\n Sampling unit SHAPE (aspect ratio, #cols/#rows) " "expressed as real number" "\n (e.g., 10 cols/5 rows = 2.0) for sampling units " "of scale %d? ", i + 1); numtrap(1, &ratio); if (ratio < 0) ratio = -ratio; else if (ratio > 25.0) if (!G_yes ("\n Are you sure you want such a large ratio? ", 1)) goto getratio; /* determine the recommended maximum size for sampling units */ getsize: dtmp = (ratio > 1) ? 1 / ratio : ratio; dtmp /= (h_d > v_d) ? h_d * h_d : v_d * v_d; tryagain: if (method == 1) { if (fmask > 0) { count = 0; row_buf = Rast_allocate_buf(CELL_TYPE); fr = Rast_open_old(n1, G_mapset()); for (j = t; j < b; j++) { Rast_zero_buf(row_buf, CELL_TYPE); Rast_get_row(fr, row_buf, j, CELL_TYPE); for (k = l; k < r; k++) { if (*(row_buf + k)) count++; } } G_free(row_buf); Rast_close(fr); cnt = (double)(count); if (cnt) cnt = sqrt(cnt); else cnt = 0; maxsize = ((cnt * dtmp / 2) * (cnt * dtmp / 2) > 1.0 / dtmp) ? (cnt * dtmp / 2) * (cnt * dtmp / 2) : 1.0 / dtmp; fprintf(stderr, "\n Recommended maximum SIZE is %d in %d cell total", maxsize, count); fprintf(stderr, " area\n"); } else { fprintf(stderr, "\n Recommended maximum SIZE is"); fprintf(stderr, " %d in %d pixel total area\n", (int)((w_l - (int)(start[0])) * (w_w - (int)(start [1])) * dtmp / 2), (w_l - (int)(start[0])) * (w_w - (int)(start[1]))); count = (w_l - (int)(start[0])) * (w_w - (int)(start[1])); maxsize = (int)((w_l - (int)(start[0])) * (w_w - (int)(start[1])) * dtmp / 2); } } else if (method == 2 || method == 3 || method == 5) { fprintf(stderr, "\n Recommended maximum SIZE is %d in %d pixel total", (int)((w_l - (int)(start[0])) * (w_w - (int)(start[1])) * dtmp / 2), (w_l - (int)(start[0])) * (w_w - (int)(start[1]))); fprintf(stderr, " area\n"); } else if (method == 4) { fprintf(stderr, "\n Recommended maximum SIZE is"); fprintf(stderr, " %d in %d pixel individual", (int)(w_w * w_l * dtmp / 2), ((w_w - (int)(start[1])) / h_d) * ((w_l - (int)(start [0])) / v_d)); fprintf(stderr, " stratum area\n"); } /* get the unit size, display the calculated size, and ask if it is OK */ fprintf(stderr, " What size (in pixels) for each sampling unit of scale %d? ", i + 1); numtrap(1, &size); thick = 1; if (size < 15 || ratio < 0.2 || ratio > 5) thick = 0; u_w = sqrt(size * ratio); u_l = sqrt(size / ratio); fprintf(stderr, "\n The nearest size is %d cells wide X %d cells high = %d", u_w, u_l, u_w * u_l); fprintf(stderr, " cells\n"); if (!u_w || !u_l) { fprintf(stderr, "\n 0 cells wide or high is not acceptable; try again"); goto tryagain; } if (!G_yes(" Is this SIZE OK? ", 1)) goto getsize; } /* for syst. noncontig. distribution, get the interval between units */ if (method == 3) { fprintf(stderr, "\n The interval, in pixels, between the units of scale"); fprintf(stderr, " %d? ", i + 1); numtrap(1, &intv); } /* if the unit dimension + the interval is too large, print a warning and try getting another size */ if (u_w + intv > w_w / h_d || u_l + intv > w_l / v_d) { fprintf(stderr, "\n Unit size too large for sampling frame; try again\n"); if (radius) goto getradius; else goto getsize; } /* for stratified random distribution, the number of units is the same as the number of strata */ if (method == 4) num = h_d * v_d; /* for the other distributions, calculate the maximum number of units, then get the number of units */ else if (method == 1 || method == 2 || method == 3) { if (method == 1) { if (! (unit_num = calc_num(w_w, w_l, ratio, u_w, u_l, method, intv, (int)(start[1]), (int)(start[0]), u_w * u_l, count))) { fprintf(stderr, "\n Something wrong with sampling unit size, try again\n"); if (radius) goto getradius; else goto getsize; } fprintf(stderr, "\n Maximum NUMBER of units in scale %d is %d\n", i + 1, unit_num); fprintf(stderr, " Usually 1/2 of this number can be successfully"); fprintf(stderr, " distributed\n More than 1/2 can sometimes be"); fprintf(stderr, " distributed\n"); } else if (method == 2 || method == 3) { numx = floor((double)(w_w - start[1]) / (u_w + intv)); numy = floor((double)(w_l - start[0]) / (u_l + intv)); if (((w_w - (int)(start[1])) % (numx * (u_w + (int)(intv)))) >= u_w) numx++; if (((w_l - (int)(start[0])) % (numy * (u_l + (int)(intv)))) >= u_l) numy++; unit_num = numx * numy; fprintf(stderr, "\n Maximum NUMBER of units in scale %d is %d as %d", i + 1, unit_num, numy); fprintf(stderr, " rows with %d units per row", numx); } do { fprintf(stderr, "\n What NUMBER of sampling units do you want to try"); fprintf(stderr, " to use? "); numtrap(1, &dtmp); if ((num = dtmp) > unit_num || num < 1) { fprintf(stderr, "\n %d is greater than the maximum number of", num); fprintf(stderr, " sampling units; try again\n"); } else if (method == 2 || method == 3) { fprintf(stderr, "\n How many sampling units do you want per row? "); numtrap(1, &dtmp); if ((nx = dtmp) > num) { fprintf(stderr, "\n Number in each row > number requested; try"); fprintf(stderr, " again\n"); } else { if (nx > numx) { fprintf(stderr, "\n Can't fit %d units in each row, try", nx); fprintf(stderr, " again\n"); } else { if (num % nx) ny = num / nx + 1; else ny = num / nx; if (ny > numy) { fprintf(stderr, "\n Can't fit the needed %d rows, try", ny); fprintf(stderr, " again\n"); } } } } } while (num > unit_num || num < 1 || nx > num || nx > numx || ny > numy); } /* dynamically allocate storage for arrays to store the upper left corner of sampling units */ if (method != 5) { ux = G_calloc(num + 1, sizeof(double)); uy = G_calloc(num + 1, sizeof(double)); } else { ux = G_calloc(250, sizeof(double)); uy = G_calloc(250, sizeof(double)); } /* calculate the upper left corner of sampling units and store them in arrays ux and uy */ if (!calc_unit_loc (radius, t, b, l, r, ratio, u_w, u_l, method, intv, num, h_d, v_d, ux, uy, &sites, (int)(start[1]), (int)(start[0]), fmask, nx, mx[0], mx[1])) goto last; signal(SIGINT, SIG_DFL); if (method == 5) num = sites; /* draw the sampling units on the screen */ if (method == 2 || method == 3 || method == 5) { R_open_driver(); R_standard_color(D_translate_color("red")); for (j = 0; j < num; j++) { if (radius) { draw_circle((int)((double)(ux[j]) / mx[0]), (int)((double)(uy[j]) / mx[1]), (int)((double)(ux[j] + u_w) / mx[0]), (int)((double)(uy[j] + u_l) / mx[1]), 3); } else { draw_box((int)((double)(ux[j]) / mx[0]), (int)((double)(uy[j]) / mx[1]), (int)((double)(ux[j] + u_w) / mx[0]), (int)((double)(uy[j] + u_l) / mx[1]), 1); } } R_close_driver(); } if (G_yes("\n Is this set of sampling units OK? ", 1)) break; last: signal(SIGINT, SIG_DFL); if (G_yes("\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0]), (int)(t / mx[1]), (int)(r / mx[0]), (int)(b / mx[1]), 1); R_close_driver(); } } /* save the sampling unit parameters in r.le.para/units file */ fprintf(fp, "%10d # of units of scale %d.\n", num, (i + 1)); fprintf(fp, "%10d%10d u_w, u_l of units in scale %d\n", u_w, u_l, (i + 1)); fprintf(fp, "%10.1f radius of circles in scale %d\n", radius, (i + 1)); for (j = 0; j < num; j++) fprintf(fp, "%10d%10d left, top of unit[%d]\n", (int)ux[j], (int)uy[j], j + 1); if (i < scales - 1 && G_yes("\n\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0]), (int)(t / mx[1]), (int)(r / mx[0]), (int)(b / mx[1]), 1); R_close_driver(); } } /* free dynamically allocated memory */ G_free(ux); G_free(uy); fclose(fp); return; }
static void graph_unit(int t, int b, int l, int r, char *n1, char *n2, char *n3, double *mx, int fmask) { int x0 = 0, y0 = 0, xp, yp, ux[250], uy[250], u_w, u_l, btn = 0, k = 0, w_w = 0, w_l = 0, *row_buf, at, ab, al, ar, circle = 0, tmpw, tmpl, au_w, au_l, lap = 0, l0 = 0, r0 = 0, t0 = 0, b0 = 0; FILE *fp; double tmp, radius = 0.0; register int i, j; /* VARIABLES: COORDINATES IN THIS ROUTINE ARE IN CELLS t = top row of sampling frame b = bottom row of sampling frame l = left col of sampling frame r = right col of sampling frame n1 = n2 = n3 = mx[0] = cols of region/width of screen mx[1] = rows of region/height of screen xp = mouse x location in screen coordinates (col) yp = mouse y location in screen coordinates (row) ar = mouse x location in map coordinates (col) al = mouse y location in map coordinates (row) */ l0 = l; r0 = r; t0 = t; b0 = b; l = (int)((double)(l * mx[0]) + 0.5); r = (int)((double)(r * mx[0]) + 0.5); t = (int)((double)(t * mx[1]) + 0.5); b = (int)((double)(b * mx[1]) + 0.5); w_w = r - l; w_l = b - t; /* draw the sampling frame */ R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0]), (int)(t / mx[1]), (int)(r / mx[0]), (int)(b / mx[1]), 1); R_close_driver(); fp = fopen0("r.le.para/units", "w"); G_sleep_on_error(0); /* get the number of scales */ do { fprintf(stderr, "\n How many different SCALES do you want? (1-15) "); numtrap(1, &tmp); if (tmp < 1 || tmp > 15) fprintf(stderr, " Too many (>15) or too few scales, try again.\n"); } while (tmp < 1 || tmp > 15); fprintf(fp, "%10d # of scales\n", (int)(tmp)); /* for each scale */ for (i = 0; i < tmp; i++) { G_system("clear"); radius = 0.0; circle = 0; /* if sampling using circles */ fprintf(stderr, "\n SCALE %d\n", i + 1); fprintf(stderr, "\n Do you want to sample using rectangles"); if (!G_yes("\n (including squares) (y) or circles (n)? ", 1)) { circle = 1; fprintf(stderr, "\n Draw a rectangular area to contain a standard circular"); fprintf(stderr, "\n sampling unit of scale %d. First select upper left", i + 1); fprintf(stderr, "\n corner, then lower right:\n"); fprintf(stderr, " Left button: Check unit size\n"); fprintf(stderr, " Middle button: Upper left corner of area here\n"); fprintf(stderr, " Right button: Lower right corner of area here\n"); } else { fprintf(stderr, "\n Draw a standard rectangular unit of scale %d.", i + 1); fprintf(stderr, "\n First select upper left corner, then lower right:\n"); fprintf(stderr, " Left button: Check unit size\n"); fprintf(stderr, " Middle button: Upper left corner of unit here\n"); fprintf(stderr, " Right button: Lower right corner of unit here\n"); } R_open_driver(); do { back1: R_get_location_with_box(x0, y0, &xp, &yp, &btn); /* convert the upper left screen coordinate (x0, y0) and the mouse position (xp, yp) on the screen to the nearest row and column; do the same for the sampling unit width (u_w) and height (u_l); then convert back */ ar = (int)((double)(xp) * mx[0] + 0.5); xp = (int)((double)(ar) / mx[0] + 0.5); al = (int)((double)(x0) * mx[0] + 0.5); x0 = (int)((double)(al) / mx[0] + 0.5); au_w = ar - al; u_w = (int)((double)(au_w) / mx[0] + 0.5); ab = (int)((double)(yp) * mx[1] + 0.5); yp = (int)((double)(ab) / mx[1] + 0.5); at = (int)((double)(y0) * mx[1] + 0.5); y0 = (int)((double)(at) / mx[1] + 0.5); au_l = ab - at; u_l = (int)((double)(au_l) / mx[1] + 0.5); /* left button, check the size of the rubber box in array system */ if (btn == 1) { if (ar > r || ab > b || ar < l || ab < t) { fprintf(stderr, "\n This point is not in the sampling frame; try again\n"); goto back1; } if (x0 < l || y0 < t) { fprintf(stderr, "\n Use the middle button to first put the upper left"); fprintf(stderr, "\n corner inside the sampling frame\n"); goto back1; } if (ar <= al || ab <= at) { fprintf(stderr, "\n Please put the lower right corner down and to"); fprintf(stderr, "\n the right of the upper left corner\n"); goto back1; } else { fprintf(stderr, "\n Unit would be %d columns wide by %d rows long\n", abs(au_w), abs(au_l)); fprintf(stderr, " Width/length would be %5.2f and size %d pixels\n", (double)abs((au_w)) / (double)abs((au_l)), abs(au_w) * abs(au_l)); } } /* mid button, move the start point of the rubber box */ else if (btn == 2) { if (ar > r || ab > b || ar < l || ab < t) { fprintf(stderr, "\n Point is not in the sampling frame; try again\n"); goto back1; } else { R_move_abs(xp, yp); x0 = xp; y0 = yp; } } /* right button, outline the unit */ else if (btn == 3) { if (circle) { if (u_w > u_l) { al = al + ((ar - al) - (ab - at)) / 2; ar = al + (ab - at); x0 = (int)((double)(al) / mx[0] + 0.5); xp = (int)((double)(ar) / mx[0] + 0.5); au_w = ar - al; u_w = u_l = (int)((double)(au_w) / mx[0] + 0.5); } if (u_l > u_w) { at = at + ((ab - at) - (ar - al)) / 2; ab = at + (ar - al); y0 = (int)((double)(at) / mx[1] + 0.5); yp = (int)((double)(ab) / mx[1] + 0.5); au_l = ab - at; u_w = u_l = (int)((double)(au_l) / mx[1] + 0.5); } } if (ar > r || ab > b || al < l || at < t) { fprintf(stderr, "\n The unit extends outside the sampling frame or map;"); fprintf(stderr, "\n try again\n"); goto back1; } if (au_w > w_w || au_l > w_l) { fprintf(stderr, "\n The unit is too big for the sampling frame; "); fprintf(stderr, "try again\n"); goto back1; } /* if there is a mask, check to see that the unit will be within the mask area, by checking to see whether the four corners of the unit are in the mask */ if (fmask > 0) { row_buf = Rast_allocate_c_buf(); Rast_get_c_row_nomask(fmask, row_buf, at); if (!(*(row_buf + al) && *(row_buf + ar - 1))) { fprintf(stderr, "\n The unit would be outside the mask; "); fprintf(stderr, "try again\n"); G_free(row_buf); goto back1; } Rast_zero_c_buf(row_buf); Rast_get_c_row_nomask(fmask, row_buf, ab - 1); if (!(*(row_buf + al) && *(row_buf + ar - 1))) { fprintf(stderr, "\n The unit would be outside the mask; "); fprintf(stderr, "try again\n"); G_free(row_buf); goto back1; } G_free(row_buf); } if (xp - x0 > 0 && yp - y0 > 0) { R_standard_color(D_translate_color("red")); if (circle) draw_circle(x0, y0, xp, yp, 3); else draw_box(x0, y0, xp, yp, 1); G_system("clear"); if (circle) { fprintf(stderr, "\n\n The standard circular sampling unit has:\n"); fprintf(stderr, " radius = %f pixels\n", (double)(ar - al) / 2.0); } else { fprintf(stderr, "\n\n The standard sampling unit has:\n"); fprintf(stderr, " columns=%d rows=%d\n", abs(ar - al), abs(ab - at)); fprintf(stderr, " width/length ratio=%5.2f\n", (double)abs(ar - al) / (double)abs(ab - at)); fprintf(stderr, " size=%d pixels\n", abs(ar - al) * abs(ab - at)); } k = 0; ux[0] = al; uy[0] = at; } else if (xp - x0 == 0 || yp - y0 == 0) { fprintf(stderr, "\n Unit has 0 rows and/or 0 columns; try again\n"); goto back1; } else { fprintf(stderr, "\n You did not put the lower right corner below"); fprintf(stderr, "\n and to the right of the upper left corner. Please try again"); goto back1; } } } while (btn != 3); R_close_driver(); /* use the size and shape of the standard unit to outline more units in that scale */ fprintf(stderr, "\n Outline more sampling units of scale %d?\n", i + 1); fprintf(stderr, " Left button: Exit\n"); fprintf(stderr, " Middle button: Check unit position\n"); fprintf(stderr, " Right button: Lower right corner of next unit here\n"); R_open_driver(); /* if not the left button (to exit) */ back2: while (btn != 1) { R_get_location_with_box(xp - u_w, yp - u_l, &xp, &yp, &btn); /* convert the left (x0), right (y0), top (y0), bottom (yp) coordinates in screen pixels to the nearest row and column; do the same for the sampling unit width (u_w) and height (u_l); then convert back */ ar = (int)((double)(xp) * mx[0] + 0.5); ab = (int)((double)(yp) * mx[1] + 0.5); xp = (int)((double)(ar) / mx[0] + 0.5); yp = (int)((double)(ab) / mx[1] + 0.5); al = (int)((double)(xp - u_w) * mx[0] + 0.5); at = (int)((double)(yp - u_l) * mx[0] + 0.5); x0 = (int)((double)(al) / mx[0] + 0.5); y0 = (int)((double)(at) / mx[1] + 0.5); /* if right button, outline the unit */ if (btn == 3) { if (ar > r || ab > b || al < l || at < t) { fprintf(stderr, "\n The unit would be outside the map; try again"); goto back2; } /* if there is a mask, check to see that the unit will be within the mask area */ if (fmask > 0) { row_buf = Rast_allocate_c_buf(); Rast_get_c_row_nomask(fmask, row_buf, at); if (!(*(row_buf + al) && *(row_buf + ar - 1))) { fprintf(stderr, "\n The unit would be outside the mask; "); fprintf(stderr, "try again"); G_free(row_buf); goto back2; } Rast_zero_c_buf(row_buf); Rast_get_c_row_nomask(fmask, row_buf, ab - 1); if (!(*(row_buf + al) && *(row_buf + ar - 1))) { fprintf(stderr, "\n The unit would be outside the mask; "); fprintf(stderr, "try again"); G_free(row_buf); goto back2; } G_free(row_buf); } /* check for sampling unit overlap */ lap = 0; for (j = 0; j < k + 1; j++) { if (overlap(al, at, ux[j], uy[j], au_w, au_l)) { fprintf(stderr, "\n The unit would overlap a previously drawn "); fprintf(stderr, "unit; try again"); lap = 1; } } if (lap) goto back2; k++; fprintf(stderr, "\n %d sampling units have been placed", (k + 1)); ux[k] = al; uy[k] = at; R_standard_color(D_translate_color("red")); if (circle) draw_circle(x0, y0, xp, yp, 3); else draw_box(x0, y0, xp, yp, 1); } } R_close_driver(); /* save the sampling units in the r.le.para/units file */ if (circle) radius = (double)(ar - al) / 2.0; else radius = 0.0; fprintf(fp, "%10d # of units of scale %d\n", k + 1, i + 1); fprintf(fp, "%10d%10d u_w, u_l of units in scale %d\n", (int)(u_w * mx[0]), (int)(u_l * mx[1]), i + 1); fprintf(fp, "%10.1f radius of circles in scale %d\n", radius, (i + 1)); for (j = 0; j < k + 1; j++) fprintf(fp, "%10d%10d left, top of unit[%d]\n", ux[j], uy[j], j + 1); if (i < tmp - 1 && G_yes("\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("red")); R_close_driver(); } } fclose(fp); return; }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *input, *vect; struct Cell_head window; int bot, right, t0, b0, l0, r0, clear = 0; double Rw_l, Rscr_wl; char *map_name = NULL, *v_name = NULL, *s_name = NULL; /* Initialize the GIS calls */ G_gisinit(argv[0]); /* must run in a term window */ G_putenv("GRASS_UI_TERM", "1"); module = G_define_module(); module->keywords = _("raster, landscape structure analysis, patch index"); module->description = _("Interactive tool used to setup the sampling and analysis framework " "that will be used by the other r.le programs."); input = G_define_standard_option(G_OPT_R_MAP); input->description = _("Raster map to use to setup sampling"); vect = G_define_standard_option(G_OPT_V_INPUT); vect->key = "vect"; vect->description = _("Vector map to overlay"); vect->required = NO; if (G_parser(argc, argv)) exit(EXIT_FAILURE); setbuf(stdout, NULL); /* unbuffered */ setbuf(stderr, NULL); G_sleep_on_error(1); /* error messages get lost on clear screen */ map_name = input->answer; v_name = vect->answer; s_name = NULL; /* sites not used in GRASS 6 */ /* setup the r.le.para directory */ get_pwd(); /* query for the map to be setup */ if (R_open_driver() != 0) G_fatal_error("No graphics device selected"); /* setup the current window for display & clear screen */ D_setup(1); Rw_l = (double)G_window_cols() / G_window_rows(); /*R_open_driver(); */ /* R_font("romant"); */ G_get_set_window(&window); t0 = R_screen_top(); b0 = R_screen_bot(); l0 = R_screen_left(); r0 = R_screen_rite(); Rscr_wl = (double)(r0 - l0) / (b0 - t0); if (Rscr_wl > Rw_l) { bot = b0; right = l0 + (b0 - t0) * Rw_l; } else { right = r0; bot = t0 + (r0 - l0) / Rw_l; } D_new_window("a", t0, bot, l0, right); D_set_cur_wind("a"); D_show_window(D_translate_color("green")); D_setup(clear); R_close_driver(); /* invoke the setup modules */ set_map(map_name, v_name, s_name, window, t0, bot, l0, right); return (EXIT_SUCCESS); }
int main(int argc, char **argv) { struct GModule *module; int i, first = 1; char *mapset; char **rast, **vect; int nrasts, nvects; struct Cell_head window, temp_window; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("display, setup"); module->description = "Sets window region so that all currently displayed raster " "and vector maps can be shown in a monitor."; if (argc > 1 && G_parser(argc, argv)) exit(-1); if (R_open_driver() != 0) G_fatal_error(_("No graphics device selected")); if (D_get_cell_list(&rast, &nrasts) < 0) rast = NULL; if (D_get_dig_list(&vect, &nvects) < 0) vect = NULL; R_close_driver(); if (rast == NULL && vect == NULL) G_fatal_error(_("No raster or vector map displayed")); G_get_window(&window); if (rast) { for (i = 0; i < nrasts; i++) { mapset = G_find_cell2(rast[i], ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), rast[i]); if (G_get_cellhd(rast[i], mapset, &temp_window) >= 0) { if (first) { first = 0; G_copy(&window, &temp_window, sizeof(window)); } else { if (window.east < temp_window.east) window.east = temp_window.east; if (window.west > temp_window.west) window.west = temp_window.west; if (window.south > temp_window.south) window.south = temp_window.south; if (window.north < temp_window.north) window.north = temp_window.north; /* if(window.ns_res < nsres) nsres = window.ns_res; if(window.ew_res < ewres) ewres = window.ew_res; */ } } } G_adjust_Cell_head3(&window, 0, 0, 0); } if (vect) { struct Map_info Map; G_copy(&temp_window, &window, sizeof(window)); Vect_set_open_level(2); for (i = 0; i < nvects; i++) { mapset = G_find_vector2(vect[i], ""); if (mapset == NULL) G_fatal_error(_("Vector map <%s> not found"), vect[i]); if (Vect_open_old_head(&Map, vect[i], mapset) == 2) { if (first) { first = 0; window.east = Map.plus.box.E; window.west = Map.plus.box.W; window.south = Map.plus.box.S; window.north = Map.plus.box.N; } else { if (window.east < Map.plus.box.E) window.east = Map.plus.box.E; if (window.west > Map.plus.box.W) window.west = Map.plus.box.W; if (window.south > Map.plus.box.S) window.south = Map.plus.box.S; if (window.north < Map.plus.box.N) window.north = Map.plus.box.N; } Vect_close(&Map); } } if (window.north == window.south) { window.north += 0.5 * temp_window.ns_res; window.south -= 0.5 * temp_window.ns_res; } if (window.east == window.west) { window.east += 0.5 * temp_window.ew_res; window.west -= 0.5 * temp_window.ew_res; } G_align_window(&window, &temp_window); } G_adjust_Cell_head3(&window, 0, 0, 0); G_put_window(&window); exit(0); }
static void set_frame(double *msc, int *t, int *b, int *l, int *r) { int t0, b0, l0, r0, btn; /* record the initial boundaries of the map */ t0 = *t; b0 = *b; l0 = *l; r0 = *r; /* if the total area to be sampled will be the whole map */ G_system("clear"); if (G_yes ("\n Will the sampling frame (total area within which sampling\n units are distributed) be the whole map? ", 1)) { R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box(*l, *t, *r, *b, 1); R_close_driver(); fprintf(stderr, "\n Sampling frame set to whole map"); } /* if the total area to be sampled is not the whole map, then have the user draw the area */ else { back: G_system("clear"); fprintf(stderr, " \n OUTLINE SAMPLING FRAME:\n"); R_open_driver(); fprintf(stderr, "\n Please move cursor to the UPPER-LEFT corner of\n"); fprintf(stderr, " the sampling frame and click any mouse button\n"); R_get_location_with_line(0, 0, l, t, &btn); fprintf(stderr, "\n Please move cursor to the LOWER-RIGHT corner of\n"); fprintf(stderr, " the sampling frame and click any mouse button again\n"); back2: R_get_location_with_box(*l, *t, r, b, &btn); /* check that sampling frame is in map */ if (*l < l0 || *r > r0 || *t < t0 || *b > b0) { fprintf(stderr, "\n The cursor is outside of the map, try again\n"); goto back; } /* check that cursor is below & to right */ if (*r <= *l || *b <= *t) { fprintf(stderr, "\n Please put the lower right corner below and to the"); fprintf(stderr, "\n right of the upper left corner\n"); goto back2; } R_standard_color(D_translate_color("grey")); *l = (int)((double)((int)(*l * msc[0] + 0.5)) / msc[0]); *r = (int)((double)((int)(*r * msc[0] + 0.5)) / msc[0]); *t = (int)((double)((int)(*t * msc[1] + 0.5)) / msc[1]); *b = (int)((double)((int)(*b * msc[1] + 0.5)) / msc[1]); draw_box(*l, *t, *r, *b, 1); R_close_driver(); fprintf(stderr, "\n Sampling frame is set to the area you just drew"); } return; }
static void set_rgn(double *msc, char *name, char *name1, char *name2) { char reg_name[20]; int x0, y0, xp, yp, *x, *y, xstart, ystart, btn, d, method, meth; static int pts, rgn_cnt = 0; double dtmp, etmp; FILE *tmp; char *tempfile; /* get the name of the regions map */ if (!G_ask_cell_new(" ENTER THE NEW REGION MAP NAME:", reg_name)) return; /* allocate memory for storing the points along the boundary of each region */ x = (int *)G_malloc(100 * sizeof(int)); y = (int *)G_malloc(100 * sizeof(int)); tempfile = G_tempfile(); tmp = fopen(tempfile, "w"); back2: G_system("clear"); fprintf(stderr, "\n\n CHOOSE AN OPTION:\n\n"); fprintf(stderr, " Draw a region 1\n"); fprintf(stderr, " Quit drawing regions and return"); fprintf(stderr, "\n to setup options menu 2\n"); fprintf(stderr, " Change the color for drawing 3\n\n"); do { fprintf(stderr, " Which Number? "); numtrap(1, &etmp); if ((meth = fabs(etmp)) > 3 || meth < 1) { fprintf(stderr, "\n Choice must between 1-3; try again"); } } while (meth > 3 || meth < 1); if (meth == 2) return; if (meth == 3) { R_open_driver(); change_draw(); } if (meth == 1) { R_open_driver(); rgn_cnt = 0; } /* ask the user to outline a region */ back: G_system("clear"); ppoint(NULL, 0, 0, -1); fprintf(stderr, "\n PLEASE OUTLINE REGION # %d\n", (++rgn_cnt)); pbutton(0); pts = 0; x0 = 0; y0 = 0; /* get the points along the boundary of the region as they are drawn */ do { btn = 0; R_get_location_with_line(x0, y0, &xp, &yp, &btn); if (btn == 1) ppoint(msc, xp, yp, 0); else if (btn == 2) { if (!pts) { pbutton(1); R_move_abs(xp, yp); xstart = xp; ystart = yp; } x[pts] = xp * msc[0]; y[pts] = yp * msc[1]; ppoint(msc, xp, yp, (++pts)); x0 = xp; y0 = yp; R_cont_abs(x0, y0); } else if (btn == 3 && pts < 3) { fprintf(stderr, "\n\n Please digitize more than 2 boundary points\n\n"); } } while (btn != 3 || pts < 3); R_cont_abs(xstart, ystart); R_close_driver(); R_open_driver(); x[pts] = x[0]; y[pts] = y[0]; pts++; /* redisplay the menu and find out what to do next */ back1: G_system("clear"); fprintf(stderr, "\n\n CHOOSE AN OPTION:\n\n"); fprintf(stderr, " Draw another region 1\n"); fprintf(stderr, " Start over drawing regions 2\n"); fprintf(stderr, " Quit drawing and save the region map 3\n"); fprintf(stderr, " Quit drawing and don't save the region map 4\n"); fprintf(stderr, " Change the color for drawing 5\n\n"); do { fprintf(stderr, " Which Number? "); numtrap(1, &dtmp); if ((method = fabs(dtmp)) > 5 || method < 1) { fprintf(stderr, "\n Choice must between 1-5; try again"); } } while (method > 5 || method < 1); /* save the region and draw another */ if (method == 1) { save_rgn(reg_name, tempfile, tmp, x, y, pts, rgn_cnt, 1); goto back; } /* start over */ else if (method == 2) { fclose(tmp); if (!(tmp = fopen(tempfile, "w"))) G_fatal_error ("Can't open temporary file for storing region info\n"); rgn_cnt = 0; R_close_driver(); paint_map(name, name1, name2); goto back2; } /* save the region and exit */ else if (method == 3) save_rgn(reg_name, tempfile, tmp, x, y, pts, rgn_cnt, 2); /* change the color for drawing */ else if (method == 5) { change_draw(); goto back1; } R_close_driver(); G_free(x); G_free(y); unlink(tempfile); return; }
int main(int argc, char **argv) { char name[128] = ""; struct Option *map; struct GModule *module; char *mapset; char buff[500]; /* must run in a term window */ G_putenv("GRASS_UI_TERM", "1"); /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("raster")); module->description = "Allows the user to interactively change the color table " "of a raster map layer displayed on the graphics monitor."; map = G_define_option(); map->key = "map"; map->type = TYPE_STRING; if (*name) map->answer = name; if (*name) map->required = NO; else map->required = YES; map->gisprompt = "old,cell,raster"; map->description = "Name of raster map"; if (G_parser(argc, argv)) exit(1); /* Make sure map is available */ if (map->answer == NULL) exit(0); mapset = G_find_raster2(map->answer, ""); if (mapset == NULL) { char msg[256]; sprintf(msg, "Raster file [%s] not available", map->answer); G_fatal_error(msg); } if (Rast_map_is_fp(map->answer, mapset)) { sprintf(buff, "Raster file [%s] is floating point! \nd.colors only works with integer maps", map->answer); G_fatal_error(buff); } /* connect to the driver */ if (R_open_driver() != 0) G_fatal_error("No graphics device selected"); /* Read in the map region associated with graphics window */ D_setup(0); get_map_info(map->answer, mapset); R_close_driver(); exit(0); }
int main(int argc, char *argv[]) { int ret; struct GModule *module; struct Option *gisdbase_opt, *location_opt, *mapset_opt; struct Flag *f_add, *f_list; char *gisdbase_old, *location_old, *mapset_old; char *gisdbase_new, *location_new, *mapset_new; char *gis_lock; char *mapset_old_path, *mapset_new_path; char *lock_prog; char path[GPATH_MAX]; char *shell, *monitor; struct MON_CAP *cap; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("general, settings"); module->label = _("Changes current mapset."); module->description = _("Optionally create new mapset or list available mapsets in given location."); mapset_opt = G_define_option(); mapset_opt->key = "mapset"; mapset_opt->type = TYPE_STRING; mapset_opt->required = NO; mapset_opt->multiple = NO; mapset_opt->description = _("Name of mapset where to switch"); mapset_opt->guisection = _("Settings"); location_opt = G_define_option(); location_opt->key = "location"; location_opt->type = TYPE_STRING; location_opt->required = NO; location_opt->multiple = NO; location_opt->description = _("Location name (not location path)"); location_opt->guisection = _("Settings"); gisdbase_opt = G_define_option(); gisdbase_opt->key = "gisdbase"; gisdbase_opt->type = TYPE_STRING; gisdbase_opt->required = NO; gisdbase_opt->multiple = NO; gisdbase_opt->key_desc = "path"; gisdbase_opt->description = _("GIS data directory (full path to the directory where the new location is)"); gisdbase_opt->guisection = _("Settings"); f_add = G_define_flag(); f_add->key = 'c'; f_add->description = _("Create mapset if it doesn't exist"); f_add->answer = FALSE; f_add->guisection = _("Create"); f_list = G_define_flag(); f_list->key = 'l'; f_list->description = _("List available mapsets"); f_list->guisection = _("Print"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (!mapset_opt->answer && !f_list->answer) G_fatal_error(_("Either mapset= or -l must be used")); /* Store original values */ gisdbase_old = G__getenv("GISDBASE"); location_old = G__getenv("LOCATION_NAME"); mapset_old = G__getenv("MAPSET"); G_asprintf(&mapset_old_path, "%s/%s/%s", gisdbase_old, location_old, mapset_old); monitor = G__getenv("MONITOR"); /* New values */ if (gisdbase_opt->answer) gisdbase_new = gisdbase_opt->answer; else gisdbase_new = gisdbase_old; if (location_opt->answer) location_new = location_opt->answer; else location_new = location_old; if (f_list->answer) { char **ms; int nmapsets; G__setenv("LOCATION_NAME", location_new); G__setenv("GISDBASE", gisdbase_new); ms = G_available_mapsets(); for (nmapsets = 0; ms[nmapsets]; nmapsets++) { if (G__mapset_permissions(ms[nmapsets]) > 0) { fprintf(stdout, "%s ", ms[nmapsets]); } } fprintf(stdout, "\n"); exit(EXIT_SUCCESS); } mapset_new = mapset_opt->answer; G_asprintf(&mapset_new_path, "%s/%s/%s", gisdbase_new, location_new, mapset_new); /* TODO: this should be checked better (repeated '/' etc.) */ if (strcmp(mapset_old_path, mapset_new_path) == 0) G_fatal_error(_("<%s> is already the current mapset"), mapset_new); /* Check if the mapset exists and user is owner */ G_debug(2, "check : %s", mapset_new_path); ret = G__mapset_permissions2(gisdbase_new, location_new, mapset_new); switch (ret) { case 0: G_fatal_error(_("You don't have permission to use this mapset")); break; case -1: if (f_add->answer == TRUE) { G_debug(2, "Mapset %s doesn't exist, attempting to create it", mapset_new); G_make_mapset(gisdbase_new, location_new, mapset_new); } else G_fatal_error(_("The mapset does not exist. Use -c flag to create it.")); break; default: break; } /* Check if the mapset is in use */ gis_lock = getenv("GIS_LOCK"); if (!gis_lock) G_fatal_error(_("Unable to read GIS_LOCK environment variable")); G_asprintf(&lock_prog, "%s/etc/lock", G_gisbase()); sprintf(path, "%s/.gislock", mapset_new_path); G_debug(2, path); ret = G_spawn(lock_prog, lock_prog, path, gis_lock, NULL); G_debug(2, "lock result = %d", ret); G_free(lock_prog); /* Warning: the value returned by system() is not that returned by exit() in executed program * e.g. exit(1) -> 256 (multiplied by 256) */ if (ret != 0) G_fatal_error(_("%s is currently running GRASS in selected mapset or lock file cannot be checked"), G_whoami()); /* Erase monitors */ G_message(_("Erasing monitors...")); while ((cap = R_parse_monitorcap(MON_NEXT, "")) != NULL) { G__setenv("MONITOR", cap->name); R__open_quiet(); if (R_open_driver() == 0) { D_erase(DEFAULT_BG_COLOR); D_add_to_list("d.erase"); R_close_driver(); R_release_driver(); } } if (monitor) G_setenv("MONITOR", monitor); /* Clean temporary directory */ sprintf(path, "%s/etc/clean_temp", G_gisbase()); G_verbose_message(_("Cleaning up temporary files...")); G_spawn(path, "clean_temp", NULL); /* Reset variables */ G_setenv("GISDBASE", gisdbase_new); G_setenv("LOCATION_NAME", location_new); G_setenv("MAPSET", mapset_new); /* Remove old lock */ sprintf(path, "%s/.gislock", mapset_old_path); remove(path); G_free(mapset_old_path); G_important_message(_("Your shell continues to use the history for the old mapset")); if ((shell = getenv("SHELL"))) { if (strstr(shell, "bash")) { G_important_message(_("You can switch the history by commands:\n" "history -w; history -r %s/.bash_history; HISTFILE=%s/.bash_history"), mapset_new_path, mapset_new_path); } else if (strstr(shell, "tcsh")) { G_important_message(_("You can switch the history by commands:\n" "history -S; history -L %s/.history; setenv histfile=%s/.history"), mapset_new_path, mapset_new_path); } } G_message(_("Your current mapset is <%s>"), mapset_new); G_free(mapset_new_path); return (EXIT_SUCCESS); }
int main(int argc, char **argv) { int colorg = 0; int colorb = 0; int colort = 0; double size = 0., gsize = 0.; /* initialize to zero */ double east, north; int do_text, fontsize, mark_type, line_width; struct GModule *module; struct Option *opt1, *opt2, *opt3, *opt4, *fsize, *tcolor, *lwidth; struct Flag *noborder, *notext, *geogrid, *nogrid, *wgs84, *cross, *fiducial, *dot; struct pj_info info_in; /* Proj structures */ struct pj_info info_out; /* Proj structures */ /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("display, cartography"); module->description = _("Overlays a user-specified grid " "in the active display frame on the graphics monitor."); opt2 = G_define_option(); opt2->key = "size"; opt2->key_desc = "value"; opt2->type = TYPE_STRING; opt2->required = YES; opt2->label = _("Size of grid to be drawn"); opt2->description = _("In map units or DDD:MM:SS format. " "Example: \"1000\" or \"0:10\""); opt3 = G_define_option(); opt3->key = "origin"; opt3->type = TYPE_STRING; opt3->key_desc = "easting,northing"; opt3->answer = "0,0"; opt3->multiple = NO; opt3->description = _("Lines of the grid pass through this coordinate"); lwidth = G_define_option(); lwidth->key = "width"; lwidth->type = TYPE_DOUBLE; lwidth->required = NO; lwidth->description = _("Grid line width"); opt1 = G_define_standard_option(G_OPT_C_FG); opt1->answer = "gray"; opt1->label = _("Grid color"); opt1->guisection = _("Color"); opt4 = G_define_standard_option(G_OPT_C_FG); opt4->key = "bordercolor"; opt4->label = _("Border color"); opt4->guisection = _("Color"); tcolor = G_define_standard_option(G_OPT_C_FG); tcolor->key = "textcolor"; tcolor->answer = "gray"; tcolor->label = _("Text color"); tcolor->guisection = _("Color"); fsize = G_define_option(); fsize->key = "fontsize"; fsize->type = TYPE_INTEGER; fsize->required = NO; fsize->answer = "9"; fsize->options = "1-72"; fsize->description = _("Font size for gridline coordinate labels"); geogrid = G_define_flag(); geogrid->key = 'g'; geogrid->description = _("Draw geographic grid (referenced to current ellipsoid)"); wgs84 = G_define_flag(); wgs84->key = 'w'; wgs84->description = _("Draw geographic grid (referenced to WGS84 ellipsoid)"); cross = G_define_flag(); cross->key = 'c'; cross->description = _("Draw '+' marks instead of grid lines"); dot = G_define_flag(); dot->key = 'd'; dot->description = _("Draw '.' marks instead of grid lines"); fiducial = G_define_flag(); fiducial->key = 'f'; fiducial->description = _("Draw fiducial marks instead of grid lines"); nogrid = G_define_flag(); nogrid->key = 'n'; nogrid->description = _("Disable grid drawing"); nogrid->guisection = _("Disable"); noborder = G_define_flag(); noborder->key = 'b'; noborder->description = _("Disable border drawing"); noborder->guisection = _("Disable"); notext = G_define_flag(); notext->key = 't'; notext->description = _("Disable text drawing"); notext->guisection = _("Disable"); /* Check command line */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* do some checking */ if (nogrid->answer && noborder->answer) G_fatal_error(_("Both grid and border drawing are disabled")); if (wgs84->answer) geogrid->answer = 1; /* -w implies -g */ if (geogrid->answer && G_projection() == PROJECTION_LL) G_fatal_error(_("Geo-Grid option is not available for LL projection")); if (geogrid->answer && G_projection() == PROJECTION_XY) G_fatal_error(_("Geo-Grid option is not available for XY projection")); if (notext->answer) do_text = FALSE; else do_text = TRUE; if (lwidth->answer) { line_width = atoi(lwidth->answer); if(line_width < 0 || line_width > 1e3) G_fatal_error("Invalid line width."); } else line_width = 0; fontsize = atoi(fsize->answer); mark_type = MARK_GRID; if (cross->answer + fiducial->answer + dot->answer > 1) G_fatal_error(_("Choose a single mark style")); if (cross->answer) mark_type = MARK_CROSS; if (fiducial->answer) mark_type = MARK_FIDUCIAL; if (dot->answer) mark_type = MARK_DOT; /* get grid size */ if (geogrid->answer) { if (!G_scan_resolution(opt2->answer, &gsize, PROJECTION_LL) || gsize <= 0.0) G_fatal_error(_("Invalid geo-grid size <%s>"), opt2->answer); } else { if (!G_scan_resolution(opt2->answer, &size, G_projection()) || size <= 0.0) G_fatal_error(_("Invalid grid size <%s>"), opt2->answer); } /* get grid easting start */ if (!G_scan_easting(opt3->answers[0], &east, G_projection())) { G_usage(); G_fatal_error(_("Illegal east coordinate <%s>"), opt3->answers[0]); } /* get grid northing start */ if (!G_scan_northing(opt3->answers[1], &north, G_projection())) { G_usage(); G_fatal_error(_("Illegal north coordinate <%s>"), opt3->answers[1]); } /* Setup driver and check important information */ if (R_open_driver() != 0) G_fatal_error(_("No graphics device selected")); /* Parse and select grid color */ colorg = D_parse_color(opt1->answer, FALSE); /* Parse and select border color */ colorb = D_parse_color(opt4->answer, FALSE); /* Parse and select text color */ colort = D_parse_color(tcolor->answer, FALSE); D_setup(0); /* draw grid */ if (!nogrid->answer) { if (geogrid->answer) { /* initialzie proj stuff */ init_proj(&info_in, &info_out, wgs84->answer); plot_geogrid(gsize, info_in, info_out, do_text, colorg, colort, fontsize, mark_type, line_width); } else { /* Do the grid plotting */ plot_grid(size, east, north, do_text, colorg, colort, fontsize, mark_type, line_width); } } /* Draw border */ if (!noborder->answer) { /* Set border color */ D_raster_use_color(colorb); /* Do the border plotting */ plot_border(size, east, north); } D_add_to_list(G_recreate_command()); R_close_driver(); exit(EXIT_SUCCESS); }
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); }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *element, *prompt; char *tempfile; char command[1024]; FILE *fd; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("display, map management"); module->description = _("Prompts the user to select a GRASS data base file from among " "files displayed in a menu on the graphics monitor."); element = G_define_option(); element->key = "element"; element->key_desc = "name,description"; element->type = TYPE_STRING; element->required = YES; element->description = _("Database element, one word description"); prompt = G_define_option(); prompt->key = "prompt"; prompt->key_desc = "message"; prompt->type = TYPE_STRING; prompt->description = _("Short user prompt message"); G_disable_interactive(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* make sure we can do graphics */ if (R_open_driver() != 0) G_fatal_error(_("No graphics device selected")); R_close_driver(); tempfile = G_tempfile(); unlink(tempfile); sprintf(command, "%s/etc/i.find %s %s %s %s", G_gisbase(), G_location(), G_mapset(), element->answers[0], tempfile); system(command); if (access(tempfile, 0) == 0) { if (prompt->answer) { sprintf(command, "%s/etc/i.ask %s '%s'", G_gisbase(), tempfile, prompt->answer); } else { sprintf(command, "%s/etc/i.ask %s", G_gisbase(), tempfile); } exit(system(command)); } else { fd = popen("d.menu tcolor=red > /dev/null", "w"); if (fd) { fprintf(fd, _("** no %s files found **\n"), element->answers[1]); fprintf(fd, _("Click here to CONTINUE\n")); pclose(fd); } exit(EXIT_SUCCESS); } }
int main(int argc, char **argv) { struct Flag *once, *terse, *txt, *topo_flag, *flash, *edit_flag; struct Option *opt1; struct GModule *module; char *mapset, *openvect(); char *str; int i, j, level, width = 0, mwidth = 0; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("display, vector"); module->description = _("Allows the user to interactively query a vector map layer " "at user-selected locations within the current geographic region."); /* Conditionalize R_open_driver() so "help" works, open quiet as well */ R__open_quiet(); if (R_open_driver() == 0) { if (D_get_dig_list(&vect, &nvects) < 0) vect = NULL; else { vect = (char **)G_realloc(vect, (nvects + 1) * sizeof(char *)); vect[nvects] = NULL; } R_close_driver(); } once = G_define_flag(); once->key = '1'; once->description = _("Identify just one location"); opt1 = G_define_option(); opt1->key = "map"; opt1->type = TYPE_STRING; opt1->multiple = YES; opt1->key_desc = "name"; if (vect) opt1->answers = vect; opt1->required = NO; opt1->gisprompt = "old,vector,vector"; opt1->description = _("Name of existing vector map"); terse = G_define_flag(); terse->key = 't'; terse->description = _("Terse output. For parsing by programs"); txt = G_define_flag(); txt->key = 'x'; txt->description = _("Print information as plain text to terminal window"); topo_flag = G_define_flag(); topo_flag->key = 'd'; topo_flag->description = _("Print topological information (debugging)"); flash = G_define_flag(); flash->key = 'f'; flash->description = _("Enable flashing (slower)"); edit_flag = G_define_flag(); edit_flag->key = 'e'; edit_flag->description = _("Open form in edit mode"); if (!vect) opt1->required = YES; if ((argc > 1 || !vect) && G_parser(argc, argv)) exit(EXIT_FAILURE); if (opt1->answers && opt1->answers[0]) vect = opt1->answers; /* Look at maps given on command line */ if (vect) { for (i = 0; vect[i]; i++) ; nvects = i; for (i = 0; i < nvects; i++) { mapset = openvect(vect[i]); if (mapset == NULL) G_fatal_error(_("Unable to open vector map <%s>"), vect[i]); } Map = (struct Map_info *)G_malloc(nvects * sizeof(struct Map_info)); width = mwidth = 0; for (i = 0; i < nvects; i++) { str = strchr(vect[i], '@'); if (str) j = str - vect[i]; else j = strlen(vect[i]); if (j > width) width = j; mapset = openvect(vect[i]); j = strlen(mapset); if (j > mwidth) mwidth = j; level = Vect_open_old(&Map[i], vect[i], mapset); if (level < 0) G_fatal_error(_("Vector map <%s> not found"), vect[i]); if (level < 2) G_fatal_error(_("%s: You must build topology on vector map"), vect[i]); G_message(_("Building spatial index...")); Vect_build_spatial_index(&Map[i]); } } if (R_open_driver() != 0) G_fatal_error(_("No graphics device selected")); D_setup(0); what(once->answer, txt->answer, terse->answer, flash->answer, width, mwidth, topo_flag->answer, edit_flag->answer); for (i = 0; i < nvects; i++) Vect_close(&Map[i]); R_close_driver(); R_pad_freelist(vect, nvects); G_message(_("Done.")); exit(EXIT_SUCCESS); }