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