defaults.c 13 KB

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