]> Pileus Git - grits/blob - src/data/grits-tms.c
Add Tile Map Service downloading
[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
29 #if 0
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);
34
35         /* Reproject the coordinates to the Mercator projection: */
36         gdouble x = lon_rad;
37         gdouble y = log(tan(lat_rad) + 1.0/cos(lat_rad));
38
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;
42
43         /* Calculate the number of tiles across the map, n, using 2^zoom */
44         gint zoom = 0;
45         for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
46                 zoom++;
47         gint n = pow(2, zoom);
48
49         /* Multiply x and y by n. Round results down to give tilex and tiley. */
50         gint xtile = x * n;
51         gint ytile = y * n;
52
53         g_message("xy=%f,%f  zoom=%d  n=%d  xy_tiles=%d,%d",
54                         x, y, zoom, n, xtile, ytile);
55 #endif
56
57 #if 1
58         /* This is broken */
59         gint zoom = 0;
60         for (GritsTile *tmp = tile->parent; tmp; tmp = tmp->parent)
61                 zoom++;
62         gint breath = pow(2,zoom);
63
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;
66
67         gdouble lon_total = 360;
68         gdouble lat_total = 85.0511*2;
69
70         gdouble lon_pct = lon_pos / lon_total;
71         gdouble lat_pct = lat_pos / lat_total;
72
73         gint xtile = lon_pct * breath;
74         gint ytile = lat_pct * breath;
75
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",
80         //              lon_pos,   lat_pos,
81         //              lon_total, lat_total,
82         //              lon_pct,   lat_pct,
83         //              xtile,     ytile);
84 #endif
85
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);
89 }
90
91 gchar *grits_tms_fetch(GritsTms *tms, GritsTile *tile, GritsCacheType mode,
92                 GritsChunkCallback callback, gpointer user_data)
93 {
94         /* Get file path */
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);
100         g_free(uri);
101         g_free(tilep);
102         g_free(local);
103         return path;
104 }
105
106 GritsTms *grits_tms_new(const gchar *uri_prefix,
107                 const gchar *prefix, const gchar *extension)
108 {
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);
113         return tms;
114 }
115
116 void grits_tms_free(GritsTms *tms)
117 {
118         grits_http_free(tms->http);
119         g_free(tms->uri_prefix);
120         g_free(tms->extension);
121         g_free(tms);
122 }