GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-66aae0a5a0
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 
72 void (*g3d_error_fun)(const char *) = NULL;
75 
76 /*---------------------------------------------------------------------------*/
77 
78 /*!
79  * \brief set compression mode
80  *
81  * <em>doCompress</em> should be one of RASTER3D_NO_COMPRESSION and
82  * RASTER3D_COMPRESSION. <em>precision</em> should be either
83  * RASTER3D_MAX_PRECISION or a positive integer.
84  *
85  * \note Note that older parameters <em>doLzw</em> and <em>doRle</em>
86  * (RASTER3D_NO_RLE or RASTER3D_USE_RLE) are no longer in the API.
87  *
88  * Calls Rast3d_fatal_error() if a wrong parameter value is provided.
89  *
90  * \param doCompress specifies if a compression should be performed
91  * \param precision a precision of compression
92  */
93 
94 void Rast3d_set_compression_mode(int doCompress, int precision)
95 {
96  if ((doCompress != RASTER3D_NO_COMPRESSION) &&
97  (doCompress != RASTER3D_COMPRESSION))
99  "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)
108  "Rast3d_set_compression_mode: wrong value for precision.");
109 
110  g3d_precision = precision;
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 Rast3d_get_compression_mode(int *doCompress, int *precision)
128 {
129  if (doCompress != NULL)
130  *doCompress = g3d_do_compression;
131  if (precision != NULL)
132  *precision = g3d_precision;
133 }
134 
135 /*---------------------------------------------------------------------------*/
136 
137 /*!
138  * \brief set cache size
139  *
140  * \param nTiles
141  * \return void
142  */
143 
144 void Rast3d_set_cache_size(int nTiles)
145 {
146  if (nTiles < 0)
147  Rast3d_fatal_error("Rast3d_set_cache_size: size out of range.");
148 
149  g3d_cache_default = nTiles;
150 }
151 
152 /*---------------------------------------------------------------------------*/
153 
154 /*!
155  * \brief get cache size
156  *
157  * \return int
158  */
159 
161 {
162  return g3d_cache_default;
163 }
164 
165 /*---------------------------------------------------------------------------*/
166 
167 /*!
168  * \brief Set cache limit
169  *
170  * \param nBytes
171  * \return void
172  */
173 
174 void Rast3d_set_cache_limit(int nBytes)
175 {
176  if (nBytes <= 0)
177  Rast3d_fatal_error("Rast3d_set_cache_limit: size out of range.");
178 
179  g3d_cache_max = nBytes;
180 }
181 
182 /*---------------------------------------------------------------------------*/
183 
184 /*!
185  * \brief Get cache limit
186  *
187  * \return int
188  */
189 
191 {
192  return g3d_cache_max;
193 }
194 
195 /*---------------------------------------------------------------------------*/
196 
197 /*!
198  * \brief set G3d file type
199  *
200  * \param type
201  * \return void
202  */
203 
204 void Rast3d_set_file_type(int type)
205 {
206  if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
207  Rast3d_fatal_error("Rast3d_setFileTypeDefault: invalid type");
208 
209  g3d_file_type = type;
210 }
211 
212 /*---------------------------------------------------------------------------*/
213 
214 /*!
215  * \brief get G3d file type
216  *
217  * \return int
218  */
219 
221 {
222  return g3d_file_type;
223 }
224 
225 /*---------------------------------------------------------------------------*/
226 
227 /*!
228  * \brief set Tile Dimension
229  *
230  * \param tileX
231  * \param tileY
232  * \param tileZ
233  * \return void
234  */
235 
236 void Rast3d_set_tile_dimension(int tileX, int tileY, int tileZ)
237 {
238  if ((g3d_tile_dimension[0] = tileX) <= 0)
239  Rast3d_fatal_error("Rast3d_set_tile_dimension: value for tile x "
240  "environment variable out of range");
241 
242  if ((g3d_tile_dimension[1] = tileY) <= 0)
243  Rast3d_fatal_error("Rast3d_set_tile_dimension: value for tile y "
244  "environment variable out of range");
245 
246  if ((g3d_tile_dimension[2] = tileZ) <= 0)
247  Rast3d_fatal_error("Rast3d_set_tile_dimension: value for tile z "
248  "environment variable out of range");
249 }
250 
251 /*---------------------------------------------------------------------------*/
252 
253 /*!
254  * \brief get Tile Dimension
255  *
256  * \param tileX
257  * \param tileY
258  * \param tileZ
259  * \return void
260  */
261 
262 void Rast3d_get_tile_dimension(int *tileX, int *tileY, int *tileZ)
263 {
264  *tileX = g3d_tile_dimension[0];
265  *tileY = g3d_tile_dimension[1];
266  *tileZ = g3d_tile_dimension[2];
267 }
268 
269 /*---------------------------------------------------------------------------*/
270 
271 /*!
272  * \brief set error function
273  *
274  * \param fun
275  * \return void
276  */
277 
278 void Rast3d_set_error_fun(void (*fun)(const char *))
279 {
280  g3d_error_fun = fun;
281 }
282 
283 /*---------------------------------------------------------------------------*/
284 
285 /*!
286  * \brief Initializes the default values described
287  * in RASTER3D Defaults. Applications have to use this function only if they
288  * need to query the default values before the first file (either old or new)
289  * has been opened.
290  *
291  * \return void
292  */
293 
295 {
296  static int firstTime = 1;
297  const char *value, *windowName;
298  RASTER3D_Region window;
299 
300  if (!firstTime)
301  return;
302  firstTime = 0;
303 
305  if (NULL != getenv(RASTER3D_COMPRESSION_ENV_VAR_YES)) {
307  }
308  else {
309  if (NULL != getenv(RASTER3D_COMPRESSION_ENV_VAR_NO)) {
311  }
312  else {
314  }
315  }
316  }
317 
319  if (NULL != getenv(RASTER3D_PRECISION_ENV_VAR_MAX)) {
321  }
322  else {
323  value = getenv(RASTER3D_PRECISION_ENV_VAR);
324  if (value == NULL) {
326  }
327  else {
328  if (sscanf(value, "%d", &g3d_precision) != 1) {
330  "Rast3d_init_defaults: precision environment variable "
331  "has invalid value");
332  }
333  else {
334  if (g3d_precision < -1) {
336  "Rast3d_init_defaults: value for cache environment "
337  "variable out of range");
338  }
339  }
340  }
341  }
342  }
343 
345  if (NULL != getenv(RASTER3D_FILE_FLOAT_ENV_VAR)) {
347  }
348  else {
349  if (NULL != getenv(RASTER3D_FILE_DOUBLE_ENV_VAR)) {
351  }
352  else {
354  }
355  }
356  }
357 
359  value = getenv(RASTER3D_CACHE_SIZE_ENV_VAR);
360 
361  if (value == NULL) {
363  }
364  else {
365  if (sscanf(value, "%d", &g3d_cache_default) != 1) {
366  Rast3d_fatal_error("Rast3d_init_defaults: cache environment "
367  "variable has invalid value");
368  }
369  if (g3d_cache_default < 0) {
370  Rast3d_fatal_error("Rast3d_init_defaults: value for cache "
371  "environment variable out of range");
372  }
373  }
374  }
375 
377  value = getenv(RASTER3D_CACHE_SIZE_MAX_ENV_VAR);
378 
379  if (value == NULL) {
381  }
382  else {
383  if (sscanf(value, "%d", &g3d_cache_max) != 1) {
384  Rast3d_fatal_error("Rast3d_init_defaults: cache environment "
385  "variable has invalid value");
386  }
387  if (g3d_cache_max < 0) {
388  Rast3d_fatal_error("Rast3d_init_defaults: value for cache "
389  "environment variable out of range");
390  }
391  }
392  }
393 
395  value = getenv(RASTER3D_TILE_DIM_X_ENV_VAR);
396 
397  if (value == NULL) {
399  }
400  else {
401  if (sscanf(value, "%d", g3d_tile_dimension) != 1) {
402  Rast3d_fatal_error("Rast3d_init_defaults: tile dimension x "
403  "environment variable has invalid value");
404  }
405  if (g3d_tile_dimension[0] <= 0) {
406  Rast3d_fatal_error("Rast3d_init_defaults: value for tile x "
407  "environment variable out of range");
408  }
409  }
410 
411  value = getenv(RASTER3D_TILE_DIM_Y_ENV_VAR);
412 
413  if (value == NULL) {
415  }
416  else {
417  if (sscanf(value, "%d", g3d_tile_dimension + 1) != 1) {
418  Rast3d_fatal_error("Rast3d_init_defaults: tile dimension y "
419  "environment variable has invalid value");
420  }
421  if (g3d_tile_dimension[1] <= 0) {
422  Rast3d_fatal_error("Rast3d_init_defaults: value for tile y "
423  "environment variable out of range");
424  }
425  }
426 
427  value = getenv(RASTER3D_TILE_DIM_Z_ENV_VAR);
428 
429  if (value == NULL) {
431  }
432  else {
433  if (sscanf(value, "%d", g3d_tile_dimension + 2) != 1) {
434  Rast3d_fatal_error("Rast3d_init_defaults: tile dimension z "
435  "environment variable has invalid value");
436  }
437  if (g3d_tile_dimension[2] <= 0) {
438  Rast3d_fatal_error("Rast3d_init_defaults: value for tile z "
439  "environment variable out of range");
440  }
441  }
442  }
443 
444  if (g3d_error_fun == NULL) {
445  value = getenv(RASTER3D_FATAL_ERROR_ENV_VAR);
446 
447  if (value != NULL) {
449  }
450  else {
451  value = getenv(RASTER3D_PRINT_ERROR_ENV_VAR);
452 
453  if (value != NULL) {
455  }
456  else {
458  }
459  }
460  }
461 
462  if (g3d_unit_default == NULL)
466 
467  windowName = Rast3d_get_window_params();
468  if (windowName == NULL) {
469  value = getenv(RASTER3D_DEFAULT_WINDOW3D);
470  if (value != NULL)
471  if (*value != 0)
472  windowName = value;
473  }
474 
475  if (!Rast3d_read_window(&window, windowName))
476  Rast3d_fatal_error("Rast3d_init_defaults: Error reading window");
477  Rast3d_set_window(&window);
478 }
#define NULL
Definition: ccmath.h:32
#define RASTER3D_COMPRESSION_ENV_VAR_NO
Name of the environmental variable specifying that compression should not be used.
Definition: defaults.c:42
void Rast3d_set_cache_limit(int nBytes)
Set cache limit.
Definition: defaults.c:174
void Rast3d_set_tile_dimension(int tileX, int tileY, int tileZ)
set Tile Dimension
Definition: defaults.c:236
#define RASTER3D_FATAL_ERROR_ENV_VAR
Definition: defaults.c:57
#define RASTER3D_TILE_Y_DEFAULT
Definition: defaults.c:16
#define RASTER3D_CACHE_SIZE_ENV_VAR
Definition: defaults.c:47
#define RASTER3D_ERROR_FUN_DEFAULT
Definition: defaults.c:18
#define RASTER3D_PRECISION_ENV_VAR_MAX
Definition: defaults.c:45
char * g3d_unit_default
Definition: defaults.c:73
void Rast3d_set_error_fun(void(*fun)(const char *))
set error function
Definition: defaults.c:278
#define RASTER3D_PRECISION_ENV_VAR
Definition: defaults.c:44
void Rast3d_get_tile_dimension(int *tileX, int *tileY, int *tileZ)
get Tile Dimension
Definition: defaults.c:262
int g3d_tile_dimension[3]
Definition: defaults.c:70
int g3d_precision
Definition: defaults.c:66
int g3d_cache_max
Definition: defaults.c:68
int Rast3d_get_cache_limit(void)
Get cache limit.
Definition: defaults.c:190
#define RASTER3D_CACHE_SIZE_MAX_ENV_VAR
Definition: defaults.c:48
int g3d_cache_default
Definition: defaults.c:67
#define RASTER3D_COMPRESSION_DEFAULT
Definition: defaults.c:10
#define RASTER3D_TILE_DIM_Z_ENV_VAR
Definition: defaults.c:55
void(* g3d_error_fun)(const char *)
Definition: defaults.c:72
#define RASTER3D_CACHE_SIZE_DEFAULT
Definition: defaults.c:12
int g3d_file_type
Definition: defaults.c:69
#define RASTER3D_PRINT_ERROR_ENV_VAR
Definition: defaults.c:58
void Rast3d_get_compression_mode(int *doCompress, int *precision)
Gets compression mode.
Definition: defaults.c:127
#define RASTER3D_TILE_DIM_X_ENV_VAR
Definition: defaults.c:53
int Rast3d_get_file_type(void)
get G3d file type
Definition: defaults.c:220
int g3d_version
Definition: defaults.c:64
#define RASTER3D_UNIT_DEFAULT
Definition: defaults.c:19
#define RASTER3D_FILE_FLOAT_ENV_VAR
Definition: defaults.c:50
#define RASTER3D_VERTICAL_UNIT_DEFAULT
Definition: defaults.c:20
#define RASTER3D_TILE_X_DEFAULT
Definition: defaults.c:15
#define RASTER3D_COMPRESSION_ENV_VAR_YES
Name of the environmental variable specifying that compression should be used.
Definition: defaults.c:35
void Rast3d_set_file_type(int type)
set G3d file type
Definition: defaults.c:204
#define RASTER3D_NO_DEFAULT
Definition: defaults.c:8
#define RASTER3D_CACHE_SIZE_MAX_DEFAULT
Definition: defaults.c:13
#define RASTER3D_TILE_Z_DEFAULT
Definition: defaults.c:17
void Rast3d_init_defaults(void)
Initializes the default values described in RASTER3D Defaults. Applications have to use this function...
Definition: defaults.c:294
#define RASTER3D_PRECISION_DEFAULT
Definition: defaults.c:11
#define RASTER3D_DEFAULT_WINDOW3D
Definition: defaults.c:60
#define RASTER3D_FILE_DOUBLE_ENV_VAR
Definition: defaults.c:51
#define RASTER3D_FILE_TYPE_DEFAULT
Definition: defaults.c:14
int Rast3d_get_cache_size(void)
get cache size
Definition: defaults.c:160
void Rast3d_set_compression_mode(int doCompress, int precision)
set compression mode
Definition: defaults.c:94
int g3d_do_compression
Definition: defaults.c:65
#define RASTER3D_TILE_DIM_Y_ENV_VAR
Definition: defaults.c:54
void Rast3d_set_cache_size(int nTiles)
set cache size
Definition: defaults.c:144
int g3d_vertical_unit_default
Definition: defaults.c:74
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void Rast3d_fatal_error_noargs(const char *) __attribute__((noreturn))
char * Rast3d_get_window_params(void)
Definition: param.c:134
void Rast3d_print_error(const char *)
Prints error message.
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:133
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...
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
#define U_UNDEFINED
List of units.
Definition: gis.h:104
#define RASTER3D_COMPRESSION
Definition: raster3d.h:14
#define RASTER3D_MAP_VERSION
Definition: raster3d.h:9
#define RASTER3D_MAX_PRECISION
Definition: raster3d.h:16
#define RASTER3D_NO_COMPRESSION
Definition: raster3d.h:13
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13