GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
region.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <math.h>
3 
4 #include <grass/raster.h>
5 #include "raster3d_intern.h"
6 
7 /*---------------------------------------------------------------------------*/
8 
9 /*!
10  * \brief
11  *
12  * Returns in <em>region2d</em> the <em>2d</em> portion of <em>region3d</em>.
13  *
14  * \param region3d
15  * \param region2d
16  * \return void
17  */
18 
20  struct Cell_head *region2d)
21 {
22  region2d->proj = region3d->proj;
23  region2d->zone = region3d->zone;
24 
25  region2d->north = region3d->north;
26  region2d->south = region3d->south;
27  region2d->east = region3d->east;
28  region2d->west = region3d->west;
29 
30  region2d->rows = region3d->rows;
31  region2d->cols = region3d->cols;
32 
33  region2d->ns_res = region3d->ns_res;
34  region2d->ew_res = region3d->ew_res;
35 }
36 
37 /*!
38  * \brief
39  *
40  * Returns in <em>region2d</em> the <em>2d</em> portion of <em>region3d</em>.
41  *
42  * \param region3d
43  * \param region2d
44  * \return void
45  */
46 
48  struct Cell_head *region2d)
49 {
50  region2d->proj = region3d->proj;
51  region2d->zone = region3d->zone;
52 
53  region2d->north = region3d->north;
54  region2d->south = region3d->south;
55  region2d->east = region3d->east;
56  region2d->west = region3d->west;
57  region2d->top = region3d->top;
58  region2d->bottom = region3d->bottom;
59 
60  region2d->rows = region3d->rows;
61  region2d->rows3 = region3d->rows;
62  region2d->cols = region3d->cols;
63  region2d->cols3 = region3d->cols;
64  region2d->depths = region3d->depths;
65 
66  region2d->ns_res = region3d->ns_res;
67  region2d->ns_res3 = region3d->ns_res;
68  region2d->ew_res = region3d->ew_res;
69  region2d->ew_res3 = region3d->ew_res;
70  region2d->tb_res = region3d->tb_res;
71 }
72 
73 /*---------------------------------------------------------------------------*/
74 
75 /*!
76  * \brief
77  *
78  * Replaces the <em>2d</em> portion of <em>region3d</em> with the
79  * values stored in <em>region2d</em>.
80  *
81  * \param region2d
82  * \param region3d
83  * \return void
84  */
85 
86 void Rast3d_incorporate2d_region(struct Cell_head *region2d,
87  RASTER3D_Region *region3d)
88 {
89  region3d->proj = region2d->proj;
90  region3d->zone = region2d->zone;
91 
92  region3d->north = region2d->north;
93  region3d->south = region2d->south;
94  region3d->east = region2d->east;
95  region3d->west = region2d->west;
96 
97  region3d->rows = region2d->rows;
98  region3d->cols = region2d->cols;
99 
100  region3d->ns_res = region2d->ns_res;
101  region3d->ew_res = region2d->ew_res;
102 }
103 
104 /*!
105  * \brief
106  *
107  * Replaces the <em>2d</em> portion of <em>region3d</em> with the
108  * values stored in <em>region2d</em>.
109  *
110  * \param region2d
111  * \param region3d
112  * \return void
113  */
114 
116  RASTER3D_Region *region3d)
117 {
118  region3d->proj = region2d->proj;
119  region3d->zone = region2d->zone;
120 
121  region3d->north = region2d->north;
122  region3d->south = region2d->south;
123  region3d->east = region2d->east;
124  region3d->west = region2d->west;
125  region3d->top = region2d->top;
126  region3d->bottom = region2d->bottom;
127 
128  region3d->rows = region2d->rows3;
129  region3d->cols = region2d->cols3;
130  region3d->depths = region2d->depths;
131 
132  region3d->ns_res = region2d->ns_res3;
133  region3d->ew_res = region2d->ew_res3;
134  region3d->tb_res = region2d->tb_res;
135 }
136 
137 /*---------------------------------------------------------------------------*/
138 
139 /*!
140  * \brief
141  *
142  * Computes an adjusts the resolutions in the region structure from the region
143  * boundaries and number of cells per dimension.
144  *
145  * \param region
146  * \return void
147  */
148 
150 {
151  struct Cell_head region2d;
152 
153  Rast3d_region_to_cell_head(region, &region2d);
154  G_adjust_Cell_head3(&region2d, 1, 1, 1);
155  Rast3d_region_from_to_cell_head(&region2d, region);
156 
157  if (region->depths <= 0)
158  Rast3d_fatal_error("Rast3d_adjust_region: depths <= 0");
159  region->tb_res = (region->top - region->bottom) / region->depths;
160 }
161 
162 /*---------------------------------------------------------------------------*/
163 
164 /*!
165  * \brief
166  *
167  * Computes an adjusts the number of cells per dimension in the region
168  * structure from the region boundaries and resolutions.
169  *
170  * \param region
171  * \return void
172  */
173 
175 {
176  struct Cell_head region2d;
177 
178  Rast3d_region_to_cell_head(region, &region2d);
179  G_adjust_Cell_head3(&region2d, 1, 1, 1);
180  Rast3d_region_from_to_cell_head(&region2d, region);
181 
182  if (region->tb_res <= 0)
183  Rast3d_fatal_error("Rast3d_adjust_region_res: tb_res <= 0");
184 
185  region->depths =
186  (region->top - region->bottom + region->tb_res / 2.0) / region->tb_res;
187  if (region->depths == 0)
188  region->depths = 1;
189 }
190 
191 /*---------------------------------------------------------------------------*/
192 
193 /*!
194  * \brief
195  *
196  * Copies the values of <em>regionSrc</em> into <em>regionDst</em>.
197  *
198  * \param regionDest
199  * \param regionSrc
200  * \return void
201  */
202 
204 {
205  regionDest->proj = regionSrc->proj;
206  regionDest->zone = regionSrc->zone;
207 
208  regionDest->north = regionSrc->north;
209  regionDest->south = regionSrc->south;
210  regionDest->east = regionSrc->east;
211  regionDest->west = regionSrc->west;
212  regionDest->top = regionSrc->top;
213  regionDest->bottom = regionSrc->bottom;
214 
215  regionDest->rows = regionSrc->rows;
216  regionDest->cols = regionSrc->cols;
217  regionDest->depths = regionSrc->depths;
218 
219  regionDest->ns_res = regionSrc->ns_res;
220  regionDest->ew_res = regionSrc->ew_res;
221  regionDest->tb_res = regionSrc->tb_res;
222 }
223 
224 /*---------------------------------------------------------------------------*/
225 
226 int Rast3d_read_region_map(const char *name, const char *mapset,
227  RASTER3D_Region *region)
228 {
229  char fullName[GPATH_MAX];
230  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
231 
232  if (G_name_is_fully_qualified(name, xname, xmapset))
233  Rast3d_filename(fullName, RASTER3D_HEADER_ELEMENT, xname, xmapset);
234  else {
235  if (!mapset || !*mapset)
236  mapset = G_find_raster3d(name, "");
237  Rast3d_filename(fullName, RASTER3D_HEADER_ELEMENT, name, mapset);
238  }
239  return Rast3d_read_window(region, fullName);
240 }
241 
242 /*---------------------------------------------------------------------------*/
243 
244 /*!
245  * \brief
246  *
247  * Returns 1 if region-coordinates <em>(north, east, top)</em> are
248  * inside the region of <em>map</em>. Returns 0 otherwise.
249  *
250  * \param region
251  * \param north
252  * \param east
253  * \param top
254  * \return int
255  */
256 
258  double top)
259 {
260  return ((north >= region->south) && (north <= region->north) &&
261  (east >= region->west) && (east <= region->east) &&
262  (((top >= region->bottom) && (top <= region->top)) ||
263  ((top <= region->bottom) && (top >= region->top))));
264 }
265 
266 /*---------------------------------------------------------------------------*/
267 
268 /*!
269  * \brief
270  *
271  * Converts region-coordinates <em>(north, east,
272  * top)</em> into cell-coordinates <em>(x, y, z)</em>.
273  *
274  * \param region
275  * \param north
276  * \param east
277  * \param top
278  * \param x
279  * \param y
280  * \param z
281  * \return void
282  */
283 
284 void Rast3d_location2coord(RASTER3D_Region *region, double north, double east,
285  double top, int *x, int *y, int *z)
286 {
287  double col, row, depth;
288 
289  LOCATION_TO_COORD(region, north, east, top, &col, &row, &depth);
290 
291  *x = (int)floor(col);
292  *y = (int)floor(row);
293  *z = (int)floor(depth);
294 }
295 
296 /*!
297  * \brief
298  *
299  * Converts region-coordinates <em>(north, east,
300  * top)</em> into cell-coordinates <em>(x, y, z)</em>.
301  *
302  * <b>Note:</b> The results are <i>double</i> numbers. Casting them to
303  * <i>int</i> will give the column, row and depth number.
304  *
305  * \param region
306  * \param north
307  * \param east
308  * \param top
309  * \param x
310  * \param y
311  * \param z
312  * \return void
313  */
314 
316  double east, double top, double *x, double *y,
317  double *z)
318 {
319  LOCATION_TO_COORD(region, north, east, top, x, y, z);
320 
321  G_debug(4, "Rast3d_location2coord_double x %f y %f z %f\n", *x, *y, *z);
322 }
323 
324 /*!
325  * \brief
326  *
327  * Converts region-coordinates <em>(north, east,
328  * top)</em> into cell-coordinates <em>(x, y, z)</em>.
329  * This function calls Rast3d_fatal_error in case location is not in window.
330  *
331  * \param region
332  * \param north
333  * \param east
334  * \param top
335  * \param x
336  * \param y
337  * \param z
338  * \return void
339  */
340 
341 void Rast3d_location2coord2(RASTER3D_Region *region, double north, double east,
342  double top, int *x, int *y, int *z)
343 {
344  if (!Rast3d_is_valid_location(region, north, east, top))
345  Rast3d_fatal_error("Rast3d_location2coord2: location not in region");
346 
347  double col, row, depth;
348 
349  LOCATION_TO_COORD(region, north, east, top, &col, &row, &depth);
350 
351  *x = (int)floor(col);
352  *y = (int)floor(row);
353  *z = (int)floor(depth);
354 }
355 
356 /*!
357  * \brief
358  *
359  * Converts cell-coordinates <em>(x, y, z)</em> into region-coordinates
360  * <em>(north, east, top)</em>.
361  *
362  * * <b>Note:</b> x, y and z is a double:
363  * - x+0.0 will return the easting for the western edge of the column.
364  * - x+0.5 will return the easting for the center of the column.
365  * - x+1.0 will return the easting for the eastern edge of the column.
366  *
367  * - y+0.0 will return the northing for the northern edge of the row.
368  * - y+0.5 will return the northing for the center of the row.
369  * - y+1.0 will return the northing for the southern edge of the row.
370  *
371  * - z+0.0 will return the top for the lower edge of the depth.
372  * - z+0.5 will return the top for the center of the depth.
373  * - z+1.0 will return the top for the upper edge of the column.
374  *
375  *
376  * \param region
377  * \param x
378  * \param y
379  * \param z
380  * \param north
381  * \param east
382  * \param top
383  * \return void
384  */
385 
386 void Rast3d_coord2location(RASTER3D_Region *region, double x, double y,
387  double z, double *north, double *east, double *top)
388 {
389  COORD_TO_LOCATION(region, x, y, z, north, east, top);
390 
391  G_debug(4, "Rast3d_coord2location north %g east %g top %g\n", *north, *east,
392  *top);
393 }
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
const char * G_find_raster3d(const char *, const char *)
Search for a 3D raster map in current search path or in a specified mapset.
Definition: find_rast3d.c:28
int G_debug(int, const char *,...) __attribute__((format(printf
void G_adjust_Cell_head3(struct Cell_head *, int, int, int)
Adjust cell header for 3D values.
Definition: adj_cellhd.c:163
void Rast3d_filename(char *, const char *, const char *, const char *)
Definition: filename.c:9
int Rast3d_read_window(RASTER3D_Region *, const char *)
Reads window from the file specified by windowName. The name is converted by the rules defined in win...
Definition: windowio.c:133
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
#define GMAPSET_MAX
Definition: gis.h:192
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
const char * name
Definition: named_colr.c:6
#define RASTER3D_HEADER_ELEMENT
Definition: raster3d.h:35
#define LOCATION_TO_COORD(region, north, east, top, x, y, z)
#define COORD_TO_LOCATION(region, x, y, z, north, east, top)
void Rast3d_location2coord_double(RASTER3D_Region *region, double north, double east, double top, double *x, double *y, double *z)
Converts region-coordinates (north, east, top) into cell-coordinates (x, y, z).
Definition: region.c:315
void Rast3d_location2coord(RASTER3D_Region *region, double north, double east, double top, int *x, int *y, int *z)
Converts region-coordinates (north, east, top) into cell-coordinates (x, y, z).
Definition: region.c:284
void Rast3d_extract2d_region(RASTER3D_Region *region3d, struct Cell_head *region2d)
Returns in region2d the 2d portion of region3d.
Definition: region.c:19
void Rast3d_adjust_region_res(RASTER3D_Region *region)
Computes an adjusts the number of cells per dimension in the region structure from the region boundar...
Definition: region.c:174
int Rast3d_is_valid_location(RASTER3D_Region *region, double north, double east, double top)
Returns 1 if region-coordinates (north, east, top) are inside the region of map. Returns 0 otherwise.
Definition: region.c:257
void Rast3d_region_copy(RASTER3D_Region *regionDest, RASTER3D_Region *regionSrc)
Copies the values of regionSrc into regionDst.
Definition: region.c:203
void Rast3d_adjust_region(RASTER3D_Region *region)
Computes an adjusts the resolutions in the region structure from the region boundaries and number of ...
Definition: region.c:149
void Rast3d_incorporate2d_region(struct Cell_head *region2d, RASTER3D_Region *region3d)
Replaces the 2d portion of region3d with the values stored in region2d.
Definition: region.c:86
void Rast3d_region_to_cell_head(RASTER3D_Region *region3d, struct Cell_head *region2d)
Returns in region2d the 2d portion of region3d.
Definition: region.c:47
void Rast3d_region_from_to_cell_head(struct Cell_head *region2d, RASTER3D_Region *region3d)
Replaces the 2d portion of region3d with the values stored in region2d.
Definition: region.c:115
int Rast3d_read_region_map(const char *name, const char *mapset, RASTER3D_Region *region)
Definition: region.c:226
void Rast3d_coord2location(RASTER3D_Region *region, double x, double y, double z, double *north, double *east, double *top)
Converts cell-coordinates (x, y, z) into region-coordinates (north, east, top).
Definition: region.c:386
void Rast3d_location2coord2(RASTER3D_Region *region, double north, double east, double top, int *x, int *y, int *z)
Converts region-coordinates (north, east, top) into cell-coordinates (x, y, z). This function calls R...
Definition: region.c:341
if(!(yy_init))
Definition: sqlp.yy.c:775
2D/3D raster map header (used also for region)
Definition: gis.h:440
int cols3
Number of columns for 3D data.
Definition: gis.h:461
double ew_res
Resolution - east to west cell size for 2D data.
Definition: gis.h:476
double north
Extent coordinates (north)
Definition: gis.h:486
double bottom
Extent coordinates (bottom) - 3D data.
Definition: gis.h:496
int zone
Projection zone (UTM)
Definition: gis.h:474
int depths
number of depths for 3D data
Definition: gis.h:463
double east
Extent coordinates (east)
Definition: gis.h:490
double ew_res3
Resolution - east to west cell size for 3D data.
Definition: gis.h:478
double ns_res
Resolution - north to south cell size for 2D data.
Definition: gis.h:480
double ns_res3
Resolution - north to south cell size for 3D data.
Definition: gis.h:482
double top
Extent coordinates (top) - 3D data.
Definition: gis.h:494
int rows3
Number of rows for 3D data.
Definition: gis.h:457
int rows
Number of rows for 2D data.
Definition: gis.h:455
int cols
Number of columns for 2D data.
Definition: gis.h:459
int proj
Projection code.
Definition: gis.h:472
double south
Extent coordinates (south)
Definition: gis.h:488
double tb_res
Resolution - top to bottom cell size for 3D data.
Definition: gis.h:484
double west
Extent coordinates (west)
Definition: gis.h:492
double tb_res
Definition: raster3d.h:56
double north
Definition: raster3d.h:49
double south
Definition: raster3d.h:49
double east
Definition: raster3d.h:50
double ns_res
Definition: raster3d.h:56
double ew_res
Definition: raster3d.h:56
double bottom
Definition: raster3d.h:51
double top
Definition: raster3d.h:51
double west
Definition: raster3d.h:50
#define x