]> git.street.me.uk Git - andy/viking.git/blob - src/maputils.c
[QA] Move file related function into file.c and rename for general reuse
[andy/viking.git] / src / maputils.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4  *
5  * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #include "maputils.h"
23
24 // World Scale: VIK_GZ(17)
25 //  down to
26 // Submeter scale: 1/VIK_GZ(5)
27 // No map provider is going to have tiles at the highest zoom in level - but we can interpolate to that.
28
29 static const gdouble scale_mpps[] = { VIK_GZ(0), VIK_GZ(1), VIK_GZ(2), VIK_GZ(3), VIK_GZ(4), VIK_GZ(5),
30                                       VIK_GZ(6), VIK_GZ(7), VIK_GZ(8), VIK_GZ(9), VIK_GZ(10), VIK_GZ(11),
31                                       VIK_GZ(12), VIK_GZ(13), VIK_GZ(14), VIK_GZ(15), VIK_GZ(16), VIK_GZ(17) };
32 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
33
34 static const gdouble scale_neg_mpps[] = { 1.0/VIK_GZ(0), 1.0/VIK_GZ(1), 1.0/VIK_GZ(2),
35                                           1.0/VIK_GZ(3), 1.0/VIK_GZ(4), 1.0/VIK_GZ(5) };
36 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
37
38 #define ERROR_MARGIN 0.01
39 /**
40  * map_utils_mpp_to_scale:
41  * @mpp: The so called 'mpp'
42  *
43  * Returns: the zoom scale value which may be negative.
44  */
45 gint map_utils_mpp_to_scale ( gdouble mpp ) {
46         gint i;
47         for ( i = 0; i < num_scales; i++ ) {
48                 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
49                         return i;
50                 }
51         }
52         for ( i = 0; i < num_scales_neg; i++ ) {
53                 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
54                         return -i;
55                 }
56         }
57
58         return 255;
59 }
60
61 /**
62  * map_utils_mpp_to_zoom_level:
63  * @mpp: The so called 'mpp'
64  *
65  * Returns: a Zoom Level
66  *  See: http://wiki.openstreetmap.org/wiki/Zoom_levels
67  */
68 guint8 map_utils_mpp_to_zoom_level ( gdouble mpp )
69 {
70         gint answer = 17 - map_utils_mpp_to_scale ( mpp );
71         if ( answer < 0 )
72                 answer = 17;
73         return answer;
74 }