GRASS 8 Programmer's Manual 8.6.0dev(2026)-745b61fbf6
Loading...
Searching...
No Matches
n_geom.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Grass PDE Numerical Library
4 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5 * soerengebbert <at> gmx <dot> de
6 *
7 * PURPOSE: part of the gpde library
8 * allocation, destroying and initializing the geometric struct
9 *
10 * SPDX-FileCopyrightText: 2000 GRASS Development Team
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 *****************************************************************************/
14
15#include <grass/N_pde.h>
16
17/* *************************************************************** *
18 * *********** Konstruktor *************************************** *
19 * *************************************************************** */
20/*!
21 * \brief Allocate the pde geometry data structure and return a pointer to the
22 * new allocated structure
23 *
24 * \return N_geom_data *
25 * */
27{
29
30 geom->area = NULL;
31 geom->planimetric = 1;
32 geom->dim = 0;
33
34 return geom;
35}
36
37/* *************************************************************** *
38 * *********** Destructor **************************************** *
39 * *************************************************************** */
40/*!
41 * \brief Release memory of a pde geometry data structure
42 *
43 * \param geom N_geom_data *
44 * \return void
45 * */
47{
48 if (geom->area != NULL)
49 G_free(geom->area);
50
51 G_free(geom);
52 return;
53}
54
55/* *************************************************************** *
56 * *************************************************************** *
57 * *************************************************************** */
58/*!
59 * \brief Initiate a pde geometry data structure with a 3d region
60 *
61 * If the projection is not planimetric, a double array will be created based on
62 * the number of rows of the provided region
63 *
64 * \param region3d RASTER3D_Region *
65 * \param geodata N_geom_data * - if a NULL pointer is given, a new structure
66 * will be allocatet and returned
67 *
68 * \return N_geom_data *
69 * */
72{
74 struct Cell_head region2d;
75
76#pragma omp critical
77 {
78
79 G_debug(2, "N_init_geom_data_3d: initializing the geometry structure");
80
81 if (geom == NULL)
83
84 geom->dz = region3d->tb_res *
85 G_database_units_to_meters_factor(); /*this function is not
86 thread safe */
87 geom->depths = region3d->depths;
88 geom->dim = 3;
89
90 /*convert the 3d into a 2d region and begin the area calculation */
91 G_get_set_window(&region2d); /*this function is not thread safe */
93 }
94
96}
97
98/* *************************************************************** *
99 * *************************************************************** *
100 * *************************************************************** */
101/*!
102 * \brief Initiate a pde geometry data structure with a 2d region
103 *
104 * If the projection is not planimetric, a double array will be created based on
105 * the number of rows of the provided region storing all computed areas for each
106 * row
107 *
108 * \param region struct Cell_head *
109 * \param geodata N_geom_data * - if a NULL pointer is given, a new structure
110 * will be allocatet and returned
111 *
112 * \return N_geom_data *
113 * */
115{
117 struct Cell_head backup;
118 double meters;
119 short ll = 0;
120 int i;
121
122 /*create an openmp lock to assure that only one thread at a time will access
123 * this function */
124#pragma omp critical
125 {
126 G_debug(2, "N_init_geom_data_2d: initializing the geometry structure");
127
128 /*make a backup from this region */
129 G_get_set_window(&backup); /*this function is not thread safe */
130 /*set the current region */
131 Rast_set_window(region); /*this function is not thread safe */
132
133 if (geom == NULL)
135
136 meters = G_database_units_to_meters_factor(); /*this function is not
137 thread safe */
138
139 /*set the dim to 2d if it was not initiated with 3, that's a bit ugly :(
140 */
141 if (geom->dim != 3)
142 geom->dim = 2;
143
144 geom->planimetric = 1;
145 geom->rows = region->rows;
146 geom->cols = region->cols;
147 geom->dx = region->ew_res * meters;
148 geom->dy = region->ns_res * meters;
149 geom->Az = geom->dy * geom->dx; /*square meters in planimetric proj */
150 /*depths and dz are initialized with a 3d region */
151
152 /*Begin the area calculation */
153 ll = G_begin_cell_area_calculations(); /*this function is not thread
154 safe */
155
156 /*if the projection is not planimetric, calc the area for each row */
157 if (ll == 2) {
158 G_debug(2, "N_init_geom_data_2d: calculating the areas for non "
159 "parametric projection");
160 geom->planimetric = 0;
161
162 if (geom->area != NULL)
163 G_free(geom->area);
164 else
165 geom->area = G_calloc(geom->rows, sizeof(double));
166
167 /*fill the area vector */
168 for (i = 0; i < geom->rows; i++) {
169 geom->area[i] = G_area_of_cell_at_row(i); /*square meters */
170 }
171 }
172
173 /*restore the old region */
174 Rast_set_window(&backup); /*this function is not thread safe */
175 }
176
177 return geom;
178}
179
180/* *************************************************************** *
181 * *************************************************************** *
182 * *************************************************************** */
183/*!
184 * \brief Get the areay size in square meter of one cell (x*y) at row
185 *
186 * This function works for two and three dimensions
187 *
188 * \param geom N_geom_data *
189 * \param row int
190 * \return area double
191 *
192 * */
194{
195 if (geom->planimetric) {
196 G_debug(6, "N_get_geom_data_area_of_cell: %g", geom->Az);
197 return geom->Az;
198 }
199 else {
200 G_debug(6, "N_get_geom_data_area_of_cell: %g", geom->area[row]);
201 return geom->area[row];
202 }
203
204 return 0.0;
205}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_calloc(m, n)
Definition defs/gis.h:137
double G_area_of_cell_at_row(int)
Cell area in specified row.
Definition gis/area.c:86
int G_begin_cell_area_calculations(void)
Begin cell area calculations.
Definition gis/area.c:43
double G_database_units_to_meters_factor(void)
Conversion to meters.
Definition proj3.c:144
void G_get_set_window(struct Cell_head *)
Get the current working window (region)
int G_debug(int, const char *,...) __attribute__((format(printf
void Rast3d_region_to_cell_head(RASTER3D_Region *, struct Cell_head *)
Returns in region2d the 2d portion of region3d.
Definition region.c:45
void Rast_set_window(struct Cell_head *)
Establishes 'window' as the current working window.
N_geom_data * N_alloc_geom_data(void)
Allocate the pde geometry data structure and return a pointer to the new allocated structure.
Definition n_geom.c:26
double N_get_geom_data_area_of_cell(N_geom_data *geom, int row)
Get the areay size in square meter of one cell (x*y) at row.
Definition n_geom.c:193
N_geom_data * N_init_geom_data_3d(RASTER3D_Region *region3d, N_geom_data *geodata)
Initiate a pde geometry data structure with a 3d region.
Definition n_geom.c:70
N_geom_data * N_init_geom_data_2d(struct Cell_head *region, N_geom_data *geodata)
Initiate a pde geometry data structure with a 2d region.
Definition n_geom.c:114
void N_free_geom_data(N_geom_data *geom)
Release memory of a pde geometry data structure.
Definition n_geom.c:46
2D/3D raster map header (used also for region)
Definition gis.h:443
double ew_res
Resolution - east to west cell size for 2D data.
Definition gis.h:479
double ns_res
Resolution - north to south cell size for 2D data.
Definition gis.h:483
int rows
Number of rows for 2D data.
Definition gis.h:458
int cols
Number of columns for 2D data.
Definition gis.h:462
Geometric information about the structured grid.
Definition N_pde.h:98