From 67209ca83325c1986fd194cd71cb4aca1e5e6a81 Mon Sep 17 00:00:00 2001 From: Guilhem Bonnefille Date: Mon, 23 Apr 2007 20:25:05 +0000 Subject: [PATCH] New module for OpenStreetMap sources. Extract slippy maps code from google.c and create a new module. This one inserts two map sources: Osmarender and Mapnik. --- ChangeLog | 4 ++ src/Makefile.am | 4 ++ src/google.c | 9 ----- src/google.h | 1 - src/modules.c | 2 + src/osm.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++ src/osm.h | 39 +++++++++++++++++++ src/vikmapslayer.c | 5 --- 8 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 src/osm.c create mode 100644 src/osm.h diff --git a/ChangeLog b/ChangeLog index 28d70522..d30cc69c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2007-04-23 +Guilhem Bonnefille + * OpenSourceMap slippy maps (in osm.c) -- Osmarender and Mapnik + 2007-04-21 Evan Battaglia * OpenSourceMap slippy maps (in google.c) -- identical to google maps except zoom numbering diff --git a/src/Makefile.am b/src/Makefile.am index 46a26fb4..fc652c6e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -83,6 +83,10 @@ viking_SOURCES += \ expedia.c expedia.h endif +viking_SOURCES += \ + osm.c osm.h + + INCLUDES = @GTK_CFLAGS@ @EXPAT_CFLAGS@ @LIBCURL_CPPFLAGS@ LDADD = @GTK_LIBS@ @EXPAT_LIBS@ @LIBCURL@ AM_CFLAGS = -Wall -g diff --git a/src/google.c b/src/google.c index d2f82848..4bc5ce5e 100644 --- a/src/google.c +++ b/src/google.c @@ -38,12 +38,10 @@ void google_init () { VikMapsLayer_MapType google_1 = { 7, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_download }; VikMapsLayer_MapType google_2 = { 10, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_trans_download }; VikMapsLayer_MapType google_3 = { 11, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_kh_download }; - VikMapsLayer_MapType slippy = { 12, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_slippy_download }; maps_layer_register_type("Google Maps", 7, &google_1); maps_layer_register_type("Transparent Google Maps", 10, &google_2); maps_layer_register_type("Google Satellite Images", 11, &google_3); - maps_layer_register_type("OpenStreetMap Slippy Maps", 12, &slippy); } /* 1 << (x) is like a 2**(x) */ @@ -156,10 +154,3 @@ void google_kh_download ( MapCoord *src, const gchar *dest_fn ) a_http_download_get_url ( "kh.google.com", uri, dest_fn ); g_free ( uri ); } - -void google_slippy_download ( MapCoord *src, const gchar *dest_fn ) -{ - gchar *uri = g_strdup_printf ( "/%d/%d/%d.png", 17-src->scale, src->x, src->y ); - a_http_download_get_url ( "tile.openstreetmap.org", uri, dest_fn ); - g_free ( uri ); -} diff --git a/src/google.h b/src/google.h index 7997865e..adde5388 100644 --- a/src/google.h +++ b/src/google.h @@ -41,6 +41,5 @@ void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); void google_download ( MapCoord *src, const gchar *dest_fn ); void google_trans_download ( MapCoord *src, const gchar *dest_fn ); void google_kh_download ( MapCoord *src, const gchar *dest_fn ); -void google_slippy_download ( MapCoord *src, const gchar *dest_fn ); #endif diff --git a/src/modules.c b/src/modules.c index 2b56aeff..da528b08 100644 --- a/src/modules.c +++ b/src/modules.c @@ -30,6 +30,7 @@ #include "terraserver.h" #include "khmaps.h" #include "expedia.h" +#include "osm.h" void modules_init() { @@ -44,5 +45,6 @@ void modules_init() #ifdef VIK_CONFIG_TERRASERVER terraserver_init(); #endif + osm_init(); } diff --git a/src/osm.c b/src/osm.c new file mode 100644 index 00000000..0c5eed74 --- /dev/null +++ b/src/osm.c @@ -0,0 +1,96 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2003-2005, Evan Battaglia + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include "viking.h" +#include "coords.h" +#include "vikcoord.h" +#include "mapcoord.h" +#include "http.h" +#include "vikmapslayer.h" + +#include "osm.h" + +/* initialisation */ +void osm_init () { + VikMapsLayer_MapType osmarender_type = { 12, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_osmarender_download }; + VikMapsLayer_MapType mapnik_type = { 13, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, osm_coord_to_mapcoord, osm_mapcoord_to_center_coord, osm_mapnik_download }; + maps_layer_register_type("OpenStreetMap (Osmarender)", 12, &osmarender_type); + maps_layer_register_type("OpenStreetMap (Mapnik)", 13, &mapnik_type); +} + +/* 1 << (x) is like a 2**(x) */ +#define GZ(x) (1<<(x)) + +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), + GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; + +static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); + +#define ERROR_MARGIN 0.01 +guint8 osm_zoom ( gdouble mpp ) { + gint i; + for ( i = 0; i < num_scales; i++ ) { + if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) + return i; + } + return 255; +} + +gboolean osm_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) +{ + g_assert ( src->mode == VIK_COORD_LATLON ); + + if ( xzoom != yzoom ) + return FALSE; + + dest->scale = osm_zoom ( xzoom ); + if ( dest->scale == 255 ) + return FALSE; + + dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom; + dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom; + dest->z = 0; + return TRUE; +} + +void osm_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ) +{ + gdouble socalled_mpp = GZ(src->scale); + dest->mode = VIK_COORD_LATLON; + dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180; + dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360)); +} + +void osm_mapnik_download ( MapCoord *src, const gchar *dest_fn ) +{ + gchar *uri = g_strdup_printf ( "/osamrender/%d/%d/%d.png", 17-src->scale, src->x, src->y ); + a_http_download_get_url ( "tile.openstreetmap.org", uri, dest_fn ); + g_free ( uri ); +} + +void osm_osmarender_download ( MapCoord *src, const gchar *dest_fn ) +{ + gchar *uri = g_strdup_printf ( "/~ojw/Tiles/tile.php/%d/%d/%d.png", 17-src->scale, src->x, src->y ); + a_http_download_get_url ( "dev.openstreetmap.org", uri, dest_fn ); + g_free ( uri ); +} diff --git a/src/osm.h b/src/osm.h new file mode 100644 index 00000000..8e16bb47 --- /dev/null +++ b/src/osm.h @@ -0,0 +1,39 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2003-2005, Evan Battaglia + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef __VIKING_OSMARENDER_H +#define __VIKING_OSMARENDER_H + +#include + +#include "vikcoord.h" +#include "mapcoord.h" + +void osm_init (); + +guint8 osm_zoom ( gdouble mpp ); + +gboolean osm_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ); +void osm_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); +void osm_mapnik_download ( MapCoord *src, const gchar *dest_fn ); +void osm_osmarender_download ( MapCoord *src, const gchar *dest_fn ); + +#endif diff --git a/src/vikmapslayer.c b/src/vikmapslayer.c index 5b0eb940..861af8ed 100644 --- a/src/vikmapslayer.c +++ b/src/vikmapslayer.c @@ -55,11 +55,6 @@ #include "mapcoord.h" #include "terraserver.h" -#include "googlemaps.h" -#include "google.h" -#include "khmaps.h" -#include "expedia.h" - /****** MAP TYPES ******/ -- 2.39.5