GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
getvalue.c
Go to the documentation of this file.
1 #include <grass/raster.h>
2 #include "raster3d_intern.h"
3 
4 /*---------------------------------------------------------------------------*/
5 
6 /*!
7  * \brief
8  *
9  * Returns in <em>*value</em> the resampled cell-value of the cell with
10  * window-coordinate <em>(x, y, z)</em>. The value returned is of
11  * <em>type</em>. This function invokes a fatal error if an error occurs.
12  *
13  * \param map
14  * \param x
15  * \param y
16  * \param z
17  * \param value
18  * \param type
19  * \return void
20  */
21 
22 void Rast3d_get_value(RASTER3D_Map *map, int x, int y, int z, void *value,
23  int type)
24 {
25  /* get the resampled value */
26  map->resampleFun(map, x, y, z, value, type);
27 }
28 
29 /*---------------------------------------------------------------------------*/
30 
31 /*!
32  * \brief
33  *
34  * Is equivalent to
35  * <tt>Rast3d_get_value (map, x, y, z, &value, FCELL_TYPE);</tt> return value.
36  *
37  * \param map
38  * \param x
39  * \param y
40  * \param z
41  * \return float
42  */
43 
44 float Rast3d_get_float(RASTER3D_Map *map, int x, int y, int z)
45 {
46  float value;
47 
48  Rast3d_get_value(map, x, y, z, &value, FCELL_TYPE);
49  return value;
50 }
51 
52 /*---------------------------------------------------------------------------*/
53 
54 /*!
55  * \brief
56  *
57  * Is equivalent
58  * to <tt>Rast3d_get_value (map, x, y, z, &value, DCELL_TYPE);</tt> return
59  * value.
60  *
61  * \param map
62  * \param x
63  * \param y
64  * \param z
65  * \return double
66  */
67 
68 double Rast3d_get_double(RASTER3D_Map *map, int x, int y, int z)
69 {
70  double value;
71 
72  Rast3d_get_value(map, x, y, z, &value, DCELL_TYPE);
73  return value;
74 }
75 
76 /*---------------------------------------------------------------------------*/
77 
78 /*!
79  * \brief
80  *
81  * Returns in <em>value</em> the value of the <em>map</em> which corresponds to
82  * window coordinates <em>(north, east, top)</em>. The
83  * value is resampled using the resampling function specified for <em>map</em>.
84  * The <em>value</em> is of <em>type</em>.
85  *
86  * \param map
87  * \param north
88  * \param east
89  * \param top
90  * \param value
91  * \param type
92  * \return void
93  */
94 
95 void Rast3d_get_window_value(RASTER3D_Map *map, double north, double east,
96  double top, void *value, int type)
97 {
98  int col, row, depth;
99 
100  Rast3d_location2coord(&(map->window), north, east, top, &col, &row, &depth);
101 
102  /* if (row, col, depth) outside window return NULL value */
103  if ((row < 0) || (row >= map->window.rows) || (col < 0) ||
104  (col >= map->window.cols) || (depth < 0) ||
105  (depth >= map->window.depths)) {
106  Rast3d_set_null_value(value, 1, type);
107  return;
108  }
109 
110  /* Get the value from the map in map-region resolution */
111  map->resampleFun(map, col, row, depth, value, type);
112 }
113 
114 /*---------------------------------------------------------------------------*/
115 
116 /*!
117  * \brief
118  *
119  * Returns in <em>value</em> the value of the <em>map</em> which corresponds to
120  * region coordinates <em>(north, east, top)</em>.
121  *
122  * \param map
123  * \param north
124  * \param east
125  * \param top
126  * \param value
127  * \param type
128  * \return void
129  */
130 
131 void Rast3d_get_region_value(RASTER3D_Map *map, double north, double east,
132  double top, void *value, int type)
133 {
134  int row, col, depth;
135 
136  Rast3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
137 
138  /* if (row, col, depth) outside region return NULL value */
139  if ((row < 0) || (row >= map->region.rows) || (col < 0) ||
140  (col >= map->region.cols) || (depth < 0) ||
141  (depth >= map->region.depths)) {
142  Rast3d_set_null_value(value, 1, type);
143  return;
144  }
145 
146  /* Get the value from the map in map-region resolution */
147  Rast3d_get_value_region(map, col, row, depth, value, type);
148 }
149 
150 /*---------------------------------------------------------------------------*/
151 
152 /*!
153  * \brief
154  *
155  * Is equivalent to <tt>Rast3d_get_value_region (map, x, y, z, &value,
156  * FCELL_TYPE);</tt> return value.
157  *
158  * \param map
159  * \param x
160  * \param y
161  * \param z
162  * \return float
163  */
164 
165 float Rast3d_get_float_region(RASTER3D_Map *map, int x, int y, int z)
166 {
167  int tileIndex, offs;
168  float *tile;
169  float value;
170 
171  if (map->typeIntern == DCELL_TYPE)
172  return (float)Rast3d_get_double_region(map, x, y, z);
173 
174  /* In case of region coordinates out of bounds, return the Null value */
175  if (x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
176  y >= map->region.rows || z >= map->region.depths) {
177  Rast3d_set_null_value(&value, 1, FCELL_TYPE);
178  return value;
179  }
180 
181  Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
182  tile = (float *)Rast3d_get_tile_ptr(map, tileIndex);
183 
184  if (tile == NULL)
186  "Rast3d_get_float_region: error in Rast3d_get_tile_ptr."
187  "Region coordinates x %i y %i z %i tile index %i offset %i",
188  x, y, z, tileIndex, offs);
189 
190  return tile[offs];
191 }
192 
193 /*---------------------------------------------------------------------------*/
194 
195 /*!
196  * \brief
197  *
198  * Is equivalent to <tt>Rast3d_get_value_region (map, x, y, z, &value,
199  * DCELL_TYPE);</tt> return value.
200  *
201  * \param map
202  * \param x
203  * \param y
204  * \param z
205  * \return double
206  */
207 
208 double Rast3d_get_double_region(RASTER3D_Map *map, int x, int y, int z)
209 {
210  int tileIndex, offs;
211  double *tile;
212  double value;
213 
214  if (map->typeIntern == FCELL_TYPE)
215  return (double)Rast3d_get_float_region(map, x, y, z);
216 
217  /* In case of region coordinates out of bounds, return the Null value */
218  if (x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
219  y >= map->region.rows || z >= map->region.depths) {
220  Rast3d_set_null_value(&value, 1, DCELL_TYPE);
221  return value;
222  }
223 
224  Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
225  tile = (double *)Rast3d_get_tile_ptr(map, tileIndex);
226 
227  if (tile == NULL)
229  "Rast3d_get_double_region: error in Rast3d_get_tile_ptr."
230  "Region coordinates x %i y %i z %i tile index %i offset %i",
231  x, y, z, tileIndex, offs);
232 
233  return tile[offs];
234 }
235 
236 /*---------------------------------------------------------------------------*/
237 
238 /*!
239  * \brief
240  *
241  * Returns in <em>*value</em> the cell-value of the cell with
242  * region-coordinate <em>(x, y, z)</em>. The value returned is of
243  * <em>type</em>. Here <em>region</em> means the coordinate in the cube of data
244  * in the file, i.e. ignoring geographic coordinates. In case the region
245  * coordinates are out of bounds, the Null value will be returned. This function
246  * invokes a fatal error if an error occurs.
247  *
248  * \param map
249  * \param x
250  * \param y
251  * \param z
252  * \param value
253  * \param type
254  * \return void
255  */
256 
257 void Rast3d_get_value_region(RASTER3D_Map *map, int x, int y, int z,
258  void *value, int type)
259 {
260  if (type == FCELL_TYPE) {
261  *((float *)value) = Rast3d_get_float_region(map, x, y, z);
262  return;
263  }
264 
265  *((double *)value) = Rast3d_get_double_region(map, x, y, z);
266 }
#define NULL
Definition: ccmath.h:32
void Rast3d_location2coord(RASTER3D_Region *, double, double, double, int *, int *, int *)
Converts region-coordinates (north, east, top) into cell-coordinates (x, y, z).
Definition: region.c:284
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:34
void * Rast3d_get_tile_ptr(RASTER3D_Map *, int)
This function returns a pointer to a tile which contains the data for the tile with index tileIndex....
Definition: tileio.c:79
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_coord2tile_index(RASTER3D_Map *, int, int, int, int *, int *)
Converts cell-coordinates (x, y, z) into tileIndex and the offset of the cell within the tile.
Definition: tilemath.c:158
void Rast3d_get_region_value(RASTER3D_Map *map, double north, double east, double top, void *value, int type)
Returns in value the value of the map which corresponds to region coordinates (north,...
Definition: getvalue.c:131
float Rast3d_get_float_region(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value_region (map, x, y, z, &value, FCELL_TYPE); return value.
Definition: getvalue.c:165
float Rast3d_get_float(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value (map, x, y, z, &value, FCELL_TYPE); return value.
Definition: getvalue.c:44
double Rast3d_get_double_region(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value_region (map, x, y, z, &value, DCELL_TYPE); return value.
Definition: getvalue.c:208
void Rast3d_get_window_value(RASTER3D_Map *map, double north, double east, double top, void *value, int type)
Returns in value the value of the map which corresponds to window coordinates (north,...
Definition: getvalue.c:95
double Rast3d_get_double(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value (map, x, y, z, &value, DCELL_TYPE); return value.
Definition: getvalue.c:68
void Rast3d_get_value_region(RASTER3D_Map *map, int x, int y, int z, void *value, int type)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: getvalue.c:257
void Rast3d_get_value(RASTER3D_Map *map, int x, int y, int z, void *value, int type)
Returns in *value the resampled cell-value of the cell with window-coordinate (x, y,...
Definition: getvalue.c:22
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
resample_fn * resampleFun
Definition: raster3d.h:88
RASTER3D_Region window
Definition: raster3d.h:85
RASTER3D_Region region
Definition: raster3d.h:82
int typeIntern
Definition: raster3d.h:150
#define x