init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * \file lib/raster/init.c
  3. *
  4. * \brief Raster Library - Handles program initialization.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 2000-2008
  14. */
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20. #include <locale.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "R.h"
  25. struct R__ R__;
  26. static int initialized = 0; /** Is set when engine is initialized */
  27. static int init(void);
  28. /**
  29. * \brief Initialize GRASS GIS engine.
  30. *
  31. * Initializes GIS engine and ensures a valid mapset is available.
  32. *
  33. * \return always returns 0 on success
  34. * \return exit() is called on error
  35. */
  36. void Rast_init(void)
  37. {
  38. Rast__init();
  39. }
  40. /**
  41. * \brief Checks to see if GIS engine is initialized.
  42. *
  43. * \return
  44. */
  45. void Rast__check_init(void)
  46. {
  47. if (initialized)
  48. return;
  49. G_fatal_error(_("Raster library not initialized. Programmer forgot to call Rast_init()."));
  50. }
  51. void Rast__init(void)
  52. {
  53. if (G_is_initialized(&initialized))
  54. return;
  55. init();
  56. G_initialize_done(&initialized);
  57. }
  58. void Rast__error_handler(void *p)
  59. {
  60. Rast__unopen_all();
  61. }
  62. static int init(void)
  63. {
  64. char *nulls, *cname;
  65. Rast__init_window();
  66. /* no histograms */
  67. R__.want_histogram = 0;
  68. /* set the write type for floating maps */
  69. R__.fp_type = getenv("GRASS_FP_DOUBLE") ? DCELL_TYPE : FCELL_TYPE;
  70. /* Set masking flag unknown */
  71. R__.auto_mask = -1;
  72. R__.mask_fd = -1;
  73. R__.nbytes = sizeof(CELL);
  74. R__.fileinfo_count = 0;
  75. R__.fileinfo = NULL;
  76. R__.compression_type = G_default_compressor();
  77. cname = getenv("GRASS_COMPRESSOR");
  78. /* 1: RLE
  79. * 2: ZLIB (DEFLATE)
  80. * 3: LZ4
  81. * 4: BZIP2
  82. * 5: ZSTD */
  83. if (cname && *cname) {
  84. /* ask gislib */
  85. R__.compression_type = G_compressor_number(cname);
  86. if (R__.compression_type < 1) {
  87. if (R__.compression_type < 0) {
  88. G_warning(_("Unknown compression method <%s>, using default %s"),
  89. cname, G_compressor_name(G_default_compressor()));
  90. }
  91. if (R__.compression_type == 0) {
  92. G_warning(_("No compression is not supported for GRASS raster maps, using default %s"),
  93. G_compressor_name(G_default_compressor()));
  94. }
  95. /* use default */
  96. R__.compression_type = G_default_compressor();
  97. }
  98. if (G_check_compressor(R__.compression_type) != 1) {
  99. G_warning(_("This GRASS version does not support %s compression, using default %s"),
  100. cname, G_compressor_name(G_default_compressor()));
  101. /* use default */
  102. R__.compression_type = G_default_compressor();
  103. }
  104. G_debug(1, "Using %s compression",
  105. G_compressor_name(R__.compression_type));
  106. }
  107. nulls = getenv("GRASS_COMPRESS_NULLS");
  108. R__.compress_nulls = (nulls && atoi(nulls) == 0) ? 0 : 1;
  109. G_add_error_handler(Rast__error_handler, NULL);
  110. initialized = 1;
  111. return 0;
  112. }
  113. void Rast_init_all(void)
  114. {
  115. Rast__init();
  116. Rast__check_for_auto_masking();
  117. Rast_init_gdal();
  118. }