defaults.c 11 KB

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