range.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/glocale.h>
  8. #include "raster3d_intern.h"
  9. /*---------------------------------------------------------------------------*/
  10. void
  11. Rast3d_range_update_from_tile(RASTER3D_Map * map, const void *tile, int rows, int cols,
  12. int depths, int xRedundant, int yRedundant,
  13. int zRedundant, int nofNum, int type)
  14. {
  15. int y, z, cellType;
  16. struct FPRange *range;
  17. range = &(map->range);
  18. cellType = Rast3d_g3d_type2cell_type(type);
  19. if (nofNum == map->tileSize) {
  20. Rast_row_update_fp_range(tile, map->tileSize, range, cellType);
  21. return;
  22. }
  23. if (xRedundant) {
  24. for (z = 0; z < depths; z++) {
  25. for (y = 0; y < rows; y++) {
  26. Rast_row_update_fp_range(tile, cols, range, cellType);
  27. tile = G_incr_void_ptr(tile, map->tileX * Rast3d_length(type));
  28. }
  29. if (yRedundant)
  30. tile =
  31. G_incr_void_ptr(tile,
  32. map->tileX * yRedundant *
  33. Rast3d_length(type));
  34. }
  35. return;
  36. }
  37. if (yRedundant) {
  38. for (z = 0; z < depths; z++) {
  39. Rast_row_update_fp_range(tile, map->tileX * rows, range, cellType);
  40. tile = G_incr_void_ptr(tile, map->tileXY * Rast3d_length(type));
  41. }
  42. return;
  43. }
  44. Rast_row_update_fp_range(tile, map->tileXY * depths, range, cellType);
  45. }
  46. /*---------------------------------------------------------------------------*/
  47. int
  48. Rast3d_read_range(const char *name, const char *mapset, struct FPRange *drange)
  49. /* adapted from Rast_read_fp_range */
  50. {
  51. int fd;
  52. int bytes_read;
  53. char xdr_buf[2 * RASTER3D_XDR_DOUBLE_LENGTH];
  54. DCELL dcell1, dcell2;
  55. Rast_init_fp_range(drange);
  56. fd = -1;
  57. fd = G_open_old_misc(RASTER3D_DIRECTORY, RASTER3D_RANGE_ELEMENT, name, mapset);
  58. if (fd < 0) {
  59. G_warning(_("Unable to open range file for [%s in %s]"), name, mapset);
  60. return -1;
  61. }
  62. bytes_read = read(fd, xdr_buf, 2 * RASTER3D_XDR_DOUBLE_LENGTH);
  63. /* if the f_range file exists, but empty the range is NULL -> a NULL map */
  64. if (bytes_read == 0) {
  65. close(fd);
  66. return 1;
  67. }
  68. if (bytes_read != 2 * RASTER3D_XDR_DOUBLE_LENGTH) {
  69. close(fd);
  70. G_warning(_("Error reading range file for [%s in %s]"), name, mapset);
  71. return 2;
  72. }
  73. G_xdr_get_double(&dcell1, &xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 0]);
  74. G_xdr_get_double(&dcell2, &xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 1]);
  75. Rast_update_fp_range(dcell1, drange);
  76. Rast_update_fp_range(dcell2, drange);
  77. close(fd);
  78. return 1;
  79. }
  80. /*---------------------------------------------------------------------------*/
  81. /*!
  82. * \brief
  83. *
  84. * Loads the range into the range structure of <em>map</em>.
  85. *
  86. * \param map
  87. * \return 1 ... if successful
  88. * 0 ... otherwise.
  89. */
  90. int Rast3d_range_load(RASTER3D_Map * map)
  91. {
  92. if (map->operation == RASTER3D_WRITE_DATA)
  93. return 1;
  94. if (Rast3d_read_range(map->fileName, map->mapset, &(map->range)) == -1) {
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. /*---------------------------------------------------------------------------*/
  100. /*!
  101. * \brief
  102. *
  103. * Returns in <em>min</em> and <em>max</em> the minimum and maximum values of
  104. * the range.
  105. *
  106. * \param map
  107. * \param min
  108. * \param max
  109. * \return void
  110. */
  111. void Rast3d_range_min_max(RASTER3D_Map * map, double *min, double *max)
  112. {
  113. Rast_get_fp_range_min_max(&(map->range), min, max);
  114. }
  115. /*-------------------------------------------------------------------------*/
  116. static int writeRange(const char *name, struct FPRange *range)
  117. /* adapted from Rast_write_fp_range */
  118. {
  119. char xdr_buf[2 * RASTER3D_XDR_DOUBLE_LENGTH];
  120. int fd;
  121. fd = G_open_new_misc(RASTER3D_DIRECTORY, RASTER3D_RANGE_ELEMENT, name);
  122. if (fd < 0) {
  123. G_warning(_("Unable to open range file for <%s>"), name);
  124. return -1;
  125. }
  126. if (range->first_time) {
  127. /* if range hasn't been updated, write empty file meaning NULLs */
  128. close(fd);
  129. return 0;
  130. }
  131. G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 0], &range->min);
  132. G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 1], &range->max);
  133. if (write(fd, xdr_buf, RASTER3D_XDR_DOUBLE_LENGTH * 2) != RASTER3D_XDR_DOUBLE_LENGTH * 2)
  134. goto error;
  135. close(fd);
  136. return 0;
  137. error:
  138. close(fd);
  139. G_remove_misc(RASTER3D_DIRECTORY, RASTER3D_RANGE_ELEMENT, name); /* remove the old file with this name */
  140. G_warning("can't write range file for [%s in %s]", name, G_mapset());
  141. return -1;
  142. }
  143. /*---------------------------------------------------------------------------*/
  144. /*!
  145. * \brief
  146. *
  147. * Writes the range which is stored in the range structure of <em>map</em>.
  148. * (This function is invoked automatically when a new file is closed).
  149. *
  150. * \param map
  151. * \return 1 ... if successful
  152. * 0 ... otherwise.
  153. */
  154. int Rast3d_range_write(RASTER3D_Map * map)
  155. {
  156. char path[GPATH_MAX];
  157. Rast3d_filename(path, RASTER3D_RANGE_ELEMENT, map->fileName, map->mapset);
  158. remove(path);
  159. if (writeRange(map->fileName, &(map->range)) == -1) {
  160. Rast3d_error("Rast3d_closeCellNew: error in writeRange");
  161. return 0;
  162. }
  163. return 1;
  164. }
  165. /*---------------------------------------------------------------------------*/
  166. int Rast3d_range_init(RASTER3D_Map * map)
  167. {
  168. Rast_init_fp_range(&(map->range));
  169. return 0;
  170. }