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_changePrecision(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_getFileType();
  29. /* Rast3d_setFileType (Rast3d_fileTypeMap (map)); */
  30. Rast3d_getCompressionMode(&saveCompression, &saveLzw, &saveRle,
  31. &savePrecision);
  32. Rast3d_setCompressionMode(RASTER3D_COMPRESSION, saveLzw, saveRle, precision);
  33. Rast3d_getTileDimension(&tileXsave, &tileYsave, &tileZsave);
  34. Rast3d_getTileDimensionsMap(map, &tileX, &tileY, &tileZ);
  35. Rast3d_setTileDimension(tileX, tileY, tileZ);
  36. typeIntern = Rast3d_tileTypeMap(map);
  37. Rast3d_getRegionStructMap(map, &region);
  38. map2 =
  39. Rast3d_openCellNew(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, &region);
  40. if (map2 == NULL)
  41. Rast3d_fatalError("Rast3d_changePrecision: error in Rast3d_openCellNew");
  42. Rast3d_setFileType(saveType);
  43. Rast3d_setCompressionMode(saveCompression, saveLzw, saveRle, savePrecision);
  44. Rast3d_setTileDimension(tileXsave, tileYsave, tileZsave);
  45. data = Rast3d_allocTiles(map, 1);
  46. if (data == NULL)
  47. Rast3d_fatalError("Rast3d_changePrecision: error in Rast3d_allocTiles");
  48. Rast3d_getNofTilesMap(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_readTile(map, Rast3d_tile2tileIndex(map, x, y, z), data,
  53. typeIntern))
  54. Rast3d_fatalError
  55. ("Rast3d_changePrecision: error in Rast3d_readTile");
  56. if (!Rast3d_writeTile
  57. (map2, Rast3d_tile2tileIndex(map2, x, y, z), data,
  58. typeIntern))
  59. Rast3d_fatalError
  60. ("Rast3d_changePrecision: error in Rast3d_writeTile");
  61. }
  62. Rast3d_freeTiles(data);
  63. if (!Rast3d_closeCell(map2))
  64. Rast3d_fatalError("Rast3d_changePrecision: error in Rast3d_closeCell");
  65. }