GRASS 8 Programmer's Manual 8.6.0dev(2026)-88f4ac5790
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 /* this is for lat-long only */
22 double (*darea0)(double);
23} state;
24
25static struct state *st = &state;
26
27/*!
28 * \brief Begin cell area calculations.
29 *
30 * This routine must be called once before any call to
31 * G_area_of_cell_at_row(). It perform all initializations needed to
32 * do area calculations for grid cells, based on the current window
33 * "projection" field. It can be used in either planimetric
34 * projections or the latitude-longitude projection.
35 *
36 * \return 0 if the projection is not measurable (ie. imagery or xy)
37 * \return 1 if the projection is planimetric (ie. UTM or SP)
38 * \return 2 if the projection is non-planimetric (ie. latitude-longitude)
39 */
41{
42 double a, e2;
43 double factor;
44
45 G_get_set_window(&st->window);
46 if ((st->projection = st->window.proj) == PROJECTION_LL) {
48 if (e2) {
49 G_begin_zone_area_on_ellipsoid(a, e2, st->window.ew_res / 360.0);
50 st->darea0 = G_darea0_on_ellipsoid;
51 }
52 else {
53 G_begin_zone_area_on_sphere(a, st->window.ew_res / 360.0);
54 st->darea0 = G_darea0_on_sphere;
55 }
56
57 return 2;
58 }
59 else {
60 st->square_meters = st->window.ns_res * st->window.ew_res;
62 if (factor > 0.0)
63 st->square_meters *= (factor * factor);
64
65 return (factor > 0.0);
66 }
67}
68
69/*!
70 * \brief Cell area in specified row.
71 *
72 * This routine returns the area in square meters of a cell in the
73 * specified <i>row</i>. This value is constant for planimetric grids
74 * and varies with the row if the projection is latitude-longitude.
75 *
76 * It can be called from multiple threads at the same time once
77 * G_begin_cell_area_calculations() has returned, as long as no thread
78 * calls G_begin_cell_area_calculations(),
79 * G_begin_polygon_area_calculations(), or G_begin_zone_area_on_*().
80 *
81 * \param row row number
82 *
83 * \return cell area
84 */
85double G_area_of_cell_at_row(int row)
86{
87 if (st->projection != PROJECTION_LL)
88 return st->square_meters;
89
90 double north = st->window.north - row * st->window.ns_res;
91 double south = st->window.north - (row + 1) * st->window.ns_res;
92
93 return st->darea0(north) - st->darea0(south);
94}
95
96/*!
97 * \brief Begin polygon area calculations.
98 *
99 * This initializes the polygon area calculation routines. It is used
100 * both for planimetric and latitude-longitude projections.
101 *
102 * \return 0 if the projection is not measurable (ie. imagery or xy)
103 * \return 1 if the projection is planimetric (ie. UTM or SP)
104 * \return 2 if the projection is non-planimetric (ie. latitude-longitude)
105 */
107{
108 double a, e2;
109 double factor;
110
111 if ((st->projection = G_projection()) == PROJECTION_LL) {
114
115 return 2;
116 }
117
119 if (factor > 0.0) {
120 st->units_to_meters_squared = factor * factor;
121
122 return 1;
123 }
124 st->units_to_meters_squared = 1.0;
125
126 return 0;
127}
128
129/*!
130 * \brief Area in square meters of polygon.
131 *
132 * Returns the area in square meters of the polygon described by the
133 * <i>n</i> pairs of <i>x,y</i> coordinate vertices. It is used both for
134 * planimetric and latitude-longitude projections.
135 *
136 * You should call G_begin_polygon_area_calculations() function before
137 * calling this function.
138 *
139 * <b>Note:</b> If the database is planimetric with the non-meter grid,
140 * this routine performs the required unit conversion to produce square
141 * meters.
142 *
143 * \param x array of x coordinates
144 * \param y array of y coordinates
145 * \param n number of x,y coordinate pairs
146 *
147 * \return area in square meters of the polygon
148 */
149double G_area_of_polygon(const double *x, const double *y, int n)
150{
151 double area = 0;
152
153 if (st->projection == PROJECTION_LL) {
154 area = G_ellipsoid_polygon_area(x, y, n);
155 }
156 else {
157 area =
158 G_planimetric_polygon_area(x, y, n) * st->units_to_meters_squared;
159 }
160
161 return area;
162}
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:85
int G_begin_cell_area_calculations(void)
Begin cell area calculations.
Definition gis/area.c:40
double G_area_of_polygon(const double *x, const double *y, int n)
Area in square meters of polygon.
Definition gis/area.c:149
int G_begin_polygon_area_calculations(void)
Begin polygon area calculations.
Definition gis/area.c:106
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:128
2D/3D raster map header (used also for region)
Definition gis.h:445
#define x