Parent Directory | Revision Log
osm-gps-map integration
1 | harbaum | 32 | /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* vim:set et sw=4 ts=4 cino=t0,(0: */ | ||
3 | /* | ||
4 | * converter.c | ||
5 | * Copyright (C) Marcus Bauer 2008 <marcus.bauer@gmail.com> | ||
6 | * Copyright (C) John Stowers 2008 <john.stowers@gmail.com> | ||
7 | * | ||
8 | * osm-gps-map.c is free software: you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the | ||
10 | * Free Software Foundation, either version 3 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * osm-gps-map.c is distributed in the hope that it will be useful, but | ||
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
16 | * See the GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along | ||
19 | * with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | ||
21 | |||
22 | #include <math.h> | ||
23 | #include <stdio.h> | ||
24 | |||
25 | #include "osm-gps-map-types.h" | ||
26 | #include "converter.h" | ||
27 | |||
28 | |||
29 | float | ||
30 | deg2rad(float deg) | ||
31 | { | ||
32 | return (deg * M_PI / 180.0); | ||
33 | } | ||
34 | |||
35 | float | ||
36 | rad2deg(float rad) | ||
37 | { | ||
38 | return (rad / M_PI * 180.0); | ||
39 | } | ||
40 | |||
41 | |||
42 | int | ||
43 | lat2pixel( int zoom, | ||
44 | float lat) | ||
45 | { | ||
46 | float lat_m; | ||
47 | int pixel_y; | ||
48 | |||
49 | lat_m = atanh(sin(lat)); | ||
50 | |||
51 | /* the formula is | ||
52 | * | ||
53 | * pixel_y = -(2^zoom * TILESIZE * lat_m) / 2PI + (2^zoom * TILESIZE) / 2 | ||
54 | */ | ||
55 | pixel_y = -(int)( (lat_m * TILESIZE * (1 << zoom) ) / (2*M_PI)) + | ||
56 | ((1 << zoom) * (TILESIZE/2) ); | ||
57 | |||
58 | |||
59 | return pixel_y; | ||
60 | } | ||
61 | |||
62 | |||
63 | int | ||
64 | lon2pixel( int zoom, | ||
65 | float lon) | ||
66 | { | ||
67 | int pixel_x; | ||
68 | |||
69 | /* the formula is | ||
70 | * | ||
71 | * pixel_x = (2^zoom * TILESIZE * lon) / 2PI + (2^zoom * TILESIZE) / 2 | ||
72 | */ | ||
73 | pixel_x = (int)(( lon * TILESIZE * (1 << zoom) ) / (2*M_PI)) + | ||
74 | ( (1 << zoom) * (TILESIZE/2) ); | ||
75 | return pixel_x; | ||
76 | } | ||
77 | |||
78 | float | ||
79 | pixel2lon( float zoom, | ||
80 | int pixel_x) | ||
81 | { | ||
82 | float lon; | ||
83 | |||
84 | lon = ((pixel_x - ( exp(zoom * M_LN2) * (TILESIZE/2) ) ) *2*M_PI) / | ||
85 | (TILESIZE * exp(zoom * M_LN2) ); | ||
86 | |||
87 | return lon; | ||
88 | } | ||
89 | |||
90 | float | ||
91 | pixel2lat( float zoom, | ||
92 | int pixel_y) | ||
93 | { | ||
94 | float lat, lat_m; | ||
95 | |||
96 | lat_m = (-( pixel_y - ( exp(zoom * M_LN2) * (TILESIZE/2) ) ) * (2*M_PI)) / | ||
97 | (TILESIZE * exp(zoom * M_LN2)); | ||
98 | |||
99 | lat = asin(tanh(lat_m)); | ||
100 | |||
101 | return lat; | ||
102 | } |