changeprecision.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, saveLzw, saveRle;
  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, &saveLzw, &saveRle,
  31. &savePrecision);
  32. Rast3d_set_compression_mode(RASTER3D_COMPRESSION, saveLzw, saveRle, precision);
  33. Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
  34. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  35. Rast3d_set_tile_dimension(tileX, tileY, tileZ);
  36. typeIntern = Rast3d_tile_type_map(map);
  37. Rast3d_get_region_struct_map(map, &region);
  38. map2 =
  39. Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, &region);
  40. if (map2 == NULL)
  41. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_open_cell_new");
  42. Rast3d_set_file_type(saveType);
  43. Rast3d_set_compression_mode(saveCompression, saveLzw, saveRle, savePrecision);
  44. Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
  45. data = Rast3d_alloc_tiles(map, 1);
  46. if (data == NULL)
  47. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_alloc_tiles");
  48. Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);
  49. for (z = 0; z < nz; z++)
  50. for (y = 0; y < ny; y++)
  51. for (x = 0; x < nx; x++) {
  52. if (!Rast3d_read_tile(map, Rast3d_tile2tile_index(map, x, y, z), data,
  53. typeIntern))
  54. Rast3d_fatal_error
  55. ("Rast3d_change_precision: error in Rast3d_read_tile");
  56. if (!Rast3d_write_tile
  57. (map2, Rast3d_tile2tile_index(map2, x, y, z), data,
  58. typeIntern))
  59. Rast3d_fatal_error
  60. ("Rast3d_change_precision: error in Rast3d_write_tile");
  61. }
  62. Rast3d_free_tiles(data);
  63. if (!Rast3d_close(map2))
  64. Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_close");
  65. }