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