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)
30 /* This doesn't make any sense.. */
31 gdouble lon_rad = deg2rad(tile->edge.n + tile->edge.s)/2;
32 gdouble lat_rad = deg2rad(tile->edge.e + tile->edge.w)/2;
33 g_message("%lf,%lf", lat_rad, lon_rad);
35 /* Reproject the coordinates to the Mercator projection: */
37 gdouble y = log(tan(lat_rad) + 1.0/cos(lat_rad));
39 /* Transform range of x and y to 0 - 1 and shift origin to top left */
40 x = (1.0 + (x / G_PI)) / 2.0;
41 y = (1.0 - (y / G_PI)) / 2.0;
43 /* Calculate the number of tiles across the map, n, using 2^zoom */
45 for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
47 gint n = pow(2, zoom);
49 /* Multiply x and y by n. Round results down to give tilex and tiley. */
53 g_message("xy=%f,%f zoom=%d n=%d xy_tiles=%d,%d",
54 x, y, zoom, n, xtile, ytile);
60 for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
62 gint breath = pow(2,zoom);
64 gdouble lon_pos = (tile->edge.e+tile->edge.w)/2 + 180;
65 gdouble lat_pos = -(tile->edge.n+tile->edge.s)/2 + 90 - 4.9489;
67 gdouble lon_total = 360;
68 gdouble lat_total = 85.0511*2;
70 gdouble lon_pct = lon_pos / lon_total;
71 gdouble lat_pct = lat_pos / lat_total;
73 gint xtile = lon_pct * breath;
74 gint ytile = lat_pct * breath;
76 //g_message("bbok=%f,%f,%f,%f",
77 // tile->edge.n, tile->edge.s,
78 // tile->edge.e, tile->edge.w);
79 //g_message("pos=%f,%f total=%f,%f pct=%f,%f tile=%d,%d",
81 // lon_total, lat_total,
86 // http://tile.openstreetmap.org/<zoom>/<xtile>/<ytile>.png
87 return g_strdup_printf("%s/%d/%d/%d.%s",
88 tms->uri_prefix, zoom, xtile, ytile, tms->extension);
91 gchar *grits_tms_fetch(GritsTms *tms, GritsTile *tile, GritsCacheType mode,
92 GritsChunkCallback callback, gpointer user_data)
95 gchar *uri = _make_uri(tms, tile);
96 gchar *tilep = grits_tile_get_path(tile);
97 gchar *local = g_strdup_printf("%s%s", tilep, tms->extension);
98 gchar *path = grits_http_fetch(tms->http, uri, local,
99 mode, callback, user_data);
106 GritsTms *grits_tms_new(const gchar *uri_prefix,
107 const gchar *prefix, const gchar *extension)
109 GritsTms *tms = g_new0(GritsTms, 1);
110 tms->http = grits_http_new(prefix);
111 tms->uri_prefix = g_strdup(uri_prefix);
112 tms->extension = g_strdup(extension);
116 void grits_tms_free(GritsTms *tms)
118 grits_http_free(tms->http);
119 g_free(tms->uri_prefix);
120 g_free(tms->extension);