GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
defaults.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <grass/raster3d.h>
4 #include "raster3d_intern.h"
5 
6 /*---------------------------------------------------------------------------*/
7 
8 #define RASTER3D_NO_DEFAULT -10
9 
10 #define RASTER3D_COMPRESSION_DEFAULT RASTER3D_COMPRESSION
11 #define RASTER3D_PRECISION_DEFAULT RASTER3D_MAX_PRECISION
12 #define RASTER3D_CACHE_SIZE_DEFAULT 1000
13 #define RASTER3D_CACHE_SIZE_MAX_DEFAULT 16777216
14 #define RASTER3D_FILE_TYPE_DEFAULT DCELL_TYPE
15 #define RASTER3D_TILE_X_DEFAULT 16
16 #define RASTER3D_TILE_Y_DEFAULT 16
17 #define RASTER3D_TILE_Z_DEFAULT 8
18 #define RASTER3D_ERROR_FUN_DEFAULT Rast3d_skip_error
19 #define RASTER3D_UNIT_DEFAULT "none"
20 #define RASTER3D_VERTICAL_UNIT_DEFAULT U_UNKNOWN
21 
22 /*---------------------------------------------------------------------------*/
23 
24 /*!
25 \brief Name of the environmental variable specifying that compression
26  should be used.
27 
28  Setting the environmental variable to any value will cause that the
29  compression will be used (set when calling Rast3d_init_defaults
30  for the first time).
31 
32  This environmental variable takes precedence before the environmental
33  variable specified by RASTER3D_COMPRESSION_ENV_VAR_NO.
34 */
35 #define RASTER3D_COMPRESSION_ENV_VAR_YES "RASTER3D_USE_COMPRESSION"
36 /*!
37 \brief Name of the environmental variable specifying that compression
38  should not be used.
39 
40  \see RASTER3D_COMPRESSION_ENV_VAR_YES
41 */
42 #define RASTER3D_COMPRESSION_ENV_VAR_NO "RASTER3D_NO_COMPRESSION"
43 
44 #define RASTER3D_PRECISION_ENV_VAR "RASTER3D_PRECISION"
45 #define RASTER3D_PRECISION_ENV_VAR_MAX "RASTER3D_MAX_PRECISION"
46 
47 #define RASTER3D_CACHE_SIZE_ENV_VAR "RASTER3D_DEFAULT_CACHE_SIZE"
48 #define RASTER3D_CACHE_SIZE_MAX_ENV_VAR "RASTER3D_MAX_CACHE_SIZE"
49 
50 #define RASTER3D_FILE_FLOAT_ENV_VAR "RASTER3D_WRITE_FLOAT"
51 #define RASTER3D_FILE_DOUBLE_ENV_VAR "RASTER3D_WRITE_DOUBLE"
52 
53 #define RASTER3D_TILE_DIM_X_ENV_VAR "RASTER3D_TILE_DIMENSION_X"
54 #define RASTER3D_TILE_DIM_Y_ENV_VAR "RASTER3D_TILE_DIMENSION_Y"
55 #define RASTER3D_TILE_DIM_Z_ENV_VAR "RASTER3D_TILE_DIMENSION_Z"
56 
57 #define RASTER3D_FATAL_ERROR_ENV_VAR "RASTER3D_USE_FATAL_ERROR"
58 #define RASTER3D_PRINT_ERROR_ENV_VAR "RASTER3D_USE_PRINT_ERROR"
59 
60 #define RASTER3D_DEFAULT_WINDOW3D "RASTER3D_DEFAULT_WINDOW3D"
61 
62 /*---------------------------------------------------------------------------*/
63 
71  { RASTER3D_NO_DEFAULT, RASTER3D_NO_DEFAULT, RASTER3D_NO_DEFAULT };
72 void (*g3d_error_fun) (const char *) = NULL;
75 
76 /*---------------------------------------------------------------------------*/
77 
78 
79 /*!
80  * \brief set compression mode
81  *
82  * <em>doCompress</em> should be one of RASTER3D_NO_COMPRESSION and
83  * RASTER3D_COMPRESSION. <em>precision</em> should be either
84  * RASTER3D_MAX_PRECISION or a positive integer.
85  *
86  * \note Note that older parameters <em>doLzw</em> and <em>doRle</em>
87  * (RASTER3D_NO_RLE or RASTER3D_USE_RLE) are no longer in the API.
88  *
89  * Calls Rast3d_fatal_error() if a wrong parameter value is provided.
90  *
91  * \param doCompress specifies if a compression should be performed
92  * \param precision a precision of compression
93  */
94 
95 void
96 Rast3d_set_compression_mode(int doCompress, int precision)
97 {
98  if ((doCompress != RASTER3D_NO_COMPRESSION) && (doCompress != RASTER3D_COMPRESSION))
99  Rast3d_fatal_error("Rast3d_set_compression_mode: wrong value for doCompress.");
100 
101  g3d_do_compression = doCompress;
102 
103  if (doCompress == RASTER3D_NO_COMPRESSION)
104  return;
105 
106  if (precision < -1)
107  Rast3d_fatal_error("Rast3d_set_compression_mode: wrong value for precision.");
108 
109  g3d_precision = precision;
110 }
111 
112 /*---------------------------------------------------------------------------*/
113 
114 
115 /*!
116  * \brief Gets compression mode
117  *
118  * \param doCompress pointer to the integer where the compression mode will
119  * be stored
120  * \param precision pointer to the integer where the precision mode will
121  * be stored
122  *
123  * \see Rast3d_set_compression_mode, RASTER3D_COMPRESSION_ENV_VAR_YES,
124  RASTER3D_COMPRESSION_ENV_VAR_YES
125  */
126 
127 void
128 Rast3d_get_compression_mode(int *doCompress, int *precision)
129 {
130  if (doCompress != NULL)
131  *doCompress = g3d_do_compression;
132  if (precision != NULL)
133  *precision = g3d_precision;
134 }
135 
136 /*---------------------------------------------------------------------------*/
137 
138 
139 /*!
140  * \brief set cache size
141  *
142  * \param nTiles
143  * \return void
144  */
145 
146 void Rast3d_set_cache_size(int nTiles)
147 {
148  if (nTiles < 0)
149  Rast3d_fatal_error("Rast3d_set_cache_size: size out of range.");
150 
151  g3d_cache_default = nTiles;
152 }
153 
154 /*---------------------------------------------------------------------------*/
155 
156 
157 /*!
158  * \brief get cache size
159  *
160  * \return int
161  */
162 
164 {
165  return g3d_cache_default;
166 }
167 
168 /*---------------------------------------------------------------------------*/
169 
170 
171 /*!
172  * \brief Set cache limit
173  *
174  * \param nBytes
175  * \return void
176  */
177 
178 void Rast3d_set_cache_limit(int nBytes)
179 {
180  if (nBytes <= 0)
181  Rast3d_fatal_error("Rast3d_set_cache_limit: size out of range.");
182 
183  g3d_cache_max = nBytes;
184 }
185 
186 /*---------------------------------------------------------------------------*/
187 
188 
189 /*!
190  * \brief Get cache limit
191  *
192  * \return int
193  */
194 
196 {
197  return g3d_cache_max;
198 }
199 
200 /*---------------------------------------------------------------------------*/
201 
202 
203 /*!
204  * \brief set G3d file type
205  *
206  * \param type
207  * \return void
208  */
209 
210 void Rast3d_set_file_type(int type)
211 {
212  if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
213  Rast3d_fatal_error("Rast3d_setFileTypeDefault: invalid type");
214 
215  g3d_file_type = type;
216 }
217 
218 /*---------------------------------------------------------------------------*/
219 
220 
221 /*!
222  * \brief get G3d file type
223  *
224  * \return int
225  */
226 
228 {
229  return g3d_file_type;
230 }
231 
232 /*---------------------------------------------------------------------------*/
233 
234 
235 /*!
236  * \brief set Tile Dimension
237  *
238  * \param tileX
239  * \param tileY
240  * \param tileZ
241  * \return void
242  */
243 
244 void Rast3d_set_tile_dimension(int tileX, int tileY, int tileZ)
245 {
246  if ((g3d_tile_dimension[0] = tileX) <= 0)
248  ("Rast3d_set_tile_dimension: value for tile x environment variable out of range");
249 
250  if ((g3d_tile_dimension[1] = tileY) <= 0)
252  ("Rast3d_set_tile_dimension: value for tile y environment variable out of range");
253 
254  if ((g3d_tile_dimension[2] = tileZ) <= 0)
256  ("Rast3d_set_tile_dimension: value for tile z environment variable out of range");
257 }
258 
259 /*---------------------------------------------------------------------------*/
260 
261 
262 /*!
263  * \brief get Tile Dimension
264  *
265  * \param tileX
266  * \param tileY
267  * \param tileZ
268  * \return void
269  */
270 
271 void Rast3d_get_tile_dimension(int *tileX, int *tileY, int *tileZ)
272 {
273  *tileX = g3d_tile_dimension[0];
274  *tileY = g3d_tile_dimension[1];
275  *tileZ = g3d_tile_dimension[2];
276 }
277 
278 /*---------------------------------------------------------------------------*/
279 
280 
281 /*!
282  * \brief set error function
283  *
284  * \param fun
285  * \return void
286  */
287 
288 void Rast3d_set_error_fun(void (*fun) (const char *))
289 {
290  g3d_error_fun = fun;
291 }
292 
293 /*---------------------------------------------------------------------------*/
294 
295 
296 /*!
297  * \brief Initializes the default values described
298  * in RASTER3D Defaults. Applications have to use this function only if they need to
299  * query the default values before the first file (either old or new) has been
300  * opened.
301  *
302  * \return void
303  */
304 
306 {
307  static int firstTime = 1;
308  const char *value, *windowName;
309  RASTER3D_Region window;
310 
311  if (!firstTime)
312  return;
313  firstTime = 0;
314 
318  }
319  else {
322  }
323  else {
325  }
326  }
327  }
328 
332  }
333  else {
335  if (value == NULL) {
337  }
338  else {
339  if (sscanf(value, "%d", &g3d_precision) != 1) {
341  ("Rast3d_init_defaults: precision environment variable has invalid value");
342  }
343  else {
344  if (g3d_precision < -1) {
346  ("Rast3d_init_defaults: value for cache environment variable out of range");
347  }
348  }
349  }
350  }
351  }
352 
356  }
357  else {
360  }
361  else {
363  }
364  }
365  }
366 
369 
370  if (value == NULL) {
372  }
373  else {
374  if (sscanf(value, "%d", &g3d_cache_default) != 1) {
376  ("Rast3d_init_defaults: cache environment variable has invalid value");
377  }
378  if (g3d_cache_default < 0) {
380  ("Rast3d_init_defaults: value for cache environment variable out of range");
381  }
382  }
383  }
384 
387 
388  if (value == NULL) {
390  }
391  else {
392  if (sscanf(value, "%d", &g3d_cache_max) != 1) {
394  ("Rast3d_init_defaults: cache environment variable has invalid value");
395  }
396  if (g3d_cache_max < 0) {
398  ("Rast3d_init_defaults: value for cache environment variable out of range");
399  }
400  }
401  }
402 
405 
406  if (value == NULL) {
408  }
409  else {
410  if (sscanf(value, "%d", g3d_tile_dimension) != 1) {
412  ("Rast3d_init_defaults: tile dimension x environment variable has invalid value");
413  }
414  if (g3d_tile_dimension[0] <= 0) {
416  ("Rast3d_init_defaults: value for tile x environment variable out of range");
417  }
418  }
419 
421 
422  if (value == NULL) {
424  }
425  else {
426  if (sscanf(value, "%d", g3d_tile_dimension + 1) != 1) {
428  ("Rast3d_init_defaults: tile dimension y environment variable has invalid value");
429  }
430  if (g3d_tile_dimension[1] <= 0) {
432  ("Rast3d_init_defaults: value for tile y environment variable out of range");
433  }
434  }
435 
437 
438  if (value == NULL) {
440  }
441  else {
442  if (sscanf(value, "%d", g3d_tile_dimension + 2) != 1) {
444  ("Rast3d_init_defaults: tile dimension z environment variable has invalid value");
445  }
446  if (g3d_tile_dimension[2] <= 0) {
448  ("Rast3d_init_defaults: value for tile z environment variable out of range");
449  }
450  }
451  }
452 
453  if (g3d_error_fun == NULL) {
455 
456  if (value != NULL) {
458  }
459  else {
461 
462  if (value != NULL) {
464  }
465  else {
467  }
468  }
469  }
470 
471  if(g3d_unit_default == NULL)
475 
476  windowName = Rast3d_get_window_params();
477  if (windowName == NULL) {
479  if (value != NULL)
480  if (*value != 0)
481  windowName = value;
482  }
483 
484  if (!Rast3d_read_window(&window, windowName))
485  Rast3d_fatal_error("Rast3d_init_defaults: Error reading window");
486  Rast3d_set_window(&window);
487 
488 }
#define RASTER3D_CACHE_SIZE_ENV_VAR
Definition: defaults.c:47
#define RASTER3D_MAX_PRECISION
Definition: raster3d.h:17
#define RASTER3D_ERROR_FUN_DEFAULT
Definition: defaults.c:18
#define RASTER3D_DEFAULT_WINDOW3D
Definition: defaults.c:60
#define RASTER3D_TILE_DIM_X_ENV_VAR
Definition: defaults.c:53
#define RASTER3D_COMPRESSION_ENV_VAR_NO
Name of the environmental variable specifying that compression should not be used.
Definition: defaults.c:42
int g3d_precision
Definition: defaults.c:66
void Rast3d_set_cache_size(int nTiles)
set cache size
Definition: defaults.c:146
#define RASTER3D_FILE_DOUBLE_ENV_VAR
Definition: defaults.c:51
#define RASTER3D_CACHE_SIZE_MAX_DEFAULT
Definition: defaults.c:13
#define RASTER3D_PRECISION_ENV_VAR_MAX
Definition: defaults.c:45
int g3d_do_compression
Definition: defaults.c:65
void Rast3d_set_cache_limit(int nBytes)
Set cache limit.
Definition: defaults.c:178
#define RASTER3D_PRECISION_ENV_VAR
Definition: defaults.c:44
#define RASTER3D_FATAL_ERROR_ENV_VAR
Definition: defaults.c:57
#define RASTER3D_PRINT_ERROR_ENV_VAR
Definition: defaults.c:58
#define RASTER3D_UNIT_DEFAULT
Definition: defaults.c:19
#define NULL
Definition: ccmath.h:32
#define RASTER3D_COMPRESSION_ENV_VAR_YES
Name of the environmental variable specifying that compression should be used.
Definition: defaults.c:35
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_set_window(RASTER3D_Region *)
Sets the default window used for every map opened later in the program. Can be used multiple times in...
char * g3d_unit_default
Definition: defaults.c:73
#define RASTER3D_TILE_X_DEFAULT
Definition: defaults.c:15
#define DCELL_TYPE
Definition: raster.h:13
#define RASTER3D_TILE_Z_DEFAULT
Definition: defaults.c:17
void Rast3d_set_file_type(int type)
set G3d file type
Definition: defaults.c:210
void Rast3d_set_compression_mode(int doCompress, int precision)
set compression mode
Definition: defaults.c:96
#define RASTER3D_VERTICAL_UNIT_DEFAULT
Definition: defaults.c:20
void(* g3d_error_fun)(const char *)
Definition: defaults.c:72
#define RASTER3D_TILE_DIM_Z_ENV_VAR
Definition: defaults.c:55
int g3d_cache_default
Definition: defaults.c:67
#define U_UNDEFINED
List of units.
Definition: gis.h:90
int Rast3d_read_window(RASTER3D_Region *, const char *)
Reads window from the file specified by windowName. The name is converted by the rules defined in win...
Definition: windowio.c:139
#define RASTER3D_NO_COMPRESSION
Definition: raster3d.h:14
void Rast3d_fatal_error_noargs(const char *) __attribute__((noreturn))
int Rast3d_get_cache_size()
get cache size
Definition: defaults.c:163
#define RASTER3D_TILE_Y_DEFAULT
Definition: defaults.c:16
#define RASTER3D_CACHE_SIZE_MAX_ENV_VAR
Definition: defaults.c:48
int g3d_cache_max
Definition: defaults.c:68
void Rast3d_init_defaults(void)
Initializes the default values described in RASTER3D Defaults. Applications have to use this function...
Definition: defaults.c:305
#define RASTER3D_TILE_DIM_Y_ENV_VAR
Definition: defaults.c:54
#define RASTER3D_COMPRESSION_DEFAULT
Definition: defaults.c:10
#define RASTER3D_PRECISION_DEFAULT
Definition: defaults.c:11
void Rast3d_get_compression_mode(int *doCompress, int *precision)
Gets compression mode.
Definition: defaults.c:128
int g3d_tile_dimension[3]
Definition: defaults.c:70
#define RASTER3D_FILE_TYPE_DEFAULT
Definition: defaults.c:14
void Rast3d_set_tile_dimension(int tileX, int tileY, int tileZ)
set Tile Dimension
Definition: defaults.c:244
int g3d_file_type
Definition: defaults.c:69
#define RASTER3D_CACHE_SIZE_DEFAULT
Definition: defaults.c:12
int g3d_vertical_unit_default
Definition: defaults.c:74
#define FCELL_TYPE
Definition: raster.h:12
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void Rast3d_set_error_fun(void(*fun)(const char *))
set error function
Definition: defaults.c:288
void Rast3d_print_error(const char *)
Prints error message.
#define RASTER3D_MAP_VERSION
Definition: raster3d.h:7
int Rast3d_get_cache_limit()
Get cache limit.
Definition: defaults.c:195
char * getenv()
#define RASTER3D_NO_DEFAULT
Definition: defaults.c:8
void Rast3d_get_tile_dimension(int *tileX, int *tileY, int *tileZ)
get Tile Dimension
Definition: defaults.c:271
#define RASTER3D_FILE_FLOAT_ENV_VAR
Definition: defaults.c:50
int g3d_version
Definition: defaults.c:64
#define RASTER3D_COMPRESSION
Definition: raster3d.h:15
char * Rast3d_get_window_params(void)
Definition: param.c:134
int Rast3d_get_file_type()
get G3d file type
Definition: defaults.c:227