main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.retile
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert <at> googlemail <dot> co
  7. *
  8. * PURPOSE: Retiles an existing RASTER3D map with user defined x, y and z tile size
  9. *
  10. * COPYRIGHT: (C) 2011 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/raster3d.h>
  23. #include <grass/glocale.h>
  24. /*- Parameters and global variables -----------------------------------------*/
  25. typedef struct {
  26. struct Option *input, *output, *tiling;
  27. struct Flag *cache;
  28. } paramType;
  29. paramType param; /*Parameters */
  30. /*- prototypes --------------------------------------------------------------*/
  31. static void fatal_error(void *map, int *fd, int depths, char *errorMsg); /*Simple Error message */
  32. static void set_params(); /*Fill the paramType structure */
  33. /* ************************************************************************* */
  34. /* Error handling ********************************************************** */
  35. /* ************************************************************************* */
  36. void fatal_error(void *map, int *fd, int depths, char *errorMsg)
  37. {
  38. int i;
  39. /* Close files and exit */
  40. if (map != NULL) {
  41. if (!Rast3d_close(map))
  42. Rast3d_fatal_error(_("Unable to close the 3D raster map"));
  43. }
  44. if (fd != NULL) {
  45. for (i = 0; i < depths; i++)
  46. Rast_unopen(fd[i]);
  47. }
  48. Rast3d_fatal_error(errorMsg);
  49. exit(EXIT_FAILURE);
  50. }
  51. /* ************************************************************************* */
  52. /* Set up the arguments we are expecting ********************************** */
  53. /* ************************************************************************* */
  54. void set_params()
  55. {
  56. param.input = G_define_standard_option(G_OPT_R3_INPUT);
  57. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  58. param.output->description = _("Name of the retiled 3D raster map");
  59. param.tiling = G_define_standard_option(G_OPT_R3_TILE_DIMENSION);
  60. param.cache = G_define_flag();
  61. param.cache->key = 'c';
  62. param.cache->description = "Disable tile caching";
  63. }
  64. /* ************************************************************************* */
  65. /* Main function, open the RASTER3D map and create the raster maps ************** */
  66. /* ************************************************************************* */
  67. int main(int argc, char *argv[])
  68. {
  69. struct GModule *module;
  70. RASTER3D_Map *map = NULL;
  71. int tileX, tileY, tileZ;
  72. char *mapset;
  73. /* Initialize GRASS */
  74. G_gisinit(argv[0]);
  75. module = G_define_module();
  76. G_add_keyword(_("raster3d"));
  77. G_add_keyword(_("tiling"));
  78. G_add_keyword(_("voxel"));
  79. module->description = _("Retiles an existing 3D raster map with user defined x, y and z tile size.");
  80. /* Get parameters from user */
  81. set_params();
  82. /* Have GRASS get inputs */
  83. if (G_parser(argc, argv))
  84. exit(EXIT_FAILURE);
  85. G_debug(3, "Open 3D raster map <%s>", param.input->answer);
  86. mapset = G_find_raster3d(param.input->answer, "");
  87. if (mapset == NULL)
  88. Rast3d_fatal_error(_("3D raster map <%s> not found"),
  89. param.input->answer);
  90. /*Set the defaults */
  91. Rast3d_init_defaults();
  92. if(!param.cache->answer)
  93. map = Rast3d_open_cell_old(param.input->answer, mapset, RASTER3D_DEFAULT_WINDOW,
  94. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  95. else
  96. map = Rast3d_open_cell_old(param.input->answer, mapset, RASTER3D_DEFAULT_WINDOW,
  97. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_NO_CACHE);
  98. if (map == NULL)
  99. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"),
  100. param.input->answer);
  101. /* Get the tile dimension */
  102. Rast3d_get_tile_dimension(&tileX, &tileY, &tileZ);
  103. if (strcmp(param.tiling->answer, "default") != 0) {
  104. if (sscanf(param.tiling->answer, "%dx%dx%d",
  105. &tileX, &tileY, &tileZ) != 3) {
  106. Rast3d_fatal_error(_("Rast3d_get_standard3d_params: tile dimension value invalid"));
  107. }
  108. }
  109. if(!param.cache->answer)
  110. G_message("Retile map with tile cache enabled");
  111. else
  112. G_message("Retile map without tile caching");
  113. Rast3d_retile(map, param.output->answer, tileX, tileY, tileZ);
  114. /* Close files and exit */
  115. if (!Rast3d_close(map))
  116. fatal_error(map, NULL, 0, _("Error closing 3D raster map"));
  117. map = NULL;
  118. return (EXIT_SUCCESS);
  119. }