static void load_input_raster3d_maps(struct Option *scalar_opt, struct Option *vector_opt, struct Gradient_info *gradient_info, RASTER3D_Region * region) { int i; if (scalar_opt->answer) { gradient_info->scalar_map = Rast3d_open_cell_old(scalar_opt->answer, G_find_raster3d(scalar_opt->answer, ""), region, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (!gradient_info->scalar_map) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), scalar_opt->answer); gradient_info->compute_gradient = TRUE; } else { for (i = 0; i < 3; i++) { gradient_info->velocity_maps[i] = Rast3d_open_cell_old(vector_opt->answers[i], G_find_raster3d(vector_opt->answers[i], ""), region, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (!gradient_info->velocity_maps[i]) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), vector_opt->answers[i]); } gradient_info->compute_gradient = FALSE; } }
static void check_vector_input_maps(struct Option *vector_opt, struct Option *seed_opt) { int i; /* Check for velocity components maps. */ if (vector_opt->answers != NULL) { for (i = 0; i < 3; i++) { if (vector_opt->answers[i] != NULL) { if (NULL == G_find_raster3d(vector_opt->answers[i], "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), vector_opt->answers[i]); } else { Rast3d_fatal_error(_("Please provide three 3D raster maps")); } } } if (seed_opt->answer != NULL) { if (NULL == G_find_vector2(seed_opt->answer, "")) G_fatal_error(_("Vector seed map <%s> not found"), seed_opt->answer); } }
/*! \brief Load color table \param[out] color_data color data buffer \param name 3D raster map name \return -1 on failure \return 1 on success */ int Gvl_load_colors_data(void **color_data, const char *name) { const char *mapset; struct Colors *colors; if (NULL == (mapset = G_find_raster3d(name, ""))) { G_warning(_("3D raster map <%s> not found"), name); return (-1); } if (NULL == (colors = (struct Colors *)G_malloc(sizeof(struct Colors)))) return (-1); if (0 > Rast3d_read_colors(name, mapset, colors)) { G_free(colors); return (-1); } *color_data = colors; return (1); }
/* ************************************************************************* */ int main(int argc, char *argv[]) { RASTER3D_Region region, inputmap_bounds; struct Cell_head region2d; struct GModule *module; struct History history; void *map = NULL; /*The 3D Rastermap */ int i = 0, changemask = 0; int *fd = NULL, output_type, cols, rows; char *RasterFileName; int overwrite = 0; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("conversion")); G_add_keyword(_("raster")); G_add_keyword(_("voxel")); module->description = _("Converts 3D raster maps to 2D raster maps"); /* Get parameters from user */ set_params(); /* Have GRASS get inputs */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_debug(3, "Open 3D raster map <%s>", param.input->answer); if (NULL == G_find_raster3d(param.input->answer, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), param.input->answer); /*Set the defaults */ Rast3d_init_defaults(); /*Set the resolution of the output maps */ if (param.res->answer) { /*Open the map with current region */ map = Rast3d_open_cell_old(param.input->answer, G_find_raster3d(param.input->answer, ""), RASTER3D_DEFAULT_WINDOW, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), param.input->answer); /*Get the region of the map */ Rast3d_get_region_struct_map(map, ®ion); /*set this region as current 3D window for map */ Rast3d_set_window_map(map, ®ion); /*Set the 2d region appropriate */ Rast3d_extract2d_region(®ion, ®ion2d); /*Make the new 2d region the default */ Rast_set_window(®ion2d); } else { /* Figure out the region from the map */ Rast3d_get_window(®ion); /*Open the 3d raster map */ map = Rast3d_open_cell_old(param.input->answer, G_find_raster3d(param.input->answer, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), param.input->answer); } /*Check if the g3d-region is equal to the 2D rows and cols */ rows = Rast_window_rows(); cols = Rast_window_cols(); /*If not equal, set the 3D window correct */ if (rows != region.rows || cols != region.cols) { G_message(_("The 2D and 3D region settings are different. " "Using the 2D window settings to adjust the 2D part of the 3D region.")); G_get_set_window(®ion2d); region.ns_res = region2d.ns_res; region.ew_res = region2d.ew_res; region.rows = region2d.rows; region.cols = region2d.cols; Rast3d_adjust_region(®ion); Rast3d_set_window_map(map, ®ion); } /* save the input map region for later use (history meta-data) */ Rast3d_get_region_struct_map(map, &inputmap_bounds); /*Get the output type */ output_type = Rast3d_file_type_map(map); /*prepare the filehandler */ fd = (int *) G_malloc(region.depths * sizeof (int)); if (fd == NULL) fatal_error(map, NULL, 0, _("Out of memory")); G_message(_("Creating %i raster maps"), region.depths); /*Loop over all output maps! open */ for (i = 0; i < region.depths; i++) { /*Create the outputmaps */ G_asprintf(&RasterFileName, "%s_%05d", param.output->answer, i + 1); G_message(_("Raster map %i Filename: %s"), i + 1, RasterFileName); overwrite = G_check_overwrite(argc, argv); if (G_find_raster2(RasterFileName, "") && !overwrite) G_fatal_error(_("Raster map %d Filename: %s already exists. Use the flag --o to overwrite."), i + 1, RasterFileName); if (output_type == FCELL_TYPE) fd[i] = open_output_map(RasterFileName, FCELL_TYPE); else if (output_type == DCELL_TYPE) fd[i] = open_output_map(RasterFileName, DCELL_TYPE); } /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask = 0; if (Rast3d_mask_is_off(map)) { Rast3d_mask_on(map); changemask = 1; } } } /*Create the Rastermaps */ g3d_to_raster(map, region, fd); /*Loop over all output maps! close */ for (i = 0; i < region.depths; i++) { close_output_map(fd[i]); /* write history */ G_asprintf(&RasterFileName, "%s_%i", param.output->answer, i + 1); G_debug(4, "Raster map %d Filename: %s", i + 1, RasterFileName); Rast_short_history(RasterFileName, "raster", &history); Rast_set_history(&history, HIST_DATSRC_1, "3D Raster map:"); Rast_set_history(&history, HIST_DATSRC_2, param.input->answer); Rast_append_format_history(&history, "Level %d of %d", i + 1, region.depths); Rast_append_format_history(&history, "Level z-range: %f to %f", region.bottom + (i * region.tb_res), region.bottom + (i + 1 * region.tb_res)); Rast_append_format_history(&history, "Input map full z-range: %f to %f", inputmap_bounds.bottom, inputmap_bounds.top); Rast_append_format_history(&history, "Input map z-resolution: %f", inputmap_bounds.tb_res); if (!param.res->answer) { Rast_append_format_history(&history, "GIS region full z-range: %f to %f", region.bottom, region.top); Rast_append_format_history(&history, "GIS region z-resolution: %f", region.tb_res); } Rast_command_history(&history); Rast_write_history(RasterFileName, &history); } /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(map) && changemask) Rast3d_mask_off(map); } /*Cleaning */ if (RasterFileName) G_free(RasterFileName); if (fd) G_free(fd); /* Close files and exit */ if (!Rast3d_close(map)) fatal_error(map, NULL, 0, _("Unable to close 3D raster map")); map = NULL; return (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct Option *vector_opt, *seed_opt, *flowlines_opt, *flowacc_opt, *sampled_opt, *scalar_opt, *unit_opt, *step_opt, *limit_opt, *skip_opt, *dir_opt, *error_opt; struct Flag *table_fl; struct GModule *module; RASTER3D_Region region; RASTER3D_Map *flowacc, *sampled; struct Integration integration; struct Seed seed; struct Gradient_info gradient_info; struct Map_info seed_Map; struct line_pnts *seed_points; struct line_cats *seed_cats; struct Map_info fl_map; struct line_cats *fl_cats; /* for flowlines */ struct line_pnts *fl_points; /* for flowlines */ struct field_info *finfo; dbDriver *driver; int cat; /* cat of flowlines */ int if_table; int i, r, c, d; char *desc; int n_seeds, seed_count, ltype; int skip[3]; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("hydrology")); G_add_keyword(_("voxel")); module->description = _("Computes 3D flow lines and 3D flow accumulation."); scalar_opt = G_define_standard_option(G_OPT_R3_INPUT); scalar_opt->required = NO; scalar_opt->guisection = _("Input"); vector_opt = G_define_standard_option(G_OPT_R3_INPUTS); vector_opt->key = "vector_field"; vector_opt->required = NO; vector_opt->description = _("Names of three 3D raster maps describing " "x, y, z components of vector field"); vector_opt->guisection = _("Input"); seed_opt = G_define_standard_option(G_OPT_V_INPUT); seed_opt->required = NO; seed_opt->key = "seed_points"; seed_opt->description = _("If no map is provided, " "flow lines are generated " "from each cell of the input 3D raster"); seed_opt->label = _("Name of vector map with points " "from which flow lines are generated"); seed_opt->guisection = _("Input"); flowlines_opt = G_define_standard_option(G_OPT_V_OUTPUT); flowlines_opt->key = "flowline"; flowlines_opt->required = NO; flowlines_opt->description = _("Name for vector map of flow lines"); flowlines_opt->guisection = _("Output"); flowacc_opt = G_define_standard_option(G_OPT_R3_OUTPUT); flowacc_opt->key = "flowaccumulation"; flowacc_opt->required = NO; flowacc_opt->description = _("Name for output flowaccumulation 3D raster"); flowacc_opt->guisection = _("Output"); sampled_opt = G_define_standard_option(G_OPT_R3_INPUT); sampled_opt->key = "sampled"; sampled_opt->required = NO; sampled_opt->label = _("Name for 3D raster sampled by flowlines"); sampled_opt->description = _("Values of this 3D raster will be stored " "as attributes of flowlines segments"); unit_opt = G_define_option(); unit_opt->key = "unit"; unit_opt->type = TYPE_STRING; unit_opt->required = NO; unit_opt->answer = "cell"; unit_opt->options = "time,length,cell"; desc = NULL; G_asprintf(&desc, "time;%s;" "length;%s;" "cell;%s", _("elapsed time"), _("length in map units"), _("length in cells (voxels)")); unit_opt->descriptions = desc; unit_opt->label = _("Unit of integration step"); unit_opt->description = _("Default unit is cell"); unit_opt->guisection = _("Integration"); step_opt = G_define_option(); step_opt->key = "step"; step_opt->type = TYPE_DOUBLE; step_opt->required = NO; step_opt->answer = "0.25"; step_opt->label = _("Integration step in selected unit"); step_opt->description = _("Default step is 0.25 cell"); step_opt->guisection = _("Integration"); limit_opt = G_define_option(); limit_opt->key = "limit"; limit_opt->type = TYPE_INTEGER; limit_opt->required = NO; limit_opt->answer = "2000"; limit_opt->description = _("Maximum number of steps"); limit_opt->guisection = _("Integration"); error_opt = G_define_option(); error_opt->key = "max_error"; error_opt->type = TYPE_DOUBLE; error_opt->required = NO; error_opt->answer = "1e-5"; error_opt->label = _("Maximum error of integration"); error_opt->description = _("Influences step, increase maximum error " "to allow bigger steps"); error_opt->guisection = _("Integration"); skip_opt = G_define_option(); skip_opt->key = "skip"; skip_opt->type = TYPE_INTEGER; skip_opt->required = NO; skip_opt->multiple = YES; skip_opt->description = _("Number of cells between flow lines in x, y and z direction"); dir_opt = G_define_option(); dir_opt->key = "direction"; dir_opt->type = TYPE_STRING; dir_opt->required = NO; dir_opt->multiple = NO; dir_opt->options = "up,down,both"; dir_opt->answer = "down"; dir_opt->description = _("Compute flowlines upstream, " "downstream or in both direction."); table_fl = G_define_flag(); table_fl->key = 'a'; table_fl->description = _("Create and fill attribute table"); G_option_required(scalar_opt, vector_opt, NULL); G_option_exclusive(scalar_opt, vector_opt, NULL); G_option_required(flowlines_opt, flowacc_opt, NULL); G_option_requires(seed_opt, flowlines_opt, NULL); G_option_requires(table_fl, flowlines_opt, NULL); G_option_requires(sampled_opt, table_fl, NULL); if (G_parser(argc, argv)) exit(EXIT_FAILURE); driver = NULL; finfo = NULL; if_table = table_fl->answer ? TRUE : FALSE; check_vector_input_maps(vector_opt, seed_opt); Rast3d_init_defaults(); Rast3d_get_window(®ion); /* set up integration variables */ if (step_opt->answer) { integration.step = atof(step_opt->answer); integration.unit = unit_opt->answer; } else { integration.unit = "cell"; integration.step = 0.25; } integration.max_error = atof(error_opt->answer); integration.max_step = 5 * integration.step; integration.min_step = integration.step / 5; integration.limit = atof(limit_opt->answer); if (strcmp(dir_opt->answer, "up") == 0) integration.direction_type = FLOWDIR_UP; else if (strcmp(dir_opt->answer, "down") == 0) integration.direction_type = FLOWDIR_DOWN; else integration.direction_type = FLOWDIR_BOTH; /* cell size is the diagonal */ integration.cell_size = sqrt(region.ns_res * region.ns_res + region.ew_res * region.ew_res + region.tb_res * region.tb_res); /* set default skip if needed */ if (skip_opt->answers) { for (i = 0; i < 3; i++) { if (skip_opt->answers[i] != NULL) { skip[i] = atoi(skip_opt->answers[i]); } else { G_fatal_error(_("Please provide 3 integer values for skip option.")); } } } else { skip[0] = fmax(1, region.cols / 10); skip[1] = fmax(1, region.rows / 10); skip[2] = fmax(1, region.depths / 10); } /* open raster 3D maps of velocity components */ gradient_info.initialized = FALSE; load_input_raster3d_maps(scalar_opt, vector_opt, &gradient_info, ®ion); /* open new 3D raster map of flowacumulation */ if (flowacc_opt->answer) { flowacc = Rast3d_open_new_opt_tile_size(flowacc_opt->answer, RASTER3D_USE_CACHE_DEFAULT, ®ion, FCELL_TYPE, 32); if (!flowacc) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), flowacc_opt->answer); init_flowaccum(®ion, flowacc); } /* open 3D raster map used for sampling */ if (sampled_opt->answer) { sampled = Rast3d_open_cell_old(sampled_opt->answer, G_find_raster3d(sampled_opt->answer, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (!sampled) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), sampled_opt->answer); } else sampled = NULL; /* open new vector map of flowlines */ if (flowlines_opt->answer) { fl_cats = Vect_new_cats_struct(); fl_points = Vect_new_line_struct(); if (Vect_open_new(&fl_map, flowlines_opt->answer, TRUE) < 0) G_fatal_error(_("Unable to create vector map <%s>"), flowlines_opt->answer); Vect_hist_command(&fl_map); if (if_table) { create_table(&fl_map, &finfo, &driver, gradient_info.compute_gradient, sampled ? 1 : 0); } } n_seeds = 0; /* open vector map of seeds */ if (seed_opt->answer) { if (Vect_open_old2(&seed_Map, seed_opt->answer, "", "1") < 0) G_fatal_error(_("Unable to open vector map <%s>"), seed_opt->answer); if (!Vect_is_3d(&seed_Map)) G_fatal_error(_("Vector map <%s> is not 3D"), seed_opt->answer); n_seeds = Vect_get_num_primitives(&seed_Map, GV_POINT); } if (flowacc_opt->answer || (!seed_opt->answer && flowlines_opt->answer)) { if (flowacc_opt->answer) n_seeds += region.cols * region.rows * region.depths; else { n_seeds += ceil(region.cols / (double)skip[0]) * ceil(region.rows / (double)skip[1]) * ceil(region.depths / (double)skip[2]); } } G_debug(1, "Number of seeds is %d", n_seeds); seed_count = 0; cat = 1; if (seed_opt->answer) { seed_points = Vect_new_line_struct(); seed_cats = Vect_new_cats_struct(); /* compute flowlines from vector seed map */ while (TRUE) { ltype = Vect_read_next_line(&seed_Map, seed_points, seed_cats); if (ltype == -1) { Vect_close(&seed_Map); G_fatal_error(_("Error during reading seed vector map")); } else if (ltype == -2) { break; } else if (ltype == GV_POINT) { seed.x = seed_points->x[0]; seed.y = seed_points->y[0]; seed.z = seed_points->z[0]; seed.flowline = TRUE; seed.flowaccum = FALSE; } G_percent(seed_count, n_seeds, 1); if (integration.direction_type == FLOWDIR_UP || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_UP; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } if (integration.direction_type == FLOWDIR_DOWN || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_DOWN; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } seed_count++; } Vect_destroy_line_struct(seed_points); Vect_destroy_cats_struct(seed_cats); Vect_close(&seed_Map); } if (flowacc_opt->answer || (!seed_opt->answer && flowlines_opt->answer)) { /* compute flowlines from points on grid */ for (r = region.rows; r > 0; r--) { for (c = 0; c < region.cols; c++) { for (d = 0; d < region.depths; d++) { seed.x = region.west + c * region.ew_res + region.ew_res / 2; seed.y = region.south + r * region.ns_res - region.ns_res / 2; seed.z = region.bottom + d * region.tb_res + region.tb_res / 2; seed.flowline = FALSE; seed.flowaccum = FALSE; if (flowacc_opt->answer) seed.flowaccum = TRUE; if (flowlines_opt->answer && !seed_opt->answer && (c % skip[0] == 0) && (r % skip[1] == 0) && (d % skip[2] == 0)) seed.flowline = TRUE; if (seed.flowaccum || seed.flowline) { G_percent(seed_count, n_seeds, 1); if (integration.direction_type == FLOWDIR_UP || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_UP; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } if (integration.direction_type == FLOWDIR_DOWN || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_DOWN; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } seed_count++; } } } } } G_percent(1, 1, 1); if (flowlines_opt->answer) { if (if_table) { db_commit_transaction(driver); db_close_database_shutdown_driver(driver); } Vect_destroy_line_struct(fl_points); Vect_destroy_cats_struct(fl_cats); Vect_build(&fl_map); Vect_close(&fl_map); } if (flowacc_opt->answer) Rast3d_close(flowacc); return EXIT_SUCCESS; }
/* ************************************************************************* */ int main(int argc, char *argv[]) { RASTER3D_Region region; struct Cell_head window2d; struct GModule *module; void *map = NULL; /*The 3D Rastermap */ int changemask = 0; int elevfd = -1, outfd = -1; /*file descriptors */ int output_type, cols, rows; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("profile")); G_add_keyword(_("raster")); G_add_keyword(_("voxel")); module->description = _("Creates cross section 2D raster map from 3D raster map based on 2D elevation map"); /* Get parameters from user */ set_params(); /* Have GRASS get inputs */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_debug(3, "Open 3D raster map %s", param.input->answer); if (NULL == G_find_raster3d(param.input->answer, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), param.input->answer); /* Figure out the region from the map */ Rast3d_init_defaults(); Rast3d_get_window(®ion); /*Check if the g3d-region is equal to the 2d rows and cols */ rows = Rast_window_rows(); cols = Rast_window_cols(); /*If not equal, set the 2D windows correct */ if (rows != region.rows || cols != region.cols) { G_message (_("The 2D and 3D region settings are different. Using the 3D raster map settings to adjust the 2D region.")); G_get_set_window(&window2d); window2d.ns_res = region.ns_res; window2d.ew_res = region.ew_res; window2d.rows = region.rows; window2d.cols = region.cols; Rast_set_window(&window2d); } /*******************/ /*Open the 3d raster map */ /*******************/ map = Rast3d_open_cell_old(param.input->answer, G_find_raster3d(param.input->answer, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), param.input->answer); /*Get the output type */ output_type = Rast3d_file_type_map(map); if (output_type == FCELL_TYPE || output_type == DCELL_TYPE) { /********************************/ /*Open the elevation raster map */ /********************************/ elevfd = Rast_open_old(param.elevation->answer, ""); globalElevMapType = Rast_get_map_type(elevfd); /**********************/ /*Open the Outputmap */ /**********************/ if (G_find_raster2(param.output->answer, "")) G_message(_("Output map already exists. Will be overwritten!")); if (output_type == FCELL_TYPE) outfd = Rast_open_new(param.output->answer, FCELL_TYPE); else if (output_type == DCELL_TYPE) outfd = Rast_open_new(param.output->answer, DCELL_TYPE); /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask = 0; if (Rast3d_mask_is_off(map)) { Rast3d_mask_on(map); changemask = 1; } } } /************************/ /*Create the Rastermaps */ /************************/ rast3d_cross_section(map, region, elevfd, outfd); /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(map) && changemask) Rast3d_mask_off(map); } Rast_close(outfd); Rast_close(elevfd); } else { fatal_error(map, -1, -1, _("Wrong 3D raster datatype! Cannot create raster map")); } /* Close files and exit */ if (!Rast3d_close(map)) Rast3d_fatal_error(_("Unable to close 3D raster map <%s>"), param.input->answer); return (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i; int print_flag = 0; int flat_flag; int set_flag; double x; int ival; int row_flag = 0, col_flag = 0; struct Cell_head window, temp_window; const char *value; const char *name; const char *mapset; char **rast_ptr, **vect_ptr; struct GModule *module; struct { struct Flag *update, *print, *gprint, *flprint, *lprint, *eprint, *nangle, *center, *res_set, *dist_res, *dflt, *z, *savedefault, *bbox, *gmt_style, *wms_style; } flag; struct { struct Option *north, *south, *east, *west, *top, *bottom, *res, *nsres, *ewres, *res3, *tbres, *rows, *cols, *save, *region, *raster, *raster3d, *align, *zoom, *vect; } parm; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("settings")); module->description = _("Manages the boundary definitions for the " "geographic region."); /* flags */ flag.dflt = G_define_flag(); flag.dflt->key = 'd'; flag.dflt->description = _("Set from default region"); flag.dflt->guisection = _("Existing"); flag.savedefault = G_define_flag(); flag.savedefault->key = 's'; flag.savedefault->label = _("Save as default region"); flag.savedefault->description = _("Only possible from the PERMANENT mapset"); flag.savedefault->guisection = _("Existing"); flag.print = G_define_flag(); flag.print->key = 'p'; flag.print->description = _("Print the current region"); flag.print->guisection = _("Print"); flag.lprint = G_define_flag(); flag.lprint->key = 'l'; flag.lprint->description = _("Print the current region in lat/long " "using the current ellipsoid/datum"); flag.lprint->guisection = _("Print"); flag.eprint = G_define_flag(); flag.eprint->key = 'e'; flag.eprint->description = _("Print the current region extent"); flag.eprint->guisection = _("Print"); flag.center = G_define_flag(); flag.center->key = 'c'; flag.center->description = _("Print the current region map center coordinates"); flag.center->guisection = _("Print"); flag.gmt_style = G_define_flag(); flag.gmt_style->key = 't'; flag.gmt_style->description = _("Print the current region in GMT style"); flag.gmt_style->guisection = _("Print"); flag.wms_style = G_define_flag(); flag.wms_style->key = 'w'; flag.wms_style->description = _("Print the current region in WMS style"); flag.wms_style->guisection = _("Print"); flag.dist_res = G_define_flag(); flag.dist_res->key = 'm'; flag.dist_res->description = _("Print region resolution in meters (geodesic)"); flag.dist_res->guisection = _("Print"); flag.nangle = G_define_flag(); flag.nangle->key = 'n'; flag.nangle->label = _("Print the convergence angle (degrees CCW)"); flag.nangle->description = _("The difference between the projection's grid north and true north, " "measured at the center coordinates of the current region."); flag.nangle->guisection = _("Print"); flag.z = G_define_flag(); flag.z->key = '3'; flag.z->description = _("Print also 3D settings"); flag.z->guisection = _("Print"); flag.bbox = G_define_flag(); flag.bbox->key = 'b'; flag.bbox->description = _("Print the maximum bounding box in lat/long on WGS84"); flag.bbox->guisection = _("Print"); flag.gprint = G_define_flag(); flag.gprint->key = 'g'; flag.gprint->description = _("Print in shell script style"); flag.gprint->guisection = _("Print"); flag.flprint = G_define_flag(); flag.flprint->key = 'f'; flag.flprint->description = _("Print in shell script style, but in one line (flat)"); flag.flprint->guisection = _("Print"); flag.res_set = G_define_flag(); flag.res_set->key = 'a'; flag.res_set->description = _("Align region to resolution (default = align to bounds, " "works only for 2D resolution)"); flag.res_set->guisection = _("Bounds"); flag.update = G_define_flag(); flag.update->key = 'u'; flag.update->description = _("Do not update the current region"); flag.update->guisection = _("Effects"); /* parameters */ parm.region = G_define_standard_option(G_OPT_M_REGION); parm.region->description = _("Set current region from named region"); parm.region->guisection = _("Existing"); parm.raster = G_define_standard_option(G_OPT_R_MAP); parm.raster->key = "raster"; parm.raster->required = NO; parm.raster->multiple = YES; parm.raster->description = _("Set region to match raster map(s)"); parm.raster->guisection = _("Existing"); parm.raster3d = G_define_standard_option(G_OPT_R3_MAP); parm.raster3d->key = "raster_3d"; parm.raster3d->required = NO; parm.raster3d->multiple = NO; parm.raster3d->description = _("Set region to match 3D raster map(s) (both 2D and 3D " "values)"); parm.raster3d->guisection = _("Existing"); parm.vect = G_define_standard_option(G_OPT_V_MAP); parm.vect->key = "vector"; parm.vect->required = NO; parm.vect->multiple = YES; parm.vect->label = _("Set region to match vector map(s)"); parm.vect->description = NULL; parm.vect->guisection = _("Existing"); parm.north = G_define_option(); parm.north->key = "n"; parm.north->key_desc = "value"; parm.north->required = NO; parm.north->multiple = NO; parm.north->type = TYPE_STRING; parm.north->description = _("Value for the northern edge"); parm.north->guisection = _("Bounds"); parm.south = G_define_option(); parm.south->key = "s"; parm.south->key_desc = "value"; parm.south->required = NO; parm.south->multiple = NO; parm.south->type = TYPE_STRING; parm.south->description = _("Value for the southern edge"); parm.south->guisection = _("Bounds"); parm.east = G_define_option(); parm.east->key = "e"; parm.east->key_desc = "value"; parm.east->required = NO; parm.east->multiple = NO; parm.east->type = TYPE_STRING; parm.east->description = _("Value for the eastern edge"); parm.east->guisection = _("Bounds"); parm.west = G_define_option(); parm.west->key = "w"; parm.west->key_desc = "value"; parm.west->required = NO; parm.west->multiple = NO; parm.west->type = TYPE_STRING; parm.west->description = _("Value for the western edge"); parm.west->guisection = _("Bounds"); parm.top = G_define_option(); parm.top->key = "t"; parm.top->key_desc = "value"; parm.top->required = NO; parm.top->multiple = NO; parm.top->type = TYPE_STRING; parm.top->description = _("Value for the top edge"); parm.top->guisection = _("Bounds"); parm.bottom = G_define_option(); parm.bottom->key = "b"; parm.bottom->key_desc = "value"; parm.bottom->required = NO; parm.bottom->multiple = NO; parm.bottom->type = TYPE_STRING; parm.bottom->description = _("Value for the bottom edge"); parm.bottom->guisection = _("Bounds"); parm.rows = G_define_option(); parm.rows->key = "rows"; parm.rows->key_desc = "value"; parm.rows->required = NO; parm.rows->multiple = NO; parm.rows->type = TYPE_INTEGER; parm.rows->description = _("Number of rows in the new region"); parm.rows->guisection = _("Resolution"); parm.cols = G_define_option(); parm.cols->key = "cols"; parm.cols->key_desc = "value"; parm.cols->required = NO; parm.cols->multiple = NO; parm.cols->type = TYPE_INTEGER; parm.cols->description = _("Number of columns in the new region"); parm.cols->guisection = _("Resolution"); parm.res = G_define_option(); parm.res->key = "res"; parm.res->key_desc = "value"; parm.res->required = NO; parm.res->multiple = NO; parm.res->type = TYPE_STRING; parm.res->description = _("2D grid resolution (north-south and east-west)"); parm.res->guisection = _("Resolution"); parm.res3 = G_define_option(); parm.res3->key = "res3"; parm.res3->key_desc = "value"; parm.res3->required = NO; parm.res3->multiple = NO; parm.res3->type = TYPE_STRING; parm.res3->description = _("3D grid resolution (north-south, east-west and top-bottom)"); parm.res3->guisection = _("Resolution"); parm.nsres = G_define_option(); parm.nsres->key = "nsres"; parm.nsres->key_desc = "value"; parm.nsres->required = NO; parm.nsres->multiple = NO; parm.nsres->type = TYPE_STRING; parm.nsres->description = _("North-south 2D grid resolution"); parm.nsres->guisection = _("Resolution"); parm.ewres = G_define_option(); parm.ewres->key = "ewres"; parm.ewres->key_desc = "value"; parm.ewres->required = NO; parm.ewres->multiple = NO; parm.ewres->type = TYPE_STRING; parm.ewres->description = _("East-west 2D grid resolution"); parm.ewres->guisection = _("Resolution"); parm.tbres = G_define_option(); parm.tbres->key = "tbres"; parm.tbres->key_desc = "value"; parm.tbres->required = NO; parm.tbres->multiple = NO; parm.tbres->type = TYPE_STRING; parm.tbres->description = _("Top-bottom 3D grid resolution"); parm.tbres->guisection = _("Resolution"); parm.zoom = G_define_option(); parm.zoom->key = "zoom"; parm.zoom->key_desc = "name"; parm.zoom->required = NO; parm.zoom->multiple = NO; parm.zoom->type = TYPE_STRING; parm.zoom->description = _("Shrink region until it meets non-NULL data from this raster map"); parm.zoom->gisprompt = "old,cell,raster"; parm.zoom->guisection = _("Bounds"); parm.align = G_define_option(); parm.align->key = "align"; parm.align->key_desc = "name"; parm.align->required = NO; parm.align->multiple = NO; parm.align->type = TYPE_STRING; parm.align->description = _("Adjust region cells to cleanly align with this raster map"); parm.align->gisprompt = "old,cell,raster"; parm.align->guisection = _("Bounds"); parm.save = G_define_option(); parm.save->key = "save"; parm.save->key_desc = "name"; parm.save->required = NO; parm.save->multiple = NO; parm.save->type = TYPE_STRING; parm.save->description = _("Save current region settings in named region file"); parm.save->gisprompt = "new,windows,region"; parm.save->guisection = _("Effects"); G_option_required(flag.dflt, flag.savedefault, flag.print, flag.lprint, flag.eprint, flag.center, flag.gmt_style, flag.wms_style, flag.dist_res, flag.nangle, flag. z, flag.bbox, flag.gprint, flag.res_set, flag.update, parm.region, parm.raster, parm.raster3d, parm.vect, parm.north, parm.south, parm.east, parm.west, parm.top, parm.bottom, parm.rows, parm.cols, parm.res, parm.res3, parm.nsres, parm.ewres, parm.tbres, parm.zoom, parm.align, parm.save, NULL); if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_get_default_window(&window); set_flag = !flag.update->answer; flat_flag = flag.flprint->answer; if (flag.print->answer) print_flag |= PRINT_REG; if (flag.gprint->answer) print_flag |= PRINT_SH; if (flag.lprint->answer) print_flag |= PRINT_LL; if (flag.eprint->answer) print_flag |= PRINT_EXTENT; if (flag.center->answer) print_flag |= PRINT_CENTER; if (flag.gmt_style->answer) print_flag |= PRINT_GMT; if (flag.wms_style->answer) print_flag |= PRINT_WMS; if (flag.nangle->answer) print_flag |= PRINT_NANGLE; if (flag.dist_res->answer) print_flag |= PRINT_METERS; if (flag.z->answer) print_flag |= PRINT_3D; if (flag.bbox->answer) print_flag |= PRINT_MBBOX; if (print_flag == PRINT_METERS) print_flag |= PRINT_SH; if (print_flag == PRINT_SH || print_flag & PRINT_3D || print_flag == PRINT_METERS + PRINT_SH) { print_flag |= PRINT_REG; } if (!flag.dflt->answer) G_get_window(&window); /* region= */ if ((name = parm.region->answer)) { mapset = G_find_file2("windows", name, ""); if (!mapset) G_fatal_error(_("Region <%s> not found"), name); G_get_element_window(&window, "windows", name, mapset); } /* raster= */ if (parm.raster->answer) { int first = 0; rast_ptr = parm.raster->answers; for (; *rast_ptr != NULL; rast_ptr++) { char rast_name[GNAME_MAX]; strcpy(rast_name, *rast_ptr); mapset = G_find_raster2(rast_name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), rast_name); Rast_get_cellhd(rast_name, mapset, &temp_window); if (!first) { window = temp_window; first = 1; } else { window.north = (window.north > temp_window.north) ? window.north : temp_window.north; window.south = (window.south < temp_window.south) ? window.south : temp_window.south; window.east = (window.east > temp_window.east) ? window.east : temp_window.east; window.west = (window.west < temp_window.west) ? window.west : temp_window.west; } } G_adjust_Cell_head3(&window, 0, 0, 0); } /* raster3d= */ if ((name = parm.raster3d->answer)) { RASTER3D_Region win; if ((mapset = G_find_raster3d(name, "")) == NULL) G_fatal_error(_("3D raster map <%s> not found"), name); if (Rast3d_read_region_map(name, mapset, &win) < 0) G_fatal_error(_("Unable to read header of 3D raster map <%s@%s>"), name, mapset); Rast3d_region_to_cell_head(&win, &window); } /* vector= */ if (parm.vect->answer) { int first = 0; vect_ptr = parm.vect->answers; for (; *vect_ptr != NULL; vect_ptr++) { struct Map_info Map; struct bound_box box; char vect_name[GNAME_MAX]; struct Cell_head map_window; strcpy(vect_name, *vect_ptr); mapset = G_find_vector2(vect_name, ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), vect_name); temp_window = window; Vect_set_open_level(2); if (2 > Vect_open_old_head(&Map, vect_name, mapset)) G_fatal_error(_("Unable to open vector map <%s> on topological level"), vect_name); Vect_get_map_box(&Map, &box); map_window = window; map_window.north = box.N; map_window.south = box.S; map_window.west = box.W; map_window.east = box.E; map_window.top = box.T; map_window.bottom = box.B; if (!first) { window = map_window; first = 1; } else { window.north = (window.north > map_window.north) ? window.north : map_window.north; window.south = (window.south < map_window.south) ? window.south : map_window.south; window.east = (window.east > map_window.east) ? window.east : map_window.east; window.west = (window.west < map_window.west) ? window.west : map_window.west; if (map_window.top > window.top) window.top = map_window.top; if (map_window.bottom < window.bottom) window.bottom = map_window.bottom; } if (window.north == window.south) { window.north = window.north + 0.5 * temp_window.ns_res; window.south = window.south - 0.5 * temp_window.ns_res; } if (window.east == window.west) { window.west = window.west - 0.5 * temp_window.ew_res; window.east = window.east + 0.5 * temp_window.ew_res; } if (window.top == window.bottom) { window.bottom = (window.bottom - 0.5 * temp_window.tb_res); window.top = (window.top + 0.5 * temp_window.tb_res); } if (flag.res_set->answer) Rast_align_window(&window, &temp_window); Vect_close(&Map); } } /* n= */ if ((value = parm.north->answer)) { if ((i = nsew(value, "n+", "n-", "s+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.north); switch (i) { case 1: window.north += x; break; case 2: window.north -= x; break; case 3: window.north = window.south + x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.north = x; else die(parm.north); } /* s= */ if ((value = parm.south->answer)) { if ((i = nsew(value, "s+", "s-", "n-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.south); switch (i) { case 1: window.south += x; break; case 2: window.south -= x; break; case 3: window.south = window.north - x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.south = x; else die(parm.south); } /* e= */ if ((value = parm.east->answer)) { if ((i = nsew(value, "e+", "e-", "w+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.east); switch (i) { case 1: window.east += x; break; case 2: window.east -= x; break; case 3: window.east = window.west + x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.east = x; else die(parm.east); } /* w= */ if ((value = parm.west->answer)) { if ((i = nsew(value, "w+", "w-", "e-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.west); switch (i) { case 1: window.west += x; break; case 2: window.west -= x; break; case 3: window.west = window.east - x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.west = x; else die(parm.west); } /* t= */ if ((value = parm.top->answer)) { if ((i = nsew(value, "t+", "t-", "b+"))) { if (sscanf(value + 2, "%lf", &x) != 1) die(parm.top); switch (i) { case 1: window.top += x; break; case 2: window.top -= x; break; case 3: window.top = window.bottom + x; break; } } else if (sscanf(value, "%lf", &x) == 1) window.top = x; else die(parm.top); } /* b= */ if ((value = parm.bottom->answer)) { if ((i = nsew(value, "b+", "b-", "t-"))) { if (sscanf(value + 2, "%lf", &x) != 1) die(parm.bottom); switch (i) { case 1: window.bottom += x; break; case 2: window.bottom -= x; break; case 3: window.bottom = window.top - x; break; } } else if (sscanf(value, "%lf", &x) == 1) window.bottom = x; else die(parm.bottom); } /* res= */ if ((value = parm.res->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.res); window.ns_res = x; window.ew_res = x; if (flag.res_set->answer) { window.north = ceil(window.north / x) * x; window.south = floor(window.south / x) * x; window.east = ceil(window.east / x) * x; window.west = floor(window.west / x) * x; } } /* res3= */ if ((value = parm.res3->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.res); window.ns_res3 = x; window.ew_res3 = x; window.tb_res = x; } /* nsres= */ if ((value = parm.nsres->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.nsres); window.ns_res = x; if (flag.res_set->answer) { window.north = ceil(window.north / x) * x; window.south = floor(window.south / x) * x; } } /* ewres= */ if ((value = parm.ewres->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.ewres); window.ew_res = x; if (flag.res_set->answer) { window.east = ceil(window.east / x) * x; window.west = floor(window.west / x) * x; } } /* tbres= */ if ((value = parm.tbres->answer)) { if (sscanf(value, "%lf", &x) != 1) die(parm.tbres); window.tb_res = x; if (flag.res_set->answer) { window.top = ceil(window.top / x) * x; window.bottom = floor(window.bottom / x) * x; } } /* rows= */ if ((value = parm.rows->answer)) { if (sscanf(value, "%i", &ival) != 1) die(parm.rows); window.rows = ival; row_flag = 1; } /* cols= */ if ((value = parm.cols->answer)) { if (sscanf(value, "%i", &ival) != 1) die(parm.cols); window.cols = ival; col_flag = 1; } /* zoom= */ if ((name = parm.zoom->answer)) { mapset = G_find_raster2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); zoom(&window, name, mapset); } /* align= */ if ((name = parm.align->answer)) { mapset = G_find_raster2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); Rast_get_cellhd(name, mapset, &temp_window); Rast_align_window(&window, &temp_window); } /* save= */ if ((name = parm.save->answer)) { temp_window = window; G_adjust_Cell_head3(&temp_window, 0, 0, 0); if (G_put_element_window(&temp_window, "windows", name) < 0) G_fatal_error(_("Unable to set region <%s>"), name); } G_adjust_Cell_head3(&window, row_flag, col_flag, 0); if (set_flag) { if (G_put_window(&window) < 0) G_fatal_error(_("Unable to update current region")); } if (flag.savedefault->answer) { if (strcmp(G_mapset(), "PERMANENT") == 0) { G_put_element_window(&window, "", "DEFAULT_WIND"); } else { G_fatal_error(_("Unable to change default region. " "The current mapset is not <PERMANENT>.")); } } /* / flag.savedefault->answer */ if (print_flag) print_window(&window, print_flag, flat_flag); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { float val_f; /* for misc use */ double val_d; /* for misc use */ stat_table *stats = NULL; double min, max; equal_val_array *eqvals = NULL; unsigned int n = 0, nsteps; int map_type; char *infile = NULL; void *map = NULL; RASTER3D_Region region; unsigned int rows, cols, depths; unsigned int x, y, z; struct Option *inputfile, *steps; struct Flag *equal, *counts_only; struct GModule *module; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("statistics")); module->description = _("Generates volume statistics for 3D raster maps."); /* Define the different options */ inputfile = G_define_standard_option(G_OPT_R3_INPUT); steps = G_define_option(); steps->key = "nsteps"; steps->type = TYPE_INTEGER; steps->required = NO; steps->answer = "20"; steps->description = _("Number of subranges to collect stats from"); equal = G_define_flag(); equal->key = 'e'; equal->description = _("Calculate statistics based on equal value groups"); counts_only = G_define_flag(); counts_only->key = 'c'; counts_only->description = _("Only print cell counts"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /*Set the defaults */ Rast3d_init_defaults(); /*get the current region */ Rast3d_get_window(®ion); cols = region.cols; rows = region.rows; depths = region.depths; sscanf(steps->answer, "%i", &nsteps); /* break if the wrong number of subranges are given */ if (nsteps <= 0) G_fatal_error(_("The number of subranges has to be equal or greater than 1")); infile = inputfile->answer; if (NULL == G_find_raster3d(infile, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), infile); map = Rast3d_open_cell_old(infile, G_find_raster3d(infile, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), infile); map_type = Rast3d_tile_type_map(map); /* calculate statistics for groups of equal values */ if ((equal->answer)) { /*search for equal values */ eqvals = NULL; n = 0; for (z = 0; z < depths; z++) { G_percent(z, depths - 1, 2); for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { if (map_type == FCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_f, map_type); if (!Rast3d_is_null_value_num(&val_f, map_type)) { /*the first entry */ if (eqvals == NULL) eqvals = add_equal_val_to_array(eqvals, (double)val_f); else check_equal_value(eqvals, (double)val_f); n++; /*count non null cells */ } } else if (map_type == DCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_d, map_type); if (!Rast3d_is_null_value_num(&val_d, map_type)) { /*the first entry */ if (eqvals == NULL) eqvals = add_equal_val_to_array(eqvals, val_d); else check_equal_value(eqvals, val_d); n++; /*count non null cells */ } } } } } if (eqvals) { /* sort the equal values array */ G_message(_("Sort non-null values")); heapsort_eqvals(eqvals, eqvals->count); /* create the statistic table with equal values */ stats = create_stat_table(eqvals->count, eqvals, 0, 0); /* compute the number of null values */ stats->null->count = rows * cols * depths - n; free_equal_val_array(eqvals); } } else { /* create the statistic table based on value ranges */ /* get the range of the map */ Rast3d_range_load(map); Rast3d_range_min_max(map, &min, &max); stats = create_stat_table(nsteps, NULL, min, max); n = 0; for (z = 0; z < depths; z++) { G_percent(z, depths - 1, 2); for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { if (map_type == FCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_f, map_type); if (!Rast3d_is_null_value_num(&val_f, map_type)) { check_range_value(stats, (double)val_f); n++; } } else if (map_type == DCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_d, map_type); if (!Rast3d_is_null_value_num(&val_d, map_type)) { check_range_value(stats, val_d); n++; } } } } } /* compute the number of null values */ stats->null->count = rows * cols * depths - n; } if(stats) { /* Compute the volume and percentage */ update_stat_table(stats, ®ion); /* Print the statistics to stdout */ print_stat_table(stats, counts_only->answer); free_stat_table(stats); } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { RASTER3D_Map *input; RASTER3D_Map *output; RASTER3D_Region region; struct GModule *module; stat_func *method_fn; double quantile; int x, y, z; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("neighbor")); G_add_keyword(_("aggregation")); G_add_keyword(_("statistics")); G_add_keyword(_("filter")); module->description = _("Makes each voxel value a " "function of the values assigned to the voxels " "around it, and stores new voxel values in an output 3D raster map"); /* Get parameters from user */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (NULL == G_find_raster3d(param.input->answer, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), param.input->answer); Rast3d_init_defaults(); Rast3d_get_window(®ion); nx = region.cols; ny = region.rows; nz = region.depths; /* Size fo the moving window */ x_size = atoi(param.window->answers[0]); y_size = atoi(param.window->answers[1]); z_size = atoi(param.window->answers[2]); /* Distances in all directions */ x_dist = x_size / 2; y_dist = y_size / 2; z_dist = z_size / 2; /* Maximum size of the buffer */ size = x_size * y_size * z_size; /* Set the computation method */ method_fn = menu[find_method(param.method->answer)].method; if (param.quantile->answer) quantile = atof(param.quantile->answer); else quantile = 0.0; input = Rast3d_open_cell_old(param.input->answer, G_find_raster3d(param.input->answer, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (input == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), param.input->answer); output = Rast3d_open_new_opt_tile_size(param.output->answer, RASTER3D_USE_CACHE_X, ®ion, DCELL_TYPE, 32); if (output == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), param.output->answer); Rast3d_min_unlocked(output, RASTER3D_USE_CACHE_X); Rast3d_autolock_on(output); Rast3d_unlock_all(output); DCELL *buff = NULL, value; buff = (DCELL *) calloc(size, sizeof(DCELL)); if (buff == NULL) Rast3d_fatal_error(_("Unable to allocate buffer")); for (z = 0; z < nz; z++) { for (y = 0; y < ny; y++) { for (x = 0; x < nx; x++) { /* Gather values in moving window */ int num = gather_values(input, buff, x, y, z); /* Compute the resulting value */ if (num > 0) (*method_fn) (&value, buff, num, &quantile); else Rast_set_d_null_value(&value, 1); /* Write the value */ Rast3d_put_double(output, x, y, z, value); } } } free(buff); if (!Rast3d_flush_all_tiles(output)) G_fatal_error(_("Error flushing tiles")); Rast3d_autolock_off(output); Rast3d_unlock_all(output); Rast3d_close(input); Rast3d_close(output); return 0; }
/* ************************************************************************* */ int main(int argc, char *argv[]) { char *output = NULL; RASTER3D_Region region; struct Cell_head window2d; struct Cell_head default_region; FILE *fp = NULL; struct GModule *module; int dp, i, changemask = 0; int rows, cols; const char *mapset, *name; double scale = 1.0, llscale = 1.0; input_maps *in; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("export")); G_add_keyword(_("voxel")); G_add_keyword("VTK"); module->description = _("Converts 3D raster maps into the VTK-ASCII format."); /* Get parameters from user */ set_params(); /* Have GRASS get inputs */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /*The precision of the output */ if (param.decimals->answer) { if (sscanf(param.decimals->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")); } else { dp = 8; /*This value is taken from the lib settings in G_format_easting */ } /*Check the input */ check_input_maps(); /*Correct the coordinates, so the precision of VTK is not hurt :( */ if (param.coorcorr->answer) { /*Get the default region for coordiante correction */ G_get_default_window(&default_region); /*Use the center of the current region as extent */ y_extent = (default_region.north + default_region.south) / 2; x_extent = (default_region.west + default_region.east) / 2; } else { x_extent = 0; y_extent = 0; } /*open the output */ if (param.output->answer) { fp = fopen(param.output->answer, "w"); if (fp == NULL) { perror(param.output->answer); G_usage(); exit(EXIT_FAILURE); } } else fp = stdout; /* Figure out the region from the map */ Rast3d_init_defaults(); Rast3d_get_window(®ion); /*initiate the input mpas structure */ in = create_input_maps_struct(); /* read and compute the scale factor */ sscanf(param.elevscale->answer, "%lf", &scale); /*if LL projection, convert the elevation values to degrees */ if (param.scalell->answer && region.proj == PROJECTION_LL) { llscale = M_PI / (180) * 6378137; scale /= llscale; } /*Open the top and bottom file */ if (param.structgrid->answer) { /*Check if the g3d-region is equal to the 2d rows and cols */ rows = Rast_window_rows(); cols = Rast_window_cols(); /*If not equal, set the 2D windows correct */ if (rows != region.rows || cols != region.cols) { G_message(_("The 2D and 3D region settings are different. " "Using the 2D window settings to adjust the 2D part of the 3D region.")); G_get_set_window(&window2d); window2d.ns_res = region.ns_res; window2d.ew_res = region.ew_res; window2d.rows = region.rows; window2d.cols = region.cols; Rast_set_window(&window2d); } /*open top */ mapset = NULL; name = NULL; name = param.top->answer; mapset = G_find_raster2(name, ""); in->top = open_input_map(name, mapset); in->topMapType = Rast_get_map_type(in->top); /*open bottom */ mapset = NULL; name = NULL; name = param.bottom->answer; mapset = G_find_raster2(name, ""); in->bottom = open_input_map(name, mapset); in->bottomMapType = Rast_get_map_type(in->bottom); /* Write the vtk-header and the points */ if (param.point->answer) { write_vtk_structured_grid_header(fp, output, region); write_vtk_points(in, fp, region, dp, 1, scale); } else { write_vtk_unstructured_grid_header(fp, output, region); write_vtk_points(in, fp, region, dp, 0, scale); write_vtk_unstructured_grid_cells(fp, region); } Rast_close(in->top); in->top = -1; Rast_close(in->bottom); in->bottom = -1; } else { /* Write the structured point vtk-header */ write_vtk_structured_point_header(fp, output, region, dp, scale); } /*Write the normal VTK data (cell or point data) */ /*Loop over all 3d input maps! */ if (param.input->answers != NULL) { for (i = 0; param.input->answers[i] != NULL; i++) { G_debug(3, "Open 3D raster map <%s>", param.input->answers[i]); /*Open the map */ in->map = Rast3d_open_cell_old(param.input->answers[i], G_find_raster3d(param.input->answers[i], ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (in->map == NULL) { G_warning(_("Unable to open 3D raster map <%s>"), param.input->answers[i]); fatal_error(" ", in); } /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask = 0; if (Rast3d_mask_is_off(in->map)) { Rast3d_mask_on(in->map); changemask = 1; } } } /* Write the point or cell data */ write_vtk_data(fp, in->map, region, param.input->answers[i], dp); /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(in->map) && changemask) Rast3d_mask_off(in->map); } /* Close the 3d raster map */ if (!Rast3d_close(in->map)) { in->map = NULL; fatal_error(_("Unable to close 3D raster map, the VTK file may be incomplete"), in); } in->map = NULL; } } /*Write the RGB voxel data */ open_write_rgb_maps(in, region, fp, dp); open_write_vector_maps(in, region, fp, dp); /*Close the output file */ if (param.output->answer && fp != NULL) if (fclose(fp)) fatal_error(_("Unable to close VTK-ASCII file"), in); /*close all open maps and free memory */ release_input_maps_struct(in); return 0; }
/* ************************************************************************* */ void open_write_vector_maps(input_maps * in, RASTER3D_Region region, FILE * fp, int dp) { int i, changemask[3] = {0, 0, 0}; void *mapvect = NULL; if (param.vectormaps->answers != NULL) { /*Loop over all input maps! */ for (i = 0; i < 3; i++) { G_debug(3, "Open vector 3D raster map <%s>", param.vectormaps->answers[i]); mapvect = NULL; /*Open the map */ mapvect = Rast3d_open_cell_old(param.vectormaps->answers[i], G_find_raster3d(param.vectormaps->answers[i], ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (mapvect == NULL) { G_warning(_("Unable to open 3D raster map <%s>"), param.vectormaps->answers[i]); fatal_error(_("No vector data will be created."), in); } /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask[i] = 0; if (Rast3d_mask_is_off(mapvect)) { Rast3d_mask_on(mapvect); changemask[i] = 1; } } } if (i == 0) in->map_x = mapvect; if (i == 1) in->map_y = mapvect; if (i == 2) in->map_z = mapvect; } G_debug(3, "Writing VTK Vector Data"); write_vtk_vector_data(in->map_x, in->map_y, in->map_z, fp, "Vector_Data", region, dp); for (i = 0; i < 3; i++) { if (i == 0) mapvect = in->map_x; if (i == 1) mapvect = in->map_y; if (i == 2) mapvect = in->map_z; /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(mapvect) && changemask[i]) Rast3d_mask_off(mapvect); } /* Close the 3d raster map */ if (!Rast3d_close(mapvect)) { fatal_error(_("Unable to close 3D raster map"), in); } /*Set the pointer to null so we know later that these files are already closed */ if (i == 0) in->map_x = NULL; if (i == 1) in->map_y = NULL; if (i == 2) in->map_z = NULL; } } return; }
/* ************************************************************************* */ void open_write_rgb_maps(input_maps * in, RASTER3D_Region region, FILE * fp, int dp) { int i, changemask[3] = {0, 0, 0}; void *maprgb = NULL; if (param.rgbmaps->answers != NULL) { /*Loop over all input maps! */ for (i = 0; i < 3; i++) { G_debug(3, "Open RGB 3D raster map <%s>", param.rgbmaps->answers[i]); maprgb = NULL; /*Open the map */ maprgb = Rast3d_open_cell_old(param.rgbmaps->answers[i], G_find_raster3d(param.rgbmaps->answers[i], ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (maprgb == NULL) { G_warning(_("Unable to open 3D raster map <%s>"), param.rgbmaps->answers[i]); fatal_error(_("No RGB Data will be created."), in); } /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask[i] = 0; if (Rast3d_mask_is_off(maprgb)) { Rast3d_mask_on(maprgb); changemask[i] = 1; } } } if (i == 0) in->map_r = maprgb; if (i == 1) in->map_g = maprgb; if (i == 2) in->map_b = maprgb; } G_debug(3, "Writing VTK VoxelData"); write_vtk_rgb_data(in->map_r, in->map_g, in->map_b, fp, "RGB_Voxel", region, dp); for (i = 0; i < 3; i++) { if (i == 0) maprgb = in->map_r; if (i == 1) maprgb = in->map_g; if (i == 2) maprgb = in->map_b; /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(maprgb) && changemask[i]) Rast3d_mask_off(maprgb); } /* Close the 3d raster map */ if (!Rast3d_close(maprgb)) { fatal_error(_("Unable to close 3D raster map"), in); } /*Set the pointer to null so we noe later that these files are already closed */ if (i == 0) in->map_r = NULL; if (i == 1) in->map_g = NULL; if (i == 2) in->map_b = NULL; } } return; }
/* ************************************************************************* */ void check_input_maps(void) { int i = 0; const char *mapset, *name; /*Check top and bottom if surface is requested */ if (param.structgrid->answer) { if (!param.top->answer || !param.bottom->answer) Rast3d_fatal_error(_("Specify top and bottom map")); mapset = NULL; name = NULL; name = param.top->answer; mapset = G_find_raster2(name, ""); if (mapset == NULL) { Rast3d_fatal_error(_("Top cell map <%s> not found"), param.top->answer); } mapset = NULL; name = NULL; name = param.bottom->answer; mapset = G_find_raster2(name, ""); if (mapset == NULL) { Rast3d_fatal_error(_("Bottom cell map <%s> not found"), param.bottom->answer); } } /*If input maps are provided, check them */ if (param.input->answers != NULL) { for (i = 0; param.input->answers[i] != NULL; i++) { if (NULL == G_find_raster3d(param.input->answers[i], "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), param.input->answers[i]); } } /*Check for rgb maps. */ if (param.rgbmaps->answers != NULL) { for (i = 0; i < 3; i++) { if (param.rgbmaps->answers[i] != NULL) { if (NULL == G_find_raster3d(param.rgbmaps->answers[i], "")) Rast3d_fatal_error(_("3D raster map RGB map <%s> not found"), param.rgbmaps->answers[i]); } else { Rast3d_fatal_error(_("Please provide three RGB 3D raster maps")); } } } /*Check for vector maps. */ if (param.vectormaps->answers != NULL) { for (i = 0; i < 3; i++) { if (param.vectormaps->answers[i] != NULL) { if (NULL == G_find_raster3d(param.vectormaps->answers[i], "")) Rast3d_fatal_error(_("3D vector map <%s> not found"), param.vectormaps->answers[i]); } else { Rast3d_fatal_error(_("Please provide three 3D raster maps for the xyz-vector maps [x,y,z]")); } } } if (param.input->answers == NULL && param.rgbmaps->answers == NULL && param.vectormaps->answers == NULL) { G_warning(_("No 3D raster data, RGB or xyz-vector maps are provided! Will only write the geometry.")); } return; }
/* *************************************************************** */ int main(int argc, char *argv[]) { FCELL val_f; /* for misc use */ DCELL val_d; /* for misc use */ int map_type, zmap_type; univar_stat *stats; char *infile, *zonemap; void *map, *zmap = NULL; RASTER3D_Region region; unsigned int i; unsigned int rows, cols, depths; unsigned int x, y, z; double dmin, dmax; int zone, n_zones, use_zone = 0; char *mapset, *name; struct GModule *module; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("statistics")); module->description = _("Calculates univariate statistics from the non-null 3d cells of a raster3d map."); /* Define the different options */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Set the defaults */ Rast3d_init_defaults(); /* get the current region */ Rast3d_get_window(®ion); cols = region.cols; rows = region.rows; depths = region.depths; name = param.output_file->answer; if (name != NULL && strcmp(name, "-") != 0) { if (NULL == freopen(name, "w", stdout)) { G_fatal_error(_("Unable to open file <%s> for writing"), name); } } /* table field separator */ zone_info.sep = param.separator->answer; if (strcmp(zone_info.sep, "\\t") == 0) zone_info.sep = "\t"; if (strcmp(zone_info.sep, "tab") == 0) zone_info.sep = "\t"; if (strcmp(zone_info.sep, "space") == 0) zone_info.sep = " "; if (strcmp(zone_info.sep, "comma") == 0) zone_info.sep = ","; dmin = 0.0 / 0.0; /* set to nan as default */ dmax = 0.0 / 0.0; /* set to nan as default */ zone_info.min = 0.0 / 0.0; /* set to nan as default */ zone_info.max = 0.0 / 0.0; /* set to nan as default */ zone_info.n_zones = 0; /* open 3D zoning raster with default region */ if ((zonemap = param.zonefile->answer) != NULL) { if (NULL == (mapset = G_find_raster3d(zonemap, ""))) Rast3d_fatal_error(_("3D raster map <%s> not found"), zonemap); zmap = Rast3d_open_cell_old(zonemap, G_find_raster3d(zonemap, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (zmap == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), zonemap); zmap_type = Rast3d_tile_type_map(zmap); if (Rast3d_read_cats(zonemap, mapset, &(zone_info.cats))) G_warning("No category support for zoning raster"); Rast3d_range_init(zmap); Rast3d_range_load(zmap); Rast3d_range_min_max(zmap, &dmin, &dmax); /* properly round dmin and dmax */ if (dmin < 0) zone_info.min = dmin - 0.5; else zone_info.min = dmin + 0.5; if (dmax < 0) zone_info.max = dmax - 0.5; else zone_info.max = dmax + 0.5; G_debug(1, "min: %d, max: %d", zone_info.min, zone_info.max); zone_info.n_zones = zone_info.max - zone_info.min + 1; use_zone = 1; } /* Open 3D input raster with default region */ infile = param.inputfile->answer; if (NULL == G_find_raster3d(infile, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), infile); map = Rast3d_open_cell_old(infile, G_find_raster3d(infile, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), infile); map_type = Rast3d_tile_type_map(map); i = 0; while (param.percentile->answers[i]) i++; n_zones = zone_info.n_zones; if (n_zones == 0) n_zones = 1; stats = create_univar_stat_struct(map_type, i); for (i = 0; i < n_zones; i++) { unsigned int j; for (j = 0; j < stats[i].n_perc; j++) { sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j])); } } for (z = 0; z < depths; z++) { /* From the bottom to the top */ if (!(param.shell_style->answer)) G_percent(z, depths - 1, 10); for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { zone = 0; if (zone_info.n_zones) { if (zmap_type == FCELL_TYPE) { Rast3d_get_value(zmap, x, y, z, &val_f, FCELL_TYPE); if (Rast3d_is_null_value_num(&val_f, FCELL_TYPE)) continue; if (val_f < 0) zone = val_f - 0.5; else zone = val_f + 0.5; } else if (zmap_type == DCELL_TYPE) { Rast3d_get_value(zmap, x, y, z, &val_d, DCELL_TYPE); if (Rast3d_is_null_value_num(&val_d, DCELL_TYPE)) continue; if (val_d < 0) zone = val_d - 0.5; else zone = val_d + 0.5; } zone -= zone_info.min; } if (map_type == FCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_f, map_type); if (!Rast3d_is_null_value_num(&val_f, map_type)) { if (param.extended->answer) { if (stats[zone].n >= stats[zone].n_alloc) { size_t msize; stats[zone].n_alloc += 1000; msize = stats[zone].n_alloc * sizeof(FCELL); stats[zone].fcell_array = (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize); } stats[zone].fcell_array[stats[zone].n] = val_f; } stats[zone].sum += val_f; stats[zone].sumsq += (val_f * val_f); stats[zone].sum_abs += fabs(val_f); if (stats[zone].first) { stats[zone].max = val_f; stats[zone].min = val_f; stats[zone].first = FALSE; } else { if (val_f > stats[zone].max) stats[zone].max = val_f; if (val_f < stats[zone].min) stats[zone].min = val_f; } stats[zone].n++; } stats[zone].size++; } else if (map_type == DCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_d, map_type); if (!Rast3d_is_null_value_num(&val_d, map_type)) { if (param.extended->answer) { if (stats[zone].n >= stats[zone].n_alloc) { size_t msize; stats[zone].n_alloc += 1000; msize = stats[zone].n_alloc * sizeof(DCELL); stats[zone].dcell_array = (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize); } stats[zone].dcell_array[stats[zone].n] = val_d; } stats[zone].sum += val_d; stats[zone].sumsq += val_d * val_d; stats[zone].sum_abs += fabs(val_d); if (stats[zone].first) { stats[zone].max = val_d; stats[zone].min = val_d; stats[zone].first = FALSE; } else { if (val_d > stats[zone].max) stats[zone].max = val_d; if (val_d < stats[zone].min) stats[zone].min = val_d; } stats[zone].n++; } stats[zone].size++; } } } } /* close maps */ Rast3d_close(map); if (zone_info.n_zones) Rast3d_close(zmap); /* create the output */ if (param.table->answer) print_stats_table(stats); else print_stats(stats); /* release memory */ free_univar_stat_struct(stats); exit(EXIT_SUCCESS); }
static void modifyNull(char *name, d_Mask * maskRules, int changeNull, double newNullVal) { void *map, *mapOut; RASTER3D_Region region; int tileX, tileY, tileZ, x, y, z; double value; int doCompress, doLzw, doRle, precision; int cacheSize; cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1); if (NULL == G_find_raster3d(name, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), name); fprintf(stderr, "name %s Mapset %s \n", name, G_mapset()); map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW, DCELL_TYPE, cacheSize); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name); Rast3d_get_region_struct_map(map, ®ion); Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ); Rast3d_get_compression_mode(&doCompress, &doLzw, &doRle, &precision); mapOut = Rast3d_open_new_param(name, DCELL_TYPE, RASTER3D_USE_CACHE_XY, ®ion, Rast3d_file_type_map(map), doLzw, doRle, Rast3d_tile_precision_map(map), tileX, tileY, tileZ); if (mapOut == NULL) Rast3d_fatal_error(_("modifyNull: error opening tmp file")); Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X); Rast3d_autolock_on(map); Rast3d_unlock_all(map); Rast3d_min_unlocked(mapOut, RASTER3D_USE_CACHE_X); Rast3d_autolock_on(mapOut); Rast3d_unlock_all(mapOut); for (z = 0; z < region.depths; z++) { if ((z % tileZ) == 0) { Rast3d_unlock_all(map); Rast3d_unlock_all(mapOut); } for (y = 0; y < region.rows; y++) for (x = 0; x < region.cols; x++) { value = Rast3d_get_double_region(map, x, y, z); if (Rast3d_is_null_value_num(&value, DCELL_TYPE)) { if (changeNull) { value = newNullVal; } } else if (Rast3d_mask_d_select((DCELL *) & value, maskRules)) { Rast3d_set_null_value(&value, 1, DCELL_TYPE); } Rast3d_put_double(mapOut, x, y, z, value); } if ((z % tileZ) == 0) { if (!Rast3d_flush_tiles_in_cube (mapOut, 0, 0, MAX(0, z - tileZ), region.rows - 1, region.cols - 1, z)) Rast3d_fatal_error(_("modifyNull: error flushing tiles in cube")); } } if (!Rast3d_flush_all_tiles(mapOut)) Rast3d_fatal_error(_("modifyNull: error flushing all tiles")); Rast3d_autolock_off(map); Rast3d_unlock_all(map); Rast3d_autolock_off(mapOut); Rast3d_unlock_all(mapOut); if (!Rast3d_close(map)) Rast3d_fatal_error(_("Unable to close raster map")); if (!Rast3d_close(mapOut)) Rast3d_fatal_error(_("modifyNull: Unable to close tmp file")); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Flag *r, *w, *l, *g, *a, *n, *c; } flag; struct { struct Option *map, *field, *colr, *rast, *volume, *rules, *attrcol, *rgbcol, *range, *use; } opt; int layer; int overwrite, remove, is_from_stdin, stat, have_colors, convert, use; const char *mapset, *cmapset; const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn; char *name; struct Map_info Map; struct FPRange range; struct Colors colors, colors_tmp; /* struct Cell_stats statf; */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("color table")); module->description = _("Creates/modifies the color table associated with a vector map."); opt.map = G_define_standard_option(G_OPT_V_MAP); opt.field = G_define_standard_option(G_OPT_V_FIELD); opt.use = G_define_option(); opt.use->key = "use"; opt.use->type = TYPE_STRING; opt.use->required = YES; opt.use->multiple = NO; opt.use->options = "attr,cat,z"; opt.use->description = _("Source values"); G_asprintf((char **) &(opt.use->descriptions), "attr;%s;cat;%s;z;%s", _("read values from attribute table (requires <column> option)"), _("use category values"), _("use z coordinate (3D points or centroids only)")); opt.use->answer = "cat"; opt.attrcol = G_define_standard_option(G_OPT_DB_COLUMN); opt.attrcol->label = _("Name of column containing numeric data"); opt.attrcol->description = _("Required for use=attr"); opt.attrcol->guisection = _("Define"); opt.range = G_define_option(); opt.range->key = "range"; opt.range->type = TYPE_DOUBLE; opt.range->required = NO; opt.range->label = _("Manually set range (refers to 'column' option)"); opt.range->description = _("Ignored when 'rules' given"); opt.range->key_desc = "min,max"; opt.colr = G_define_standard_option(G_OPT_M_COLR); opt.colr->guisection = _("Define"); opt.rast = G_define_standard_option(G_OPT_R_INPUT); opt.rast->key = "raster"; opt.rast->required = NO; opt.rast->description = _("Raster map from which to copy color table"); opt.rast->guisection = _("Define"); opt.volume = G_define_standard_option(G_OPT_R3_INPUT); opt.volume->key = "raster_3d"; opt.volume->required = NO; opt.volume->description = _("3D raster map from which to copy color table"); opt.volume->guisection = _("Define"); 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"); opt.rules->guisection = _("Define"); opt.rgbcol = G_define_standard_option(G_OPT_DB_COLUMN); opt.rgbcol->key = "rgb_column"; opt.rgbcol->label = _("Name of color column to populate RGB values"); opt.rgbcol->description = _("If not given writes color table"); flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Remove existing color table"); flag.r->guisection = _("Remove"); flag.w = G_define_flag(); flag.w->key = 'w'; flag.w->description = _("Only write new color table if it does not already exist"); flag.l = G_define_flag(); flag.l->key = 'l'; flag.l->description = _("List available rules then exit"); flag.l->suppress_required = YES; flag.l->guisection = _("Print"); flag.n = G_define_flag(); flag.n->key = 'n'; flag.n->description = _("Invert colors"); flag.n->guisection = _("Define"); flag.g = G_define_flag(); flag.g->key = 'g'; flag.g->description = _("Logarithmic scaling"); flag.g->guisection = _("Define"); flag.a = G_define_flag(); flag.a->key = 'a'; flag.a->description = _("Logarithmic-absolute scaling"); flag.a->guisection = _("Define"); flag.c = G_define_flag(); flag.c->key = 'c'; flag.c->label = _("Convert color rules from RGB values to color table"); flag.c->description = _("Option 'rgb_column' with valid RGB values required"); /* TODO ? flag.e = G_define_flag(); flag.e->key = 'e'; flag.e->description = _("Histogram equalization"); flag.e->guisection = _("Define"); */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (flag.l->answer) { G_list_color_rules(stdout); return EXIT_SUCCESS; } overwrite = !flag.w->answer; remove = flag.r->answer; name = opt.map->answer; style = opt.colr->answer; rules = opt.rules->answer; attrcolumn = opt.attrcol->answer; rgbcolumn = opt.rgbcol->answer; convert = flag.c->answer; use = USE_CAT; if (opt.use->answer) { switch (opt.use->answer[0]) { case 'a': use = USE_ATTR; break; case 'c': use = USE_CAT; break; case 'z': use = USE_Z; break; default: break; } } G_debug(1, "use=%d", use); if (!name) G_fatal_error(_("No vector map specified")); if (use == USE_ATTR && !attrcolumn) G_fatal_error(_("Option <%s> required"), opt.attrcol->key); if (use != USE_ATTR && attrcolumn) { G_important_message(_("Option <%s> given, assuming <use=attr>..."), opt.attrcol->key); use = USE_ATTR; } if (opt.rast->answer && opt.volume->answer) G_fatal_error(_("%s= and %s= are mutually exclusive"), opt.rast->key, opt.volume->key); cmap = NULL; if (opt.rast->answer) cmap = opt.rast->answer; if (opt.volume->answer) cmap = opt.volume->answer; if (!cmap && !style && !rules && !remove && !convert) G_fatal_error(_("One of -%c, -%c or %s=, %s= or %s= " "must be specified"), flag.r->key, flag.c->key, opt.colr->key, opt.rast->key, opt.rules->key); if (!!style + !!cmap + !!rules > 1) G_fatal_error(_("%s=, %s= and %s= are mutually exclusive"), opt.colr->key, opt.rules->key, opt.rast->key); if (flag.g->answer && flag.a->answer) G_fatal_error(_("-%c and -%c are mutually exclusive"), flag.g->key, flag.a->key); if (flag.c->answer && !rgbcolumn) G_fatal_error(_("%s= required for -%c"), opt.rgbcol->key, flag.c->key); is_from_stdin = rules && strcmp(rules, "-") == 0; if (is_from_stdin) G_fatal_error(_("Reading rules from standard input is not implemented yet, please provide path to rules file instead.")); mapset = G_find_vector(name, ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), name); stat = -1; if (remove) { stat = Vect_remove_colors(name, mapset); if (stat < 0) G_fatal_error(_("Unable to remove color table of vector map <%s>"), name); if (stat == 0) G_warning(_("Color table of vector map <%s> not found"), name); return EXIT_SUCCESS; } G_suppress_warnings(TRUE); have_colors = Vect_read_colors(name, mapset, NULL); if (have_colors > 0 && !overwrite) { G_fatal_error(_("Color table exists. Exiting.")); } G_suppress_warnings(FALSE); /* open map and get min/max values */ Vect_set_open_level(1); /* no topology required */ if (Vect_open_old2(&Map, name, mapset, opt.field->answer) < 0) G_fatal_error(_("Unable to open vector map <%s>"), name); Vect_set_error_handler_io(&Map, NULL); if (use == USE_Z && !Vect_is_3d(&Map)) G_fatal_error(_("Vector map <%s> is not 3D"), Vect_get_full_name(&Map)); layer = Vect_get_field_number(&Map, opt.field->answer); if (layer < 1) G_fatal_error(_("Layer <%s> not found"), opt.field->answer); if (opt.range->answer) { range.min = atof(opt.range->answers[0]); range.max = atof(opt.range->answers[1]); if (range.min > range.max) G_fatal_error(_("Option <%s>: min must be greater or equal to max"), opt.range->key); } Rast_init_colors(&colors); if (is_from_stdin) { G_fatal_error(_("Reading color rules from standard input is currently not supported")); /* if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); */ } else if (style || rules) { if (style && !G_find_color_rule(style)) G_fatal_error(_("Color table <%s> not found"), style); if (use == USE_CAT) { scan_cats(&Map, layer, style, rules, opt.range->answer ? &range : NULL, &colors); } else if (use == USE_Z) { scan_z(&Map, layer, style, rules, opt.range->answer ? &range : NULL, &colors); } else { scan_attr(&Map, layer, attrcolumn, style, rules, opt.range->answer ? &range : NULL, &colors); } } else { /* use color from another map (cmap) */ if (opt.rast->answer) { cmapset = G_find_raster2(cmap, ""); if (!cmapset) G_fatal_error(_("Raster map <%s> not found"), cmap); if (Rast_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap); } else if (opt.volume->answer) { cmapset = G_find_raster3d(cmap, ""); if (!cmapset) G_fatal_error(_("3D raster map <%s> not found"), cmap); if (Rast3d_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for 3D raster map <%s>"), cmap); } } if (flag.n->answer) Rast_invert_colors(&colors); /* TODO ? if (flag.e->answer) { if (!have_stats) have_stats = get_stats(name, mapset, &statf); Rast_histogram_eq_colors(&colors_tmp, &colors, &statf); colors = colors_tmp; } */ if (flag.g->answer) { Rast_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (flag.a->answer) { Rast_abs_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } G_important_message(_("Writing color rules...")); if (style || rules || opt.rast->answer || opt.volume->answer) { if (rgbcolumn) write_rgb_values(&Map, layer, rgbcolumn, &colors); else Vect_write_colors(name, mapset, &colors); } if (convert) { /* convert RGB values to color tables */ rgb2colr(&Map, layer, rgbcolumn, &colors); Vect_write_colors(name, mapset, &colors); } Vect_close(&Map); G_message(_("Color table for vector map <%s> set to '%s'"), G_fully_qualified_name(name, mapset), is_from_stdin || convert ? "rules" : style ? style : rules ? rules : cmap); exit(EXIT_SUCCESS); }
static void makeMask(char *name, d_Mask * maskRules) { void *map, *mask; RASTER3D_Region region; int tileX, tileY, tileZ, x, y, z, cacheSize; double value; float floatNull; cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1); if (NULL == G_find_raster3d(name, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), name); map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW, DCELL_TYPE, cacheSize); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name); Rast3d_get_region_struct_map(map, ®ion); Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ); mask = Rast3d_open_new_param(Rast3d_mask_file(), FCELL_TYPE, cacheSize, ®ion, FCELL_TYPE, RASTER3D_COMPRESSION, 0, tileX, tileY, tileZ); if (mask == NULL) Rast3d_fatal_error(_("Unable to open 3D raster mask file")); Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X); Rast3d_autolock_on(map); Rast3d_unlock_all(map); Rast3d_min_unlocked(mask, RASTER3D_USE_CACHE_X); Rast3d_autolock_on(mask); Rast3d_unlock_all(mask); Rast3d_set_null_value(&floatNull, 1, FCELL_TYPE); for (z = 0; z < region.depths; z++) { if ((z % tileZ) == 0) { Rast3d_unlock_all(map); Rast3d_unlock_all(mask); } for (y = 0; y < region.rows; y++) /* We count from north to south in the cube coordinate system */ for (x = 0; x < region.cols; x++) { value = Rast3d_get_double_region(map, x, y, z); if (Rast3d_mask_d_select((DCELL *) & value, maskRules)) Rast3d_put_float(mask, x, y, z, (float)floatNull); /* mask-out value */ else Rast3d_put_float(mask, x, y, z, (float)0.0); /* not mask-out value */ } if ((z % tileZ) == 0) { if (!Rast3d_flush_tiles_in_cube (mask, 0, 0, MAX(0, z - tileZ), region.rows - 1, region.cols - 1, z)) Rast3d_fatal_error(_("makeMask: error flushing tiles in cube")); } } if (!Rast3d_flush_all_tiles(mask)) Rast3d_fatal_error(_("makeMask: error flushing all tiles")); Rast3d_autolock_off(map); Rast3d_unlock_all(map); Rast3d_autolock_off(mask); Rast3d_unlock_all(mask); if (!Rast3d_close(mask)) Rast3d_fatal_error(_("Unable to close 3D raster mask file")); if (!Rast3d_close(map)) Rast3d_fatal_error(_("Unable to close raster map <%s>"), name); }
int main(int argc, char *argv[]) { const char *mapset; struct GModule *module; struct Option *raster, *title_opt, *history_opt; struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt; struct Option *map_opt, *units_opt, *vunits_opt; struct Option *load_opt, *save_opt; struct Flag *stats_flag; 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(_("raster3d")); G_add_keyword(_("metadata")); G_add_keyword(_("voxel")); module->description = _("Allows creation and/or modification of " "3D raster map layer support files."); raster = G_define_standard_option(G_OPT_R3_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 = _("Text to use for map title"); 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 = "unit"; units_opt->type = TYPE_STRING; units_opt->required = NO; units_opt->description = _("The map data unit"); vunits_opt = G_define_option(); vunits_opt->key = "vunit"; vunits_opt->type = TYPE_STRING; vunits_opt->required = NO; vunits_opt->description = _("The vertical unit of the map"); 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 range"); /* 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_raster3d(infile, G_mapset()); /* current mapset only for editing */ if (!mapset || strcmp(mapset, G_mapset()) != 0) G_fatal_error(_("3D raster map <%s> not found"), infile); if (title_opt->answer) { strncpy(title, title_opt->answer, MAX_TITLE_LEN); title[MAX_TITLE_LEN - 1] = '\0'; /* strncpy doesn't null terminate over-sized input */ G_strip(title); G_debug(3, "map title= [%s] (%d chars)", title, (int)strlen(title)); Rast3d_read_history(raster->answer, "", &hist); Rast_set_history(&hist, HIST_TITLE, title); Rast3d_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); Rast3d_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); Rast3d_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); Rast3d_write_history(raster->answer, &hist); } if (history_opt->answer) { Rast3d_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); Rast3d_write_history(raster->answer, &hist); } if(units_opt->answer || vunits_opt->answer) { RASTER3D_Map *map; map = Rast3d_open_cell_old(raster->answer, G_mapset(), \ RASTER3D_DEFAULT_WINDOW, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); /* Modify the units */ if (units_opt->answer) Rast3d_set_unit(map, units_opt->answer); if (vunits_opt->answer) Rast3d_set_vertical_unit(map, vunits_opt->answer); Rast3d_rewrite_header(map); Rast3d_close(map); } if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) { Rast3d_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); Rast3d_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 (Rast3d_read_cats(map_opt->answer, "", &cats) < 0) G_fatal_error(_("Unable to read category file of raster map <%s>"), map_opt->answer); Rast3d_write_cats(infile, &cats); G_message(_("cats table for [%s] set to %s"), infile, map_opt->answer); Rast_close(fd); Rast_free_cats(&cats); } /* Check the histogram and range */ if (stats_flag->answer) check_stats(raster->answer); return EXIT_SUCCESS; }