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