int main( int argc, char **argv ) { char *mapset; char *name; int fp; struct GModule *module; struct Option *map; struct Option *win; struct Cell_head window; /* Initialize the GIS calls */ G_gisinit( argv[0] ); module = G_define_module(); module->keywords = ( "display, raster" ); module->description = ( "Output raster map layers in a format suitable for display in QGIS" ); map = G_define_standard_option( G_OPT_R_MAP ); map->description = ( "Raster map to be displayed" ); win = G_define_option(); win->key = "window"; win->type = TYPE_DOUBLE; win->multiple = YES; win->description = "xmin,ymin,xmax,ymax,ncols,nrows"; if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); name = map->answer; /* Make sure map is available */ mapset = G_find_cell2( name, "" ); if ( mapset == NULL ) G_fatal_error(( "Raster map <%s> not found" ), name ); /* It can happen that GRASS data set is 'corrupted' and zone differs in WIND and * cellhd, and G_open_cell_old fails, so it is better to read window from map */ /* G_get_window( &window ); */ G_get_cellhd( name, mapset, &window ); window.west = atof( win->answers[0] ); window.south = atof( win->answers[1] ); window.east = atof( win->answers[2] ); window.north = atof( win->answers[3] ); window.cols = atoi( win->answers[4] ); window.rows = atoi( win->answers[5] ); G_adjust_Cell_head( &window, 1, 1 ); G_set_window( &window ); fp = G_raster_map_is_fp( name, mapset ); /* use DCELL even if the map is FCELL */ if ( fp ) display( name, mapset, DCELL_TYPE ); else display( name, mapset, CELL_TYPE ); exit( EXIT_SUCCESS ); }
/*! \brief Pack color table (floating-point map) Passed a array of floats that will be converted from cell values to packed colors (0xbbggrr) and float to int Floating point data not freed here, use: gsds_free_data_buff(id, ATTY_FLOAT) \param filename raster map name \param fbuf \param ibuf \param rows number of rows \param cols number of cols */ void Gs_pack_colors_float(const char *filename, float *fbuf, int *ibuf, int rows, int cols) { const char *mapset; struct Colors colrules; unsigned char *r, *g, *b, *set; int i, j, *icur; FCELL *fcur; mapset = G_find_cell2(filename, ""); if (!mapset) { G_warning(_("Raster map <%s> not found"), filename); return; } r = (unsigned char *)G_malloc(cols); g = (unsigned char *)G_malloc(cols); b = (unsigned char *)G_malloc(cols); set = (unsigned char *)G_malloc(cols); G_read_colors(filename, mapset, &colrules); fcur = fbuf; icur = ibuf; G_message(_("Translating colors from raster map <%s>..."), G_fully_qualified_name(filename, mapset)); for (i = 0; i < rows; i++) { G_lookup_f_raster_colors(fcur, r, g, b, set, cols, &colrules); G_percent(i, rows, 2); for (j = 0; j < cols; j++) { if (set[j]) { icur[j] = (r[j] & 0xff) | ((g[j] & 0xff) << 8) | ((b[j] & 0xff) << 16); } else { icur[j] = NO_DATA_COL; } } icur = &(icur[cols]); fcur = &(fcur[cols]); } G_percent(1, 1, 1); G_free_colors(&colrules); G_free(r); G_free(g); G_free(b); G_free(set); return; }
/*! \brief Load raster map as integer map Calling function must have already allocated space in buff for struct BM of wind->rows & wind->cols. This routine simply loads the map into the bitmap by repetitve calls to get_map_row. Any value other than 0 in the map will set the bitmap. (may want to change later to allow specific value to set) Changed to use null. \param wind current window \param map_name raster map name \param[out] buff data buffer \returns 1 on success \return -1 on failure */ int Gs_loadmap_as_bitmap(struct Cell_head *wind, const char *map_name, struct BM *buff) { FILEDESC cellfile; const char *map_set; char *nullflags; int *tmp_buf; int row, col; G_debug(3, "Gs_loadmap_as_bitmap"); map_set = G_find_cell2(map_name, ""); if (!map_set) { G_warning(_("Raster map <%s> not found"), map_name); return -1; } if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) { G_fatal_error(_("Unable to open raster map <%s>"), map_name); } tmp_buf = (int *)G_malloc(wind->cols * sizeof(int)); /* G_fatal_error */ if (!tmp_buf) { return -1; } nullflags = G_allocate_null_buf(); if (!nullflags) { G_fatal_error(_("Unable to allocate memory for a null buffer")); } G_message(_("Loading raster map <%s>..."), G_fully_qualified_name(map_name, map_set)); for (row = 0; row < wind->rows; row++) { G_get_null_value_row(cellfile, nullflags, row); for (col = 0; col < wind->cols; col++) { if (nullflags[col]) { /* no data */ BM_set(buff, col, row, 1); } else { BM_set(buff, col, row, 0); } } } G_close_cell(cellfile); G_free(tmp_buf); G_free(nullflags); return (1); }
/*! \brief Load raster map as floating point map Calling function must have already allocated space in buff for wind->rows * wind->cols floats. This routine simply loads the map into a 2d array by repetitve calls to get_f_raster_row. \param wind current window \param map_name raster map name \param[out] buff data buffer \param[out] nullmap null map buffer \param[out] has_null indicates if raster map contains null-data \return 1 on success \return 0 on failure */ int Gs_loadmap_as_float(struct Cell_head *wind, const char *map_name, float *buff, struct BM *nullmap, int *has_null) { FILEDESC cellfile; const char *map_set; char *nullflags; int offset, row, col; G_debug(3, "Gs_loadmap_as_float(): name=%s", map_name); map_set = G_find_cell2(map_name, ""); if (!map_set) { G_warning(_("Raster map <%s> not found"), map_name); return 0; } *has_null = 0; nullflags = G_allocate_null_buf(); /* G_fatal_error */ if (!nullflags) { G_fatal_error(_("Unable to allocate memory for a null buffer")); } if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) { G_fatal_error(_("Unable to open raster map <%s>"), map_name); } G_message(_("Loading raster map <%s>..."), G_fully_qualified_name(map_name, map_set)); for (row = 0; row < wind->rows; row++) { offset = row * wind->cols; G_get_f_raster_row(cellfile, &(buff[offset]), row); G_get_null_value_row(cellfile, nullflags, row); G_percent(row, wind->rows, 2); for (col = 0; col < wind->cols; col++) { if (nullflags[col] || G_is_f_null_value(buff + offset + col)) { *has_null = 1; BM_set(nullmap, col, row, 1); } /* set nm */ } } G_percent(1, 1, 1); G_debug(4, " has_null=%d", *has_null); G_close_cell(cellfile); G_free(nullflags); return (1); }
/*! \brief Build color table (256) Calling function must have already allocated space in buff for range of data (256 for now) - simply calls get_color for each cat in color range \param filename raster map name \param[out] buff data buffer \return 1 on success \return 0 on failure */ int Gs_build_256lookup(const char *filename, int *buff) { const char *mapset; struct Colors colrules; CELL min, max, cats[256]; int i; unsigned char r[256], g[256], b[256], set[256]; G_debug(3, "building color table"); mapset = G_find_cell2(filename, ""); if (!mapset) { G_warning(_("Raster map <%s> not found"), filename); return 0; } G_read_colors(filename, mapset, &colrules); G_get_color_range(&min, &max, &colrules); if (min < 0 || max > 255) { G_warning(_("Color table range doesn't match data (mincol=%d, maxcol=%d"), min, max); min = min < 0 ? 0 : min; max = max > 255 ? 255 : max; } G_zero(cats, 256 * sizeof(CELL)); for (i = min; i <= max; i++) { cats[i] = i; } G_lookup_colors(cats, r, g, b, set, 256, &colrules); for (i = 0; i < 256; i++) { if (set[i]) { buff[i] = (r[i] & 0xff) | ((g[i] & 0xff) << 8) | ((b[i] & 0xff) << 16); } else { buff[i] = NO_DATA_COL; } } return (1); }
int main(int argc, char **argv) { int overwrite; int interactive; int remove; int have_colors; struct Colors colors, colors_tmp; struct Cell_stats statf; int have_stats = 0; struct FPRange range; DCELL min, max; char *name, *mapset; char *style, *cmap, *cmapset; char *rules; int fp; struct GModule *module; struct { struct Flag *r, *w, *l, *g, *a, *e, *i, *q, *n; } flag; struct { struct Option *map, *colr, *rast, *rules; } opt; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, color table"); module->description = _("Creates/modifies the color table associated with a raster map layer."); opt.map = G_define_standard_option(G_OPT_R_MAP); opt.map->required = NO; opt.map->guisection = _("Required"); scan_rules(); opt.colr = G_define_option(); opt.colr->key = "color"; opt.colr->key_desc = "style"; opt.colr->type = TYPE_STRING; opt.colr->required = NO; opt.colr->options = rules_list(); opt.colr->description = _("Type of color table"); opt.colr->descriptions = rules_descriptions(); opt.colr->guisection = _("Colors"); opt.rast = G_define_option(); opt.rast->key = "raster"; opt.rast->type = TYPE_STRING; opt.rast->required = NO; opt.rast->gisprompt = "old,cell,raster"; opt.rast->description = _("Raster map name from which to copy color table"); opt.rules = G_define_standard_option(G_OPT_F_INPUT); opt.rules->key = "rules"; opt.rules->required = NO; opt.rules->description = _("Path to rules file (\"-\" to read rules from stdin)"); opt.rules->guisection = _("Colors"); flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Remove existing color table"); flag.w = G_define_flag(); flag.w->key = 'w'; flag.w->description = _("Only write new color table if one doesn't already exist"); flag.l = G_define_flag(); flag.l->key = 'l'; flag.l->description = _("List available rules then exit"); flag.n = G_define_flag(); flag.n->key = 'n'; flag.n->description = _("Invert colors"); flag.n->guisection = _("Colors"); flag.g = G_define_flag(); flag.g->key = 'g'; flag.g->description = _("Logarithmic scaling"); flag.g->guisection = _("Colors"); flag.a = G_define_flag(); flag.a->key = 'a'; flag.a->description = _("Logarithmic-absolute scaling"); flag.a->guisection = _("Colors"); flag.e = G_define_flag(); flag.e->key = 'e'; flag.e->description = _("Histogram equalization"); flag.e->guisection = _("Colors"); flag.i = G_define_flag(); flag.i->key = 'i'; flag.i->description = _("Enter rules interactively"); /* please, remove before GRASS 7 released */ flag.q = G_define_flag(); flag.q->key = 'q'; flag.q->description = _("Run quietly"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* please, remove before GRASS 7 released */ if (flag.q->answer) { G_putenv("GRASS_VERBOSE", "0"); G_warning(_("The '-q' flag is superseded and will be removed " "in future. Please use '--quiet' instead.")); } if (flag.l->answer) { list_rules(); return EXIT_SUCCESS; } overwrite = !flag.w->answer; interactive = flag.i->answer; remove = flag.r->answer; name = opt.map->answer; style = opt.colr->answer; cmap = opt.rast->answer; rules = opt.rules->answer; if (!name) G_fatal_error(_("No raster map specified")); if (!cmap && !style && !rules && !interactive && !remove) G_fatal_error(_("One of \"-i\" or \"-r\" or options \"color\", \"rast\" or \"rules\" must be specified!")); if (interactive && (style || rules || cmap)) G_fatal_error(_("Interactive mode is incompatible with \"color\", \"rules\", and \"raster\" options")); if ((style && (cmap || rules)) || (cmap && rules)) { if ((style && rules && !cmap) && strcmp(style, "rules") == 0) style = NULL; else G_fatal_error( _("\"color\", \"rules\", and \"raster\" options are mutually exclusive")); } /* handle rules="-" (from stdin) by translating that to colors=rules */ /* this method should not be ported to GRASS 7 verbatim, as color=rules DNE */ if (rules && strcmp(rules, "-") == 0) { style = G_store("rules"); rules = NULL; } if (flag.g->answer && flag.a->answer) G_fatal_error(_("-g and -a flags are mutually exclusive")); mapset = G_find_cell2(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); if (remove) { int stat = G_remove_colors(name, mapset); if (stat < 0) G_fatal_error(_("Unable to remove color table of raster map <%s>"), name); if (stat == 0) G_warning(_("Color table of raster map <%s> not found"), name); return EXIT_SUCCESS; } G_suppress_warnings(1); have_colors = G_read_colors(name, mapset, &colors); /*if (have_colors >= 0) G_free_colors(&colors); */ if (have_colors > 0 && !overwrite) { G_warning(_("Color table exists. Exiting.")); exit(EXIT_FAILURE); } G_suppress_warnings(0); fp = G_raster_map_is_fp(name, mapset); G_read_fp_range(name, mapset, &range); G_get_fp_range_min_max(&range, &min, &max); if (interactive) { if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); } else if (style) { /* * here the predefined color-table color-styles are created by GRASS library calls. */ if (strcmp(style, "random") == 0) { if (fp) G_fatal_error(_("Color table 'random' is not supported for floating point raster map")); G_make_random_colors(&colors, (CELL) min, (CELL) max); } else if (strcmp(style, "grey.eq") == 0) { if (fp) G_fatal_error(_("Color table 'grey.eq' is not supported for floating point raster map")); if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_make_histogram_eq_colors(&colors, &statf); } else if (strcmp(style, "grey.log") == 0) { if (fp) G_fatal_error(_("Color table 'grey.log' is not supported for floating point raster map")); if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_make_histogram_log_colors(&colors, &statf, (CELL) min, (CELL) max); } else if (strcmp(style, "rules") == 0) { if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); } else if (find_rule(style)) G_make_fp_colors(&colors, style, min, max); else G_fatal_error(_("Unknown color request '%s'"), style); } else if (rules) { if (!G_load_fp_colors(&colors, rules, min, max)) { /* for backwards compatibility try as std name; remove for GRASS 7 */ char path[GPATH_MAX]; /* don't bother with native dirsep as not needed for backwards compatibility */ sprintf(path, "%s/etc/colors/%s", G_gisbase(), rules); if (!G_load_fp_colors(&colors, path, min, max)) G_fatal_error(_("Unable to load rules file <%s>"), rules); } } else { /* use color from another map (cmap) */ cmapset = G_find_cell2(cmap, ""); if (cmapset == NULL) G_fatal_error(_("Raster map <%s> not found"), cmap); if (G_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap); } if (fp) G_mark_colors_as_fp(&colors); if (flag.n->answer) G_invert_colors(&colors); if (flag.e->answer) { if (fp) { struct FP_stats fpstats; get_fp_stats(name, mapset, &fpstats, min, max, flag.g->answer, flag.a->answer); G_histogram_eq_colors_fp(&colors_tmp, &colors, &fpstats); } else { if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_histogram_eq_colors(&colors_tmp, &colors, &statf); } colors = colors_tmp; } if (flag.g->answer) { G_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (flag.a->answer) { G_abs_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (fp) G_mark_colors_as_fp(&colors); if (G_write_colors(name, mapset, &colors) >= 0) G_message(_("Color table for raster map <%s> set to '%s'"), name, interactive ? "rules" : style ? style : rules ? rules : cmap); exit(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); }
int main(int argc, char **argv) { char *mapset; int o_method; struct GModule *module; struct Option *method, *basemap, *covermap, *outputmap; struct Flag *flag_c; struct Categories cats; char methods[1024]; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Calculates category or object oriented statistics."); basemap = G_define_standard_option(G_OPT_R_BASE); covermap = G_define_standard_option(G_OPT_R_COVER); for (o_method = 0; menu[o_method].name; o_method++) { if (o_method) strcat(methods, ","); else *(methods) = 0; strcat(methods, menu[o_method].name); } method = G_define_option(); method->key = "method"; method->type = TYPE_STRING; method->required = YES; method->description = _("Method of object-based statistic"); method->options = methods; outputmap = G_define_standard_option(G_OPT_R_OUTPUT); outputmap->description = _("Resultant raster map (not used with 'distribution')"); outputmap->required = NO; flag_c = G_define_flag(); flag_c->key = 'c'; flag_c->description = _("Cover values extracted from the category labels of the cover map"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if ((mapset = G_find_cell2(basemap->answer, "")) == 0) G_fatal_error(_("Raster map <%s> not found"), basemap->answer); if (G_raster_map_is_fp(basemap->answer, mapset) != 0) G_fatal_error(_("This module currently only works for integer (CELL) maps")); if ((mapset = G_find_cell2(covermap->answer, "")) == 0) G_fatal_error(_("Raster map <%s> not found"), covermap->answer); if (G_raster_map_is_fp(covermap->answer, mapset) != 0) G_fatal_error(_("This module currently only works for integer (CELL) maps")); if (G_read_cats(covermap->answer, mapset, &cats) < 0) { G_fatal_error(_("Unable to read category file of raster map <%s@%s>"), covermap->answer, mapset); } for (o_method = 0; menu[o_method].name; o_method++) if (strcmp(menu[o_method].name, method->answer) == 0) break; if (!menu[o_method].name) { G_warning(_("<%s=%s> unknown %s"), method->key, method->answer, method->key); G_usage(); exit(EXIT_FAILURE); } switch (menu[o_method].val) { case DISTRIB: if (outputmap->answer != NULL) G_warning(_("Output map <%s> ignored"), outputmap->answer); o_distrib(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer); break; case AVERAGE: is_ok(method->answer, outputmap->answer); o_average(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MODE: is_ok(method->answer, outputmap->answer); o_mode(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case ADEV: is_ok(method->answer, outputmap->answer); o_adev(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SDEV: is_ok(method->answer, outputmap->answer); o_sdev(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case VARIANC: is_ok(method->answer, outputmap->answer); o_var(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SKEWNES: is_ok(method->answer, outputmap->answer); o_skew(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case KURTOSI: is_ok(method->answer, outputmap->answer); o_kurt(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MEDIAN: is_ok(method->answer, outputmap->answer); o_median(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MIN: is_ok(method->answer, outputmap->answer); o_min(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MAX: is_ok(method->answer, outputmap->answer); o_max(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SUM: is_ok(method->answer, outputmap->answer); o_sum(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case DIV: is_ok(method->answer, outputmap->answer); o_divr(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; default: G_fatal_error(_("Not yet implemented!")); } return 0; }
int main( int argc, char **argv ) { char *mapset = 0; char *name = 0; struct GModule *module; struct Option *map; struct Option *win; struct Option *format; struct Cell_head window; RASTER_MAP_TYPE raster_type; /* Initialize the GIS calls */ G_gisinit( argv[0] ); module = G_define_module(); module->description = ( "Output raster map layers in a format suitable for display in QGIS" ); map = G_define_standard_option( G_OPT_R_MAP ); map->description = ( "Raster map to be displayed" ); format = G_define_option(); format->key = "format"; format->type = TYPE_STRING; format->description = "format"; format->options = "color,value"; win = G_define_option(); win->key = "window"; win->type = TYPE_DOUBLE; win->multiple = YES; win->description = "xmin,ymin,xmax,ymax,ncols,nrows"; if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); name = map->answer; /* Make sure map is available */ #if GRASS_VERSION_MAJOR < 7 mapset = G_find_cell2( name, "" ); if ( !mapset ) G_fatal_error( ( "Raster map <%s> not found" ), name ); #else mapset = ""; #endif /* It can happen that GRASS data set is 'corrupted' and zone differs in WIND and * cellhd, and G_open_cell_old fails, so it is better to read window from map */ /* G_get_window( &window ); */ G_get_cellhd( name, mapset, &window ); window.west = atof( win->answers[0] ); window.south = atof( win->answers[1] ); window.east = atof( win->answers[2] ); window.north = atof( win->answers[3] ); window.cols = atoi( win->answers[4] ); window.rows = atoi( win->answers[5] ); G_adjust_Cell_head( &window, 1, 1 ); G_set_window( &window ); G_suppress_masking(); // must be after G_set_window() raster_type = G_raster_map_type( name, "" ); display( name, mapset, raster_type, format->answer ); exit( EXIT_SUCCESS ); }
int main(int argc, char *argv[]) { RASTER_MAP_TYPE out_type, map_type; char *name; char *mapset; char *null_str; char surfer_null_str[13] = { "1.70141e+038" }; int fd; int nrows, ncols, dp, width; int rc; FILE *fp; struct GModule *module; struct { struct Option *map; struct Option *output; struct Option *dp; struct Option *width; struct Option *null; } parm; struct { struct Flag *noheader; struct Flag *surfer; struct Flag *modflow; struct Flag *int_out; } flag; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, export"); module->description = _("Converts a raster map layer into an ASCII text file."); /* Define the different options */ parm.map = G_define_option(); parm.map->key = "input"; parm.map->type = TYPE_STRING; parm.map->required = YES; parm.map->gisprompt = "old,cell,raster"; parm.map->description = _("Name of an existing raster map"); parm.output = G_define_option(); parm.output->key = "output"; parm.output->type = TYPE_STRING; parm.output->required = NO; parm.output->gisprompt = "new_file,file,output"; parm.output->description = _("Name for output ASCII grid map (use out=- for stdout)"); parm.dp = G_define_option(); parm.dp->key = "dp"; parm.dp->type = TYPE_INTEGER; parm.dp->required = NO; parm.dp->description = _("Number of significant digits (floating point only)"); parm.width = G_define_option(); parm.width->key = "width"; parm.width->type = TYPE_INTEGER; parm.width->required = NO; parm.width->description = _("Number of values printed before wrapping a line (only SURFER or MODFLOW format)"); parm.null = G_define_option(); parm.null->key = "null"; parm.null->type = TYPE_STRING; parm.null->required = NO; parm.null->answer = "*"; parm.null->description = _("String to represent null cell (GRASS grid only)"); flag.noheader = G_define_flag(); flag.noheader->key = 'h'; flag.noheader->description = _("Suppress printing of header information"); flag.surfer = G_define_flag(); flag.surfer->key = 's'; flag.surfer->description = _("Write SURFER (Golden Software) ASCII grid"); flag.modflow = G_define_flag(); flag.modflow->key = 'm'; flag.modflow->description = _("Write MODFLOW (USGS) ASCII array"); flag.int_out = G_define_flag(); flag.int_out->key = 'i'; flag.int_out->description = _("Force output of integer values"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (parm.dp->answer) { if (sscanf(parm.dp->answer, "%d", &dp) != 1) G_fatal_error(_("Failed to interpret dp as an integer")); if (dp > 20 || dp < 0) G_fatal_error(_("dp has to be from 0 to 20")); } width = 10; if (parm.width->answer) { if (sscanf(parm.width->answer, "%d", &width) != 1) G_fatal_error(_("Failed to interpret width as an integer")); } null_str = parm.null->answer; if (flag.surfer->answer && flag.noheader->answer) G_fatal_error(_("Both -s and -h doesn't make sense")); if (flag.surfer->answer && flag.modflow->answer) G_fatal_error(_("Use -M or -s, not both")); name = parm.map->answer; mapset = G_find_cell2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); /* open raster map */ fd = G_open_cell_old(name, mapset); if (fd < 0) G_fatal_error(_("Unable to open raster map <%s>"), name); map_type = G_get_raster_map_type(fd); if (!flag.int_out->answer) out_type = map_type; else out_type = CELL_TYPE; if (!parm.dp->answer) { dp = 6; if (out_type == DCELL_TYPE) dp = 16; } nrows = G_window_rows(); ncols = G_window_cols(); /* open ascii file for writing or use stdout */ if (parm.output->answer && strcmp("-", parm.output->answer) != 0) { if (NULL == (fp = fopen(parm.output->answer, "w"))) G_fatal_error(_("Unable to open file <%s>"), parm.output->answer); } else fp = stdout; /* process the requested output format */ if (flag.surfer->answer) { if (!flag.noheader->answer) { if (writeGSheader(fp, name, mapset)) G_fatal_error(_("Unable to read fp range for <%s>"), name); } rc = write_GSGRID(fd, fp, nrows, ncols, out_type, dp, surfer_null_str, width); } else if (flag.modflow->answer) { if (!flag.noheader->answer) writeMFheader(fp, dp, width, out_type); rc = write_MODFLOW(fd, fp, nrows, ncols, out_type, dp, width); } else { if (!flag.noheader->answer) writeGRASSheader(fp); rc = write_GRASS(fd, fp, nrows, ncols, out_type, dp, null_str); } if (rc) { G_fatal_error(_("Read failed at row %d"), rc); } /* tidy up and go away */ G_close_cell(fd); fclose(fp); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char *name, *mapset; char rname[GNAME_MAX], rmapset[GMAPSET_MAX]; char path[GPATH_MAX]; int row, col, null_fd; unsigned char *null_bits; RASTER_MAP_TYPE map_type; int change_null = 0, create, remove, only_int, only_fp, only_null; int is_reclass; struct GModule *module; struct { struct Option *map; struct Option *setnull; struct Option *null; } parms; struct { struct Flag *f; struct Flag *n; struct Flag *i; struct Flag *c; struct Flag *r; } flags; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, null data"); module->description = _("Manages NULL-values of given raster map."); parms.map = G_define_standard_option(G_OPT_R_MAP); parms.map->description = _("Name of raster map for which to edit null file"); parms.setnull = G_define_option(); parms.setnull->key = "setnull"; parms.setnull->key_desc = "val[-val]"; parms.setnull->type = TYPE_STRING; parms.setnull->required = NO; parms.setnull->multiple = YES; parms.setnull->description = _("List of cell values to be set to NULL"); parms.setnull->guisection = _("Modify"); parms.null = G_define_option(); parms.null->key = "null"; parms.null->type = TYPE_DOUBLE; parms.null->required = NO; parms.null->multiple = NO; parms.null->description = _("The value to replace the null value by"); parms.null->guisection = _("Modify"); flags.f = G_define_flag(); flags.f->key = 'f'; flags.f->description = _("Only do the work if the map is floating-point"); flags.f->guisection = _("Check"); flags.i = G_define_flag(); flags.i->key = 'i'; flags.i->description = _("Only do the work if the map is integer"); flags.i->guisection = _("Check"); flags.n = G_define_flag(); flags.n->key = 'n'; flags.n->description = _("Only do the work if the map doesn't have a NULL-value bitmap file"); flags.n->guisection = _("Check"); flags.c = G_define_flag(); flags.c->key = 'c'; flags.c->description = _("Create NULL-value bitmap file validating all data cells"); flags.r = G_define_flag(); flags.r->key = 'r'; flags.r->description = _("Remove NULL-value bitmap file"); flags.r->guisection = _("Remove"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); only_int = flags.i->answer; only_fp = flags.f->answer; only_null = flags.n->answer; create = flags.c->answer; remove = flags.r->answer; name = parms.map->answer; mapset = G_find_cell2(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); is_reclass = (G_is_reclass(name, mapset, rname, rmapset) > 0); if (is_reclass) G_fatal_error(_("Raster map <%s> is a reclass of map <%s@%s>. " "Consider to generate a copy with r.mapcalc. Exiting."), name, rname, rmapset); if (strcmp(mapset, G_mapset()) != 0) G_fatal_error(_("Raster map <%s> is not in your mapset <%s>"), name, G_mapset()); if (parms.null->answer) { if (sscanf(parms.null->answer, "%lf", &new_null) == 1) change_null = 1; else G_fatal_error(_("%s is illegal entry for null"), parms.null->answer); } map_type = G_raster_map_type(name, mapset); if (only_null && G_find_file_misc("cell_misc", "null", name, mapset)) G_fatal_error(_("Raster map <%s> already has a null bitmap file"), name); if (map_type == CELL_TYPE) { if (only_fp) G_fatal_error(_("<%s> is integer raster map (CELL)"), name); if ((double)((int)new_null) != new_null) { G_warning(_("<%s> is integer raster map (CELL). Using null=%d."), name, (int)new_null); new_null = (double)((int)new_null); } } else if (only_int) G_fatal_error(_("<%s> is floating pointing raster map"), name); parse_vallist(parms.setnull->answers, &d_mask); if (G_get_cellhd(name, mapset, &cellhd) < 0) G_fatal_error(_("Unable to read header of raster map <%s>"), G_fully_qualified_name(name, mapset)); if (create) { /* write a file of no-nulls */ null_bits = (unsigned char *)G__allocate_null_bits(cellhd.cols); /* init all cells to 0's */ for (col = 0; col < G__null_bitstream_size(cellhd.cols); col++) null_bits[col] = 0; null_fd = G_open_new_misc("cell_misc", "null", name); G_verbose_message(_("Writing new null file for raster map <%s>..."), name); for (row = 0; row < cellhd.rows; row++) { G_percent(row, cellhd.rows, 1); if (G__write_null_bits(null_fd, null_bits, row, cellhd.cols, 0) < 0) G_fatal_error(_("Error writing null row %d"), row); } G_percent(row, cellhd.rows, 1); close(null_fd); G_done_msg(_("Raster map <%s> modified."), name); exit(EXIT_SUCCESS); } if (remove) { /* write a file of no-nulls */ G_verbose_message(_("Removing null file for raster map <%s>..."), name); null_fd = G_open_new_misc("cell_misc", "null", name); G__file_name_misc(path, "cell_misc", "null", name, mapset); unlink(path); G_done_msg(_("Raster map <%s> modified."), name); exit(EXIT_SUCCESS); } process(name, mapset, change_null, map_type); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char name[128]; char *mapset; struct Categories cats; int vector = 0; int stat; /* Initialize GIS engine */ G_gisinit(argv[0]); if (argc > 1 && (strcmp(argv[1], "-v") == 0)) { vector = 1; argc--; argv++; } if (argc < 2) { if (vector) mapset = G_ask_vector_in_mapset(_("Which vector map needs " "updated categories?"), name); else mapset = G_ask_cell_in_mapset(_("Which raster map needs " "updated categories?"), name); if (mapset == NULL) G_fatal_error(_("%s map <%s> not found"), vector ? "Vector" : "Raster", name); } else { strncpy(name, argv[1], sizeof(name)); mapset = (vector) ? G_find_vector2(name, G_mapset()) : G_find_cell2(name, G_mapset()); if (mapset == NULL) G_fatal_error(_("%s map <%s> not found"), vector ? "Vector" : "Raster", name); } stat = (vector) ? G_read_vector_cats(name, mapset, &cats) : G_read_cats(name, mapset, &cats); if (stat < 0) G_init_cats((CELL) 0, "", &cats); if (!vector && G_raster_map_is_fp(name, mapset)) { if (E_edit_fp_cats(name, &cats) < 0) { G_message(_("Category file for <%s> not updated"), name); return EXIT_SUCCESS; } } else { if (E_edit_cats(name, &cats, stat < 0) < 0) { G_message(_("Category file for <%s> not updated"), name); return EXIT_SUCCESS; } } if (vector) G_write_vector_cats(name, &cats); else G_write_cats(name, &cats); G_message(_("Category file for <%s> updated"), name); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *rastin, *rastout, *method; struct History history; char title[64]; char buf_nsres[100], buf_ewres[100]; struct Colors colors; char *inmap; int infile, outfile; DCELL *outbuf; int row, col; struct Cell_head dst_w, src_w; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, resample"); module->description = _("Resamples raster map layers to a finer grid using interpolation."); rastin = G_define_standard_option(G_OPT_R_INPUT); rastout = G_define_standard_option(G_OPT_R_OUTPUT); method = G_define_option(); method->key = "method"; method->type = TYPE_STRING; method->required = NO; method->description = _("Interpolation method"); method->options = "nearest,bilinear,bicubic"; method->answer = "bilinear"; if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (G_strcasecmp(method->answer, "nearest") == 0) neighbors = 1; else if (G_strcasecmp(method->answer, "bilinear") == 0) neighbors = 2; else if (G_strcasecmp(method->answer, "bicubic") == 0) neighbors = 4; else G_fatal_error(_("Invalid method: %s"), method->answer); G_get_set_window(&dst_w); inmap = G_find_cell2(rastin->answer, ""); if (!inmap) G_fatal_error(_("Raster map <%s> not found"), rastin->answer); /* set window to old map */ G_get_cellhd(rastin->answer, inmap, &src_w); /* enlarge source window */ { double north = G_row_to_northing(0.5, &dst_w); double south = G_row_to_northing(dst_w.rows - 0.5, &dst_w); int r0 = (int)floor(G_northing_to_row(north, &src_w) - 0.5) - 1; int r1 = (int)floor(G_northing_to_row(south, &src_w) - 0.5) + 3; double west = G_col_to_easting(0.5, &dst_w); double east = G_col_to_easting(dst_w.cols - 0.5, &dst_w); int c0 = (int)floor(G_easting_to_col(west, &src_w) - 0.5) - 1; int c1 = (int)floor(G_easting_to_col(east, &src_w) - 0.5) + 3; src_w.south -= src_w.ns_res * (r1 - src_w.rows); src_w.north += src_w.ns_res * (-r0); src_w.west -= src_w.ew_res * (-c0); src_w.east += src_w.ew_res * (c1 - src_w.cols); src_w.rows = r1 - r0; src_w.cols = c1 - c0; } G_set_window(&src_w); /* allocate buffers for input rows */ for (row = 0; row < neighbors; row++) bufs[row] = G_allocate_d_raster_buf(); cur_row = -100; /* open old map */ infile = G_open_cell_old(rastin->answer, inmap); if (infile < 0) G_fatal_error(_("Unable to open raster map <%s>"), rastin->answer); /* reset window to current region */ G_set_window(&dst_w); outbuf = G_allocate_d_raster_buf(); /* open new map */ outfile = G_open_raster_new(rastout->answer, DCELL_TYPE); if (outfile < 0) G_fatal_error(_("Unable to create raster map <%s>"), rastout->answer); G_suppress_warnings(1); /* otherwise get complaints about window changes */ switch (neighbors) { case 1: /* nearest */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f + 0.5); G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f + 0.5); double c = bufs[0][mapcol0]; if (G_is_d_null_value(&c)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = c; } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 2: /* bilinear */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f); double v = maprow_f - maprow0; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f); int mapcol1 = mapcol0 + 1; double u = mapcol_f - mapcol0; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bilinear(u, v, c00, c01, c10, c11); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 4: /* bicubic */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow1 = (int)floor(maprow_f); int maprow0 = maprow1 - 1; double v = maprow_f - maprow1; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol1 = (int)floor(mapcol_f); int mapcol0 = mapcol1 - 1; int mapcol2 = mapcol1 + 1; int mapcol3 = mapcol1 + 2; double u = mapcol_f - mapcol1; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c02 = bufs[0][mapcol2]; double c03 = bufs[0][mapcol3]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; double c12 = bufs[1][mapcol2]; double c13 = bufs[1][mapcol3]; double c20 = bufs[2][mapcol0]; double c21 = bufs[2][mapcol1]; double c22 = bufs[2][mapcol2]; double c23 = bufs[2][mapcol3]; double c30 = bufs[3][mapcol0]; double c31 = bufs[3][mapcol1]; double c32 = bufs[3][mapcol2]; double c33 = bufs[3][mapcol3]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c02) || G_is_d_null_value(&c03) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11) || G_is_d_null_value(&c12) || G_is_d_null_value(&c13) || G_is_d_null_value(&c20) || G_is_d_null_value(&c21) || G_is_d_null_value(&c22) || G_is_d_null_value(&c23) || G_is_d_null_value(&c30) || G_is_d_null_value(&c31) || G_is_d_null_value(&c32) || G_is_d_null_value(&c33)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bicubic(u, v, c00, c01, c02, c03, c10, c11, c12, c13, c20, c21, c22, c23, c30, c31, c32, c33); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; } G_percent(dst_w.rows, dst_w.rows, 2); G_close_cell(infile); G_close_cell(outfile); /* record map metadata/history info */ sprintf(title, "Resample by %s interpolation", method->answer); G_put_cell_title(rastout->answer, title); G_short_history(rastout->answer, "raster", &history); strncpy(history.datsrc_1, rastin->answer, RECORD_LEN); history.datsrc_1[RECORD_LEN - 1] = '\0'; /* strncpy() doesn't null terminate if maxfill */ G_format_resolution(src_w.ns_res, buf_nsres, src_w.proj); G_format_resolution(src_w.ew_res, buf_ewres, src_w.proj); sprintf(history.datsrc_2, "Source map NS res: %s EW res: %s", buf_nsres, buf_ewres); G_command_history(&history); G_write_history(rastout->answer, &history); /* copy color table from source map */ if (G_read_colors(rastin->answer, inmap, &colors) < 0) G_fatal_error(_("Unable to read color table for %s"), rastin->answer); G_mark_colors_as_fp(&colors); if (G_write_colors(rastout->answer, G_mapset(), &colors) < 0) G_fatal_error(_("Unable to write color table for %s"), rastout->answer); return (EXIT_SUCCESS); }
/* * main function */ int main(int argc, char *argv[]) { /* struct Cell_head window; database window */ struct Cell_head cellhd; /* it stores region information, and header information of rasters */ char *name, *name2; /* input raster name */ char *result; /* output raster name */ char *mapset; /* mapset name */ void *inrast; /* input buffer */ unsigned char *outrast; /* output buffer */ int nrows, ncols; int row, col; int infd, outfd; /* file descriptor */ int verbose; struct History history; /* holds meta-data (title, comments,..) */ struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output, *input2; /* options */ FILE *in; /*file for Path loss factors*/ struct Flag *flag1; /* flags */ char buffer_out[1000]; strcpy(buffer_out,getenv("GISBASE")); strcat(buffer_out,"/etc/radio_coverage/lossfactors_new.txt"); /*G_message(_("1!! Pot1: %s, Pot2: %s"), buffer_path, buffer_path1); buffer_out=strcat(buffer_path,buffer_path1); G_message(_("Pot1: %s, Pot2: %s"), buffer_path, buffer_path1);*/ /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); module->keywords = _("raster, clutter"); module->description = _("Clutter convert module"); /* Define the different options as defined in gis.h */ input = G_define_standard_option(G_OPT_R_INPUT); input2 = G_define_standard_option(G_OPT_F_INPUT); input2->key = "Path_loss_values"; input2->type = TYPE_STRING; input2->required = YES; input2->answer = buffer_out;//getenv("GISBASE"); //strcat(buffer_path1,(char *)getenv("GISBASE"));//,"/etc/radio_coverage"); input2->gisprompt = "old_file,file,input"; input2->description = _("Path loss factors for land usage"); /* Define the different flags */ flag1 = G_define_flag(); flag1->key = 'o'; flag1->description = _("Old_Cipher"); output = G_define_standard_option(G_OPT_R_OUTPUT); /* options and flags parser */ if (G_parser(argc, argv)) { exit(EXIT_FAILURE); } /*G_message(_("1!! Pot1: %s, Pot2: %s"), buffer_path, buffer_path1); strcat(buffer_path,buffer_path1); G_message(_("Pot1: %s, Pot2: %s"), buffer_path, buffer_path1); input2->answer = buffer_path;//getenv("GISBASE"); //strcat(buffer_path1,(char *)getenv("GISBASE"));//,"/etc/radio_coverage");*/ /* stores options and flags to variables */ name = input->answer; name2 = input2->answer; result = output->answer; verbose = (flag1->answer); G_message(_("Verbose: %d"),verbose); /* returns NULL if the map was not found in any mapset, * mapset name otherwise */ //G_message(_("3_START")); mapset = G_find_cell2(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); if (G_legal_filename(result) < 0) G_fatal_error(_("<%s> is an illegal file name"), result); /* G_open_cell_old - returns file destriptor (>0) */ if ((infd = G_open_cell_old(name, mapset)) < 0) G_fatal_error(_("Unable to open raster map <%s>"), name); /* controlling, if we can open input raster */ if (G_get_cellhd(name, mapset, &cellhd) < 0) G_fatal_error(_("Unable to read file header of <%s>"), name); G_debug(3, "number of rows %d", cellhd.rows); G_set_window(&cellhd); G_get_set_window(&cellhd); /* Allocate input buffer */ inrast = G_allocate_raster_buf(FCELL_TYPE); /* Allocate output buffer, use input map data_type */ nrows = G_window_rows(); ncols = G_window_cols(); outrast = G_allocate_raster_buf(FCELL_TYPE); G_message(_("nrows %d and ncols %d"),nrows,ncols); /* controlling, if we can write the raster */ if ((outfd = G_open_raster_new(result, FCELL_TYPE)) < 0) G_fatal_error(_("Unable to create raster map <%s>"), result); /* do Clutter Convert */ /* open file for model tuning parameters*/ char fileName[150]; strcpy (fileName, name2); //G_message(_("Path: %s"),name2); if( (in = fopen(fileName,"r")) == NULL ) G_fatal_error(_("Unable to open file <%s>"), fileName); char buffer[256]; double terr_path_loss[100]; int counter=0; fgets (buffer, 250, in); while(fgets(buffer,250,in)!=NULL){ sscanf(buffer,"%lf %lf", &terr_path_loss[counter]); counter++; } int cipher_cont=0; /* old or new clutter */ if (verbose == 1) { cipher_cont=1; G_message(_("Parameter UR: %f, GP: %f, RP: %f, GI: %f, GL: %f, GM: %f, GR: %f, VO: %f, KM: %f, OD: %f"), terr_path_loss[0], terr_path_loss[1], terr_path_loss[2], terr_path_loss[3], terr_path_loss[4], terr_path_loss[5], terr_path_loss[6], terr_path_loss[7], terr_path_loss[8], terr_path_loss[9]); } else if (verbose == 0) { cipher_cont=11; G_message(_("Parameter UR1: %f, UR2: %f, UR3: %f, UR4: %f, UR5: %f, GI: %f, GL: %f, GM: %f, GR: %f, VO: %f, KM: %f, OD: %f"), terr_path_loss[0], terr_path_loss[1], terr_path_loss[2], terr_path_loss[3], terr_path_loss[4], terr_path_loss[5], terr_path_loss[6], terr_path_loss[7], terr_path_loss[8], terr_path_loss[9], terr_path_loss[10], terr_path_loss[11]); } G_message(_("Counter: %d"),counter); G_message(_("cipher_cont: %d"),cipher_cont); G_message(_("START")); /* for each row */ for (row = 0; row < nrows; row++) { FCELL f_in, f_out; /* read input map */ if (G_get_raster_row(infd, inrast, row, FCELL_TYPE) < 0) G_fatal_error(_("Unable to read raster map <%s> row %d"), name, row); /* process the data */ for (col = 0; col < ncols; col++) { f_in = ((FCELL *) inrast)[col]; //G_message(_("Input data: %d"),(int)f_in); f_out = terr_path_loss[(int)f_in-cipher_cont]; //G_message(_("Output data: %f"),(double)f_out); ((FCELL *) outrast)[col] = f_out; } /* write raster row to output raster map */ if (G_put_raster_row(outfd, outrast, FCELL_TYPE) < 0) G_fatal_error(_("Failed writing raster map <%s>"), result); } //G_message(_("END_clutconvert_test")); G_message(_("END")); /* memory cleanup */ G_free(inrast); G_free(outrast); /* closing raster maps */ G_close_cell(infd); G_close_cell(outfd); /* add command line incantation to history file */ G_short_history(result, "raster", &history); G_command_history(&history); G_write_history(result, &history); exit(EXIT_SUCCESS); }
char GRASS_LIB_EXPORT *G_find_cell( char * name, const char * mapset ) { // Not really sure about differences between G_find_cell and G_find_cell2 return G_find_cell2( name, mapset ); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Option *base, *cover, *output; } parm; char *basemap, *base_mapset; char *covermap, *cover_mapset; char *outmap; char command[1024]; struct Categories cover_cats; FILE *stats, *reclass; int first; long basecat, covercat, catb, catc; double value, max; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Finds the mode of values in a cover map within " "areas assigned the same category value in a " "user-specified base map."); parm.base = G_define_option(); parm.base->key = "base"; parm.base->description = _("Base map to be reclassified"); parm.base->required = YES; parm.base->type = TYPE_STRING; parm.base->gisprompt = "old,cell,raster"; parm.cover = G_define_option(); parm.cover->key = "cover"; parm.cover->description = _("Coverage map"); parm.cover->required = YES; parm.cover->type = TYPE_STRING; parm.cover->gisprompt = "old,cell,raster"; parm.output = G_define_option(); parm.output->key = "output"; parm.output->description = _("Output map"); parm.output->required = YES; parm.output->type = TYPE_STRING; parm.output->gisprompt = "new,cell,raster"; if (G_parser(argc, argv)) exit(1); basemap = parm.base->answer; covermap = parm.cover->answer; outmap = parm.output->answer; base_mapset = G_find_cell2(basemap, ""); if (base_mapset == NULL) { G_fatal_error(_("%s: base raster map not found"), basemap); } cover_mapset = G_find_cell2(covermap, ""); if (cover_mapset == NULL) { G_fatal_error(_("%s: cover raster map not found"), covermap); } if (G_legal_filename(outmap) < 0) { G_fatal_error(_("<%s> is an illegal file name"), outmap); } if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0) { G_fatal_error(_("%s: base map and output map must be different"), outmap); } if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0) { G_fatal_error(_("%s: Unable to read category labels"), covermap); } strcpy(command, "r.stats -an \""); strcat(command, G_fully_qualified_name(basemap, base_mapset)); strcat(command, ","); strcat(command, G_fully_qualified_name(covermap, cover_mapset)); strcat(command, "\""); /* printf(command); */ stats = popen(command, "r"); sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); /* printf(command); */ reclass = popen(command, "w"); first = 1; while (read_stats(stats, &basecat, &covercat, &value)) { if (first) { first = 0; catb = basecat; catc = covercat; max = value; } if (basecat != catb) { write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats)); catb = basecat; catc = covercat; max = value; } if (value > max) { catc = covercat; max = value; } } if (first) { catb = catc = 0; } write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats)); pclose(stats); pclose(reclass); exit(0); }
/*! \brief Load raster map as integer map Calling function must have already allocated space in buff for wind->rows * wind->cols unsigned chars. This routine simply loads the map into a 2d array by repetitve calls to get_map_row. Since signs of chars can be tricky, we only load positive chars between 0-255. \todo fn body Gs_loadmap_as_float() \param wind current window \param map_name raster map name \param[out] buff data buffer \param[out] nullmap null map buffer \param[out] has_null indicates if raster map contains null-data \return 1 on success \return -1 on failure \return -2 if read ok, but 1 or more values were too large (small) to fit into an unsigned char. (in which case the max (min) char is used) */ int Gs_loadmap_as_char(struct Cell_head *wind, const char *map_name, unsigned char *buff, struct BM *nullmap, int *has_null) { FILEDESC cellfile; const char *map_set; char *nullflags; int *ti, *tmp_buf; int offset, row, col, val, max_char, overflow, charsize, bitplace; unsigned char *tc; G_debug(3, "Gs_loadmap_as_char"); overflow = 0; charsize = 8 * sizeof(unsigned char); /* 0 bits for sign! */ max_char = 1; for (bitplace = 0; bitplace < charsize; ++bitplace) { max_char *= 2; } max_char -= 1; map_set = G_find_cell2(map_name, ""); if (!map_set) { G_warning(_("Raster map <%s> not found"), map_name); return -1; } *has_null = 0; nullflags = G_allocate_null_buf(); /* G_fatal_error */ if (!nullflags) { G_fatal_error(_("Unable to allocate memory for a null buffer")); } if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) { G_fatal_error(_("Unable to open raster map <%s>"), map_name); } tmp_buf = (int *)G_malloc(wind->cols * sizeof(int)); /* G_fatal_error */ if (!tmp_buf) { return -1; } G_message(_("Loading raster map <%s>..."), G_fully_qualified_name(map_name, map_set)); for (row = 0; row < wind->rows; row++) { offset = row * wind->cols; G_get_c_raster_row(cellfile, tmp_buf, row); G_get_null_value_row(cellfile, nullflags, row); tc = (unsigned char *)&(buff[offset]); ti = tmp_buf; G_percent(row, wind->rows, 2); for (col = 0; col < wind->cols; col++) { if (nullflags[col]) { *has_null = 1; BM_set(nullmap, col, row, 1); } else { val = *ti; if (val > max_char) { overflow = 1; *tc = (unsigned char)max_char; } else if (val < 0) { overflow = 1; *tc = 0; } else { *tc = (unsigned char)val; } } ti++; tc++; } } G_percent(1, 1, 1); G_close_cell(cellfile); G_free(tmp_buf); G_free(nullflags); return (overflow ? -2 : 1); }
/*! \brief Load raster map as integer map Calling function must have already allocated space in buff for wind->rows * wind->cols shorts. This routine simply loads the map into a 2d array by repetitve calls to get_map_row. \param wind current window \param map_name raster map name \param[out] buff data buffer \param[out] nullmap null map buffer \param[out] has_null indicates if raster map contains null-data \return 1 on success \return -1 on failure, \return -2 if read ok, but 1 or more values were too large (small) to fit into a short (in which case the max (min) short is used) */ int Gs_loadmap_as_short(struct Cell_head *wind, const char *map_name, short *buff, struct BM *nullmap, int *has_null) { FILEDESC cellfile; const char *map_set; char *nullflags; int *ti, *tmp_buf; int offset, row, col, val, max_short, overflow, shortsize, bitplace; short *ts; G_debug(3, "Gs_loadmap_as_short"); overflow = 0; shortsize = 8 * sizeof(short); /* 1 bit for sign */ /* same as 2 << (shortsize-1) */ for (max_short = bitplace = 1; bitplace < shortsize; ++bitplace) { max_short *= 2; } max_short -= 1; map_set = G_find_cell2(map_name, ""); if (!map_set) { G_warning(_("Raster map <%s> not found"), map_name); return -1; } *has_null = 0; nullflags = G_allocate_null_buf(); if (!nullflags) { G_fatal_error(_("Unable to allocate memory for a null buffer")); } if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) { G_fatal_error(_("Unable to open raster map <%s>"), map_name); } tmp_buf = (int *)G_malloc(wind->cols * sizeof(int)); /* G_fatal_error */ if (!tmp_buf) { return -1; } G_message(_("Loading raster map <%s>..."), G_fully_qualified_name(map_name, map_set)); for (row = 0; row < wind->rows; row++) { offset = row * wind->cols; G_get_c_raster_row(cellfile, tmp_buf, row); G_get_null_value_row(cellfile, nullflags, row); G_percent(row, wind->rows, 2); ts = &(buff[offset]); ti = tmp_buf; for (col = 0; col < wind->cols; col++) { if (nullflags[col]) { *has_null = 1; BM_set(nullmap, col, row, 1); } else { val = *ti; if (abs(val) > max_short) { overflow = 1; /* assign floor/ceiling value? */ *ts = (short)(max_short * val / abs(val)); } else { *ts = (short)val; } } ti++; ts++; } } G_percent(1, 1, 1); G_close_cell(cellfile); G_free(tmp_buf); G_free(nullflags); return (overflow ? -2 : 1); }
/*! \brief Get map data type \param filename raster map name \param negflag \return -1 if map is integer and G_read_range() fails \return data type (ARRY_*) */ int Gs_numtype(const char *filename, int *negflag) { CELL max = 0, min = 0; struct Range range; const char *mapset; int shortbits, charbits, bitplace; static int max_short, max_char; static int first = 1; if (first) { max_short = max_char = 1; shortbits = 8 * sizeof(short); for (bitplace = 1; bitplace < shortbits; ++bitplace) { /*1 bit for sign */ max_short *= 2; } max_short -= 1; /* NO bits for sign, using unsigned char */ charbits = 8 * sizeof(unsigned char); for (bitplace = 0; bitplace < charbits; ++bitplace) { max_char *= 2; } max_char -= 1; first = 0; } mapset = G_find_cell2(filename, ""); if (!mapset) { G_warning(_("Raster map <%s> not found"), filename); return -1; } if (G_raster_map_is_fp(filename, mapset)) { G_debug(3, "Gs_numtype(): fp map detected"); return (ATTY_FLOAT); } if (-1 == G_read_range(filename, mapset, &range)) { return (-1); } G_get_range_min_max(&range, &min, &max); *negflag = (min < 0); if (max < max_char && min > 0) { return (ATTY_CHAR); } if (max < max_short && min > -max_short) { return (ATTY_SHORT); } return (ATTY_INT); }
void* raster2array(const char* name, struct Cell_head* header, int* rows, int* cols, RASTER_MAP_TYPE out_type) { // Open the raster map and load the dem // for simplicity sake, the dem will be an array of // doubles, converted from any possible GRASS CELL type. char* mapset = G_find_cell2(name, ""); if (mapset == NULL) G_fatal_error("Raster map <%s> not found", name); // Find out the cell type of the DEM RASTER_MAP_TYPE type = G_raster_map_type(name, mapset); // Get a file descriptor for the DEM raster map int infd; if ((infd = G_open_cell_old(name, mapset)) < 0) G_fatal_error("Unable to open raster map <%s>", name); // Get header info for the DEM raster map struct Cell_head cellhd; if (G_get_cellhd(name, mapset, &cellhd) < 0) G_fatal_error("Unable to open raster map <%s>", name); // Create a GRASS buffer for the DEM raster void* inrast = G_allocate_raster_buf(type); // Get the max rows and max cols from the window information, since the // header gives the values for the full raster const int maxr = G_window_rows(); const int maxc = G_window_cols(); // Read in the raster line by line, copying it into the double array // rast for return. void* rast; switch (out_type) { case CELL_TYPE: rast = (int*) calloc(maxr * maxc, sizeof(int)); break; case FCELL_TYPE: rast = (float*) calloc(maxr * maxc, sizeof(float)); break; case DCELL_TYPE: rast = (double*) calloc(maxr * maxc, sizeof(double)); break; } if (rast == NULL) { G_fatal_error("Unable to allocate memory for raster map <%s>", name); } int row, col; for (row = 0; row < maxr; ++row) { if (G_get_raster_row(infd, inrast, row, type) < 0) G_fatal_error("Unable to read raster map <%s> row %d", name, row); for (col = 0; col < maxc; ++col) { int index = col + row * maxc; if (out_type == CELL_TYPE) { switch (type) { case CELL_TYPE: ((int*) rast)[index] = ((int *) inrast)[col]; break; case FCELL_TYPE: ((int*) rast)[index] = (int) ((float *) inrast)[col]; break; case DCELL_TYPE: ((int*) rast)[index] = (int) ((double *) inrast)[col]; break; default: G_fatal_error("Unknown cell type"); break; } } if (out_type == FCELL_TYPE) { switch (type) { case CELL_TYPE: ((float*) rast)[index] = (float) ((int *) inrast)[col]; break; case FCELL_TYPE: ((float*) rast)[index] = ((float *) inrast)[col]; break; case DCELL_TYPE: ((float*) rast)[index] = (float) ((double *) inrast)[col]; break; default: G_fatal_error("Unknown cell type"); break; } } if (out_type == DCELL_TYPE) { switch (type) { case CELL_TYPE: ((double*) rast)[index] = (double) ((int *) inrast)[col]; break; case FCELL_TYPE: ((double*) rast)[index] = (double) ((float *) inrast)[col]; break; case DCELL_TYPE: ((double*) rast)[index] = ((double *) inrast)[col]; break; default: G_fatal_error("Unknown cell type"); break; } } } } // Return cellhd, maxr, and maxc by pointer if (header != NULL) *header = cellhd; if (rows != NULL) *rows = maxr; if (cols != NULL) *cols = maxc; return rast; }
int main(int argc, char *argv[]) { char *terrainmap, *seedmap, *lakemap, *mapset; int rows, cols, in_terran_fd, out_fd, lake_fd, row, col, pases, pass; int lastcount, curcount, start_col = 0, start_row = 0; double east, north, area = 0, volume = 0; FCELL **in_terran, **out_water, water_level, max_depth = 0, min_depth = 0; FCELL water_window[3][3]; struct Option *tmap_opt, *smap_opt, *wlvl_opt, *lake_opt, *sdxy_opt; struct Flag *negative_flag, *overwrite_flag; struct GModule *module; struct Colors colr; struct Cell_head window; struct History history; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, hydrology"); module->description = _("Fills lake at given point to given level."); tmap_opt = G_define_option(); tmap_opt->key = "dem"; tmap_opt->key_desc = "name"; tmap_opt->description = _("Name of terrain raster map (DEM)"); tmap_opt->type = TYPE_STRING; tmap_opt->gisprompt = "old,cell,raster"; tmap_opt->required = YES; wlvl_opt = G_define_option(); wlvl_opt->key = "wl"; wlvl_opt->description = _("Water level"); wlvl_opt->type = TYPE_DOUBLE; wlvl_opt->required = YES; lake_opt = G_define_option(); lake_opt->key = "lake"; lake_opt->key_desc = "name"; lake_opt->description = _("Name for output raster map with lake"); lake_opt->type = TYPE_STRING; lake_opt->gisprompt = "new,cell,raster"; lake_opt->required = NO; sdxy_opt = G_define_option(); sdxy_opt->key = "xy"; sdxy_opt->description = _("Seed point coordinates"); sdxy_opt->type = TYPE_DOUBLE; sdxy_opt->key_desc = "east,north"; sdxy_opt->required = NO; sdxy_opt->multiple = NO; smap_opt = G_define_option(); smap_opt->key = "seed"; smap_opt->key_desc = "name"; smap_opt->description = _("Name of raster map with given starting point(s) (at least 1 cell > 0)"); smap_opt->type = TYPE_STRING; smap_opt->gisprompt = "old,cell,raster"; smap_opt->required = NO; negative_flag = G_define_flag(); negative_flag->key = 'n'; negative_flag->description = _("Use negative depth values for lake raster map"); overwrite_flag = G_define_flag(); overwrite_flag->key = 'o'; overwrite_flag->description = _("Overwrite seed map with result (lake) map"); if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */ exit(EXIT_FAILURE); if (smap_opt->answer && sdxy_opt->answer) G_fatal_error(_("Both seed map and coordinates cannot be specified")); if (!smap_opt->answer && !sdxy_opt->answer) G_fatal_error(_("Seed map or seed coordinates must be set!")); if (sdxy_opt->answer && !lake_opt->answer) G_fatal_error(_("Seed coordinates and output map lake= must be set!")); if (lake_opt->answer && overwrite_flag->answer) G_fatal_error(_("Both lake and overwrite cannot be specified")); if (!lake_opt->answer && !overwrite_flag->answer) G_fatal_error(_("Output lake map or overwrite flag must be set!")); terrainmap = tmap_opt->answer; seedmap = smap_opt->answer; sscanf(wlvl_opt->answer, "%f", &water_level); lakemap = lake_opt->answer; /* If lakemap is set, write to it, else is set overwrite flag and we should write to seedmap. */ if (lakemap) { lake_fd = G_open_raster_new(lakemap, 1); if (lake_fd < 0) G_fatal_error(_("Unable to create raster map <%s>"), lakemap); } rows = G_window_rows(); cols = G_window_cols(); /* If we use x,y as seed... */ if (sdxy_opt->answer) { G_get_window(&window); east = window.east; north = window.north; G_scan_easting(sdxy_opt->answers[0], &east, G_projection()); G_scan_northing(sdxy_opt->answers[1], &north, G_projection()); start_col = (int)G_easting_to_col(east, &window); start_row = (int)G_northing_to_row(north, &window); if (start_row < 0 || start_row > rows || start_col < 0 || start_col > cols) G_fatal_error(_("Seed point outside the current region")); } /* Open terran map */ mapset = G_find_cell2(terrainmap, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), terrainmap); in_terran_fd = G_open_cell_old(terrainmap, mapset); if (in_terran_fd < 0) G_fatal_error(_("Unable to open raster map <%s>"), G_fully_qualified_name(terrainmap, mapset)); /* Open seed map */ if (smap_opt->answer) { mapset = G_find_cell2(seedmap, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), seedmap); out_fd = G_open_cell_old(seedmap, mapset); if (out_fd < 0) G_fatal_error(_("Unable to open raster map <%s>"), G_fully_qualified_name(seedmap, mapset)); } /* Pointers to rows. Row = ptr to 'col' size array. */ in_terran = (FCELL **) G_malloc(rows * sizeof(FCELL *)); out_water = (FCELL **) G_malloc(rows * sizeof(FCELL *)); if (in_terran == NULL || out_water == NULL) G_fatal_error(_("G_malloc: out of memory")); G_debug(1, "Loading maps..."); /* foo_rows[row] == array with data (2d array). */ for (row = 0; row < rows; row++) { in_terran[row] = (FCELL *) G_malloc(cols * sizeof(FCELL)); out_water[row] = (FCELL *) G_calloc(cols, sizeof(FCELL)); /* In newly created space load data from file. */ if (G_get_f_raster_row(in_terran_fd, in_terran[row], row) != 1) G_fatal_error(_("Unable to read raster map <%s> row %d"), terrainmap, row); if (smap_opt->answer) if (G_get_f_raster_row(out_fd, out_water[row], row) != 1) G_fatal_error(_("Unable to read raster map <%s> row %d"), seedmap, row); G_percent(row + 1, rows, 5); } /* Set seed point */ if (sdxy_opt->answer) /* Check is water level higher than seed point */ if (in_terran[start_row][start_col] >= water_level) G_fatal_error(_("Given water level at seed point is below earth surface. " "Increase water level or move seed point.")); out_water[start_row][start_col] = 1; /* Close seed map for reading. */ if (smap_opt->answer) G_close_cell(out_fd); /* Open output map for writing. */ if (lakemap) { out_fd = lake_fd; } else { out_fd = G_open_raster_new(seedmap, 1); if (out_fd < 0) G_fatal_error(_("Unable to create raster map <%s>"), seedmap); } /* More pases are renudant. Real pases count is controled by altered cell count. */ pases = (int)(rows * cols) / 2; G_debug(1, "Starting lake filling at level of %8.4f in %d passes. Percent done:", water_level, pases); lastcount = 0; for (pass = 0; pass < pases; pass++) { G_debug(3, "Pass: %d", pass); curcount = 0; /* Move from left upper corner to right lower corner. */ for (row = 0; row < rows; row++) { for (col = 0; col < cols; col++) { /* Loading water data into window. */ load_window_values(out_water, water_window, rows, cols, row, col); /* Cheking presence of water. */ if (is_near_water(water_window) == 1) { if (in_terran[row][col] < water_level) { out_water[row][col] = water_level - in_terran[row][col]; curcount++; } else { out_water[row][col] = 0; /* Cell is higher than water level -> NULL. */ } } } } if (curcount == lastcount) break; /* We done. */ lastcount = curcount; curcount = 0; /* Move backwards - from lower right corner to upper left corner. */ for (row = rows - 1; row >= 0; row--) { for (col = cols - 1; col >= 0; col--) { load_window_values(out_water, water_window, rows, cols, row, col); if (is_near_water(water_window) == 1) { if (in_terran[row][col] < water_level) { out_water[row][col] = water_level - in_terran[row][col]; curcount++; } else { out_water[row][col] = 0; } } } } G_percent(pass + 1, pases, 10); if (curcount == lastcount) break; /* We done. */ lastcount = curcount; } /*pases */ G_percent(pases, pases, 10); /* Show 100%. */ save_map(out_water, out_fd, rows, cols, negative_flag->answer, &min_depth, &max_depth, &area, &volume); G_message(_("Lake depth from %f to %f"), min_depth, max_depth); G_message(_("Lake area %f square meters"), area); G_message(_("Lake volume %f cubic meters"), volume); G_warning(_("Volume is correct only if lake depth (terrain raster map) is in meters")); /* Close all files. Lake map gets written only now. */ G_close_cell(in_terran_fd); G_close_cell(out_fd); /* Add blue color gradient from light bank to dark depth */ G_init_colors(&colr); if (negative_flag->answer == 1) { G_add_f_raster_color_rule(&max_depth, 0, 240, 255, &min_depth, 0, 50, 170, &colr); } else { G_add_f_raster_color_rule(&min_depth, 0, 240, 255, &max_depth, 0, 50, 170, &colr); } if (G_write_colors(lakemap, G_mapset(), &colr) != 1) G_fatal_error(_("Unable to read color file of raster map <%s>"), lakemap); G_short_history(lakemap, "raster", &history); G_command_history(&history); G_write_history(lakemap, &history); return EXIT_SUCCESS; }
/*! \brief Get categories/labels Formats label as in d.what.rast -> (catval) catlabel \param filename raster map name \param drow \param dcol \param catstr category string \return 1 on success \return 0 on failure */ int Gs_get_cat_label(const char *filename, int drow, int dcol, char *catstr) { struct Categories cats; const char *mapset; CELL *buf; DCELL *dbuf; RASTER_MAP_TYPE map_type; int fd; if ((mapset = G_find_cell2(filename, "")) == NULL) { G_warning(_("Raster map <%s> not found"), filename); return 0; } if (-1 != G_read_cats(filename, mapset, &cats)) { fd = G_open_cell_old(filename, mapset); map_type = G_get_raster_map_type(fd); if (map_type == CELL_TYPE) { buf = G_allocate_c_raster_buf(); if (G_get_c_raster_row(fd, buf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_c_null_value(&buf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_c_raster_cat(&buf[dcol], &cats)); } else { sprintf(catstr, "(%d) %s", buf[dcol], G_get_c_raster_cat(&buf[dcol], &cats)); } G_free(buf); } else { /* fp map */ dbuf = G_allocate_d_raster_buf(); if (G_get_d_raster_row(fd, dbuf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_d_null_value(&dbuf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_d_raster_cat(&dbuf[dcol], &cats)); } else { sprintf(catstr, "(%g) %s", dbuf[dcol], G_get_d_raster_cat(&dbuf[dcol], &cats)); } G_free(dbuf); } } else { strcpy(catstr, "no category label"); } /* TODO: may want to keep these around for multiple queries */ G_free_cats(&cats); G_close_cell(fd); return (1); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Option *base, *cover, *output; } parm; char *basemap, *base_mapset; char *covermap, *cover_mapset; char *outmap; char command[1024]; struct Categories cover_cats; FILE *stats_fd, *reclass_fd; int first; long basecat, covercat, catb, catc; double area; struct stats stats; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Finds the median of values in a cover map within " "areas assigned the same category value in a " "user-specified base map."); parm.base = G_define_standard_option(G_OPT_R_INPUT); parm.base->key = "base"; parm.base->description = _("Name of base raster map"); parm.cover = G_define_standard_option(G_OPT_R_INPUT); parm.cover->key = "cover"; parm.cover->description = _("Name of cover raster map"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); if (G_parser(argc, argv)) exit(EXIT_FAILURE); basemap = parm.base->answer; covermap = parm.cover->answer; outmap = parm.output->answer; base_mapset = G_find_cell2(basemap, ""); if (base_mapset == NULL) G_fatal_error(_("Base raster map <%s> not found"), basemap); cover_mapset = G_find_cell2(covermap, ""); if (cover_mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), covermap); if (G_legal_filename(outmap) < 0) G_fatal_error(_("<%s> is an illegal file name"), outmap); if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0) G_fatal_error(_("Base map and output map <%s> must be different"), outmap); if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0) G_fatal_error(_("Unable to read category labels of raster map <%s>"), covermap); strcpy(command, "r.stats -an \""); strcat(command, G_fully_qualified_name(basemap, base_mapset)); strcat(command, ","); strcat(command, G_fully_qualified_name(covermap, cover_mapset)); strcat(command, "\""); /* strcpy (command,"cat /tmp/t"); */ G_debug(3, "command: %s", command); stats_fd = popen(command, "r"); G_debug(3, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); reclass_fd = popen(command, "w"); first = 1; while (read_stats(stats_fd, &basecat, &covercat, &area)) { if (first) { stats.n = 0; stats.nalloc = 16; stats.cat = (long *) G_calloc(stats.nalloc, sizeof(long)); stats.area = (double *) G_calloc(stats.nalloc, sizeof(double)); first = 0; catb = basecat; } if (basecat != catb) { catc = median(&stats); write_reclass(reclass_fd, catb, catc, G_get_cat(catc, &cover_cats)); catb = basecat; stats.n = 0; } stats.n++; if (stats.n > stats.nalloc) { stats.nalloc *= 2; stats.cat = (long *) G_realloc(stats.cat, stats.nalloc * sizeof(long)); stats.area = (double *) G_realloc(stats.area, stats.nalloc * sizeof(double)); } stats.cat[stats.n - 1] = covercat; stats.area[stats.n - 1] = area; } if (!first) { catc = median(&stats); write_reclass(reclass_fd, catb, catc, G_get_cat(catc, &cover_cats)); } pclose(stats_fd); pclose(reclass_fd); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct Cell_head cellhd; struct Range range; char *name; /* input raster name */ char *result; /* output raster name */ char *mapset; /* mapset name */ DCELL *inrast; /* input buffer */ DCELL *outrast; /* output buffer */ int row,col; int infd, outfd; /* file descriptor */ int verbose; double *weights; /* array of weights */ DCELL **D_rows; DCELL *tmp; int nrows; int ncols; double min, max; /* raster map range */ RASTER_MAP_TYPE data_type; /* type of the map */ void *values; /* neighborhood values */ int n,i; /* number of neighborhood cells */ int size; /* matrix size */ double ssigma; /* sigma of the spatial part */ double csigma; /* sigma of the color part */ char title[1024]; /* map title */ struct GModule *module; /* GRASS module for parsing arguments */ struct { struct Option *input, *output; struct Option *sigma_s, *sigma_c, *size; struct Option *title; } parm; struct { struct Flag *quiet; struct Flag *print_sigmas; } flag; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name */ /* initialize module */ module = G_define_module(); module->description = ("Gaussian filter for raster maps."); /* Define the different options */ parm.input = G_define_option() ; parm.input->key = "input"; parm.input->type = TYPE_STRING; parm.input->required = YES; parm.input->description= ("Name of an input layer" ); parm.output = G_define_option() ; parm.output->key = "output"; parm.output->type = TYPE_STRING; parm.output->required = YES; parm.output->description= ("Name of an output layer"); parm.sigma_s = G_define_option() ; parm.sigma_s->key = "ssigma"; parm.sigma_s->type = TYPE_DOUBLE; parm.sigma_s->required = NO; parm.sigma_s->description= ("Sigma for space part of the filter\n\t(default: 0.465*((size-1)/2)"); parm.sigma_c = G_define_option() ; parm.sigma_c->key = "csigma"; parm.sigma_c->type = TYPE_DOUBLE; parm.sigma_c->required = NO; parm.sigma_c->description= ("Sigma for color part of the filter\n(default: 0.465*((color_range)/2)"); parm.size = G_define_option() ; parm.size->key = "size"; parm.size->type = TYPE_INTEGER; parm.size->required = YES; parm.size->description= ("Size of the matrix (odd number)"); flag.print_sigmas = G_define_flag() ; flag.print_sigmas->key = 's' ; flag.print_sigmas->description = "Print calculated values for sigmas" ; flag.quiet = G_define_flag() ; flag.quiet->key = 'q' ; flag.quiet->description = "Quiet" ; /* options and flags pareser */ if (G_parser(argc, argv)) exit (-1); /* stores options and flags to variables */ name = parm.input->answer; result = parm.output->answer; verbose = (! flag.quiet->answer); sscanf(parm.size->answer, "%d", &size); if (!parm.sigma_s->answer) ssigma = 0.465*((size-1)/2); else sscanf(parm.sigma_s->answer, "%lf", &ssigma); /* controlling the input values */ if (size%2 == 0) G_fatal_error("Size <%d> is not odd number", size); /* returs NULL if the map was not found in any mapset, * mapset name otherwise*/ mapset = G_find_cell2 (name, ""); if (mapset == NULL) G_fatal_error ("cell file [%s] not found", name); /* color sigma next */ if (!parm.sigma_c->answer) { if (G_read_range(name, mapset, &range) < 0) G_fatal_error("Could not read the raster map range"); /* for raster maps with range from 0-255 the result * should be around 60 */ min = (double)range.min; max = (double)range.max; csigma = 0.456*(max - min)/2; } else sscanf(parm.sigma_c->answer, "%lf", &csigma); /* print if appropriate */ if (flag.print_sigmas->answer) printf("Space sigma: %f\nColor sigma: %f\n", ssigma, csigma); if (G_legal_filename (result) < 0) G_fatal_error ("[%s] is an illegal name", result); /* count weights */ weights = (double *)malloc(size * size * sizeof(double)); /* stores values of gauss. bell into 'weigts'*/ count_weights(weights, size, ssigma); /* determine the inputmap type (CELL/FCELL/DCELL) */ data_type = G_raster_map_type(name, mapset); /* G_open_cell_old - returns file destriptor (>0) */ if ( (infd = G_open_cell_old (name, mapset)) < 0) G_fatal_error ("Cannot open cell file [%s]", name); /* controlling, if we can open input raster */ if (G_get_cellhd (name, mapset, &cellhd) < 0) G_fatal_error ("Cannot read file header of [%s]", name); /* Allocate input buffer */ inrast = G_allocate_raster_buf(data_type); /* Allocate output buffer, use input map data_type */ nrows = G_window_rows(); ncols = G_window_cols(); outrast = G_allocate_d_raster_buf(); /* Allocate values buffers */ values = (DCELL *) malloc(size * size * sizeof(DCELL)); /* allocating memory for rows */ D_rows = (DCELL **)malloc(size * sizeof(DCELL)); for (i = 0; i < size; i++) { D_rows[i] = G_allocate_raster_buf(DCELL_TYPE); } if (values == NULL) G_fatal_error("Cannot allocate memory"); /* controlling, if we can write the raster */ if ( (outfd = G_open_raster_new (result, data_type)) < 0) G_fatal_error ("Could not open <%s>",result); /* write first rows as NULL values */ for (row = 0; row < size/2; row++) { G_set_d_null_value(outrast, ncols); if (G_put_d_raster_row (outfd, outrast) < 0) G_fatal_error ("Cannot write to <%s>",result); } /* allocate first size/2 rows */ for (row = 0; row < size; row++) if (G_get_d_raster_row(infd, D_rows[row], row) < 0) G_fatal_error ("Could not open <%s>",result); /****************************************************************/ /* for each row inside the region */ for ( row = size/2; row < nrows - size/2; row++) { if (verbose) G_percent (row, nrows, 2); /* allocate new last row */ G_get_d_raster_row(infd, D_rows[size-1], row+(size/2)); /*process the data */ for (col=0; col < ncols; col++){ /* skip the outside columns */ if ( (col - size/2) < 0 || ncols <= (col + size/2)) { G_set_d_null_value(outrast, 1); } /* work only with columns, which are inside */ else { /* store values of the matrix into arry 'values', 'n' is * number of elements of the matrix */ n = D_gather(infd, values, D_rows, col, row,size); ((DCELL *)outrast)[col] = D_bilateral(values, ssigma, csigma, size, weights); } } /* for each column */ /* write raster row to output raster file */ G_put_d_raster_row (outfd, outrast); /* switch rows */ tmp = D_rows[0]; for (i = 0; i < size; i++){ D_rows[i] = D_rows[i + 1]; } D_rows[size-1] = tmp; } /* for each row */ /* write last rows as NULL values */ for (i = 0; i < size/2; i++) { G_set_d_null_value(outrast, ncols); G_put_d_raster_row (outfd, outrast); } /* memory cleaning */ G_free(outrast); G_free(inrast); G_free(values); for (i = 0; i < size; i++) { G_free(D_rows[i]); } free((void *) D_rows); /* closing rastr files */ G_close_cell (infd); G_close_cell (outfd); /* set the map title */ sprintf(title, "Bilateral filter of %s with %dx%d matrix: ssigma %.3f, csigma %.3f", name, size, size, ssigma, csigma ); G_put_cell_title (result, title ); return 0; }