2 * Copyright (C) 2009, 2012 Andy Spencer <spenceal@rose-hulman.edu>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "grits-tms.h"
26 static gchar *_make_uri(GritsTms *tms, GritsTile *tile)
29 for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
31 gint breath = pow(2,zoom);
33 gdouble lat_top = asinh(tan(deg2rad(tile->edge.n)));
34 gdouble lat_bot = asinh(tan(deg2rad(tile->edge.s)));
36 gdouble lat_mid = (lat_top + lat_bot)/2.0;
37 gdouble lon_mid = (tile->edge.e + tile->edge.w)/2.0;
39 gdouble lat_pos = 1.0 - (lat_mid + G_PI) / (2.0*G_PI);
40 gdouble lon_pos = (lon_mid + 180.0) / 360.0;
42 gint xtile = lon_pos * breath;
43 gint ytile = lat_pos * breath;
45 //g_message("tile=%f,%f,%f,%f t=%p p=%p",
46 // tile->edge.n, tile->edge.s,
47 // tile->edge.e, tile->edge.w, tile, tile->parent);
48 //g_message("top=%lf->%lf bot=%lf->%lf pos=%lf,%lf tile=%d,%d,%d",
49 // tile->edge.n, lat_top,
50 // tile->edge.s, lat_bot,
52 // zoom, xtile, ytile);
54 // http://tile.openstreetmap.org/<zoom>/<xtile>/<ytile>.png
55 return g_strdup_printf("%s/%d/%d/%d.%s",
56 tms->uri_prefix, zoom, xtile, ytile, tms->extension);
59 gchar *grits_tms_fetch(GritsTms *tms, GritsTile *tile, GritsCacheType mode,
60 GritsChunkCallback callback, gpointer user_data)
63 gchar *uri = _make_uri(tms, tile);
64 gchar *tilep = grits_tile_get_path(tile);
65 gchar *local = g_strdup_printf("%s%s", tilep, tms->extension);
66 gchar *path = grits_http_fetch(tms->http, uri, local,
67 mode, callback, user_data);
74 GritsTms *grits_tms_new(const gchar *uri_prefix,
75 const gchar *prefix, const gchar *extension)
77 GritsTms *tms = g_new0(GritsTms, 1);
78 tms->http = grits_http_new(prefix);
79 tms->uri_prefix = g_strdup(uri_prefix);
80 tms->extension = g_strdup(extension);
84 void grits_tms_free(GritsTms *tms)
86 grits_http_free(tms->http);
87 g_free(tms->uri_prefix);
88 g_free(tms->extension);