Document previous patch
[viking.git] / src / osm.c
blob18c10a5de5382934d3f010af1732a4624060380e
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <gtk/gtk.h>
26 #include <glib/gi18n.h>
27 #ifdef HAVE_MATH_H
28 #include <math.h>
29 #endif
30 #include "viking.h"
31 #include "coords.h"
32 #include "vikcoord.h"
33 #include "mapcoord.h"
34 #include "vikmapslayer.h"
35 #include "vikwebtoolcenter.h"
36 #include "vikexttools.h"
38 #include "osm.h"
40 static guint8 osm_zoom ( gdouble mpp );
42 static gboolean osm_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
43 static void osm_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
44 static int osm_maplint_download ( MapCoord *src, const gchar *dest_fn );
45 static int osm_mapnik_download ( MapCoord *src, const gchar *dest_fn );
46 static int osm_osmarender_download ( MapCoord *src, const gchar *dest_fn );
47 static int osm_cycle_download ( MapCoord *src, const gchar *dest_fn );
48 #ifdef VIK_CONFIG_BLUEMARBLE
49 static int bluemarble_download ( MapCoord *src, const gchar *dest_fn );
50 #endif
51 #ifdef VIK_CONFIG_OPENAERIAL
52 static int openaerialmap_download ( MapCoord *src, const gchar *dest_fn );
53 #endif
55 static DownloadOptions osm_options = { NULL, 0, a_check_map_file };
57 /* initialisation */
58 void osm_init () {
59 VikMapsLayer_MapType osmarender_type = { 12, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_osmarender_download };
60 VikMapsLayer_MapType mapnik_type = { 13, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_mapnik_download }; VikMapsLayer_MapType maplint_type = { 14, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_maplint_download };
61 VikMapsLayer_MapType cycle_type = { 17, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_cycle_download };
63 #ifdef VIK_CONFIG_BLUEMARBLE
64 VikMapsLayer_MapType bluemarble_type = { 15, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, bluemarble_download };
65 #endif
67 #ifdef VIK_CONFIG_OPENAERIAL
68 VikMapsLayer_MapType openaerialmap_type = { 20, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, openaerialmap_download };
69 #endif
71 maps_layer_register_type("OpenStreetMap (Osmarender)", 12, &osmarender_type);
72 maps_layer_register_type("OpenStreetMap (Mapnik)", 13, &mapnik_type);
73 maps_layer_register_type("OpenStreetMap (Maplint)", 14, &maplint_type);
74 maps_layer_register_type("OpenStreetMap (Cycle)", 17, &cycle_type);
76 #ifdef VIK_CONFIG_BLUEMARBLE
77 maps_layer_register_type("BlueMarble", 15, &bluemarble_type);
78 #endif
79 #ifdef VIK_CONFIG_OPENAERIAL
80 maps_layer_register_type("OpenAerialMap", 20, &openaerialmap_type);
81 #endif
83 // Webtools
84 VikWebtoolCenter *webtool = NULL;
85 webtool = vik_webtool_center_new_with_members ( _("OSM (view)"), "http://openstreetmap.org/?lat=%s&lon=%s&zoom=%d&layers=B000FTF" );
86 vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) );
87 g_object_unref ( webtool );
89 webtool = vik_webtool_center_new_with_members ( _("OSM (edit)"), "http://www.openstreetmap.org/edit?lat=%s&lon=%s&zoom=%d" );
90 vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) );
91 g_object_unref ( webtool );
93 webtool = vik_webtool_center_new_with_members ( _("OSM (render)"), "http://www.informationfreeway.org/?lat=%s&lon=%s&zoom=%d&layers=B0000F000F" );
94 vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) );
95 g_object_unref ( webtool );
98 /* 1 << (x) is like a 2**(x) */
99 #define GZ(x) (1<<(x))
101 static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
102 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
104 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
106 #define ERROR_MARGIN 0.01
107 guint8 osm_zoom ( gdouble mpp ) {
108 gint i;
109 for ( i = 0; i < num_scales; i++ ) {
110 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
111 return i;
113 return 255;
116 static gboolean osm_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
118 g_assert ( src->mode == VIK_COORD_LATLON );
120 if ( xzoom != yzoom )
121 return FALSE;
123 dest->scale = osm_zoom ( xzoom );
124 if ( dest->scale == 255 )
125 return FALSE;
127 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
128 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
129 dest->z = 0;
130 return TRUE;
133 static void osm_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
135 gdouble socalled_mpp = GZ(src->scale);
136 dest->mode = VIK_COORD_LATLON;
137 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
138 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
141 /* Maplint tiles
142 * Ex: http://dev.openstreetmap.org/~ojw/Tiles/maplint.php/10/517/375.png
144 static int osm_maplint_download ( MapCoord *src, const gchar *dest_fn )
146 int res = -1;
147 gchar *uri = g_strdup_printf ( "/Tiles/maplint.php/%d/%d/%d.png", 17-src->scale, src->x, src->y );
148 res = a_http_download_get_url ( "tah.openstreetmap.org", uri, dest_fn, &osm_options );
149 g_free ( uri );
150 return res;
153 static int osm_mapnik_download ( MapCoord *src, const gchar *dest_fn )
155 int res = -1;
156 gchar *uri = g_strdup_printf ( "/%d/%d/%d.png", 17-src->scale, src->x, src->y );
157 res = a_http_download_get_url ( "tile.openstreetmap.org", uri, dest_fn, &osm_options );
158 g_free ( uri );
159 return res;
162 static int osm_osmarender_download ( MapCoord *src, const gchar *dest_fn )
164 int res = -1;
165 gchar *uri = g_strdup_printf ( "/Tiles/tile/%d/%d/%d.png", 17-src->scale, src->x, src->y );
166 res = a_http_download_get_url ( "tah.openstreetmap.org", uri, dest_fn, &osm_options );
167 g_free ( uri );
168 return res;
171 #ifdef VIK_CONFIG_BLUEMARBLE
172 static int bluemarble_download ( MapCoord *src, const gchar *dest_fn )
174 int res = -1;
175 gchar *uri = g_strdup_printf ( "/com.modestmaps.bluemarble/%d-r%d-c%d.jpg", 17-src->scale, src->y, src->x );
176 res = a_http_download_get_url ( "s3.amazonaws.com", uri, dest_fn, &osm_options );
178 g_free ( uri );
179 return res;
182 #endif
184 #ifdef VIK_CONFIG_OPENAERIAL
185 static int openaerialmap_download ( MapCoord *src, const gchar *dest_fn )
187 int res = -1;
188 gchar *uri = g_strdup_printf ( "/tiles/1.0.0/openaerialmap-900913/%d/%d/%d.jpg", 17-src->scale, src->x, src->y );
189 res = a_http_download_get_url ( "tile.openaerialmap.org", uri, dest_fn, &osm_options );
191 g_free ( uri );
192 return res;
194 #endif
196 static int osm_cycle_download ( MapCoord *src, const gchar *dest_fn )
198 int res = -1;
199 gchar *uri = g_strdup_printf ( "/tiles/cycle/%d/%d/%d.png", 17-src->scale, src->x, src->y );
200 res = a_http_download_get_url ( "andy.sandbox.cloudmade.com", uri, dest_fn, &osm_options );
201 g_free ( uri );
202 return res;