2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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/>.
20 * @short_description: Latitude/longitude overlays
22 * Each #GisTile corresponds to a latitude/longitude box on the surface of the
23 * earth. When drawn, the #GisTile renders an images associated with it to the
24 * surface of the earth. This is primarily used to draw ground overlays.
26 * Each GisTile can be split into subtiles in order to draw higher resolution
27 * overlays. Pointers to subtitles are stored in the parent tile and a parent
28 * pointer is stored in each child.
30 * Each #GisTile has a data filed which must be set by the user in order for
31 * the tile to be drawn. When used with GisOpenGL the data must be an integer
32 * representing the OpenGL texture to use when drawing the tile.
38 gchar *gis_tile_path_table[2][2] = {
45 * @parent: the parent for the tile, or NULL
46 * @n: the northern border of the tile
47 * @s: the southern border of the tile
48 * @e: the eastern border of the tile
49 * @w: the western border of the tile
51 * Create a tile associated with a particular latitude/longitude box.
53 * Returns: the new #GisTile
55 GisTile *gis_tile_new(GisTile *parent,
56 gdouble n, gdouble s, gdouble e, gdouble w)
58 GisTile *tile = g_object_new(GIS_TYPE_TILE, NULL);
59 tile->parent = parent;
60 tile->atime = time(NULL);
61 gis_bbox_set_bounds(&tile->coords, 0, 1, 1, 0);
62 gis_bbox_set_bounds(&tile->edge, n, s, e, w);
68 * @child: the tile to generate a path for
70 * Generate a string representation of a tiles location in a group of nested
71 * tiles. The string returned consists of groups of two digits separated by a
72 * delimiter. Each group of digits the tiles location with respect to it's
75 * Returns: the path representing the tiles's location
77 gchar *gis_tile_get_path(GisTile *child)
79 /* This could be easily cached if necessary */
82 for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
83 gis_tile_foreach_index(child, x, y)
84 if (parent->children[x][y] == child)
85 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
86 GString *path = g_string_new("");
87 for (GList *cur = parts; cur; cur = cur->next)
88 g_string_append(path, cur->data);
90 return g_string_free(path, FALSE);
93 static gdouble _gis_tile_get_min_dist(GisPoint *eye, GisBBox *bounds)
96 pos.lat = eye->lat > bounds->n ? bounds->n :
97 eye->lat < bounds->s ? bounds->s : eye->lat;
98 pos.lon = eye->lon > bounds->e ? bounds->e :
99 eye->lon < bounds->w ? bounds->w : eye->lon;
100 //if (eye->lat == pos.lat && eye->lon == pos.lon)
101 // return elev; /* Shortcut? */
103 lle2xyz(eye->lat, eye->lon, eye->elev, a+0, a+1, a+2);
104 lle2xyz(pos.lat, pos.lon, pos.elev, b+0, b+1, b+2);
108 static gboolean _gis_tile_precise(GisPoint *eye, GisBBox *bounds,
109 gdouble max_res, gint width, gint height)
111 gdouble min_dist = _gis_tile_get_min_dist(eye, bounds);
112 gdouble view_res = MPPX(min_dist);
114 gdouble lat_point = bounds->n < 0 ? bounds->n :
115 bounds->s > 0 ? bounds->s : 0;
116 gdouble lon_dist = bounds->e - bounds->w;
117 gdouble tile_res = ll2m(lon_dist, lat_point)/width;
119 /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
121 gdouble scale = eye->elev / min_dist;
123 //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
124 //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
125 // "eye=(%9.1f %9.1f %9.1f) "
126 // "elev=%9.1f / dist=%9.1f = %f",
127 // tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
129 // elev, min_dist, scale);
131 return tile_res < max_res ||
137 * @root: the root tile to split
138 * @eye: the point the tile is viewed from, for calculating distances
139 * @res: a maximum resolution in meters per pixel to split tiles to
140 * @width: width in pixels of the image associated with the tile
141 * @height: height in pixels of the image associated with the tile
142 * @load_func: function used to load the image when a new tile is created
143 * @user_data: user data to past to the load function
145 * Recursively split a tile into children of appropriate detail. The resolution
146 * of the tile in pixels per meter is compared to the resolution which the tile
147 * is being drawn at on the screen. If the screen resolution is insufficient
148 * the tile is recursively subdivided until a sufficient resolution is
151 void gis_tile_update(GisTile *root, GisPoint *eye,
152 gdouble res, gint width, gint height,
153 GisTileLoadFunc load_func, gpointer user_data)
155 root->atime = time(NULL);
156 //g_debug("GisTile: update - %p->atime = %u",
157 // root, (guint)root->atime);
158 const gdouble rows = G_N_ELEMENTS(root->children);
159 const gdouble cols = G_N_ELEMENTS(root->children[0]);
160 const gdouble lat_dist = root->edge.n - root->edge.s;
161 const gdouble lon_dist = root->edge.e - root->edge.w;
162 const gdouble lat_step = lat_dist / rows;
163 const gdouble lon_step = lon_dist / cols;
165 gis_tile_foreach_index(root, row, col) {
166 GisTile **child = &root->children[row][col];
168 edge.n = root->edge.n-(lat_step*(row+0));
169 edge.s = root->edge.n-(lat_step*(row+1));
170 edge.e = root->edge.w+(lon_step*(col+1));
171 edge.w = root->edge.w+(lon_step*(col+0));
172 if (!_gis_tile_precise(eye, &edge, res,
173 width/cols, height/rows)) {
175 *child = gis_tile_new(root, edge.n, edge.s,
177 load_func(*child, user_data);
179 gis_tile_update(*child, eye,
181 load_func, user_data);
188 * @root: the root tile to search from
189 * @lat: target latitude
190 * @lon: target longitude
192 * Locate the subtile with the highest resolution which contains the given
195 * Returns: the child tile
197 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
199 gint rows = G_N_ELEMENTS(root->children);
200 gint cols = G_N_ELEMENTS(root->children[0]);
202 gdouble lat_step = (root->edge.n - root->edge.s) / rows;
203 gdouble lon_step = (root->edge.e - root->edge.w) / cols;
205 gdouble lat_offset = root->edge.n - lat;;
206 gdouble lon_offset = lon - root->edge.w;
208 gint row = lat_offset / lat_step;
209 gint col = lon_offset / lon_step;
211 if (lon == 180) col--;
212 if (lat == -90) row--;
214 //if (lon == 180 || lon == -180)
215 // g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
216 // lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
218 if (row < 0 || row >= rows || col < 0 || col >= cols)
220 else if (root->children[row][col] && root->children[row][col]->data)
221 return gis_tile_find(root->children[row][col], lat, lon);
228 * @root: the root tile to start garbage collection at
229 * @atime: most recent time at which tiles will be kept
230 * @free_func: function used to free the image when a new tile is collected
231 * @user_data: user data to past to the free function
233 * Garbage collect old tiles. This removes and deallocate tiles that have not
234 * been used since before @atime.
236 * Returns: a pointer to the original tile, or NULL if it was garbage collected
238 GisTile *gis_tile_gc(GisTile *root, time_t atime,
239 GisTileFreeFunc free_func, gpointer user_data)
243 gboolean has_children = FALSE;
245 gis_tile_foreach_index(root, x, y) {
246 root->children[x][y] = gis_tile_gc(
247 root->children[x][y], atime,
248 free_func, user_data);
249 if (root->children[x][y])
252 //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
253 // root, (guint)root->atime, (guint)atime);
254 if (!has_children && root->atime < atime && root->data) {
255 free_func(root, user_data);
256 g_object_unref(root);
262 /* Use GObject for this */
265 * @root: the root tile to free
266 * @free_func: function used to free the image when a new tile is collected
267 * @user_data: user data to past to the free function
269 * Recursively free a tile and all it's children.
271 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
276 gis_tile_foreach(root, child)
277 gis_tile_free(child, free_func, user_data);
279 free_func(root, user_data);
280 g_object_unref(root);
284 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
285 static void gis_tile_init(GisTile *tile)
289 static void gis_tile_class_init(GisTileClass *klass)