]> Pileus Git - grits/blob - src/gis-util.c
Update copyright and email address
[grits] / src / gis-util.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <glib.h>
19 #include <math.h>
20
21 #include "gis-util.h"
22
23 /******************
24  * Global helpers *
25  ******************/
26 void lle2xyz(gdouble lat, gdouble lon, gdouble elev,
27                 gdouble *x, gdouble *y, gdouble *z)
28 {
29         gdouble rad  = elev2rad(elev);
30         gdouble azim = lon2azim(lon);
31         gdouble incl = lat2incl(lat);
32         *z = rad * cos(azim) * sin(incl);
33         *x = rad * sin(azim) * sin(incl);
34         *y = rad * cos(incl);
35 }
36
37 void xyz2lle(gdouble x, gdouble y, gdouble z,
38                 gdouble *lat, gdouble *lon, gdouble *elev)
39 {
40         gdouble rad = sqrt(x*x + y*y + z*z);
41         *lat  = incl2lat(acos(y / rad));
42         *lon  = azim2lon(atan2(x,z));
43         *elev = rad2elev(rad);
44 }
45
46 void xyz2ll(gdouble x, gdouble y, gdouble z,
47                 gdouble *lat, gdouble *lon)
48 {
49         gdouble rad = sqrt(x*x + y*y + z*z);
50         *lat = incl2lat(acos(y / rad));
51         *lon = azim2lon(atan2(x,z));
52 }
53
54 gdouble ll2m(gdouble lon_dist, gdouble lat)
55 {
56         gdouble azim = (-lat+90)/180*M_PI;
57         gdouble rad  = sin(azim) * EARTH_R;
58         gdouble circ = 2 * M_PI * rad;
59         return lon_dist/360 * circ;
60 }
61
62 gdouble distd(gdouble *a, gdouble *b)
63 {
64         return sqrt((a[0]-b[0])*(a[0]-b[0]) +
65                     (a[1]-b[1])*(a[1]-b[1]) +
66                     (a[2]-b[2])*(a[2]-b[2]));
67 }
68
69 gdouble lon_avg(gdouble a, gdouble b)
70 {
71         gdouble diff = ABS(a-b);
72         gdouble avg  = (a+b)/2;
73         if (diff > 180) {
74                 if (avg >= 0)
75                         avg -= 180;
76                 else
77                         avg += 180;
78         }
79         return avg;
80 }