]> Pileus Git - grits/blob - src/gis-util.c
Merging GisWorld and GisView into GisViewer
[grits] / src / gis-util.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 /******************
25  * Global helpers *
26  ******************/
27 void lle2xyz(gdouble lat, gdouble lon, gdouble elev,
28                 gdouble *x, gdouble *y, gdouble *z)
29 {
30         gdouble rad  = elev2rad(elev);
31         gdouble azim = lon2azim(lon);
32         gdouble incl = lat2incl(lat);
33         *z = rad * cos(azim) * sin(incl);
34         *x = rad * sin(azim) * sin(incl);
35         *y = rad * cos(incl);
36 }
37
38 void xyz2lle(gdouble x, gdouble y, gdouble z,
39                 gdouble *lat, gdouble *lon, gdouble *elev)
40 {
41         gdouble rad = sqrt(x*x + y*y + z*z);
42         *lat  = incl2lat(acos(y / rad));
43         *lon  = azim2lon(atan2(x,z));
44         *elev = rad2elev(rad);
45 }
46
47 void xyz2ll(gdouble x, gdouble y, gdouble z,
48                 gdouble *lat, gdouble *lon)
49 {
50         gdouble rad = sqrt(x*x + y*y + z*z);
51         *lat = incl2lat(acos(y / rad));
52         *lon = azim2lon(atan2(x,z));
53 }
54
55 gdouble ll2m(gdouble lon_dist, gdouble lat)
56 {
57         gdouble azim = (-lat+90)/180*M_PI;
58         gdouble rad  = sin(azim) * EARTH_R;
59         gdouble circ = 2 * M_PI * rad;
60         return lon_dist/360 * circ;
61 }
62
63 gdouble distd(gdouble *a, gdouble *b)
64 {
65         return sqrt((a[0]-b[0])*(a[0]-b[0]) +
66                     (a[1]-b[1])*(a[1]-b[1]) +
67                     (a[2]-b[2])*(a[2]-b[2]));
68 }