range.c 5.4 KB

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