GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
pole_in_poly.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/pole_in_poly.c
3
4 \brief GIS Library - Pole in polygon
5
6 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author CERL
10 */
11
12#include <grass/gis.h>
13
14static void mystats(double, double, double, double, double *, double *);
15
16/*!
17 * \brief Check if pole is in polygon
18 *
19 * For latitude-longitude coordinates, this routine determines if the polygon
20 * defined by the <i>n</i> coordinate vertices <i>x,y</i> contains one of the
21 * poles.
22 *
23 * <b>Note:</b> Use this routine only if the projection is PROJECTION_LL.
24 *
25 * \param x array of x coordinates
26 * \param y array of y coordinates
27 * \param n number of coordinates
28 *
29 * \return -1 if it contains the south pole
30 * \return 1 if it contains the north pole
31 * \return 0 if it contains neither pole.
32 */
33int G_pole_in_polygon(const double *x, const double *y, int n)
34{
35 int i;
36 double len, area, total_len, total_area;
37
38 if (n <= 1)
39 return 0;
40
41 mystats(x[n - 1], y[n - 1], x[0], y[0], &total_len, &total_area);
42 for (i = 1; i < n; i++) {
43 mystats(x[i - 1], y[i - 1], x[i], y[i], &len, &area);
44 total_len += len;
45 total_area += area;
46 }
47
48 /* if polygon contains a pole then the x-coordinate length of
49 * the perimeter should compute to 0, otherwise it should be about 360
50 * (or -360, depending on the direction of perimeter traversal)
51 *
52 * instead of checking for exactly 0, check from -1 to 1 to avoid
53 * roundoff error.
54 */
56 return 0;
57
58 return total_area >= 0.0 ? 1 : -1;
59}
60
61static void mystats(double x0, double y0, double x1, double y1, double *len,
62 double *area)
63{
64 if (x1 > x0)
65 while (x1 - x0 > 180)
66 x0 += 360;
67 else if (x0 > x1)
68 while (x0 - x1 > 180)
69 x0 -= 360;
70
71 *len = x0 - x1;
72
73 if (x0 > x1)
74 *area = (x0 - x1) * (y0 + y1) / 2.0;
75 else
76 *area = (x1 - x0) * (y1 + y0) / 2.0;
77}
int G_pole_in_polygon(const double *x, const double *y, int n)
Check if pole is in polygon.
#define x