| 1 |
#include <math.h> |
| 2 |
#include <grass/gis.h> |
| 3 |
#include "pi.h" |
| 4 |
|
| 5 |
/* distance from point to point along a geodesic |
| 6 |
* code from |
| 7 |
* Paul D. Thomas |
| 8 |
* "Spheroidal Geodesics, Reference Systems, and Local Geometry" |
| 9 |
* U.S. Naval Oceanographic Office, p. 162 |
| 10 |
* Engineering Library 526.3 T36s |
| 11 |
*/ |
| 12 |
|
| 13 |
/* a corruption of geodist.c library module of GRASS, used to enhance |
| 14 |
execution efficiency of interpolation routine */ |
| 15 |
|
| 16 |
static double boa; |
| 17 |
static double f; |
| 18 |
static double ff64; |
| 19 |
static double al; |
| 20 |
|
| 21 |
static double t1r, t2r; |
| 22 |
|
| 23 |
#define DIST_PARAMS struct dist_params |
| 24 |
DIST_PARAMS |
| 25 |
{ |
| 26 |
int targetrow; /* interpolation row for which params apply */ |
| 27 |
double t1, t2, t3, t4; |
| 28 |
}; |
| 29 |
|
| 30 |
static DIST_PARAMS *lat_params, *nextcalc; |
| 31 |
|
| 32 |
/* must be called once to establish the ellipsoid */ |
| 33 |
int G_begin_geodesic_distance_l(int nrows, double a, double e2) |
| 34 |
{ |
| 35 |
int i; |
| 36 |
|
| 37 |
al = a; |
| 38 |
boa = sqrt(1 - e2); |
| 39 |
f = 1 - boa; |
| 40 |
ff64 = f * f / 64; |
| 41 |
|
| 42 |
/* initialize lat_params array and indicate no prior data storage */ |
| 43 |
lat_params = (DIST_PARAMS *)G_calloc(nrows, sizeof(DIST_PARAMS)); |
| 44 |
for (i = 0, nextcalc = lat_params; i < nrows; i++, nextcalc++) |
| 45 |
nextcalc->targetrow = -1; |
| 46 |
|
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
double LL_set_geodesic_distance_lat(double lat) |
| 51 |
{ |
| 52 |
return (atan(boa * tan(Radians(lat)))); |
| 53 |
} |
| 54 |
|
| 55 |
double set_sdlmr(double lon_diff) |
| 56 |
{ |
| 57 |
return (sin(Radians(lon_diff) / 2)); |
| 58 |
} |
| 59 |
|
| 60 |
/* must be called first */ |
| 61 |
int LL_set_geodesic_distance( |
| 62 |
double *rowlook, /* preprocessed latitude data by row */ |
| 63 |
int unk, int data /* row (y) of interpolation target, data value */ |
| 64 |
) |
| 65 |
{ |
| 66 |
double stm, ctm, sdtm, cdtm; |
| 67 |
double tm, dtm; |
| 68 |
double temp; |
| 69 |
|
| 70 |
t1r = *(rowlook + unk); |
| 71 |
t2r = *(rowlook + data); |
| 72 |
|
| 73 |
tm = (t1r + t2r) / 2; |
| 74 |
dtm = (t2r - t1r) / 2; |
| 75 |
|
| 76 |
stm = sin(tm); |
| 77 |
ctm = cos(tm); |
| 78 |
sdtm = sin(dtm); |
| 79 |
cdtm = cos(dtm); |
| 80 |
|
| 81 |
nextcalc = lat_params + data; |
| 82 |
if (nextcalc->targetrow != unk) { /* reset latitude offset parameters */ |
| 83 |
temp = stm * cdtm; |
| 84 |
nextcalc->t1 = temp * temp * 2; |
| 85 |
|
| 86 |
temp = sdtm * ctm; |
| 87 |
nextcalc->t2 = temp * temp * 2; |
| 88 |
|
| 89 |
nextcalc->t3 = sdtm * sdtm; |
| 90 |
nextcalc->t4 = cdtm * cdtm - stm * stm; |
| 91 |
|
| 92 |
nextcalc->targetrow = unk; /* parameterization tagged to row */ |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
double LL_geodesic_distance(double sdlmr) |
| 99 |
{ |
| 100 |
double a, cd, d, e, q, sd, t, u, v, x, y; |
| 101 |
|
| 102 |
/* special case - shapiro */ |
| 103 |
if (sdlmr == 0.0 && t1r == t2r) |
| 104 |
return 0.0; |
| 105 |
|
| 106 |
q = nextcalc->t3 + sdlmr * sdlmr * nextcalc->t4; |
| 107 |
/* special case - shapiro */ |
| 108 |
if (q == 1.0) |
| 109 |
return PI * al; |
| 110 |
|
| 111 |
cd = 1 - 2 * q; /* ill-conditioned subtraction for small q */ |
| 112 |
/* mod starts here */ |
| 113 |
sd = 2 * sqrt(q - q * q); /* sd^2 = 1 - cd^2 */ |
| 114 |
if (q != 0.0 && cd == 1.0) /* test for small q */ |
| 115 |
t = 1.0; |
| 116 |
else if (sd == 0.0) |
| 117 |
t = 1.0; |
| 118 |
else |
| 119 |
t = acos(cd) / sd; /* don't know how to fix acos(1-2*q) yet */ |
| 120 |
/* mod ends here */ |
| 121 |
|
| 122 |
u = nextcalc->t1 / (1 - q); |
| 123 |
v = nextcalc->t2 / q; |
| 124 |
d = 4 * t * t; |
| 125 |
x = u + v; |
| 126 |
e = -2 * cd; |
| 127 |
y = u - v; |
| 128 |
a = -d * e; |
| 129 |
|
| 130 |
return (al * sd * |
| 131 |
(t - f / 4 * (t * x - y) + |
| 132 |
ff64 * (x * (a + (t - (a + e) / 2) * x) + y * (-2 * d + e * y) + |
| 133 |
d * x * y))); |
| 134 |
} |
| 135 |
|
| 136 |
int free_dist_params(void) |
| 137 |
{ |
| 138 |
G_free(lat_params); |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |