]> git.street.me.uk Git - andy/viking.git/blame - src/vikcoord.c
[QA] Mark some GPS layer strings for translation.
[andy/viking.git] / src / vikcoord.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <gtk/gtk.h>
23#include "coords.h"
24#include "vikcoord.h"
25
26/* all coord operations MUST BE ABSTRACTED!!! */
27
28void vik_coord_convert(VikCoord *coord, VikCoordMode dest_mode)
29{
06fe50c3 30 VikCoord tmp;
50a14534
EB
31 if ( coord->mode != dest_mode )
32 {
33 if ( dest_mode == VIK_COORD_LATLON ) {
34 a_coords_utm_to_latlon ( (struct UTM *)coord, (struct LatLon *)&tmp );
35 *((struct LatLon *)coord) = *((struct LatLon *)&tmp);
36 } else {
37 a_coords_latlon_to_utm ( (struct LatLon *)coord, (struct UTM *)&tmp );
38 *((struct UTM *)coord) = *((struct UTM *)&tmp);
39 }
40 coord->mode = dest_mode;
41 }
42}
43
44void vik_coord_copy_convert(const VikCoord *coord, VikCoordMode dest_mode, VikCoord *dest)
45{
46 if ( coord->mode == dest_mode ) {
47 *dest = *coord;
48 } else {
49 if ( dest_mode == VIK_COORD_LATLON )
50 a_coords_utm_to_latlon ( (struct UTM *)coord, (struct LatLon *)dest );
51 else
52 a_coords_latlon_to_utm ( (struct LatLon *)coord, (struct UTM *)dest );
53 dest->mode = dest_mode;
54 }
55}
56
57static gdouble vik_coord_diff_safe(const VikCoord *c1, const VikCoord *c2)
58{
06fe50c3 59 struct LatLon a, b;
50a14534
EB
60 vik_coord_to_latlon ( c1, &a );
61 vik_coord_to_latlon ( c2, &b );
62 return a_coords_latlon_diff ( &a, &b );
63}
64
65gdouble vik_coord_diff(const VikCoord *c1, const VikCoord *c2)
66{
67 if ( c1->mode == c2->mode )
68 return vik_coord_diff_safe ( c1, c2 );
69 if ( c1->mode == VIK_COORD_UTM )
70 return a_coords_utm_diff ( (const struct UTM *) c1, (const struct UTM *) c2 );
71 else
72 return a_coords_latlon_diff ( (const struct LatLon *) c1, (const struct LatLon *) c2 );
73}
74
75void vik_coord_load_from_latlon ( VikCoord *coord, VikCoordMode mode, const struct LatLon *ll )
76{
77 if ( mode == VIK_COORD_LATLON )
78 *((struct LatLon *)coord) = *ll;
79 else
80 a_coords_latlon_to_utm ( ll, (struct UTM *) coord );
81 coord->mode = mode;
82}
83
84void vik_coord_load_from_utm ( VikCoord *coord, VikCoordMode mode, const struct UTM *utm )
85{
86 if ( mode == VIK_COORD_UTM )
87 *((struct UTM *)coord) = *utm;
88 else
89 a_coords_utm_to_latlon ( utm, (struct LatLon *) coord );
90 coord->mode = mode;
91}
92
93void vik_coord_to_latlon ( const VikCoord *coord, struct LatLon *dest )
94{
95 if ( coord->mode == VIK_COORD_LATLON )
96 *dest = *((const struct LatLon *)coord);
97 else
98 a_coords_utm_to_latlon ( (const struct UTM *) coord, dest );
99}
100
101void vik_coord_to_utm ( const VikCoord *coord, struct UTM *dest )
102{
103 if ( coord->mode == VIK_COORD_UTM )
104 *dest = *((const struct UTM *)coord);
105 else
106 a_coords_latlon_to_utm ( (const struct LatLon *) coord, dest );
107}
108
109gboolean vik_coord_equals ( const VikCoord *coord1, const VikCoord *coord2 )
110{
111 if ( coord1->mode != coord2->mode )
112 return FALSE;
113 if ( coord1->mode == VIK_COORD_LATLON )
114 return coord1->north_south == coord2->north_south && coord1->east_west == coord2->east_west;
115 else /* VIK_COORD_UTM */
116 return coord1->utm_zone == coord2->utm_zone && coord1->north_south == coord2->north_south && coord1->east_west == coord2->east_west;
117}
7114e879
QT
118
119static void get_north_west(struct LatLon *center, struct LatLon *dist, struct LatLon *nw)
120{
121 nw->lat = center->lat + dist->lat;
122 nw->lon = center->lon - dist->lon;
123 if (nw->lon < -180)
124 nw->lon += 360.0;
125 if (nw->lat > 90.0) { /* over north pole */
126 nw->lat = 180 - nw->lat;
127 nw->lon = nw->lon - 180;
128 }
129}
130
131static void get_south_east(struct LatLon *center, struct LatLon *dist, struct LatLon *se)
132{
133 se->lat = center->lat - dist->lat;
134 se->lon = center->lon + dist->lon;
135 if (se->lon > 180.0)
136 se->lon -= 360.0;
137 if (se->lat < -90.0) { /* over south pole */
138 se->lat += 180;
139 se->lon = se->lon - 180;
140 }
141}
142
143void vik_coord_set_area(const VikCoord *coord, const struct LatLon *wh, VikCoord *tl, VikCoord *br)
144{
145 struct LatLon center, nw, se;
146 struct LatLon dist;
147
148 dist.lat = wh->lat/2;
149 dist.lon = wh->lon/2;
150
151 vik_coord_to_latlon(coord, &center);
152 get_north_west(&center, &dist, &nw);
153 get_south_east(&center, &dist, &se);
154
7114e879
QT
155 *((struct LatLon *)tl) = nw;
156 *((struct LatLon *)br) = se;
35e22ed8 157 tl->mode = br->mode = VIK_COORD_LATLON;
7114e879
QT
158}
159
160gboolean vik_coord_inside(const VikCoord *coord, const VikCoord *tl, const VikCoord *br)
161{
162 struct LatLon ll, tl_ll, br_ll;
163
164 vik_coord_to_latlon(coord, &ll);
165 vik_coord_to_latlon(tl, &tl_ll);
166 vik_coord_to_latlon(br, &br_ll);
167
7114e879
QT
168 if ((ll.lat > tl_ll.lat) || (ll.lon < tl_ll.lon))
169 return FALSE;
170 if ((ll.lat < br_ll.lat) || (ll.lon > br_ll.lon))
171 return FALSE;
172 return TRUE;
173}