]> Pileus Git - grits/blob - src/data/grits-tms.c
Add support for Mercator projections in tiles
[grits] / src / data / grits-tms.c
1 /*
2  * Copyright (C) 2009, 2012 Andy Spencer <spenceal@rose-hulman.edu>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include <glib.h>
23
24 #include "grits-tms.h"
25
26 static gchar *_make_uri(GritsTms *tms, GritsTile *tile)
27 {
28         gint zoom = 0;
29         for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
30                 zoom++;
31         gint breath = pow(2,zoom);
32
33         gdouble lat_top = asinh(tan(deg2rad(tile->edge.n)));
34         gdouble lat_bot = asinh(tan(deg2rad(tile->edge.s)));
35
36         gdouble lat_mid = (lat_top + lat_bot)/2.0;
37         gdouble lon_mid = (tile->edge.e + tile->edge.w)/2.0;
38
39         gdouble lat_pos = 1.0 - (lat_mid + G_PI) / (2.0*G_PI);
40         gdouble lon_pos = (lon_mid + 180.0) / 360.0;
41
42         gint xtile = lon_pos * breath;
43         gint ytile = lat_pos * breath;
44
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,
51         //              lat_pos, lon_pos,
52         //              zoom, xtile, ytile);
53
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);
57 }
58
59 gchar *grits_tms_fetch(GritsTms *tms, GritsTile *tile, GritsCacheType mode,
60                 GritsChunkCallback callback, gpointer user_data)
61 {
62         /* Get file path */
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);
68         g_free(uri);
69         g_free(tilep);
70         g_free(local);
71         return path;
72 }
73
74 GritsTms *grits_tms_new(const gchar *uri_prefix,
75                 const gchar *prefix, const gchar *extension)
76 {
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);
81         return tms;
82 }
83
84 void grits_tms_free(GritsTms *tms)
85 {
86         grits_http_free(tms->http);
87         g_free(tms->uri_prefix);
88         g_free(tms->extension);
89         g_free(tms);
90 }