GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
rhumbline.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/rhumbline.c
3 *
4 * \brief GIS Library - Rhumbline calculation routines.
5 *
6 * From "Map Projections" by Peter Richardus and Ron K. Alder, 1972<br>
7 * (526.8 R39m in Map & Geography Library)<br>
8 * Page 20,21, formulas 2.21, 2.22
9 *
10 * Formula is the equation of a rhumbline from (lat1,lon1) to
11 * (lat2,lon2). Input is lon, output is lat (all in degrees).
12 *
13 * <b>Note:</b> Formula only works if 0 < abs(lon2-lon1) < 180.
14 * If lon1 == lon2 then rhumbline is the merdian lon1 (and the formula
15 * will fail).
16 * <br>
17 * <b>WARNING:</b> This code is preliminary. It may not even be correct.
18 *
19 * SPDX-FileCopyrightText: 2001-2014 GRASS Development Team
20 * SPDX-License-Identifier: GPL-2.0-or-later
21 *
22 * \author GRASS Development Team
23 *
24 * \date 1999-2014
25 */
26
27#include <math.h>
28#include <grass/gis.h>
29#include "pi.h"
30
31static void adjust_lat(double *);
32
33#if 0
34static void adjust_lon(double *);
35#endif /* unused */
36
37static struct state {
38 double TAN_A, TAN1, TAN2, L;
39 int parallel;
40} state;
41
42static struct state *st = &state;
43
44/**
45 * \brief Start rhumbline calculations.
46 *
47 * <b>Note:</b> This function must be called before other rhumbline
48 * functions to initialize parameters.
49 *
50 * \param[in] lon1,lat1 longitude, latitude of first point
51 * \param[in] lon2,lat2 longitude, latitude of second point
52 * \return 1 on success
53 * \return 0 on error
54 */
55int G_begin_rhumbline_equation(double lon1, double lat1, double lon2,
56 double lat2)
57{
58 adjust_lat(&lat1);
59 adjust_lat(&lat2);
60
61 if (lon1 == lon2) {
62 st->parallel = 1; /* a lie */
63 st->L = lat1;
64 return 0;
65 }
66 if (lat1 == lat2) {
67 st->parallel = 1;
68 st->L = lat1;
69 return 1;
70 }
71 st->parallel = 0;
72 lon1 = Radians(lon1);
73 lon2 = Radians(lon2);
74 lat1 = Radians(lat1);
75 lat2 = Radians(lat2);
76
77 st->TAN1 = tan(M_PI_4 + lat1 / 2.0);
78 st->TAN2 = tan(M_PI_4 + lat2 / 2.0);
79 st->TAN_A = (lon2 - lon1) / (log(st->TAN2) - log(st->TAN1));
80 st->L = lon1;
81
82 return 1;
83}
84
85/**
86 * \brief Calculates rhumbline latitude.
87 *
88 * <b>Note:</b> Function only works if lon1 < lon < lon2.
89 *
90 * \param[in] lon longitude
91 * \return double latitude in degrees
92 */
94{
95 if (st->parallel)
96 return st->L;
97
98 lon = Radians(lon);
99
100 return Degrees(2 * atan(exp((lon - st->L) / st->TAN_A) * st->TAN1) -
101 M_PI_2);
102}
103
104#if 0
105static void adjust_lon(double *lon)
106{
107 while (*lon > 180.0)
108 *lon -= 360.0;
109 while (*lon < -180.0)
110 *lon += 360.0;
111}
112#endif /* unused */
113
114static void adjust_lat(double *lat)
115{
116 if (*lat > 90.0)
117 *lat = 90.0;
118 if (*lat < -90.0)
119 *lat = -90.0;
120}
#define M_PI_2
Definition gis.h:157
#define M_PI_4
Definition gis.h:165
struct state state
Definition parser.c:101
struct state * st
Definition parser.c:102
#define Degrees(x)
Definition pi.h:7
#define Radians(x)
Definition pi.h:6
int G_begin_rhumbline_equation(double lon1, double lat1, double lon2, double lat2)
Start rhumbline calculations.
Definition rhumbline.c:55
double G_rhumbline_lat_from_lon(double lon)
Calculates rhumbline latitude.
Definition rhumbline.c:93