GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
g3dregion.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <grass/gis.h>
3 #include <grass/G3d.h>
4 #include "G3d_intern.h"
5 
6 /*---------------------------------------------------------------------------*/
7 
8 
19 void G3d_extract2dRegion(G3D_Region * region3d, 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 
46 void G3d_regionToCellHead(G3D_Region * region3d, 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 
85 void
86 G3d_incorporate2dRegion(struct Cell_head *region2d, G3D_Region * region3d)
87 {
88  region3d->proj = region2d->proj;
89  region3d->zone = region2d->zone;
90 
91  region3d->north = region2d->north;
92  region3d->south = region2d->south;
93  region3d->east = region2d->east;
94  region3d->west = region2d->west;
95 
96  region3d->rows = region2d->rows;
97  region3d->cols = region2d->cols;
98 
99  region3d->ns_res = region2d->ns_res;
100  region3d->ew_res = region2d->ew_res;
101 }
102 
114 void
115 G3d_regionFromToCellHead(struct Cell_head *region2d, G3D_Region * region3d)
116 {
117  region3d->proj = region2d->proj;
118  region3d->zone = region2d->zone;
119 
120  region3d->north = region2d->north;
121  region3d->south = region2d->south;
122  region3d->east = region2d->east;
123  region3d->west = region2d->west;
124  region3d->top = region2d->top;
125  region3d->bottom = region2d->bottom;
126 
127  region3d->rows = region2d->rows3;
128  region3d->cols = region2d->cols3;
129  region3d->depths = region2d->depths;
130 
131  region3d->ns_res = region2d->ns_res3;
132  region3d->ew_res = region2d->ew_res3;
133  region3d->tb_res = region2d->tb_res;
134 }
135 
136 /*---------------------------------------------------------------------------*/
137 
138 
149 void G3d_adjustRegion(G3D_Region * region)
150 {
151  struct Cell_head region2d;
152  const char *err;
153 
154  G3d_regionToCellHead(region, &region2d);
155  if ((err = G_adjust_Cell_head3(&region2d, 1, 1, 1)) != NULL) {
156  G_fatal_error("G3d_adjustRegion: %s", err);
157  }
158  G3d_regionFromToCellHead(&region2d, region);
159 
160  if (region->depths <= 0)
161  G3d_fatalError("G3d_adjustRegion: depths <= 0");
162  region->tb_res = (region->top - region->bottom) / region->depths;
163 }
164 
165 /*---------------------------------------------------------------------------*/
166 
167 
178 void G3d_adjustRegionRes(G3D_Region * region)
179 {
180  struct Cell_head region2d;
181  const char *err;
182 
183  G3d_regionToCellHead(region, &region2d);
184  if ((err = G_adjust_Cell_head3(&region2d, 1, 1, 1)) != NULL) {
185  G_fatal_error("G3d_adjustRegionRes: %s", err);
186  }
187  G3d_regionFromToCellHead(&region2d, region);
188 
189  if (region->tb_res <= 0)
190  G3d_fatalError("G3d_adjustRegionRes: tb_res <= 0");
191 
192  region->depths = (region->top - region->bottom + region->tb_res / 2.0) /
193  region->tb_res;
194  if (region->depths == 0)
195  region->depths = 1;
196 }
197 
198 /*---------------------------------------------------------------------------*/
199 
200 
213 void G3d_regionCopy(G3D_Region * regionDest, G3D_Region * regionSrc)
214 {
215  G_copy(regionDest, regionSrc, sizeof(G3D_Region));
216 }
217 
218 /*---------------------------------------------------------------------------*/
219 
220 
238 void
239 G3d_getRegionValue(G3D_Map * map, double north, double east, double top,
240  void *value, int type)
241 {
242  int row, col, depth;
243 
244  /* convert (north, east, top) into (row, col, depth) */
245 
246  row = map->region.rows -
247  (north - map->region.south) / (map->region.north -
248  map->region.south) * map->region.rows;
249  col =
250  (east - map->region.west) / (map->region.east -
251  map->region.west) * map->region.cols;
252  depth =
253  (top - map->region.bottom) / (map->region.top -
254  map->region.bottom) *
255  map->region.depths;
256 
257  /* if (row, col, depth) outside window return NULL value */
258  if ((row < 0) || (row >= map->region.rows) ||
259  (col < 0) || (col >= map->region.cols) ||
260  (depth < 0) || (depth >= map->region.depths)) {
261  G3d_setNullValue(value, 1, type);
262  return;
263  }
264 
265  /* get value */
266  map->resampleFun(map, row, col, depth, value, type);
267 }
268 
269 /*---------------------------------------------------------------------------*/
270 
271 int
272 G3d_readRegionMap(const char *name, const char *mapset, G3D_Region * region)
273 {
274  char fullName[GPATH_MAX];
275  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
276 
277  if (G__name_is_fully_qualified(name, xname, xmapset))
278  G3d_filename(fullName, G3D_HEADER_ELEMENT, xname, xmapset);
279  else {
280  if (!mapset || !*mapset)
281  mapset = G_find_grid3(name, "");
282  G3d_filename(fullName, G3D_HEADER_ELEMENT, name, mapset);
283  }
284  return G3d_readWindow(region, fullName);
285 }