range.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Loads the range into the range structure of <em>map</em>.
  83. *
  84. * \param map a pointer to a raster 3D map object
  85. * \return 1 ... if successful
  86. * 0 ... otherwise.
  87. */
  88. int Rast3d_range_load(RASTER3D_Map * map)
  89. {
  90. if (map->operation == RASTER3D_WRITE_DATA)
  91. return 1;
  92. if (Rast3d_read_range(map->fileName, map->mapset, &(map->range)) == -1) {
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. /*---------------------------------------------------------------------------*/
  98. /*!
  99. * \brief Returns in <em>min</em> and <em>max</em> the minimum and maximum values of
  100. * the range.
  101. *
  102. * \param map a pointer to a raster 3D map object
  103. * \param min a pointer to a double to store minumim
  104. * \param max a pointer to a double to store maximum
  105. */
  106. void Rast3d_range_min_max(RASTER3D_Map * map, double *min, double *max)
  107. {
  108. Rast_get_fp_range_min_max(&(map->range), min, max);
  109. }
  110. /*-------------------------------------------------------------------------*/
  111. static int writeRange(const char *name, struct FPRange *range)
  112. /* adapted from Rast_write_fp_range */
  113. {
  114. char xdr_buf[2 * RASTER3D_XDR_DOUBLE_LENGTH];
  115. int fd;
  116. fd = G_open_new_misc(RASTER3D_DIRECTORY, RASTER3D_RANGE_ELEMENT, name);
  117. if (fd < 0) {
  118. G_warning(_("Unable to open range file for <%s>"), name);
  119. return -1;
  120. }
  121. if (range->first_time) {
  122. /* if range hasn't been updated, write empty file meaning NULLs */
  123. close(fd);
  124. return 0;
  125. }
  126. G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 0], &range->min);
  127. G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 1], &range->max);
  128. if (write(fd, xdr_buf, RASTER3D_XDR_DOUBLE_LENGTH * 2) != RASTER3D_XDR_DOUBLE_LENGTH * 2)
  129. goto error;
  130. close(fd);
  131. return 0;
  132. error:
  133. close(fd);
  134. G_remove_misc(RASTER3D_DIRECTORY, RASTER3D_RANGE_ELEMENT, name); /* remove the old file with this name */
  135. G_warning("can't write range file for [%s in %s]", name, G_mapset());
  136. return -1;
  137. }
  138. /*---------------------------------------------------------------------------*/
  139. /*!
  140. * \brief
  141. *
  142. * Writes the range which is stored in the range structure of <em>map</em>.
  143. * (This function is invoked automatically when a new file is closed).
  144. *
  145. * \param map
  146. * \return 1 ... if successful
  147. * 0 ... otherwise.
  148. */
  149. int Rast3d_range_write(RASTER3D_Map * map)
  150. {
  151. char path[GPATH_MAX];
  152. Rast3d_filename(path, RASTER3D_RANGE_ELEMENT, map->fileName, map->mapset);
  153. remove(path);
  154. if (writeRange(map->fileName, &(map->range)) == -1) {
  155. Rast3d_error("Rast3d_closeCellNew: error in writeRange");
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. int Rast3d_range_init(RASTER3D_Map * map)
  162. {
  163. Rast_init_fp_range(&(map->range));
  164. return 0;
  165. }