void *Rast3d_open_cell_old_no_header(const char *name, const char *mapset) { RASTER3D_Map *map; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; Rast3d_init_defaults(); if (!Rast3d_mask_open_old()) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_mask_open_old")); return (void *)NULL; } map = Rast3d_malloc(sizeof(RASTER3D_Map)); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_malloc")); return (void *)NULL; } G_unqualified_name(name, mapset, xname, xmapset); map->fileName = G_store(xname); map->mapset = G_store(xmapset); map->data_fd = G_open_old_misc(RASTER3D_DIRECTORY, RASTER3D_CELL_ELEMENT, xname, xmapset); if (map->data_fd < 0) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in G_open_old")); return (void *)NULL; } Rast3d_range_init(map); Rast3d_mask_off(map); return map; }
int Rast3d_get_standard3d_params(int *useTypeDefault, int *type, int *useCompressionDefault, int *doCompression, int *usePrecisionDefault, int *precision, int *useDimensionDefault, int *tileX, int *tileY, int *tileZ) { *useTypeDefault = *useCompressionDefault = 0; *usePrecisionDefault = *useDimensionDefault = 0; Rast3d_init_defaults(); if (strcmp(param->type->answer, "double") == 0) *type = DCELL_TYPE; else if (strcmp(param->type->answer, "float") == 0) *type = FCELL_TYPE; else { *type = Rast3d_get_file_type(); *useTypeDefault = 1; } Rast3d_get_compression_mode(doCompression, precision); if (strcmp(param->precision->answer, "default") != 0) { if (strcmp(param->precision->answer, "max") == 0) *precision = -1; else if ((sscanf(param->precision->answer, "%d", precision) != 1) || (*precision < 0)) { Rast3d_error(_("Rast3d_get_standard3d_params: precision value invalid")); return 0; } } else *usePrecisionDefault = 1; if (strcmp(param->compression->answer, "default") != 0) { if (strcmp(param->compression->answer, "zip") == 0) *doCompression = RASTER3D_COMPRESSION; else *doCompression = RASTER3D_NO_COMPRESSION; } else { *useCompressionDefault = 1; } Rast3d_get_tile_dimension(tileX, tileY, tileZ); if (strcmp(param->dimension->answer, "default") != 0) { if (sscanf(param->dimension->answer, "%dx%dx%d", tileX, tileY, tileZ) != 3) { Rast3d_error(_("Rast3d_get_standard3d_params: tile dimension value invalid")); return 0; } } else *useDimensionDefault = 1; Rast3d_free(param); 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 test_array_3d(void) { int sum = 0, res = 0; char buff[1024]; RASTER3D_Region region; N_array_3d *data1; N_array_3d *data11; N_array_3d *data2; N_array_3d *data22; N_array_3d *tmp; double min, max, ssum; int nonzero; /*Alloacte memory for all arrays */ data1 = N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 2, FCELL_TYPE); N_print_array_3d_info(data1); data11 = N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 2, FCELL_TYPE); data2 = N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 2, DCELL_TYPE); N_print_array_3d_info(data2); data22 = N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 2, DCELL_TYPE); /*Fill the first arrays with data */ res = fill_array_3d(data1); if (res != 0) G_warning("test_array_3d: error while filling array with values"); sum += res; res = fill_array_3d(data2); if (res != 0) G_warning("test_array_3d: error while filling array with values"); sum += res; /*Copy the data */ N_copy_array_3d(data1, data11); N_copy_array_3d(data2, data22); /*Compare the data */ res = compare_array_3d(data1, data11); if (res != 0) G_warning("test_array_3d: error in N_copy_array_2d"); sum += res; res = compare_array_3d(data1, data11); if (res != 0) G_warning("test_array_3d: error in N_copy_array_2d"); sum += res; /*compute statistics */ N_calc_array_3d_stats(data1, &min, &max, &ssum, &nonzero, 0); G_message("FELL Min %g Max %g Sum %g nonzero %i\n", min, max, ssum, nonzero); if (min != 0 || max != 729 || ssum != 91125 || nonzero != 1000) { G_warning("test_array_3d: error in N_calc_array_3d_stats"); sum++; } N_calc_array_3d_stats(data1, &min, &max, &ssum, &nonzero, 1); G_message("FELL Min %g Max %g Sum %g nonzero %i\n", min, max, ssum, nonzero); if (min != 0 || max != 729 || ssum != 91125 || nonzero != 2744) { G_warning("test_array_3d: error in N_calc_array_3d_stats"); sum++; } N_calc_array_3d_stats(data2, &min, &max, &ssum, &nonzero, 0); G_message("DCELL Min %g Max %g Sum %g nonzero %i\n", min, max, ssum, nonzero); if (min != 0 || max != 729 || ssum != 91125 || nonzero != 1000) { G_warning("test_array_3d: error in N_calc_array_3d_stats"); sum++; } N_calc_array_3d_stats(data2, &min, &max, &ssum, &nonzero, 1); G_message("DCELL Min %g Max %g Sum %g nonzero %i\n", min, max, ssum, nonzero); if (min != 0 || max != 729 || ssum != 91125 || nonzero != 2744) { G_warning("test_array_3d: error in N_calc_array_3d_stats"); sum++; } /*test the array math functions */ tmp = N_math_array_3d(data1, data2, NULL, N_ARRAY_SUM); N_math_array_3d(data2, data2, tmp, N_ARRAY_SUM); res = N_convert_array_3d_null_to_zero(tmp); if (res != 0) G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum = res; N_free_array_3d(tmp); tmp = N_math_array_3d(data2, data1, NULL, N_ARRAY_DIF); N_math_array_3d(data1, data2, tmp, N_ARRAY_DIF); res = N_convert_array_3d_null_to_zero(tmp); if (res != 0) G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum = res; N_free_array_3d(tmp); tmp = N_math_array_3d(data1, data1, NULL, N_ARRAY_MUL); N_math_array_3d(data1, data1, tmp, N_ARRAY_MUL); res = N_convert_array_3d_null_to_zero(tmp); if (res != 0) G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum = res; N_free_array_3d(tmp); tmp = N_math_array_3d(data2, data1, NULL, N_ARRAY_DIV); N_math_array_3d(data1, data2, tmp, N_ARRAY_DIV); res = N_convert_array_3d_null_to_zero(tmp); if (res == 0) { /* if a division with zero is detected, the value is set to null, not to nan */ G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum++; } N_free_array_3d(tmp); /*check for correct norm calculation */ if (N_norm_array_3d(data1, data11, N_EUKLID_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data11, N_MAXIMUM_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data2, N_EUKLID_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data2, N_MAXIMUM_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } /*fill arrays with null values */ res = fill_array_3d_null(data1); if (res != 0) G_warning ("test_array_3d: error while filling array with float null values"); sum += res; res = fill_array_3d_null(data2); if (res != 0) G_warning ("test_array_3d: error while filling array with double null values"); sum += res; /*Copy the data */ N_copy_array_3d(data1, data11); N_copy_array_3d(data2, data22); /*Compare the data */ compare_array_3d(data1, data11); compare_array_3d(data2, data22); /*test the array math functions */ tmp = N_math_array_3d(data1, data2, NULL, N_ARRAY_SUM); N_math_array_3d(data2, data2, tmp, N_ARRAY_SUM); res = N_convert_array_3d_null_to_zero(tmp); if (res == 0) { G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum++; } N_free_array_3d(tmp); tmp = N_math_array_3d(data2, data1, NULL, N_ARRAY_DIF); N_math_array_3d(data1, data2, tmp, N_ARRAY_DIF); res = N_convert_array_3d_null_to_zero(tmp); if (res == 0) { G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum++; } N_free_array_3d(tmp); tmp = N_math_array_3d(data1, data1, NULL, N_ARRAY_MUL); N_math_array_3d(data1, data1, tmp, N_ARRAY_MUL); res = N_convert_array_3d_null_to_zero(tmp); if (res == 0) { G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum++; } N_free_array_3d(tmp); tmp = N_math_array_3d(data2, data1, NULL, N_ARRAY_DIV); N_math_array_3d(data1, data2, tmp, N_ARRAY_DIV); res = N_convert_array_3d_null_to_zero(tmp); if (res == 0) { G_warning("test_array_3d: error in N_convert_array_3d_null_to_zero"); sum++; } N_free_array_3d(tmp); /*check for correct norm calculation in case of null values */ if (N_norm_array_3d(data1, data11, N_EUKLID_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data11, N_MAXIMUM_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data2, N_EUKLID_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } if (N_norm_array_3d(data1, data2, N_MAXIMUM_NORM) != 0.0) { G_warning("test_array_3d: error in N_norm_array_3d"); sum++; } N_free_array_3d(data1); N_free_array_3d(data2); /*Set the defaults */ Rast3d_init_defaults(); Rast3d_get_window(®ion); data1 = N_alloc_array_3d(region.cols, region.rows, region.depths, 0, FCELL_TYPE); data2 = N_alloc_array_3d(region.cols, region.rows, region.depths, 0, DCELL_TYPE); fill_array_3d(data1); fill_array_3d(data2); /*Volume IO methods */ N_write_array_3d_to_rast3d(data1, "gpde_lib_test_volume_1", 1); N_write_array_3d_to_rast3d(data2, "gpde_lib_test_volume_2", 1); tmp = N_read_rast3d_to_array_3d("gpde_lib_test_volume_1", NULL, 1); N_read_rast3d_to_array_3d("gpde_lib_test_volume_1", tmp, 1); N_free_array_3d(tmp); tmp = N_read_rast3d_to_array_3d("gpde_lib_test_volume_2", NULL, 1); N_read_rast3d_to_array_3d("gpde_lib_test_volume_2", tmp, 1); N_free_array_3d(tmp); sprintf(buff, "g.remove rast3d=gpde_lib_test_volume_1,gpde_lib_test_volume_2"); system(buff); N_free_array_3d(data1); N_free_array_3d(data11); N_free_array_3d(data2); N_free_array_3d(data22); return sum; }
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; }
void *Rast3d_open_cell_new(const char *name, int typeIntern, int cache, RASTER3D_Region * region) { RASTER3D_Map *map; int nofHeaderBytes, dummy = 0, compression, precision; long ldummy = 0; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; Rast3d_init_defaults(); if (!Rast3d_mask_open_old()) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_mask_open_old")); return (void *)NULL; } compression = g3d_do_compression; precision = g3d_precision; map = Rast3d_malloc(sizeof(RASTER3D_Map)); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_malloc")); return (void *)NULL; } if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0) { G_warning(_("map <%s> is not in the current mapset"), name); return (void *)NULL; } map->fileName = G_store(xname); map->mapset = G_store(xmapset); map->tempName = G_tempfile(); map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666); if (map->data_fd < 0) { Rast3d_error(_("Rast3d_open_cell_new: could not open file")); return (void *)NULL; } Rast3d_make_mapset_map_directory(map->fileName); map->useXdr = RASTER3D_USE_XDR; if (g3d_file_type == FCELL_TYPE) { if (precision > 23) precision = 23; /* 32 - 8 - 1 */ else if (precision < -1) precision = 0; } else if (precision > 52) precision = 52; /* 64 - 11 - 1 */ else if (precision < -1) precision = 0; /* no need to write trailing zeros */ if ((typeIntern == FCELL_TYPE) && (g3d_file_type == DCELL_TYPE)) { if (precision == -1) precision = 23; else precision = RASTER3D_MIN(precision, 23); } if (compression == RASTER3D_NO_COMPRESSION) precision = RASTER3D_MAX_PRECISION; if (compression == RASTER3D_COMPRESSION) map->useXdr = RASTER3D_USE_XDR; if (RASTER3D_HAS_INDEX) { map->indexLongNbytes = sizeof(long); /* at the beginning of the file write */ /* nof bytes of "long" */ /* max nof bytes used for index */ /* position of index in file */ /* the index is appended at the end of the file at closing time. since */ /* we do not know this position yet we write dummy values */ if ((!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexLongNbytes), 1)) || (!Rast3d_write_ints(map->data_fd, map->useXdr, &dummy, 1))) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } if (write(map->data_fd, &ldummy, map->indexLongNbytes) != map->indexLongNbytes) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } } /* can't use a constant since this depends on sizeof (long) */ nofHeaderBytes = lseek(map->data_fd, (long)0, SEEK_CUR); Rast3d_range_init(map); Rast3d_adjust_region(region); if (!Rast3d_fill_header(map, RASTER3D_WRITE_DATA, compression, g3d_do_rle_compression, g3d_do_lzw_compression, g3d_file_type, precision, cache, RASTER3D_HAS_INDEX, map->useXdr, typeIntern, nofHeaderBytes, g3d_tile_dimension[0], g3d_tile_dimension[1], g3d_tile_dimension[2], region->proj, region->zone, region->north, region->south, region->east, region->west, region->top, region->bottom, region->rows, region->cols, region->depths, region->ew_res, region->ns_res, region->tb_res, g3d_unit_default)) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_fill_header")); return (void *)NULL; } /*Set the map window to the map region */ Rast3d_region_copy(&(map->window), region); /*Set the resampling function to nearest neighbor for data access */ Rast3d_get_nearest_neighbor_fun_ptr(&(map->resampleFun)); Rast3d_mask_off(map); return (void *)map; }
/* ************************************************************************* */ int main(int argc, char *argv[]) { struct GModule *module; int returnstat = 0, i; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); module->description = _("Performs unit and integration tests for the g3d library"); /* Get parameters from user */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Initiate the defaults for testing */ Rast3d_init_defaults(); /*Run the unit tests */ if (param.testunit->answer || param.full->answer) { returnstat += unit_test_coordinate_transform(); returnstat += unit_test_put_get_value(); } /*Run the integration tests */ if (param.testint->answer || param.full->answer) { ; } /*Run single tests */ if (!param.full->answer) { /*unit tests */ if (!param.testunit->answer) { i = 0; if (param.unit->answers) while (param.unit->answers[i]) { if (strcmp(param.unit->answers[i], "coord") == 0) returnstat += unit_test_coordinate_transform(); if (strcmp(param.unit->answers[i], "putget") == 0) returnstat += unit_test_put_get_value(); i++; } } /*integration tests */ if (!param.testint->answer) { i = 0; if (param.integration->answers) while (param.integration->answers[i]) { ; } } } if (returnstat != 0) G_warning("Errors detected while testing the g3d lib"); else G_message("\n-- g3d lib tests finished successfully --"); return (returnstat); }
/* ************************************************************************* */ 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[]) { struct GModule *module; struct { struct Option *input; struct Option *output; struct Option *null; struct Option *bytes; struct Option *order; struct Option *north; struct Option *south; struct Option *top; struct Option *bottom; struct Option *east; struct Option *west; struct Option *rows; struct Option *cols; struct Option *depths; } parm; struct { struct Flag *integer_in; struct Flag *sign; struct Flag *depth; struct Flag *row; } flag; const char *input; const char *output; int is_integer; int is_signed; int bytes; int order; int byte_swap; RASTER_MAP_TYPE map_type; off_t file_size; struct History history; off_t expected; /* Need to be allocated later */ in_cell = NULL; G_gisinit(argv[0]); /* Set description */ module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("import")); G_add_keyword(_("voxel")); module->description = _("Imports a binary raster file into a GRASS 3D raster map."); parm.input = G_define_standard_option(G_OPT_F_BIN_INPUT); parm.input->description = _("Name of binary 3D raster file to be imported"); parm.output = G_define_standard_option(G_OPT_R3_OUTPUT); parm.bytes = G_define_option(); parm.bytes->key = "bytes"; parm.bytes->type = TYPE_INTEGER; parm.bytes->required = YES; parm.bytes->options = "1,2,4,8"; parm.bytes->description = _("Number of bytes per cell in binary file"); parm.order = G_define_option(); parm.order->key = "order"; parm.order->type = TYPE_STRING; parm.order->required = NO; parm.order->options = "big,little,native,swap"; parm.order->description = _("Byte order in binary file"); parm.order->answer = "native"; parm.north = G_define_option(); parm.north->key = "north"; parm.north->type = TYPE_DOUBLE; parm.north->required = YES; parm.north->description = _("Northern limit of geographic region (outer edge)"); parm.north->guisection = _("Bounds"); parm.south = G_define_option(); parm.south->key = "south"; parm.south->type = TYPE_DOUBLE; parm.south->required = YES; parm.south->description = _("Southern limit of geographic region (outer edge)"); parm.south->guisection = _("Bounds"); parm.east = G_define_option(); parm.east->key = "east"; parm.east->type = TYPE_DOUBLE; parm.east->required = YES; parm.east->description = _("Eastern limit of geographic region (outer edge)"); parm.east->guisection = _("Bounds"); parm.west = G_define_option(); parm.west->key = "west"; parm.west->type = TYPE_DOUBLE; parm.west->required = YES; parm.west->description = _("Western limit of geographic region (outer edge)"); parm.west->guisection = _("Bounds"); parm.bottom = G_define_option(); parm.bottom->key = "bottom"; parm.bottom->type = TYPE_DOUBLE; parm.bottom->required = YES; parm.bottom->description = _("Bottom limit of geographic region (outer edge)"); parm.bottom->guisection = _("Bounds"); parm.top = G_define_option(); parm.top->key = "top"; parm.top->type = TYPE_DOUBLE; parm.top->required = YES; parm.top->description = _("Top limit of geographic region (outer edge)"); parm.top->guisection = _("Bounds"); parm.rows = G_define_option(); parm.rows->key = "rows"; parm.rows->type = TYPE_INTEGER; parm.rows->required = YES; parm.rows->description = _("Number of rows"); parm.rows->guisection = _("Bounds"); parm.cols = G_define_option(); parm.cols->key = "cols"; parm.cols->type = TYPE_INTEGER; parm.cols->required = YES; parm.cols->description = _("Number of columns"); parm.cols->guisection = _("Bounds"); parm.depths = G_define_option(); parm.depths->key = "depths"; parm.depths->type = TYPE_INTEGER; parm.depths->required = YES; parm.depths->description = _("Number of depths"); parm.depths->guisection = _("Bounds"); parm.null = G_define_option(); parm.null->key = "null"; parm.null->type = TYPE_DOUBLE; parm.null->required = NO; parm.null->description = _("Set Value to NULL"); flag.row = G_define_flag(); flag.row->key = 'r'; flag.row->description = _("Switch the row order in output from " "north->south to south->north"); flag.depth = G_define_flag(); flag.depth->key = 'd'; flag.depth->description = _("Switch the depth order in output " "from bottom->top to top->bottom"); flag.integer_in = G_define_flag(); flag.integer_in->key = 'i'; flag.integer_in->description = _("Binary data is of type integer"); flag.sign = G_define_flag(); flag.sign->key = 's'; flag.sign->description = _("Signed data (two's complement)"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); input = parm.input->answer; output = parm.output->answer; if (G_strcasecmp(parm.order->answer, "big") == 0) order = 0; else if (G_strcasecmp(parm.order->answer, "little") == 0) order = 1; else if (G_strcasecmp(parm.order->answer, "native") == 0) order = G_is_little_endian() ? 1 : 0; else if (G_strcasecmp(parm.order->answer, "swap") == 0) order = G_is_little_endian() ? 0 : 1; byte_swap = order == (G_is_little_endian() ? 0 : 1); is_signed = !!flag.sign->answer; is_integer = 0; bytes = 8; if (parm.bytes->answer) bytes = atoi(parm.bytes->answer); if (!flag.integer_in->answer) { if (bytes && bytes < 4) G_fatal_error( _("bytes=%d; must be 4 or 8 in case of floating point input"), bytes); if (!bytes) bytes = 4; } else { is_integer = 1; } #ifndef HAVE_LONG_LONG_INT if (is_integer && bytes > 4) G_fatal_error(_("Integer input doesn't support size=8 in this build")); #endif if (bytes != 1 && bytes != 2 && bytes != 4 && bytes != 8) G_fatal_error(_("bytes= must be 1, 2, 4 or 8")); region.zone = G_zone(); region.proj = G_projection(); region.rows = atoi(parm.rows->answer); region.cols = atoi(parm.cols->answer); region.depths = atoi(parm.depths->answer); region.top = atof(parm.top->answer); region.bottom = atof(parm.bottom->answer); if (!G_scan_northing(parm.north->answer, ®ion.north, region.proj)) G_fatal_error(_("Illegal north coordinate <%s>"), parm.north->answer); if (!G_scan_northing(parm.south->answer, ®ion.south, region.proj)) G_fatal_error(_("Illegal south coordinate <%s>"), parm.south->answer); if (!G_scan_easting(parm.east->answer, ®ion.east, region.proj)) G_fatal_error(_("Illegal east coordinate <%s>"), parm.east->answer); if (!G_scan_easting(parm.west->answer, ®ion.west, region.proj)) G_fatal_error(_("Illegal west coordinate <%s>"), parm.west->answer); Rast3d_adjust_region(®ion); expected = (off_t) region.rows * region.cols * region.depths * bytes; fp = fopen(input, "rb"); if (!fp) G_fatal_error(_("Unable to open <%s>"), input); /* Find File Size in Byte and Check against byte size */ G_fseek(fp, 0, SEEK_END); file_size = G_ftell(fp); G_fseek(fp, 0, SEEK_SET); if (file_size != expected) { G_warning(_("File Size %lld ... Total Bytes %lld"), (long long int) file_size, (long long int) expected); G_fatal_error(_("Bytes do not match file size")); } map_type = (bytes > 4 ? DCELL_TYPE : FCELL_TYPE); if(is_integer && bytes >= 4) map_type = DCELL_TYPE; Rast3d_init_defaults(); /*Open the new 3D raster map */ map = Rast3d_open_new_opt_tile_size(output, RASTER3D_USE_CACHE_DEFAULT, ®ion, map_type, 32); if (map == NULL) G_fatal_error(_("Unable to open 3D raster map")); in_cell = G_malloc(bytes); bin_to_raster3d(parm.null->answer, map_type, is_integer, is_signed, bytes, byte_swap, flag.row->answer, flag.depth->answer); if (!Rast3d_close(map)) G_fatal_error(_("Unable to close 3D raster map")); /* write input name to map history */ Rast3d_read_history(output, G_mapset(), &history); Rast_set_history(&history, HIST_DATSRC_1, input); Rast3d_write_history(output, &history); fclose(fp); if (in_cell) G_free(in_cell); return 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 *input, *output; int convertNull; char nullValue[256]; int useTypeDefault, type, useCompressionDefault, doCompression; int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY, tileZ; RASTER3D_Region region; FILE *fp; struct GModule *module; struct History history; map = NULL; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("import")); G_add_keyword(_("voxel")); G_add_keyword(_("conversion")); G_add_keyword("ASCII"); module->description = _("Converts a 3D ASCII raster text file into a (binary) 3D raster map."); setParams(); Rast3d_set_standard3d_input_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); getParams(&input, &output, &convertNull, nullValue); if (!Rast3d_get_standard3d_params(&useTypeDefault, &type, &useCompressionDefault, &doCompression, &usePrecisionDefault, &precision, &useDimensionDefault, &tileX, &tileY, &tileZ)) fatalError("Error getting standard parameters"); Rast3d_init_defaults(); fp = openAscii(input, ®ion); /*Open the new RASTER3D map */ map = Rast3d_open_new_param(output, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_XY, ®ion, type, doCompression, precision, tileX, tileY, tileZ); if (map == NULL) fatalError(_("Unable to open 3D raster map")); /*Create the new RASTER3D Map */ asciiToG3d(fp, ®ion, convertNull, nullValue); if (!Rast3d_close(map)) fatalError(_("Unable to close 3D raster map")); /* write input name to map history */ Rast3d_read_history(output, G_mapset(), &history); Rast_command_history(&history); Rast_set_history(&history, HIST_DATSRC_1, input); Rast3d_write_history(output, &history); map = NULL; if (fclose(fp)) fatalError(_("Unable to close ASCII file")); return EXIT_SUCCESS; }
/* ************************************************************************* */ 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; }
/* *************************************************************** */ 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); }
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); }