getvalue.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <grass/raster.h>
  2. #include "raster3d_intern.h"
  3. /*---------------------------------------------------------------------------*/
  4. /*!
  5. * \brief
  6. *
  7. * Returns in <em>*value</em> the resampled cell-value of the cell with
  8. * window-coordinate <em>(x, y, z)</em>. The value returned is of <em>type</em>.
  9. * This function invokes a fatal error if an error occurs.
  10. *
  11. * \param map
  12. * \param x
  13. * \param y
  14. * \param z
  15. * \param value
  16. * \param type
  17. * \return void
  18. */
  19. void Rast3d_get_value(RASTER3D_Map * map, int x, int y, int z, void *value, int type)
  20. {
  21. /* get the resampled value */
  22. map->resampleFun(map, x, y, z, value, type);
  23. }
  24. /*---------------------------------------------------------------------------*/
  25. /*!
  26. * \brief
  27. *
  28. * Is equivalent to
  29. * <tt>Rast3d_get_value (map, x, y, z, &value, FCELL_TYPE);</tt> return value.
  30. *
  31. * \param map
  32. * \param x
  33. * \param y
  34. * \param z
  35. * \return float
  36. */
  37. float Rast3d_get_float(RASTER3D_Map * map, int x, int y, int z)
  38. {
  39. float value;
  40. Rast3d_get_value(map, x, y, z, &value, FCELL_TYPE);
  41. return value;
  42. }
  43. /*---------------------------------------------------------------------------*/
  44. /*!
  45. * \brief
  46. *
  47. * Is equivalent
  48. * to <tt>Rast3d_get_value (map, x, y, z, &value, DCELL_TYPE);</tt> return value.
  49. *
  50. * \param map
  51. * \param x
  52. * \param y
  53. * \param z
  54. * \return double
  55. */
  56. double Rast3d_get_double(RASTER3D_Map * map, int x, int y, int z)
  57. {
  58. double value;
  59. Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
  60. return value;
  61. }
  62. /*---------------------------------------------------------------------------*/
  63. /*!
  64. * \brief
  65. *
  66. * Returns in <em>value</em> the value of the <em>map</em> which corresponds to
  67. * window coordinates <em>(north, east, top)</em>. The
  68. * value is resampled using the resampling function specified for <em>map</em>. The
  69. * <em>value</em> is of <em>type</em>.
  70. *
  71. * \param map
  72. * \param north
  73. * \param east
  74. * \param top
  75. * \param value
  76. * \param type
  77. * \return void
  78. */
  79. void
  80. Rast3d_get_window_value(RASTER3D_Map * map, double north, double east, double top,
  81. void *value, int type)
  82. {
  83. int col, row, depth;
  84. Rast3d_location2coord(&(map->window), north, east, top, &col, &row, &depth);
  85. /* if (row, col, depth) outside window return NULL value */
  86. if ((row < 0) || (row >= map->window.rows) ||
  87. (col < 0) || (col >= map->window.cols) ||
  88. (depth < 0) || (depth >= map->window.depths)) {
  89. Rast3d_set_null_value(value, 1, type);
  90. return;
  91. }
  92. /* Get the value from the map in map-region resolution */
  93. map->resampleFun(map, col, row, depth, value, type);
  94. }
  95. /*---------------------------------------------------------------------------*/
  96. /*!
  97. * \brief
  98. *
  99. * Returns in <em>value</em> the value of the <em>map</em> which corresponds to
  100. * region coordinates <em>(north, east, top)</em>.
  101. *
  102. * \param map
  103. * \param north
  104. * \param east
  105. * \param top
  106. * \param value
  107. * \param type
  108. * \return void
  109. */
  110. void
  111. Rast3d_get_region_value(RASTER3D_Map * map, double north, double east, double top,
  112. void *value, int type)
  113. {
  114. int row, col, depth;
  115. Rast3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  116. /* if (row, col, depth) outside region return NULL value */
  117. if ((row < 0) || (row >= map->region.rows) ||
  118. (col < 0) || (col >= map->region.cols) ||
  119. (depth < 0) || (depth >= map->region.depths)) {
  120. Rast3d_set_null_value(value, 1, type);
  121. return;
  122. }
  123. /* Get the value from the map in map-region resolution */
  124. Rast3d_get_value_region(map, col, row, depth, value, type);
  125. }
  126. /*---------------------------------------------------------------------------*/
  127. /*!
  128. * \brief
  129. *
  130. * Is equivalent to <tt>Rast3d_get_value_region (map, x, y, z, &value, FCELL_TYPE);</tt>
  131. * return value.
  132. *
  133. * \param map
  134. * \param x
  135. * \param y
  136. * \param z
  137. * \return float
  138. */
  139. float Rast3d_get_float_region(RASTER3D_Map * map, int x, int y, int z)
  140. {
  141. int tileIndex, offs;
  142. float *tile;
  143. float value;
  144. if (map->typeIntern == DCELL_TYPE)
  145. return (float)Rast3d_get_double_region(map, x, y, z);
  146. /* In case of region coordinates out of bounds, return the Null value */
  147. if(x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
  148. y >= map->region.rows || z >= map->region.depths) {
  149. Rast3d_set_null_value(&value, 1, FCELL_TYPE);
  150. return value;
  151. }
  152. Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
  153. tile = (float *)Rast3d_get_tile_ptr(map, tileIndex);
  154. if (tile == NULL)
  155. Rast3d_fatal_error("Rast3d_get_float_region: error in Rast3d_get_tile_ptr."
  156. "Region coordinates x %i y %i z %i tile index %i offset %i",
  157. x, y, z, tileIndex, offs);
  158. return tile[offs];
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. /*!
  162. * \brief
  163. *
  164. * Is equivalent to <tt>Rast3d_get_value_region (map, x, y, z, &value,
  165. * DCELL_TYPE);</tt> return value.
  166. *
  167. * \param map
  168. * \param x
  169. * \param y
  170. * \param z
  171. * \return double
  172. */
  173. double Rast3d_get_double_region(RASTER3D_Map * map, int x, int y, int z)
  174. {
  175. int tileIndex, offs;
  176. double *tile;
  177. double value;
  178. if (map->typeIntern == FCELL_TYPE)
  179. return (double)Rast3d_get_float_region(map, x, y, z);
  180. /* In case of region coordinates out of bounds, return the Null value */
  181. if(x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
  182. y >= map->region.rows || z >= map->region.depths) {
  183. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  184. return value;
  185. }
  186. Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
  187. tile = (double *)Rast3d_get_tile_ptr(map, tileIndex);
  188. if (tile == NULL)
  189. Rast3d_fatal_error("Rast3d_get_double_region: error in Rast3d_get_tile_ptr."
  190. "Region coordinates x %i y %i z %i tile index %i offset %i",
  191. x, y, z, tileIndex, offs);
  192. return tile[offs];
  193. }
  194. /*---------------------------------------------------------------------------*/
  195. /*!
  196. * \brief
  197. *
  198. * Returns in <em>*value</em> the cell-value of the cell with
  199. * region-coordinate <em>(x, y, z)</em>. The value returned is of <em>type</em>.
  200. * Here <em>region</em> means the coordinate in the cube of data in the file, i.e.
  201. * ignoring geographic coordinates.
  202. * In case the region coordinates are out of bounds, the Null value will be returned.
  203. * This function invokes a fatal error if an error occurs.
  204. *
  205. * \param map
  206. * \param x
  207. * \param y
  208. * \param z
  209. * \param value
  210. * \param type
  211. * \return void
  212. */
  213. void
  214. Rast3d_get_value_region(RASTER3D_Map * map, int x, int y, int z, void *value, int type)
  215. {
  216. if (type == FCELL_TYPE) {
  217. *((float *)value) = Rast3d_get_float_region(map, x, y, z);
  218. return;
  219. }
  220. *((double *)value) = Rast3d_get_double_region(map, x, y, z);
  221. }