Contents of /trunk/src/converter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (hide annotations)
Sat Sep 19 19:19:42 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 2207 byte(s)
Map handles overwritten coordinates
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 harbaum 120 int zoom_p = (1<<zoom) * TILESIZE/2;
69 harbaum 32
70     /* the formula is
71     *
72     * pixel_x = (2^zoom * TILESIZE * lon) / 2PI + (2^zoom * TILESIZE) / 2
73     */
74 harbaum 120 pixel_x = (int)(lon * (zoom_p / M_PI)) + zoom_p;
75    
76 harbaum 32 return pixel_x;
77     }
78    
79     float
80 harbaum 120 pixel2lon( int zoom,
81 harbaum 32 int pixel_x)
82     {
83     float lon;
84 harbaum 120 int zoom_p = (1<<zoom) * TILESIZE/2;
85 harbaum 32
86 harbaum 120 lon = (pixel_x - zoom_p) / (zoom_p / M_PI) ;
87 harbaum 32
88     return lon;
89     }
90    
91     float
92 harbaum 120 pixel2lat( int zoom,
93 harbaum 32 int pixel_y)
94     {
95     float lat, lat_m;
96    
97 harbaum 120 lat_m = (-( pixel_y - ( (1<<zoom) * (TILESIZE/2) ) ) * (2*M_PI)) /
98     (TILESIZE * (1<<zoom));
99 harbaum 32
100     lat = asin(tanh(lat_m));
101    
102     return lat;
103     }