changeprecision.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/raster3d.h>
  6. /*---------------------------------------------------------------------------*/
  7. /*!
  8. * \brief
  9. *
  10. * Makes a copy of <em>map</em> with name <em>nameOut</em> which is
  11. * written with <em>precision</em>.
  12. * The source code can be found in <em>changeprecision.c</em>.
  13. *
  14. * \param map
  15. * \param precision
  16. * \param nameOut
  17. * \return void
  18. */
  19. void Rast3d_change_precision(void *map, int precision, const char *nameOut)
  20. {
  21. void *map2;
  22. int x, y, z, savePrecision, saveCompression;
  23. char *data;
  24. RASTER3D_Region region;
  25. int typeIntern;
  26. int nx, ny, nz;
  27. int tileXsave, tileYsave, tileZsave, tileX, tileY, tileZ, saveType;
  28. saveType = Rast3d_get_file_type();
  29. /* Rast3d_set_file_type (Rast3d_file_type_map (map)); */
  30. Rast3d_get_compression_mode(&saveCompression, &savePrecision);
  31. Rast3d_set_compression_mode(RASTER3D_COMPRESSION, precision);
  32. Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
  33. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  34. Rast3d_set_tile_dimension(tileX, tileY, tileZ);
  35. typeIntern = Rast3d_tile_type_map(map);
  36. Rast3d_get_region_struct_map(map, &region);
  37. map2 =
  38. Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, &region);
  39. if (map2 == NULL)
  40. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_open_cell_new");
  41. Rast3d_set_file_type(saveType);
  42. Rast3d_set_compression_mode(saveCompression, savePrecision);
  43. Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
  44. data = Rast3d_alloc_tiles(map, 1);
  45. if (data == NULL)
  46. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_alloc_tiles");
  47. Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);
  48. for (z = 0; z < nz; z++)
  49. for (y = 0; y < ny; y++)
  50. for (x = 0; x < nx; x++) {
  51. if (!Rast3d_read_tile(map, Rast3d_tile2tile_index(map, x, y, z), data,
  52. typeIntern))
  53. Rast3d_fatal_error
  54. ("Rast3d_change_precision: error in Rast3d_read_tile");
  55. if (!Rast3d_write_tile
  56. (map2, Rast3d_tile2tile_index(map2, x, y, z), data,
  57. typeIntern))
  58. Rast3d_fatal_error
  59. ("Rast3d_change_precision: error in Rast3d_write_tile");
  60. }
  61. Rast3d_free_tiles(data);
  62. if (!Rast3d_close(map2))
  63. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_close");
  64. }