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->edge, n, s, e, w);
67 * @child: the tile to generate a path for
69 * Generate a string representation of a tiles location in a group of nested
70 * tiles. The string returned consists of groups of two digits separated by a
71 * delimiter. Each group of digits the tiles location with respect to it's
74 * Returns: the path representing the tiles's location
76 gchar *gis_tile_get_path(GisTile *child)
78 /* This could be easily cached if necessary */
81 for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
82 gis_tile_foreach_index(child, x, y)
83 if (parent->children[x][y] == child)
84 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
85 GString *path = g_string_new("");
86 for (GList *cur = parts; cur; cur = cur->next)
87 g_string_append(path, cur->data);
89 return g_string_free(path, FALSE);
92 static gdouble _gis_tile_get_min_dist(GisPoint *eye, GisBBox *bounds)
95 pos.lat = eye->lat > bounds->n ? bounds->n :
96 eye->lat < bounds->s ? bounds->s : eye->lat;
97 pos.lon = eye->lon > bounds->e ? bounds->e :
98 eye->lon < bounds->w ? bounds->w : eye->lon;
99 //if (eye->lat == pos.lat && eye->lon == pos.lon)
100 // return elev; /* Shortcut? */
102 lle2xyz(eye->lat, eye->lon, eye->elev, a+0, a+1, a+2);
103 lle2xyz(pos.lat, pos.lon, pos.elev, b+0, b+1, b+2);
107 static gboolean _gis_tile_precise(GisPoint *eye, GisBBox *bounds,
108 gdouble max_res, gint width, gint height)
110 gdouble min_dist = _gis_tile_get_min_dist(eye, bounds);
111 gdouble view_res = MPPX(min_dist);
113 gdouble lat_point = bounds->n < 0 ? bounds->n :
114 bounds->s > 0 ? bounds->s : 0;
115 gdouble lon_dist = bounds->e - bounds->w;
116 gdouble tile_res = ll2m(lon_dist, lat_point)/width;
118 /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
120 gdouble scale = eye->elev / min_dist;
122 //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
123 //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
124 // "eye=(%9.1f %9.1f %9.1f) "
125 // "elev=%9.1f / dist=%9.1f = %f",
126 // tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
128 // elev, min_dist, scale);
130 return tile_res < max_res ||
136 * @root: the root tile to split
137 * @eye: the point the tile is viewed from, for calculating distances
138 * @res: a maximum resolution in meters per pixel to split tiles to
139 * @width: width in pixels of the image associated with the tile
140 * @height: height in pixels of the image associated with the tile
141 * @load_func: function used to load the image when a new tile is created
142 * @user_data: user data to past to the load function
144 * Recursively split a tile into children of appropriate detail. The resolution
145 * of the tile in pixels per meter is compared to the resolution which the tile
146 * is being drawn at on the screen. If the screen resolution is insufficient
147 * the tile is recursively subdivided until a sufficient resolution is
150 void gis_tile_update(GisTile *root, GisPoint *eye,
151 gdouble res, gint width, gint height,
152 GisTileLoadFunc load_func, gpointer user_data)
154 root->atime = time(NULL);
155 //g_debug("GisTile: update - %p->atime = %u",
156 // root, (guint)root->atime);
157 const gdouble rows = G_N_ELEMENTS(root->children);
158 const gdouble cols = G_N_ELEMENTS(root->children[0]);
159 const gdouble lat_dist = root->edge.n - root->edge.s;
160 const gdouble lon_dist = root->edge.e - root->edge.w;
161 const gdouble lat_step = lat_dist / rows;
162 const gdouble lon_step = lon_dist / cols;
164 gis_tile_foreach_index(root, row, col) {
165 GisTile **child = &root->children[row][col];
167 edge.n = root->edge.n-(lat_step*(row+0));
168 edge.s = root->edge.n-(lat_step*(row+1));
169 edge.e = root->edge.w+(lon_step*(col+1));
170 edge.w = root->edge.w+(lon_step*(col+0));
171 if (!_gis_tile_precise(eye, &edge, res,
172 width/cols, height/rows)) {
174 *child = gis_tile_new(root, edge.n, edge.s,
176 load_func(*child, user_data);
178 gis_tile_update(*child, eye,
180 load_func, user_data);
187 * @root: the root tile to search from
188 * @lat: target latitude
189 * @lon: target longitude
191 * Locate the subtile with the highest resolution which contains the given
194 * Returns: the child tile
196 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
198 gint rows = G_N_ELEMENTS(root->children);
199 gint cols = G_N_ELEMENTS(root->children[0]);
201 gdouble lat_step = (root->edge.n - root->edge.s) / rows;
202 gdouble lon_step = (root->edge.e - root->edge.w) / cols;
204 gdouble lat_offset = root->edge.n - lat;;
205 gdouble lon_offset = lon - root->edge.w;
207 gint row = lat_offset / lat_step;
208 gint col = lon_offset / lon_step;
210 if (lon == 180) col--;
211 if (lat == -90) row--;
213 //if (lon == 180 || lon == -180)
214 // g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
215 // lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
217 if (row < 0 || row >= rows || col < 0 || col >= cols)
219 else if (root->children[row][col] && root->children[row][col]->data)
220 return gis_tile_find(root->children[row][col], lat, lon);
227 * @root: the root tile to start garbage collection at
228 * @atime: most recent time at which tiles will be kept
229 * @free_func: function used to free the image when a new tile is collected
230 * @user_data: user data to past to the free function
232 * Garbage collect old tiles. This removes and deallocate tiles that have not
233 * been used since before @atime.
235 * Returns: a pointer to the original tile, or NULL if it was garbage collected
237 GisTile *gis_tile_gc(GisTile *root, time_t atime,
238 GisTileFreeFunc free_func, gpointer user_data)
242 gboolean has_children = FALSE;
244 gis_tile_foreach_index(root, x, y) {
245 root->children[x][y] = gis_tile_gc(
246 root->children[x][y], atime,
247 free_func, user_data);
248 if (root->children[x][y])
251 //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
252 // root, (guint)root->atime, (guint)atime);
253 if (!has_children && root->atime < atime && root->data) {
254 free_func(root, user_data);
255 g_object_unref(root);
261 /* Use GObject for this */
264 * @root: the root tile to free
265 * @free_func: function used to free the image when a new tile is collected
266 * @user_data: user data to past to the free function
268 * Recursively free a tile and all it's children.
270 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
275 gis_tile_foreach(root, child)
276 gis_tile_free(child, free_func, user_data);
278 free_func(root, user_data);
279 g_object_unref(root);
283 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
284 static void gis_tile_init(GisTile *tile)
288 static void gis_tile_class_init(GisTileClass *klass)