changetype.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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> in which the
  11. * cells are of type FCELL_TYPE if they are DCELL_TYPE in <em>map</em>,
  12. * and in DCELL_TYPE otherwise.
  13. * The source code can be found in <em>changetype.c</em>.
  14. *
  15. * \param map
  16. * \param nameOut
  17. * \return void
  18. */
  19. void Rast3d_change_type(void *map, const char *nameOut)
  20. {
  21. void *map2;
  22. int x, y, z, saveType;
  23. void *data, *data2;
  24. RASTER3D_Region region;
  25. int tileSize;
  26. int tileX, tileY, tileZ, typeIntern, typeIntern2;
  27. int tileXsave, tileYsave, tileZsave, nx, ny, nz;
  28. saveType = Rast3d_get_file_type();
  29. Rast3d_set_file_type(Rast3d_file_type_map(map) == FCELL_TYPE ?
  30. DCELL_TYPE : FCELL_TYPE);
  31. Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
  32. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  33. Rast3d_set_tile_dimension(tileX, tileY, tileZ);
  34. Rast3d_get_region_struct_map(map, &region);
  35. map2 =
  36. Rast3d_open_cell_new(nameOut, FCELL_TYPE, RASTER3D_USE_CACHE_DEFAULT, &region);
  37. if (map2 == NULL)
  38. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_open_cell_new");
  39. Rast3d_set_file_type(saveType);
  40. Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
  41. data = Rast3d_alloc_tiles(map, 1);
  42. if (data == NULL)
  43. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_alloc_tiles");
  44. data2 = Rast3d_alloc_tiles(map2, 1);
  45. if (data2 == NULL)
  46. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_alloc_tiles");
  47. Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);
  48. typeIntern = Rast3d_tile_type_map(map);
  49. typeIntern2 = Rast3d_tile_type_map(map2);
  50. tileSize = tileX * tileY * tileZ;
  51. for (z = 0; z < nz; z++)
  52. for (y = 0; y < ny; y++)
  53. for (x = 0; x < nx; x++) {
  54. if (!Rast3d_read_tile(map, Rast3d_tile2tile_index(map, x, y, z), data,
  55. typeIntern))
  56. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_read_tile");
  57. Rast3d_copy_values(data, 0, typeIntern, data2, 0, typeIntern2,
  58. tileSize);
  59. if (!Rast3d_write_tile
  60. (map2, Rast3d_tile2tile_index(map2, x, y, z), data2,
  61. typeIntern2))
  62. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_write_tile");
  63. }
  64. Rast3d_free_tiles(data);
  65. Rast3d_free_tiles(data2);
  66. if (!Rast3d_close(map2))
  67. Rast3d_fatal_error("Rast3d_change_type: error in Rast3d_close");
  68. }