static void retileNocache(void *map, const char *nameOut, int tileX, int tileY, int tileZ) { void *map2; int x, y, z, saveType, nx, ny, nz; int typeIntern; void *data; int tileXsave, tileYsave, tileZsave; RASTER3D_Region region; saveType = Rast3d_get_file_type(); Rast3d_set_file_type(Rast3d_file_type_map(map)); Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave); Rast3d_set_tile_dimension(tileX, tileY, tileZ); typeIntern = Rast3d_tile_type_map(map); Rast3d_get_region_struct_map(map, ®ion); map2 = Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_NO_CACHE, ®ion); if (map2 == NULL) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new"); Rast3d_set_file_type(saveType); Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave); data = Rast3d_alloc_tiles(map2, 1); if (data == NULL) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_alloc_tiles"); Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz); for (z = 0; z < nz; z++) { G_percent(z, nz, 1); for (y = 0; y < ny; y++) for (x = 0; x < nx; x++) { Rast3d_get_block(map, x * tileX, y * tileY, z * tileZ, tileX, tileY, tileZ, data, typeIntern); if (!Rast3d_write_tile (map2, Rast3d_tile2tile_index(map2, x, y, z), data, typeIntern)) Rast3d_fatal_error ("Rast3d_retileNocache: error in Rast3d_write_tile"); } } G_percent(1, 1, 1); Rast3d_free_tiles(data); Rast3d_close(map2); }
/* ************************************************************************* */ 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[]) { 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); }
void Rast3d_retile(void *map, const char *nameOut, int tileX, int tileY, int tileZ) { void *map2; double value; int x, y, z, saveType; int rows, cols, depths, typeIntern; int xTile, yTile, zTile; int xOffs, yOffs, zOffs, prev; int tileXsave, tileYsave, tileZsave; RASTER3D_Region region; if (!Rast3d_tile_use_cache_map(map)) { retileNocache(map, nameOut, tileX, tileY, tileZ); return; } saveType = Rast3d_get_file_type(); Rast3d_set_file_type(Rast3d_file_type_map(map)); Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave); Rast3d_set_tile_dimension(tileX, tileY, tileZ); typeIntern = Rast3d_tile_type_map(map); Rast3d_get_region_struct_map(map, ®ion); map2 = Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, ®ion); if (map2 == NULL) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new"); Rast3d_set_file_type(saveType); Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave); Rast3d_coord2tile_coord(map2, 0, 0, 0, &xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs); prev = zTile; x = 0; y = 0; Rast3d_get_coords_map(map, &rows, &cols, &depths); for (z = 0; z < depths; z++) { G_percent(z, depths, 1); Rast3d_coord2tile_coord(map2, x, y, z, &xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs); if (zTile > prev) { if (!Rast3d_flush_all_tiles(map2)) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_flush_all_tiles"); prev++; } for (y = 0; y < rows; y++) for (x = 0; x < cols; x++) { Rast3d_get_value_region(map, x, y, z, &value, typeIntern); if (!Rast3d_put_value(map2, x, y, z, &value, typeIntern)) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_put_value"); } } G_percent(1, 1, 1); if (!Rast3d_flush_all_tiles(map2)) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_flush_all_tiles"); if (!Rast3d_close(map2)) Rast3d_fatal_error("Rast3d_retile: error in Rast3d_close"); }
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")); }