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