defaults.c 12 KB

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