static void do_reclass(const char *basemap, char **outputs) { const char *tempfile = G_tempfile(); char *input_arg = G_malloc(strlen(basemap) + 7); char *rules_arg = G_malloc(strlen(tempfile) + 7); int quant; G_message(_("Generating reclass maps")); sprintf(input_arg, "input=%s", basemap); sprintf(rules_arg, "rules=%s", tempfile); for (quant = 0; quant < num_quants; quant++) { const char *output = outputs[quant]; char *output_arg = G_malloc(strlen(output) + 8); FILE *fp; int cat; sprintf(output_arg, "output=%s", output); fp = fopen(tempfile, "w"); if (!fp) G_fatal_error(_("Unable to open temporary file")); for (cat = 0; cat < num_cats; cat++) fprintf(fp, "%d = %d %f\n", min + cat, min + cat, basecats[cat].quants[quant]); fclose(fp); G_spawn("r.reclass", "r.reclass", input_arg, output_arg, rules_arg, NULL); } remove(tempfile); }
/* * run_system() - Run system command * * RETURN: >0 success : 0 failure */ int run_system(char *pgm) { int stat; if ((stat = G_spawn(pgm, pgm, NULL))) G_sleep(3); return stat; }
int run(char *pgm, char *flags, char *name) { char path[GPATH_MAX]; int stat; sprintf(path, "%s/etc/mon.%s", G_gisbase(), pgm); if (*flags) { if ((stat = G_spawn(path, pgm, flags, name, NULL))) G_sleep(3); } else { if ((stat = G_spawn(path, pgm, name, NULL))) G_sleep(3); } return stat; }
/* * run_etc_support() - Run command in etc/support * * RETURN: >0 success : 0 failure */ int run_etc_support(char *pgm, char *rast) { char path[GPATH_MAX]; int stat; sprintf(path, "%s/etc/%s", G_gisbase(), pgm); if ((stat = G_spawn(path, pgm, rast, NULL))) G_sleep(3); return stat; }
static int use_r_out(void) { char *mpfilename; int ret; mpfilename = G_tempfile(); write_params(mpfilename, vfiles[0], outfile, frames, quality, 0, 0, 1); if (G_verbose() <= G_verbose_min()) ret = G_spawn(encoder, encoder, mpfilename, SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_OUT, G_DEV_NULL, SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, G_DEV_NULL, NULL); else ret = G_spawn(encoder, encoder, mpfilename, NULL); if (ret != 0) G_warning(_("mpeg_encode ERROR")); clean_files(mpfilename, NULL, 0); return (1); }
static int load_files(void) { void *voidc; int rtype; register int i, rowoff, row, col, vxoff, vyoff, offset; int cnt, fd, size, tsiz, coff; int vnum; int y_rows, y_cols; char *pr, *pg, *pb; unsigned char *tr, *tg, *tb, *tset; char *mpfilename, *name; char *yfiles[MAXIMAGES]; struct Colors colors; int ret; size = nrows * ncols; pr = G_malloc(size); pg = G_malloc(size); pb = G_malloc(size); tsiz = Rast_window_cols(); tr = (unsigned char *)G_malloc(tsiz); tg = (unsigned char *)G_malloc(tsiz); tb = (unsigned char *)G_malloc(tsiz); tset = (unsigned char *)G_malloc(tsiz); for (cnt = 0; cnt < frames; cnt++) { if (cnt > MAXIMAGES) { cnt--; break; } for (i = 0; i < size; i++) pr[i] = pg[i] = pb[i] = 0; for (vnum = 0; vnum < numviews; vnum++) { if (icols == vcols) { vxoff = BORDER_W; vyoff = (irows == vrows) ? BORDER_W : BORDER_W + vnum * (BORDER_W + vrows); } else if (irows == vrows) { vxoff = (icols == vcols) ? BORDER_W : BORDER_W + vnum * (BORDER_W + vcols); vyoff = BORDER_W; } else { /* 4 views */ /* assumes we want: view1 view2 view3 view4 */ vxoff = vnum % 2 ? BORDER_W : vcols + 2 * BORDER_W; vyoff = vnum > 1 ? vrows + 2 * BORDER_W : BORDER_W; } name = vfiles[vnum][cnt]; G_message(_("Reading raster map <%s>..."), name); fd = Rast_open_old(name, ""); if (Rast_read_colors(name, "", &colors) < 0) G_fatal_error(_("Unable to read color table for <%s>"), name); rtype = Rast_get_map_type(fd); voidc = Rast_allocate_buf(rtype); for (row = 0; row < vrows; row++) { Rast_get_row(fd, voidc, (int)(row / vscale), rtype); rowoff = (vyoff + row) * ncols; Rast_lookup_colors(voidc, tr, tg, tb, tset, tsiz, &colors, rtype); for (col = 0; col < vcols; col++) { coff = (int)(col / vscale); offset = rowoff + col + vxoff; if (!tset[coff]) pr[offset] = pg[offset] = pb[offset] = (char)255; else { pr[offset] = (char)tr[coff]; pg[offset] = (char)tg[coff]; pb[offset] = (char)tb[coff]; } } } Rast_close(fd); } yfiles[cnt] = G_tempfile(); #ifdef USE_PPM write_ppm(pr, pg, pb, nrows, ncols, &y_rows, &y_cols, yfiles[cnt]); #else write_ycc(pr, pg, pb, nrows, ncols, &y_rows, &y_cols, yfiles[cnt]); #endif } mpfilename = G_tempfile(); write_params(mpfilename, yfiles, outfile, cnt, quality, y_rows, y_cols, 0); if (G_verbose() <= G_verbose_min()) ret = G_spawn(encoder, encoder, mpfilename, SF_REDIRECT_FILE, SF_STDOUT, SF_MODE_OUT, G_DEV_NULL, SF_REDIRECT_FILE, SF_STDERR, SF_MODE_OUT, G_DEV_NULL, NULL); else ret = G_spawn(encoder, encoder, mpfilename, NULL); if (ret != 0) G_warning(_("mpeg_encode ERROR")); clean_files(mpfilename, yfiles, cnt); G_free(voidc); G_free(tset); G_free(tr); G_free(tg); G_free(tb); G_free(pr); G_free(pg); G_free(pb); return (cnt); }
int main(int argc, char **argv) { static DCELL *count, *sum, *mean, *sumu, *sum2, *sum3, *sum4, *min, *max; DCELL *result; struct GModule *module; struct { struct Option *method, *basemap, *covermap, *output; } opt; struct { struct Flag *c, *r; } flag; char methods[2048]; const char *basemap, *covermap, *output; int usecats; int reclass; int base_fd, cover_fd; struct Categories cats; CELL *base_buf; DCELL *cover_buf; struct Range range; CELL mincat, ncats; int method; int rows, cols; int row, col, i; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("statistics")); module->description = _("Calculates category or object oriented statistics (accumulator-based statistics)."); opt.basemap = G_define_standard_option(G_OPT_R_BASE); opt.covermap = G_define_standard_option(G_OPT_R_COVER); opt.method = G_define_option(); opt.method->key = "method"; opt.method->type = TYPE_STRING; opt.method->required = YES; opt.method->description = _("Method of object-based statistic"); for (i = 0; menu[i].name; i++) { if (i) strcat(methods, ","); else *(methods) = 0; strcat(methods, menu[i].name); } opt.method->options = G_store(methods); for (i = 0; menu[i].name; i++) { if (i) strcat(methods, ";"); else *(methods) = 0; strcat(methods, menu[i].name); strcat(methods, ";"); strcat(methods, menu[i].text); } opt.method->descriptions = G_store(methods); opt.output = G_define_standard_option(G_OPT_R_OUTPUT); opt.output->description = _("Resultant raster map"); opt.output->required = YES; flag.c = G_define_flag(); flag.c->key = 'c'; flag.c->description = _("Cover values extracted from the category labels of the cover map"); flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Create reclass map with statistics as category labels"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); basemap = opt.basemap->answer; covermap = opt.covermap->answer; output = opt.output->answer; usecats = flag.c->answer; reclass = flag.r->answer; for (i = 0; menu[i].name; i++) if (strcmp(menu[i].name, opt.method->answer) == 0) break; if (!menu[i].name) { G_warning(_("<%s=%s> unknown %s"), opt.method->key, opt.method->answer, opt.method->key); G_usage(); exit(EXIT_FAILURE); } method = menu[i].val; base_fd = Rast_open_old(basemap, ""); cover_fd = Rast_open_old(covermap, ""); if (usecats && Rast_read_cats(covermap, "", &cats) < 0) G_fatal_error(_("Unable to read category file of cover map <%s>"), covermap); if (Rast_map_is_fp(basemap, "") != 0) G_fatal_error(_("The base map must be an integer (CELL) map")); if (Rast_read_range(basemap, "", &range) < 0) G_fatal_error(_("Unable to read range of base map <%s>"), basemap); mincat = range.min; ncats = range.max - range.min + 1; rows = Rast_window_rows(); cols = Rast_window_cols(); switch (method) { case COUNT: count = G_calloc(ncats, sizeof(DCELL)); break; case SUM: sum = G_calloc(ncats, sizeof(DCELL)); break; case MIN: min = G_malloc(ncats * sizeof(DCELL)); break; case MAX: max = G_malloc(ncats * sizeof(DCELL)); break; case RANGE: min = G_malloc(ncats * sizeof(DCELL)); max = G_malloc(ncats * sizeof(DCELL)); break; case AVERAGE: case ADEV: case VARIANCE2: case STDDEV2: case SKEWNESS2: case KURTOSIS2: count = G_calloc(ncats, sizeof(DCELL)); sum = G_calloc(ncats, sizeof(DCELL)); break; case VARIANCE1: case STDDEV1: count = G_calloc(ncats, sizeof(DCELL)); sum = G_calloc(ncats, sizeof(DCELL)); sum2 = G_calloc(ncats, sizeof(DCELL)); break; case SKEWNESS1: count = G_calloc(ncats, sizeof(DCELL)); sum = G_calloc(ncats, sizeof(DCELL)); sum2 = G_calloc(ncats, sizeof(DCELL)); sum3 = G_calloc(ncats, sizeof(DCELL)); break; case KURTOSIS1: count = G_calloc(ncats, sizeof(DCELL)); sum = G_calloc(ncats, sizeof(DCELL)); sum2 = G_calloc(ncats, sizeof(DCELL)); sum4 = G_calloc(ncats, sizeof(DCELL)); break; } if (min) for (i = 0; i < ncats; i++) min[i] = 1e300; if (max) for (i = 0; i < ncats; i++) max[i] = -1e300; base_buf = Rast_allocate_c_buf(); cover_buf = Rast_allocate_d_buf(); G_message(_("First pass")); for (row = 0; row < rows; row++) { Rast_get_c_row(base_fd, base_buf, row); Rast_get_d_row(cover_fd, cover_buf, row); for (col = 0; col < cols; col++) { int n; DCELL v; if (Rast_is_c_null_value(&base_buf[col])) continue; if (Rast_is_d_null_value(&cover_buf[col])) continue; n = base_buf[col] - mincat; if (n < 0 || n >= ncats) continue; v = cover_buf[col]; if (usecats) sscanf(Rast_get_c_cat((CELL *) &v, &cats), "%lf", &v); if (count) count[n]++; if (sum) sum[n] += v; if (sum2) sum2[n] += v * v; if (sum3) sum3[n] += v * v * v; if (sum4) sum4[n] += v * v * v * v; if (min && min[n] > v) min[n] = v; if (max && max[n] < v) max[n] = v; } G_percent(row, rows, 2); } G_percent(row, rows, 2); result = G_calloc(ncats, sizeof(DCELL)); switch (method) { case ADEV: case VARIANCE2: case STDDEV2: case SKEWNESS2: case KURTOSIS2: mean = G_calloc(ncats, sizeof(DCELL)); for (i = 0; i < ncats; i++) mean[i] = sum[i] / count[i]; G_free(sum); break; } switch (method) { case ADEV: sumu = G_calloc(ncats, sizeof(DCELL)); break; case VARIANCE2: case STDDEV2: sum2 = G_calloc(ncats, sizeof(DCELL)); break; case SKEWNESS2: sum2 = G_calloc(ncats, sizeof(DCELL)); sum3 = G_calloc(ncats, sizeof(DCELL)); break; case KURTOSIS2: sum2 = G_calloc(ncats, sizeof(DCELL)); sum4 = G_calloc(ncats, sizeof(DCELL)); break; } if (mean) { G_message(_("Second pass")); for (row = 0; row < rows; row++) { Rast_get_c_row(base_fd, base_buf, row); Rast_get_d_row(cover_fd, cover_buf, row); for (col = 0; col < cols; col++) { int n; DCELL v, d; if (Rast_is_c_null_value(&base_buf[col])) continue; if (Rast_is_d_null_value(&cover_buf[col])) continue; n = base_buf[col] - mincat; if (n < 0 || n >= ncats) continue; v = cover_buf[col]; if (usecats) sscanf(Rast_get_c_cat((CELL *) &v, &cats), "%lf", &v); d = v - mean[n]; if (sumu) sumu[n] += fabs(d); if (sum2) sum2[n] += d * d; if (sum3) sum3[n] += d * d * d; if (sum4) sum4[n] += d * d * d * d; } G_percent(row, rows, 2); } G_percent(row, rows, 2); G_free(mean); G_free(cover_buf); } switch (method) { case COUNT: for (i = 0; i < ncats; i++) result[i] = count[i]; break; case SUM: for (i = 0; i < ncats; i++) result[i] = sum[i]; break; case AVERAGE: for (i = 0; i < ncats; i++) result[i] = sum[i] / count[i]; break; case MIN: for (i = 0; i < ncats; i++) result[i] = min[i]; break; case MAX: for (i = 0; i < ncats; i++) result[i] = max[i]; break; case RANGE: for (i = 0; i < ncats; i++) result[i] = max[i] - min[i]; break; case VARIANCE1: for (i = 0; i < ncats; i++) { double n = count[i]; double var = (sum2[i] - sum[i] * sum[i] / n) / (n - 1); result[i] = var; } break; case STDDEV1: for (i = 0; i < ncats; i++) { double n = count[i]; double var = (sum2[i] - sum[i] * sum[i] / n) / (n - 1); result[i] = sqrt(var); } break; case SKEWNESS1: for (i = 0; i < ncats; i++) { double n = count[i]; double var = (sum2[i] - sum[i] * sum[i] / n) / (n - 1); double skew = (sum3[i] / n - 3 * sum[i] * sum2[i] / (n * n) + 2 * sum[i] * sum[i] * sum[i] / (n * n * n)) / (pow(var, 1.5)); result[i] = skew; } break; case KURTOSIS1: for (i = 0; i < ncats; i++) { double n = count[i]; double var = (sum2[i] - sum[i] * sum[i] / n) / (n - 1); double kurt = (sum4[i] / n - 4 * sum[i] * sum3[i] / (n * n) + 6 * sum[i] * sum[i] * sum2[i] / (n * n * n) - 3 * sum[i] * sum[i] * sum[i] * sum[i] / (n * n * n * n)) / (var * var) - 3; result[i] = kurt; } break; case ADEV: for (i = 0; i < ncats; i++) result[i] = sumu[i] / count[i]; break; case VARIANCE2: for (i = 0; i < ncats; i++) result[i] = sum2[i] / (count[i] - 1); break; case STDDEV2: for (i = 0; i < ncats; i++) result[i] = sqrt(sum2[i] / (count[i] - 1)); break; case SKEWNESS2: for (i = 0; i < ncats; i++) { double n = count[i]; double var = sum2[i] / (n - 1); double sdev = sqrt(var); result[i] = sum3[i] / (sdev * sdev * sdev) / n; } G_free(count); G_free(sum2); G_free(sum3); break; case KURTOSIS2: for (i = 0; i < ncats; i++) { double n = count[i]; double var = sum2[i] / (n - 1); result[i] = sum4[i] / (var * var) / n - 3; } G_free(count); G_free(sum2); G_free(sum4); break; } if (reclass) { const char *tempfile = G_tempfile(); char *input_arg = G_malloc(strlen(basemap) + 7); char *output_arg = G_malloc(strlen(output) + 8); char *rules_arg = G_malloc(strlen(tempfile) + 7); FILE *fp; G_message(_("Generating reclass map")); sprintf(input_arg, "input=%s", basemap); sprintf(output_arg, "output=%s", output); sprintf(rules_arg, "rules=%s", tempfile); fp = fopen(tempfile, "w"); if (!fp) G_fatal_error(_("Unable to open temporary file")); for (i = 0; i < ncats; i++) fprintf(fp, "%d = %d %f\n", mincat + i, mincat + i, result[i]); fclose(fp); G_spawn("r.reclass", "r.reclass", input_arg, output_arg, rules_arg, NULL); } else { int out_fd; DCELL *out_buf; struct Colors colors; G_message(_("Writing output map")); out_fd = Rast_open_fp_new(output); out_buf = Rast_allocate_d_buf(); for (row = 0; row < rows; row++) { Rast_get_c_row(base_fd, base_buf, row); for (col = 0; col < cols; col++) if (Rast_is_c_null_value(&base_buf[col])) Rast_set_d_null_value(&out_buf[col], 1); else out_buf[col] = result[base_buf[col] - mincat]; Rast_put_d_row(out_fd, out_buf); G_percent(row, rows, 2); } G_percent(row, rows, 2); Rast_close(out_fd); if (Rast_read_colors(covermap, "", &colors) > 0) Rast_write_colors(output, G_mapset(), &colors); } return 0; }
int main(int argc, char **argv) { struct Cell_head window; struct Categories cats; struct GModule *module; struct Option *opt1, *opt2, *opt3; struct Flag *fancy_mode, *simple_mode, *draw; char *tmpfile; FILE *fp; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("cartography")); module->description = _("Create a TITLE for a raster map in a form suitable " "for display with d.text."); opt1 = G_define_standard_option(G_OPT_R_MAP); opt2 = G_define_option(); opt2->key = "color"; opt2->type = TYPE_STRING; opt2->answer = DEFAULT_FG_COLOR; opt2->required = NO; opt2->gisprompt = "old_color,color,color"; opt2->description = _("Sets the text color"); opt3 = G_define_option(); opt3->key = "size"; opt3->type = TYPE_DOUBLE; opt3->answer = "4.0"; opt3->options = "0-100"; opt3->description = _("Sets the text size as percentage of the frame's height"); draw = G_define_flag(); draw->key = 'd'; draw->description = _("Draw title on current display"); fancy_mode = G_define_flag(); fancy_mode->key = 'f'; fancy_mode->description = _("Do a fancier title"); /* currently just title, but it doesn't have to be /that/ simple */ simple_mode = G_define_flag(); simple_mode->key = 's'; simple_mode->description = _("Do a simple title"); /* Check command line */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); map_name = opt1->answer; color = opt2->answer; if (opt3->answer != NULL) sscanf(opt3->answer, "%f", &size); type = fancy_mode->answer ? FANCY : NORMAL; if (fancy_mode->answer && simple_mode->answer) G_fatal_error(_("Title can be fancy or simple, not both")); if (!strlen(map_name)) G_fatal_error(_("No map name given")); Rast_get_cellhd(map_name, "", &window); if (Rast_read_cats(map_name, "", &cats) == -1) G_fatal_error(_("Unable to read category file of raster map <%s>"), map_name); if (draw->answer) { tmpfile = G_convert_dirseps_to_host(G_tempfile()); if (!(fp = fopen(tmpfile, "w"))) G_fatal_error(_("Unable to open temporary file <%s>"), tmpfile); } else fp = stdout; if (type == NORMAL) normal(&window, &cats, simple_mode->answer, fp); else fancy(&window, &cats, fp); if (draw->answer) { char inarg[GPATH_MAX]; fclose(fp); sprintf(inarg, "input=%s", tmpfile); G_spawn("d.text", "d.text", inarg, NULL); unlink(tmpfile); /* note a tmp file will remain, created by d.text so it can survive d.redraw */ } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int n, i; int skip; const char *cur_mapset, *mapset; char **ptr; char **tokens; int no_tokens; FILE *fp; char path_buf[GPATH_MAX]; char *path, *fs; int operation, nchoices; char **mapset_name; int nmapsets; struct GModule *module; struct _opt { struct Option *mapset, *op, *fs; struct Flag *print, *list, *dialog; } opt; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("settings")); G_add_keyword(_("search path")); module->label = _("Modifies/prints the user's current mapset search path."); module->description = _("Affects the user's access to data existing " "under the other mapsets in the current location."); opt.mapset = G_define_standard_option(G_OPT_M_MAPSET); opt.mapset->required = YES; opt.mapset->multiple = YES; opt.mapset->description = _("Name(s) of existing mapset(s) to add/remove or set"); opt.mapset->guisection = _("Search path"); opt.op = G_define_option(); opt.op->key = "operation"; opt.op->type = TYPE_STRING; opt.op->required = YES; opt.op->multiple = NO; opt.op->options = "set,add,remove"; opt.op->description = _("Operation to be performed"); opt.op->guisection = _("Search path"); opt.op->answer = "add"; opt.fs = G_define_standard_option(G_OPT_F_SEP); opt.fs->label = _("Field separator for printing (-l and -p flags)"); opt.fs->answer = "space"; opt.fs->guisection = _("Print"); opt.list = G_define_flag(); opt.list->key = 'l'; opt.list->description = _("List all available mapsets in alphabetical order"); opt.list->guisection = _("Print"); opt.list->suppress_required = YES; opt.print = G_define_flag(); opt.print->key = 'p'; opt.print->description = _("Print mapsets in current search path"); opt.print->guisection = _("Print"); opt.print->suppress_required = YES; opt.dialog = G_define_flag(); opt.dialog->key = 's'; opt.dialog->description = _("Launch mapset selection GUI dialog"); opt.dialog->suppress_required = YES; path = NULL; mapset_name = NULL; nmapsets = nchoices = 0; if (G_parser(argc, argv)) exit(EXIT_FAILURE); operation = OP_UKN; if (opt.mapset->answer && opt.op->answer) { switch(opt.op->answer[0]) { case 's': operation = OP_SET; break; case 'a': operation = OP_ADD; break; case 'r': operation = OP_REM; break; default: G_fatal_error(_("Unknown operation '%s'"), opt.op->answer); break; } } fs = G_option_to_separator(opt.fs); /* list available mapsets */ if (opt.list->answer) { if (opt.print->answer) G_warning(_("Flag -%c ignored"), opt.print->key); if (opt.dialog->answer) G_warning(_("Flag -%c ignored"), opt.dialog->key); if (opt.mapset->answer) G_warning(_("Option <%s> ignored"), opt.mapset->key); mapset_name = get_available_mapsets(&nmapsets); list_available_mapsets((const char **)mapset_name, nmapsets, fs); exit(EXIT_SUCCESS); } if (opt.print->answer) { if (opt.dialog->answer) G_warning(_("Flag -%c ignored"), opt.dialog->key); if (opt.mapset->answer) G_warning(_("Option <%s> ignored"), opt.mapset->key); list_accessible_mapsets(fs); exit(EXIT_SUCCESS); } /* show GUI dialog */ if (opt.dialog->answer) { if (opt.mapset->answer) G_warning(_("Option <%s> ignored"), opt.mapset->key); sprintf(path_buf, "%s/gui/wxpython/modules/mapsets_picker.py", G_gisbase()); G_spawn(getenv("GRASS_PYTHON"), "mapsets_picker.py", path_buf, NULL); exit(EXIT_SUCCESS); } cur_mapset = G_mapset(); /* modify search path */ if (operation == OP_SET) { int cur_found; cur_found = FALSE; for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) { mapset = substitute_mapset(*ptr); if (G__mapset_permissions(mapset) < 0) G_fatal_error(_("Mapset <%s> not found"), mapset); if (strcmp(mapset, cur_mapset) == 0) cur_found = TRUE; nchoices++; append_mapset(&path, mapset); } if (!cur_found) G_warning(_("Current mapset (<%s>) must always included in the search path"), cur_mapset); } else if (operation == OP_ADD) { /* add to existing search path */ const char *oldname; if (path) { G_free(path); path = NULL; } /* read existing mapsets from SEARCH_PATH */ for (n = 0; (oldname = G_get_mapset_name(n)); n++) append_mapset(&path, oldname); /* fetch and add new mapsets from param list */ for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) { mapset = substitute_mapset(*ptr); if (G_is_mapset_in_search_path(mapset)) { G_message(_("Mapset <%s> already in the path"), mapset); continue; } if (G__mapset_permissions(mapset) < 0) G_fatal_error(_("Mapset <%s> not found"), mapset); else G_verbose_message(_("Mapset <%s> added to search path"), mapset); nchoices++; append_mapset(&path, mapset); } } else if (operation == OP_REM) { /* remove from existing search path */ const char *oldname; int found; if (path) { G_free(path); path = NULL; } /* read existing mapsets from SEARCH_PATH */ for (n = 0; (oldname = G_get_mapset_name(n)); n++) { found = FALSE; for (ptr = opt.mapset->answers; *ptr && !found; ptr++) mapset = substitute_mapset(*ptr); if (strcmp(oldname, mapset) == 0) found = TRUE; if (found) { if (strcmp(oldname, cur_mapset) == 0) G_warning(_("Current mapset (<%s>) must always included in the search path"), cur_mapset); else G_verbose_message(_("Mapset <%s> removed from search path"), oldname); continue; } nchoices++; append_mapset(&path, oldname); } } /* stuffem sets nchoices */ if (nchoices == 0) { G_important_message(_("Search path not modified")); if (path) G_free(path); if (nmapsets) { for(nmapsets--; nmapsets >= 0; nmapsets--) G_free(mapset_name[nmapsets]); G_free(mapset_name); } exit(EXIT_SUCCESS); } /* note I'm assuming that mapsets cannot have ' 's in them */ tokens = G_tokenize(path, " "); fp = G_fopen_new("", "SEARCH_PATH"); if (!fp) G_fatal_error(_("Unable to open SEARCH_PATH for write")); /* * make sure current mapset is specified in the list if not add it * to the head of the list */ skip = 0; for (n = 0; n < nchoices; n++) if (strcmp(cur_mapset, tokens[n]) == 0) { skip = 1; break; } if (!skip) { fprintf(fp, "%s\n", cur_mapset); } /* * output the list, removing duplicates */ no_tokens = G_number_of_tokens(tokens); for (n = 0; n < no_tokens; n++) { skip = 0; for (i = n; i < no_tokens; i++) { if (i != n) { if (strcmp(tokens[i], tokens[n]) == 0) skip = 1; } } if (!skip) fprintf(fp, "%s\n", tokens[n]); } fclose(fp); G_free_tokens(tokens); if (path) G_free(path); if (nmapsets) { for(nmapsets--; nmapsets >= 0; nmapsets--) G_free(mapset_name[nmapsets]); G_free(mapset_name); } exit(EXIT_SUCCESS); }
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); }