GRASS Programmer's Manual  6.4.2(2012)
g3dregion.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <grass/gis.h>
00003 #include <grass/G3d.h>
00004 #include "G3d_intern.h"
00005 
00006 /*---------------------------------------------------------------------------*/
00007 
00008 
00019 void G3d_extract2dRegion(G3D_Region * region3d, struct Cell_head *region2d)
00020 {
00021     region2d->proj = region3d->proj;
00022     region2d->zone = region3d->zone;
00023 
00024     region2d->north = region3d->north;
00025     region2d->south = region3d->south;
00026     region2d->east = region3d->east;
00027     region2d->west = region3d->west;
00028 
00029     region2d->rows = region3d->rows;
00030     region2d->cols = region3d->cols;
00031 
00032     region2d->ns_res = region3d->ns_res;
00033     region2d->ew_res = region3d->ew_res;
00034 }
00035 
00046 void G3d_regionToCellHead(G3D_Region * region3d, struct Cell_head *region2d)
00047 {
00048     region2d->proj = region3d->proj;
00049     region2d->zone = region3d->zone;
00050 
00051     region2d->north = region3d->north;
00052     region2d->south = region3d->south;
00053     region2d->east = region3d->east;
00054     region2d->west = region3d->west;
00055     region2d->top = region3d->top;
00056     region2d->bottom = region3d->bottom;
00057 
00058     region2d->rows = region3d->rows;
00059     region2d->rows3 = region3d->rows;
00060     region2d->cols = region3d->cols;
00061     region2d->cols3 = region3d->cols;
00062     region2d->depths = region3d->depths;
00063 
00064     region2d->ns_res = region3d->ns_res;
00065     region2d->ns_res3 = region3d->ns_res;
00066     region2d->ew_res = region3d->ew_res;
00067     region2d->ew_res3 = region3d->ew_res;
00068     region2d->tb_res = region3d->tb_res;
00069 }
00070 
00071 /*---------------------------------------------------------------------------*/
00072 
00073 
00085 void
00086 G3d_incorporate2dRegion(struct Cell_head *region2d, G3D_Region * region3d)
00087 {
00088     region3d->proj = region2d->proj;
00089     region3d->zone = region2d->zone;
00090 
00091     region3d->north = region2d->north;
00092     region3d->south = region2d->south;
00093     region3d->east = region2d->east;
00094     region3d->west = region2d->west;
00095 
00096     region3d->rows = region2d->rows;
00097     region3d->cols = region2d->cols;
00098 
00099     region3d->ns_res = region2d->ns_res;
00100     region3d->ew_res = region2d->ew_res;
00101 }
00102 
00114 void
00115 G3d_regionFromToCellHead(struct Cell_head *region2d, G3D_Region * region3d)
00116 {
00117     region3d->proj = region2d->proj;
00118     region3d->zone = region2d->zone;
00119 
00120     region3d->north = region2d->north;
00121     region3d->south = region2d->south;
00122     region3d->east = region2d->east;
00123     region3d->west = region2d->west;
00124     region3d->top = region2d->top;
00125     region3d->bottom = region2d->bottom;
00126 
00127     region3d->rows = region2d->rows3;
00128     region3d->cols = region2d->cols3;
00129     region3d->depths = region2d->depths;
00130 
00131     region3d->ns_res = region2d->ns_res3;
00132     region3d->ew_res = region2d->ew_res3;
00133     region3d->tb_res = region2d->tb_res;
00134 }
00135 
00136 /*---------------------------------------------------------------------------*/
00137 
00138 
00149 void G3d_adjustRegion(G3D_Region * region)
00150 {
00151     struct Cell_head region2d;
00152     const char *err;
00153 
00154     G3d_regionToCellHead(region, &region2d);
00155     if ((err = G_adjust_Cell_head3(&region2d, 1, 1, 1)) != NULL) {
00156         G_fatal_error("G3d_adjustRegion: %s", err);
00157     }
00158     G3d_regionFromToCellHead(&region2d, region);
00159 
00160     if (region->depths <= 0)
00161         G3d_fatalError("G3d_adjustRegion: depths <= 0");
00162     region->tb_res = (region->top - region->bottom) / region->depths;
00163 }
00164 
00165 /*---------------------------------------------------------------------------*/
00166 
00167 
00178 void G3d_adjustRegionRes(G3D_Region * region)
00179 {
00180     struct Cell_head region2d;
00181     const char *err;
00182 
00183     G3d_regionToCellHead(region, &region2d);
00184     if ((err = G_adjust_Cell_head3(&region2d, 1, 1, 1)) != NULL) {
00185         G_fatal_error("G3d_adjustRegionRes: %s", err);
00186     }
00187     G3d_regionFromToCellHead(&region2d, region);
00188 
00189     if (region->tb_res <= 0)
00190         G3d_fatalError("G3d_adjustRegionRes: tb_res <= 0");
00191 
00192     region->depths = (region->top - region->bottom + region->tb_res / 2.0) /
00193         region->tb_res;
00194     if (region->depths == 0)
00195         region->depths = 1;
00196 }
00197 
00198 /*---------------------------------------------------------------------------*/
00199 
00200 
00213 void G3d_regionCopy(G3D_Region * regionDest, G3D_Region * regionSrc)
00214 {
00215     G_copy(regionDest, regionSrc, sizeof(G3D_Region));
00216 }
00217 
00218 /*---------------------------------------------------------------------------*/
00219 
00220 
00238 void
00239 G3d_getRegionValue(G3D_Map * map, double north, double east, double top,
00240                    void *value, int type)
00241 {
00242     int row, col, depth;
00243 
00244     /* convert (north, east, top) into (row, col, depth) */
00245 
00246     row = map->region.rows -
00247         (north - map->region.south) / (map->region.north -
00248                                        map->region.south) * map->region.rows;
00249     col =
00250         (east - map->region.west) / (map->region.east -
00251                                      map->region.west) * map->region.cols;
00252     depth =
00253         (top - map->region.bottom) / (map->region.top -
00254                                       map->region.bottom) *
00255         map->region.depths;
00256 
00257     /* if (row, col, depth) outside window return NULL value */
00258     if ((row < 0) || (row >= map->region.rows) ||
00259         (col < 0) || (col >= map->region.cols) ||
00260         (depth < 0) || (depth >= map->region.depths)) {
00261         G3d_setNullValue(value, 1, type);
00262         return;
00263     }
00264 
00265     /* get value */
00266     map->resampleFun(map, row, col, depth, value, type);
00267 }
00268 
00269 /*---------------------------------------------------------------------------*/
00270 
00271 int
00272 G3d_readRegionMap(const char *name, const char *mapset, G3D_Region * region)
00273 {
00274     char fullName[GPATH_MAX];
00275     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00276 
00277     if (G__name_is_fully_qualified(name, xname, xmapset))
00278         G3d_filename(fullName, G3D_HEADER_ELEMENT, xname, xmapset);
00279     else {
00280         if (!mapset || !*mapset)
00281             mapset = G_find_grid3(name, "");
00282         G3d_filename(fullName, G3D_HEADER_ELEMENT, name, mapset);
00283     }
00284     return G3d_readWindow(region, fullName);
00285 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines