static void write_support_files(int xtile, int ytile, int overlap) { char name[GNAME_MAX]; struct Cell_head cellhd; char title[64]; struct History history; struct Colors colors; struct Categories cats; sprintf(name, "%s-%03d-%03d", parm.rastout->answer, ytile, xtile); Rast_get_cellhd(name, G_mapset(), &cellhd); cellhd.north = src_w.north - ytile * dst_w.rows * src_w.ns_res; cellhd.south = cellhd.north - (dst_w.rows + 2 * overlap) * src_w.ns_res; cellhd.west = src_w.west + xtile * dst_w.cols * src_w.ew_res; cellhd.east = cellhd.west + (dst_w.cols + 2 * overlap) * src_w.ew_res; Rast_put_cellhd(name, &cellhd); /* copy cats from source map */ if (Rast_read_cats(parm.rastin->answer, "", &cats) < 0) G_fatal_error(_("Unable to read cats for %s"), parm.rastin->answer); Rast_write_cats(name, &cats); /* record map metadata/history info */ G_debug(1, "Tile %d,%d of %s: writing %s", xtile, ytile, parm.rastin->answer, name); sprintf(title, "Tile %d,%d of %s", xtile, ytile, parm.rastin->answer); Rast_put_cell_title(name, title); Rast_short_history(name, "raster", &history); Rast_set_history(&history, HIST_DATSRC_1, parm.rastin->answer); Rast_command_history(&history); Rast_write_history(name, &history); /* copy color table from source map */ if (Rast_read_colors(parm.rastin->answer, "", &colors) < 0) G_fatal_error(_("Unable to read color table for %s"), parm.rastin->answer); if (map_type != CELL_TYPE) Rast_mark_colors_as_fp(&colors); Rast_write_colors(name, G_mapset(), &colors); }
int write_form_cat_colors (char* raster, CATCOLORS* ccolors) { struct Colors colors; struct Categories cats; int i; Rast_init_colors(&colors); for(i=1;i<CNT;++i) Rast_add_color_rule( &ccolors[i].cat, ccolors[i].r, ccolors[i].g, ccolors[i].b, &ccolors[i].cat, ccolors[i].r, ccolors[i].g, ccolors[i].b, &colors, CELL_TYPE); Rast_write_colors(raster, G_mapset(), &colors); Rast_free_colors(&colors); Rast_init_cats("Forms", &cats); for(i=1;i<CNT;++i) Rast_set_cat(&ccolors[i].cat, &ccolors[i].cat, ccolors[i].label, &cats, CELL_TYPE); Rast_write_cats(raster, &cats); Rast_free_cats(&cats); return 0; }
int main(int argc, char *argv[]) { const char *name; const char *mapset; long x, y; double dx; RASTER_MAP_TYPE map_type; int i; int from_stdin = FALSE; struct GModule *module; struct { struct Option *map, *fs, *cats, *vals, *raster, *file, *fmt_str, *fmt_coeff; } parm; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("category")); module->description = _("Manages category values and labels associated " "with user-specified raster map layers."); parm.map = G_define_standard_option(G_OPT_R_MAP); parm.cats = G_define_standard_option(G_OPT_V_CATS); parm.cats->multiple = YES; parm.cats->guisection = _("Selection"); parm.vals = G_define_option(); parm.vals->key = "vals"; parm.vals->type = TYPE_DOUBLE; parm.vals->multiple = YES; parm.vals->required = NO; parm.vals->label = _("Comma separated value list"); parm.vals->description = _("Example: 1.4,3.8,13"); parm.vals->guisection = _("Selection"); parm.fs = G_define_standard_option(G_OPT_F_SEP); parm.fs->answer = "tab"; parm.raster = G_define_standard_option(G_OPT_R_INPUT); parm.raster->key = "raster"; parm.raster->required = NO; parm.raster->description = _("Raster map from which to copy category table"); parm.raster->guisection = _("Define"); parm.file = G_define_standard_option(G_OPT_F_INPUT); parm.file->key = "rules"; parm.file->required = NO; parm.file->description = _("File containing category label rules (or \"-\" to read from stdin)"); parm.file->guisection = _("Define"); parm.fmt_str = G_define_option(); parm.fmt_str->key = "format"; parm.fmt_str->type = TYPE_STRING; parm.fmt_str->required = NO; parm.fmt_str->label = _("Default label or format string for dynamic labeling"); parm.fmt_str->description = _("Used when no explicit label exists for the category"); parm.fmt_coeff = G_define_option(); parm.fmt_coeff->key = "coefficients"; parm.fmt_coeff->type = TYPE_DOUBLE; parm.fmt_coeff->required = NO; parm.fmt_coeff->key_desc = "mult1,offset1,mult2,offset2"; /* parm.fmt_coeff->answer = "0.0,0.0,0.0,0.0"; */ parm.fmt_coeff->label = _("Dynamic label coefficients"); parm.fmt_coeff->description = _("Two pairs of category multiplier and offsets, for $1 and $2"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); name = parm.map->answer; fs = G_option_to_separator(parm.fs); mapset = G_find_raster2(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); map_type = Rast_map_type(name, mapset); /* create category labels */ if (parm.raster->answer || parm.file->answer || parm.fmt_str->answer || parm.fmt_coeff->answer) { /* restrict editing to current mapset */ if (strcmp(mapset, G_mapset()) != 0) G_fatal_error(_("Raster map <%s> not found in current mapset"), name); /* use cats from another map */ if (parm.raster->answer) { int fd; const char *cmapset; cmapset = G_find_raster2(parm.raster->answer, ""); if (cmapset == NULL) G_fatal_error(_("Raster map <%s> not found"), parm.raster->answer); fd = Rast_open_old(name, mapset); Rast_init_cats("", &cats); if (0 > Rast_read_cats(parm.raster->answer, cmapset, &cats)) G_fatal_error(_("Unable to read category file of raster map <%s@%s>"), parm.raster->answer, cmapset); Rast_write_cats(name, &cats); G_message(_("Category table for <%s> set from <%s>"), name, parm.raster->answer); Rast_close(fd); } /* load cats from rules file */ if (parm.file->answer) { FILE *fp; char **tokens; int ntokens; char *e1; char *e2; if (strcmp("-", parm.file->answer) == 0) { from_stdin = TRUE; fp = stdin; } else { fp = fopen(parm.file->answer, "r"); if (!fp) G_fatal_error(_("Unable to open file <%s>"), parm.file->answer); } Rast_init_cats("", &cats); for (;;) { char buf[1024]; DCELL d1, d2; int parse_error = 0; if (!G_getl2(buf, sizeof(buf), fp)) break; tokens = G_tokenize(buf, fs); ntokens = G_number_of_tokens(tokens); if (ntokens == 3) { d1 = strtod(tokens[0], &e1); d2 = strtod(tokens[1], &e2); if (*e1 == 0 && *e2 == 0) Rast_set_d_cat(&d1, &d2, tokens[2], &cats); else parse_error = 1; } else if (ntokens == 2) { d1 = strtod(tokens[0], &e1); if (*e1 == 0) Rast_set_d_cat(&d1, &d1, tokens[1], &cats); else parse_error = 1; } else if (!strlen(buf)) continue; else parse_error = 1; if (parse_error) G_fatal_error(_("Incorrect format of input rules. " "Check separators. Invalid line is:\n%s"), buf); } G_free_tokens(tokens); Rast_write_cats(name, &cats); if (!from_stdin) fclose(fp); } /* set dynamic cat rules for cats without explicit labels */ if (parm.fmt_str->answer || parm.fmt_coeff->answer) { char *fmt_str; double m1, a1, m2, a2; /* read existing values */ Rast_init_cats("", &cats); if (0 > Rast_read_cats(name, G_mapset(), &cats)) G_warning(_("Unable to read category file of raster map <%s@%s>"), name, G_mapset()); if (parm.fmt_str->answer) { fmt_str = G_malloc(strlen(parm.fmt_str->answer) > strlen(cats.fmt) ? strlen(parm.fmt_str->answer) + 1 : strlen(cats.fmt) + 1); strcpy(fmt_str, parm.fmt_str->answer); } else { fmt_str = G_malloc(strlen(cats.fmt) + 1); strcpy(fmt_str, cats.fmt); } m1 = cats.m1; a1 = cats.a1; m2 = cats.m2; a2 = cats.a2; if (parm.fmt_coeff->answer) { m1 = atof(parm.fmt_coeff->answers[0]); a1 = atof(parm.fmt_coeff->answers[1]); m2 = atof(parm.fmt_coeff->answers[2]); a2 = atof(parm.fmt_coeff->answers[3]); } Rast_set_cats_fmt(fmt_str, m1, a1, m2, a2, &cats); Rast_write_cats(name, &cats); } Rast_free_cats(&cats); exit(EXIT_SUCCESS); } else { if (Rast_read_cats(name, mapset, &cats) < 0) G_fatal_error(_("Unable to read category file of raster map <%s> in <%s>"), name, mapset); } /* describe the category labels */ /* if no cats requested, use r.describe to get the cats */ if (parm.cats->answer == NULL) { if (map_type == CELL_TYPE) { get_cats(name, mapset); while (next_cat(&x)) print_label(x); exit(EXIT_SUCCESS); } } else { if (map_type != CELL_TYPE) G_warning(_("The map is floating point! Ignoring cats list, using vals list")); else { /* integer map */ for (i = 0; parm.cats->answers[i]; i++) if (!scan_cats(parm.cats->answers[i], &x, &y)) { G_usage(); exit(EXIT_FAILURE); } for (i = 0; parm.cats->answers[i]; i++) { scan_cats(parm.cats->answers[i], &x, &y); while (x <= y) print_label(x++); } exit(EXIT_SUCCESS); } } if (parm.vals->answer == NULL) G_fatal_error(_("vals argument is required for floating point map!")); for (i = 0; parm.vals->answers[i]; i++) if (!scan_vals(parm.vals->answers[i], &dx)) { G_usage(); exit(EXIT_FAILURE); } for (i = 0; parm.vals->answers[i]; i++) { scan_vals(parm.vals->answers[i], &dx); print_d_label(dx); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char *p; int method; int in_fd; int selection_fd; int out_fd; DCELL *result; char *selection; RASTER_MAP_TYPE map_type; int row, col; int readrow; int nrows, ncols; int n; int copycolr; int half; stat_func *newvalue; stat_func_w *newvalue_w; ifunc cat_names; double quantile; const void *closure; struct Colors colr; struct Cell_head cellhd; struct Cell_head window; struct History history; struct GModule *module; struct { struct Option *input, *output, *selection; struct Option *method, *size; struct Option *title; struct Option *weight; struct Option *gauss; struct Option *quantile; } parm; struct { struct Flag *align, *circle; } flag; DCELL *values; /* list of neighborhood values */ DCELL(*values_w)[2]; /* list of neighborhood values and weights */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("algebra")); G_add_keyword(_("statistics")); module->description = _("Makes each cell category value a " "function of the category values assigned to the cells " "around it, and stores new cell values in an output raster " "map layer."); parm.input = G_define_standard_option(G_OPT_R_INPUT); parm.selection = G_define_standard_option(G_OPT_R_INPUT); parm.selection->key = "selection"; parm.selection->required = NO; parm.selection->description = _("Name of an input raster map to select the cells which should be processed"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.method = G_define_option(); parm.method->key = "method"; parm.method->type = TYPE_STRING; parm.method->required = NO; parm.method->answer = "average"; p = G_malloc(1024); for (n = 0; menu[n].name; n++) { if (n) strcat(p, ","); else *p = 0; strcat(p, menu[n].name); } parm.method->options = p; parm.method->description = _("Neighborhood operation"); parm.method->guisection = _("Neighborhood"); parm.size = G_define_option(); parm.size->key = "size"; parm.size->type = TYPE_INTEGER; parm.size->required = NO; parm.size->description = _("Neighborhood size"); parm.size->answer = "3"; parm.size->guisection = _("Neighborhood"); parm.title = G_define_option(); parm.title->key = "title"; parm.title->key_desc = "phrase"; parm.title->type = TYPE_STRING; parm.title->required = NO; parm.title->description = _("Title of the output raster map"); parm.weight = G_define_standard_option(G_OPT_F_INPUT); parm.weight->key = "weight"; parm.weight->required = NO; parm.weight->description = _("Text file containing weights"); parm.gauss = G_define_option(); parm.gauss->key = "gauss"; parm.gauss->type = TYPE_DOUBLE; parm.gauss->required = NO; parm.gauss->description = _("Sigma (in cells) for Gaussian filter"); parm.quantile = G_define_option(); parm.quantile->key = "quantile"; parm.quantile->type = TYPE_DOUBLE; parm.quantile->required = NO; parm.quantile->description = _("Quantile to calculate for method=quantile"); parm.quantile->options = "0.0-1.0"; parm.quantile->answer = "0.5"; flag.align = G_define_flag(); flag.align->key = 'a'; flag.align->description = _("Do not align output with the input"); flag.circle = G_define_flag(); flag.circle->key = 'c'; flag.circle->description = _("Use circular neighborhood"); flag.circle->guisection = _("Neighborhood"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); sscanf(parm.size->answer, "%d", &ncb.nsize); if (ncb.nsize <= 0) G_fatal_error(_("Neighborhood size must be positive")); if (ncb.nsize % 2 == 0) G_fatal_error(_("Neighborhood size must be odd")); ncb.dist = ncb.nsize / 2; if (parm.weight->answer && flag.circle->answer) G_fatal_error(_("weight= and -c are mutually exclusive")); if (parm.weight->answer && parm.gauss->answer) G_fatal_error(_("weight= and gauss= are mutually exclusive")); ncb.oldcell = parm.input->answer; ncb.newcell = parm.output->answer; if (!flag.align->answer) { Rast_get_cellhd(ncb.oldcell, "", &cellhd); G_get_window(&window); Rast_align_window(&window, &cellhd); Rast_set_window(&window); } nrows = Rast_window_rows(); ncols = Rast_window_cols(); /* open raster maps */ in_fd = Rast_open_old(ncb.oldcell, ""); map_type = Rast_get_map_type(in_fd); /* get the method */ for (method = 0; (p = menu[method].name); method++) if ((strcmp(p, parm.method->answer) == 0)) break; if (!p) { G_warning(_("<%s=%s> unknown %s"), parm.method->key, parm.method->answer, parm.method->key); G_usage(); exit(EXIT_FAILURE); } if (menu[method].method == c_quant) { quantile = atoi(parm.quantile->answer); closure = &quantile; } half = (map_type == CELL_TYPE) ? menu[method].half : 0; /* establish the newvalue routine */ newvalue = menu[method].method; newvalue_w = menu[method].method_w; /* copy color table? */ copycolr = menu[method].copycolr; if (copycolr) { G_suppress_warnings(1); copycolr = (Rast_read_colors(ncb.oldcell, "", &colr) > 0); G_suppress_warnings(0); } /* read the weights */ if (parm.weight->answer) { read_weights(parm.weight->answer); if (!newvalue_w) weights_mask(); } else if (parm.gauss->answer) { if (!newvalue_w) G_fatal_error(_("Method %s not compatible with Gaussian filter"), parm.method->answer); gaussian_weights(atof(parm.gauss->answer)); } else newvalue_w = NULL; /* allocate the cell buffers */ allocate_bufs(); result = Rast_allocate_d_buf(); /* get title, initialize the category and stat info */ if (parm.title->answer) strcpy(ncb.title, parm.title->answer); else sprintf(ncb.title, "%dx%d neighborhood: %s of %s", ncb.nsize, ncb.nsize, menu[method].name, ncb.oldcell); /* initialize the cell bufs with 'dist' rows of the old cellfile */ readrow = 0; for (row = 0; row < ncb.dist; row++) readcell(in_fd, readrow++, nrows, ncols); /* open the selection raster map */ if (parm.selection->answer) { G_message(_("Opening selection map <%s>"), parm.selection->answer); selection_fd = Rast_open_old(parm.selection->answer, ""); selection = Rast_allocate_null_buf(); } else { selection_fd = -1; selection = NULL; } /*open the new raster map */ out_fd = Rast_open_new(ncb.newcell, map_type); if (flag.circle->answer) circle_mask(); if (newvalue_w) values_w = (DCELL(*)[2]) G_malloc(ncb.nsize * ncb.nsize * 2 * sizeof(DCELL)); else values = (DCELL *) G_malloc(ncb.nsize * ncb.nsize * sizeof(DCELL)); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); readcell(in_fd, readrow++, nrows, ncols); if (selection) Rast_get_null_value_row(selection_fd, selection, row); for (col = 0; col < ncols; col++) { DCELL *rp = &result[col]; if (selection && selection[col]) { *rp = ncb.buf[ncb.dist][col]; continue; } if (newvalue_w) n = gather_w(values_w, col); else n = gather(values, col); if (n < 0) Rast_set_d_null_value(rp, 1); else { if (newvalue_w) newvalue_w(rp, values_w, n, closure); else newvalue(rp, values, n, closure); if (half && !Rast_is_d_null_value(rp)) *rp += 0.5; } } Rast_put_d_row(out_fd, result); } G_percent(row, nrows, 2); Rast_close(out_fd); Rast_close(in_fd); if (selection) Rast_close(selection_fd); /* put out category info */ null_cats(); if ((cat_names = menu[method].cat_names)) cat_names(); Rast_write_cats(ncb.newcell, &ncb.cats); if (copycolr) Rast_write_colors(ncb.newcell, G_mapset(), &colr); Rast_short_history(ncb.newcell, "raster", &history); Rast_command_history(&history); Rast_write_history(ncb.newcell, &history); exit(EXIT_SUCCESS); }
int make_support(struct rr_state *theState, int percent, double percentage) { char title[100]; struct History hist; struct Categories cats; struct Colors clr; char *inraster; struct RASTER_MAP_PTR nulls; /* write categories for output raster use values from input or cover map */ if (theState->docover == 1) { inraster = theState->inrcover; nulls = theState->cnulls; } else { inraster = theState->inraster; nulls = theState->nulls; } if (Rast_read_cats(inraster, "", &cats) >= 0) { sprintf(title, "Random points on <%s>", inraster); Rast_set_cats_title(title, &cats); if (theState->use_nulls) Rast_set_cat(nulls.data.v, nulls.data.v, "Points with NULL values in original", &cats, nulls.type); Rast_write_cats(theState->outraster, &cats); } /* write history for output raster */ if (Rast_read_history(theState->outraster, G_mapset(), &hist) >= 0) { Rast_short_history(theState->outraster, "raster", &hist); Rast_format_history(&hist, HIST_DATSRC_1, "Based on map <%s>", inraster); if (percent) Rast_format_history( &hist, HIST_DATSRC_2, "Random points over %.2f percent of the base map <%s>", percentage, inraster); else Rast_format_history( &hist, HIST_DATSRC_2, "%ld random points on the base map <%s>", theState->nRand, theState->inraster); Rast_command_history(&hist); Rast_write_history(theState->outraster, &hist); } /* write commandline to output vector */ if (theState->outvector) { struct Map_info map; Vect_open_old(&map, theState->outvector, G_mapset()); Vect_hist_command(&map); Vect_close(&map); } /* set colors for output raster */ if (Rast_read_colors(inraster, "", &clr) >= 0) { if (theState->use_nulls) { Rast_add_color_rule(nulls.data.v, 127, 127, 127, nulls.data.v, 127, 127, 127, &clr, nulls.type); } Rast_write_colors(theState->outraster, G_mapset(), &clr); } return 0; }
/* * check_stats() - Check and update statistics * * RETURN: 0 on success / 1 on failure */ int check_stats(const char *name) { RASTER_MAP_TYPE data_type; struct Histogram histogram; struct Categories cats; struct Range range; struct FPRange fprange; int i, histo_num; int cats_ok; int max; data_type = Rast_map_type(name, ""); G_message(_("Updating statistics for [%s]..."), name); if (!do_histogram(name)) return 1; if (Rast_read_histogram(name, "", &histogram) <= 0) return 1; /* Init histogram range */ if (data_type == CELL_TYPE) Rast_init_range(&range); else Rast_init_fp_range(&fprange); G_message(_("Updating histogram range...")); i = histo_num = Rast_get_histogram_num(&histogram); while (i >= 0) { G_percent(i, histo_num, 2); if (data_type == CELL_TYPE) Rast_update_range(Rast_get_histogram_cat(i--, &histogram), &range); else Rast_update_fp_range((DCELL) Rast_get_histogram_cat(i--, &histogram), &fprange); } /* Write histogram range */ if (data_type == CELL_TYPE) Rast_write_range(name, &range); else Rast_write_fp_range(name, &fprange); /* Get category status and max */ cats_ok = (Rast_read_cats(name, "", &cats) >= 0); max = (data_type == CELL_TYPE ? range.max : fprange.max); /* Further category checks */ if (!cats_ok) Rast_init_cats("", &cats); else if (cats.num != max) { cats.num = max; cats_ok = 0; } /* Update categories if needed */ if (!cats_ok) { G_message(_("Updating the number of categories for [%s]..."), name); Rast_write_cats(name, &cats); } Rast_free_histogram(&histogram); Rast_free_cats(&cats); return 0; }
int main(int argc, char *argv[]) { int fd[NFILES]; int outfd; int i; const char *name; const char *output; const char *mapset; int non_zero; struct Range range; CELL ncats, max_cats; int primary; struct Categories pcats; struct Colors pcolr; char buf[1024]; CELL result; struct GModule *module; struct { struct Option *input, *output; } parm; struct { struct Flag *z; } flag; G_gisinit(argv[0]); /* Define the different options */ module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("statistics")); module->description = _("Creates a cross product of the category values from " "multiple raster map layers."); parm.input = G_define_option(); parm.input->key = "input"; parm.input->type = TYPE_STRING; parm.input->required = YES; parm.input->multiple = YES; parm.input->gisprompt = "old,cell,raster"; sprintf(buf, _("Names of 2-%d input raster maps"), NFILES); parm.input->description = G_store(buf); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); /* Define the different flags */ flag.z = G_define_flag(); flag.z->key = 'z'; flag.z->description = _("Non-zero data only"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); nrows = Rast_window_rows(); ncols = Rast_window_cols(); nfiles = 0; non_zero = flag.z->answer; for (nfiles = 0; (name = parm.input->answers[nfiles]); nfiles++) { if (nfiles >= NFILES) G_fatal_error(_("More than %d files not allowed"), NFILES); mapset = G_find_raster2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); names[nfiles] = name; fd[nfiles] = Rast_open_old(name, mapset); Rast_read_range(name, mapset, &range); ncats = range.max - range.min; if (nfiles == 0 || ncats > max_cats) { primary = nfiles; max_cats = ncats; } } if (nfiles <= 1) G_fatal_error(_("Must specify 2 or more input maps")); output = parm.output->answer; outfd = Rast_open_c_new(output); sprintf(buf, "Cross of %s", names[0]); for (i = 1; i < nfiles - 1; i++) { strcat(buf, ", "); strcat(buf, names[i]); } strcat(buf, " and "); strcat(buf, names[i]); Rast_init_cats(buf, &pcats); /* first step is cross product, but un-ordered */ result = cross(fd, non_zero, primary, outfd); /* print message STEP mesage */ G_message(_("%s: STEP 2 ..."), G_program_name()); /* now close all files */ for (i = 0; i < nfiles; i++) Rast_close(fd[i]); Rast_close(outfd); if (result <= 0) exit(0); /* build the renumbering/reclass and the new cats file */ qsort(reclass, result + 1, sizeof(RECLASS), cmp); table = (CELL *) G_calloc(result + 1, sizeof(CELL)); for (i = 0; i < nfiles; i++) { mapset = G_find_raster2(names[i], ""); Rast_read_cats(names[i], mapset, &labels[i]); } for (ncats = 0; ncats <= result; ncats++) { table[reclass[ncats].result] = ncats; set_cat(ncats, reclass[ncats].cat, &pcats); } for (i = 0; i < nfiles; i++) Rast_free_cats(&labels[i]); /* reopen the output cell for reading and for writing */ fd[0] = Rast_open_old(output, G_mapset()); outfd = Rast_open_c_new(output); renumber(fd[0], outfd); G_message(_("Creating support files for <%s>..."), output); Rast_close(fd[0]); Rast_close(outfd); Rast_write_cats(output, &pcats); Rast_free_cats(&pcats); if (result > 0) { Rast_make_random_colors(&pcolr, (CELL) 1, result); Rast_write_colors(output, G_mapset(), &pcolr); } G_message(_("%ld categories"), (long)result); exit(EXIT_SUCCESS); }
int exec_rectify(char *extension, char *interp_method, char *angle_map) { char *name; char *mapset; char *result; char *type = "raster"; int n; struct Colors colr; struct Categories cats; struct History hist; int colr_ok, cats_ok; long start_time, rectify_time; double aver_z; int elevfd; struct cache *ebuffer; G_debug(1, "Open elevation raster: "); /* open elevation raster */ select_target_env(); G_set_window(&target_window); G_debug(1, "target window: rs=%d cs=%d n=%f s=%f w=%f e=%f\n", target_window.rows, target_window.cols, target_window.north, target_window.south, target_window.west, target_window.east); elevfd = Rast_open_old(elev_name, elev_mapset); if (elevfd < 0) { G_fatal_error(_("Could not open elevation raster")); return 1; } ebuffer = readcell(elevfd, seg_mb_elev, 1); select_target_env(); Rast_close(elevfd); /* get an average elevation of the control points */ /* this is used only if target cells have no elevation */ get_aver_elev(&group.control_points, &aver_z); G_message("-----------------------------------------------"); /* rectify each file */ for (n = 0; n < group.group_ref.nfiles; n++) { if (!ref_list[n]) continue; name = group.group_ref.file[n].name; mapset = group.group_ref.file[n].mapset; result = G_malloc(strlen(group.group_ref.file[n].name) + strlen(extension) + 1); strcpy(result, group.group_ref.file[n].name); strcat(result, extension); G_debug(2, "ORTHO RECTIFYING:"); G_debug(2, "NAME %s", name); G_debug(2, "MAPSET %s", mapset); G_debug(2, "RESULT %s", result); G_debug(2, "select_current_env..."); select_current_env(); cats_ok = Rast_read_cats(name, mapset, &cats) >= 0; colr_ok = Rast_read_colors(name, mapset, &colr) > 0; /* Initialze History */ if (Rast_read_history(name, mapset, &hist) < 0) Rast_short_history(result, type, &hist); G_debug(2, "reading was fine..."); time(&start_time); G_debug(2, "Starting the rectification..."); if (rectify(name, mapset, ebuffer, aver_z, result, interp_method)) { G_debug(2, "Done. Writing results..."); select_target_env(); if (cats_ok) { Rast_write_cats(result, &cats); Rast_free_cats(&cats); } if (colr_ok) { Rast_write_colors(result, G_mapset(), &colr); Rast_free_colors(&colr); } /* Write out History */ Rast_command_history(&hist); Rast_write_history(result, &hist); select_current_env(); time(&rectify_time); report(rectify_time - start_time, 1); } else report((long)0, 0); G_free(result); } close(ebuffer->fd); release_cache(ebuffer); if (angle_map) { camera_angle(angle_map); } return 0; }
int main(int argc, char *argv[]) { char rname[GNAME_MAX]; /* Reclassed map name */ char rmapset[GMAPSET_MAX]; /* Reclassed mapset */ const char *mapset; /* Raster mapset */ struct Cell_head cellhd; struct GModule *module; struct Option *raster, *title_opt, *history_opt; struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt; struct Option *map_opt, *units_opt, *vdatum_opt; struct Option *load_opt, *save_opt; struct Flag *stats_flag, *null_flag, *del_flag; int is_reclass; /* Is raster reclass? */ const char *infile; char title[MAX_TITLE_LEN + 1]; struct History hist; /* Initialize GIS engine */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("metadata")); module->description = _("Allows creation and/or modification of " "raster map layer support files."); raster = G_define_standard_option(G_OPT_R_MAP); title_opt = G_define_option(); title_opt->key = "title"; title_opt->key_desc = "phrase"; title_opt->type = TYPE_STRING; title_opt->required = NO; title_opt->description = _("Title for resultant raster map"); history_opt = G_define_option(); history_opt->key = "history"; history_opt->key_desc = "phrase"; history_opt->type = TYPE_STRING; history_opt->required = NO; history_opt->description = _("Text to append to the next line of the map's metadata file"); units_opt = G_define_option(); units_opt->key = "units"; units_opt->type = TYPE_STRING; units_opt->required = NO; units_opt->description = _("Text to use for map data units"); vdatum_opt = G_define_option(); vdatum_opt->key = "vdatum"; vdatum_opt->type = TYPE_STRING; vdatum_opt->required = NO; vdatum_opt->description = _("Text to use for map vertical datum"); datasrc1_opt = G_define_option(); datasrc1_opt->key = "source1"; datasrc1_opt->key_desc = "phrase"; datasrc1_opt->type = TYPE_STRING; datasrc1_opt->required = NO; datasrc1_opt->description = _("Text to use for data source, line 1"); datasrc2_opt = G_define_option(); datasrc2_opt->key = "source2"; datasrc2_opt->key_desc = "phrase"; datasrc2_opt->type = TYPE_STRING; datasrc2_opt->required = NO; datasrc2_opt->description = _("Text to use for data source, line 2"); datadesc_opt = G_define_option(); datadesc_opt->key = "description"; datadesc_opt->key_desc = "phrase"; datadesc_opt->type = TYPE_STRING; datadesc_opt->required = NO; datadesc_opt->description = _("Text to use for data description or keyword(s)"); map_opt = G_define_option(); map_opt->key = "raster"; map_opt->type = TYPE_STRING; map_opt->required = NO; map_opt->gisprompt = "old,cell,raster"; map_opt->description = _("Raster map from which to copy category table"); load_opt = G_define_standard_option(G_OPT_F_INPUT); load_opt->key = "loadhistory"; load_opt->required = NO; load_opt->description = _("Text file from which to load history"); save_opt = G_define_standard_option(G_OPT_F_OUTPUT); save_opt->key = "savehistory"; save_opt->required = NO; save_opt->description = _("Text file in which to save history"); stats_flag = G_define_flag(); stats_flag->key = 's'; stats_flag->description = _("Update statistics (histogram, range)"); null_flag = G_define_flag(); null_flag->key = 'n'; null_flag->description = _("Create/reset the null file"); del_flag = G_define_flag(); del_flag->key = 'd'; del_flag->description = _("Delete the null file"); /* Parse command-line options */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Make sure raster exists and set mapset */ infile = raster->answer; mapset = G_find_raster2(infile, G_mapset()); /* current mapset only for editing */ if (!mapset || strcmp(mapset, G_mapset()) != 0) G_fatal_error(_("Raster map <%s> not found in current mapset"), infile); Rast_get_cellhd(raster->answer, "", &cellhd); is_reclass = (Rast_is_reclass(raster->answer, "", rname, rmapset) > 0); if (title_opt->answer) { strncpy(title, title_opt->answer, MAX_TITLE_LEN); title[MAX_TITLE_LEN - 1] = '\0'; /* strncpy doesn't null terminate oversized input */ G_strip(title); G_debug(3, "map title= [%s] (%li chars)", title, strlen(title)); Rast_read_history(raster->answer, "", &hist); Rast_set_history(&hist, HIST_TITLE, title); Rast_write_history(raster->answer, &hist); } if (save_opt->answer) { FILE *fp = fopen(save_opt->answer, "w"); int i; if (!fp) G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer); Rast_read_history(raster->answer, "", &hist); for (i = 0; i < Rast_history_length(&hist); i++) fprintf(fp, "%s\n", Rast_history_line(&hist, i)); fclose(fp); } if (load_opt->answer) { FILE *fp = fopen(load_opt->answer, "r"); if (!fp) G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer); Rast_read_history(raster->answer, "", &hist); Rast_clear_history(&hist); for (;;) { char buf[80]; if (!G_getl2(buf, sizeof(buf), fp)) break; Rast_append_history(&hist, buf); } fclose(fp); Rast_write_history(raster->answer, &hist); } if (history_opt->answer) { Rast_read_history(raster->answer, "", &hist); /* two less than defined as if only one less a newline gets appended in the hist file. bug? */ /* Should be RECORD_LEN, but r.info truncates at > 71 chars */ if (strlen(history_opt->answer) > 71) { int i; for (i = 0; i < strlen(history_opt->answer); i += 71) { char buf[72]; strncpy(buf, &history_opt->answer[i], sizeof(buf)-1); buf[sizeof(buf)-1] = '\0'; Rast_append_history(&hist, buf); } } else Rast_append_history(&hist, history_opt->answer); Rast_write_history(raster->answer, &hist); } if (units_opt->answer) Rast_write_units(raster->answer, units_opt->answer); if (vdatum_opt->answer) Rast_write_vdatum(raster->answer, vdatum_opt->answer); if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) { Rast_read_history(raster->answer, "", &hist); if (datasrc1_opt->answer) Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer); if (datasrc2_opt->answer) Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer); if (datadesc_opt->answer) Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer); Rast_write_history(raster->answer, &hist); } if (map_opt->answer) { /* use cats from another map */ int fd; struct Categories cats; fd = Rast_open_old(infile, ""); Rast_init_cats("", &cats); if (Rast_read_cats(map_opt->answer, "", &cats) < 0) G_fatal_error(_("Unable to read category file of raster map <%s>"), map_opt->answer); Rast_write_cats(infile, &cats); G_message(_("cats table for [%s] set to %s"), infile, map_opt->answer); Rast_close(fd); Rast_free_cats(&cats); } if (title_opt->answer || history_opt->answer || units_opt->answer || vdatum_opt->answer || datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer || map_opt->answer) exit(EXIT_SUCCESS); /* Check the histogram and range */ if (stats_flag->answer) check_stats(raster->answer); /* null file */ if (null_flag->answer) { unsigned char *null_bits; int row, col; int fd; if (is_reclass) G_fatal_error(_("[%s] is a reclass of another map. Exiting."), raster->answer); /* Create a file of no-nulls */ null_bits = Rast__allocate_null_bits(cellhd.cols); for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++) null_bits[col] = 0; /* Open null file for writing */ Rast_set_window(&cellhd); fd = Rast__open_null_write(raster->answer); G_message(_("Writing new null file for [%s]... "), raster->answer); for (row = 0; row < cellhd.rows; row++) { G_percent(row, cellhd.rows, 1); Rast__write_null_bits(fd, null_bits); } G_percent(row, cellhd.rows, 1); /* Cleanup */ Rast__close_null(fd); G_free(null_bits); } if (del_flag->answer) { char path[GPATH_MAX]; if (is_reclass) G_fatal_error(_("[%s] is a reclass of another map. Exiting."), raster->answer); /* Write a file of no-nulls */ G_message(_("Removing null file for [%s]...\n"), raster->answer); G_file_name_misc(path, "cell_misc", "null", raster->answer, G_mapset()); unlink(path); G_file_name_misc(path, "cell_misc", "null2", raster->answer, G_mapset()); unlink(path); G_done_msg(_("Done.")); } return EXIT_SUCCESS; }