GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
gis/area.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/area.c
3 *
4 * \brief GIS Library - Area calculation functions.
5 *
6 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <grass/gis.h>
13
14static struct state {
15 struct Cell_head window;
16 double square_meters;
17 int projection;
18
19 double units_to_meters_squared;
20
21 /* these next are for lat-long only */
22 int next_row;
23 double north_value;
24 double north;
25 double (*darea0)(double);
26} state;
27
28static struct state *st = &state;
29
30/*!
31 * \brief Begin cell area calculations.
32 *
33 * This routine must be called once before any call to
34 * G_area_of_cell_at_row(). It perform all initializations needed to
35 * do area calculations for grid cells, based on the current window
36 * "projection" field. It can be used in either planimetric
37 * projections or the latitude-longitude projection.
38 *
39 * \return 0 if the projection is not measurable (ie. imagery or xy)
40 * \return 1 if the projection is planimetric (ie. UTM or SP)
41 * \return 2 if the projection is non-planimetric (ie. latitude-longitude)
42 */
44{
45 double a, e2;
46 double factor;
47
48 G_get_set_window(&st->window);
49 if ((st->projection = st->window.proj) == PROJECTION_LL) {
51 if (e2) {
52 G_begin_zone_area_on_ellipsoid(a, e2, st->window.ew_res / 360.0);
53 st->darea0 = G_darea0_on_ellipsoid;
54 }
55 else {
56 G_begin_zone_area_on_sphere(a, st->window.ew_res / 360.0);
57 st->darea0 = G_darea0_on_sphere;
58 }
59 st->next_row = 0;
60 st->north = st->window.north;
61 st->north_value = st->darea0(st->north);
62
63 return 2;
64 }
65 else {
66 st->square_meters = st->window.ns_res * st->window.ew_res;
68 if (factor > 0.0)
69 st->square_meters *= (factor * factor);
70
71 return (factor > 0.0);
72 }
73}
74
75/*!
76 * \brief Cell area in specified row.
77 *
78 * This routine returns the area in square meters of a cell in the
79 * specified <i>row</i>. This value is constant for planimetric grids
80 * and varies with the row if the projection is latitude-longitude.
81 *
82 * \param row row number
83 *
84 * \return cell area
85 */
86double G_area_of_cell_at_row(int row)
87{
88 double south_value;
89 double cell_area;
90
91 if (st->projection != PROJECTION_LL)
92 return st->square_meters;
93
94 if (row != st->next_row) {
95 st->north = st->window.north - row * st->window.ns_res;
96 st->north_value = st->darea0(st->north);
97 }
98
99 st->north -= st->window.ns_res;
100 south_value = st->darea0(st->north);
101 cell_area = st->north_value - south_value;
102
103 st->next_row = row + 1;
104 st->north_value = south_value;
105
106 return cell_area;
107}
108
109/*!
110 * \brief Begin polygon area calculations.
111 *
112 * This initializes the polygon area calculation routines. It is used
113 * both for planimetric and latitude-longitude projections.
114 *
115 * \return 0 if the projection is not measurable (ie. imagery or xy)
116 * \return 1 if the projection is planimetric (ie. UTM or SP)
117 * \return 2 if the projection is non-planimetric (ie. latitude-longitude)
118 */
120{
121 double a, e2;
122 double factor;
123
124 if ((st->projection = G_projection()) == PROJECTION_LL) {
127
128 return 2;
129 }
130
132 if (factor > 0.0) {
133 st->units_to_meters_squared = factor * factor;
134
135 return 1;
136 }
137 st->units_to_meters_squared = 1.0;
138
139 return 0;
140}
141
142/*!
143 * \brief Area in square meters of polygon.
144 *
145 * Returns the area in square meters of the polygon described by the
146 * <i>n</i> pairs of <i>x,y</i> coordinate vertices. It is used both for
147 * planimetric and latitude-longitude projections.
148 *
149 * You should call G_begin_polygon_area_calculations() function before
150 * calling this function.
151 *
152 * <b>Note:</b> If the database is planimetric with the non-meter grid,
153 * this routine performs the required unit conversion to produce square
154 * meters.
155 *
156 * \param x array of x coordinates
157 * \param y array of y coordinates
158 * \param n number of x,y coordinate pairs
159 *
160 * \return area in square meters of the polygon
161 */
162double G_area_of_polygon(const double *x, const double *y, int n)
163{
164 double area = 0;
165
166 if (st->projection == PROJECTION_LL) {
167 area = G_ellipsoid_polygon_area(x, y, n);
168 }
169 else {
170 area =
171 G_planimetric_polygon_area(x, y, n) * st->units_to_meters_squared;
172 }
173
174 return area;
175}
void G_begin_ellipsoid_polygon_area(double, double)
Begin area calculations.
Definition area_poly1.c:73
double G_darea0_on_sphere(double)
Calculates integral for area between two latitudes.
Definition area_sphere.c:45
int G_get_ellipsoid_parameters(double *, double *)
get ellipsoid parameters
Definition get_ellipse.c:64
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)
double G_planimetric_polygon_area(const double *, const double *, int)
Calculates planimetric polygon area.
Definition area_poly2.c:23
void G_begin_zone_area_on_sphere(double, double)
Initialize calculations for sphere.
Definition area_sphere.c:33
double G_darea0_on_ellipsoid(double)
Calculate integral for area between two latitudes.
void G_begin_zone_area_on_ellipsoid(double, double, double)
Begin area calculations for ellipsoid.
double G_ellipsoid_polygon_area(const double *, const double *, int)
Area of lat-long polygon.
Definition area_poly1.c:152
int G_projection(void)
Query cartographic projection.
Definition proj1.c:30
double G_area_of_cell_at_row(int row)
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_area_of_polygon(const double *x, const double *y, int n)
Area in square meters of polygon.
Definition gis/area.c:162
int G_begin_polygon_area_calculations(void)
Begin polygon area calculations.
Definition gis/area.c:119
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:126
2D/3D raster map header (used also for region)
Definition gis.h:443
#define x