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.
39 gchar *gis_tile_path_table[2][2] = {
46 * @parent: the parent for the tile, or NULL
47 * @n: the northern border of the tile
48 * @s: the southern border of the tile
49 * @e: the eastern border of the tile
50 * @w: the western border of the tile
52 * Create a tile associated with a particular latitude/longitude box.
54 * Returns: the new #GisTile
56 GisTile *gis_tile_new(GisTile *parent,
57 gdouble n, gdouble s, gdouble e, gdouble w)
59 GisTile *tile = g_object_new(GIS_TYPE_TILE, NULL);
60 tile->parent = parent;
65 tile->atime = time(NULL);
71 * @child: the tile to generate a path for
73 * Generate a string representation of a tiles location in a group of nested
74 * tiles. The string returned consists of groups of two digits separated by a
75 * delimiter. Each group of digits the tiles location with respect to it's
78 * Returns: the path representing the tiles's location
80 gchar *gis_tile_get_path(GisTile *child)
82 /* This could be easily cached if necessary */
85 for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
86 gis_tile_foreach_index(child, x, y)
87 if (parent->children[x][y] == child)
88 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
89 GString *path = g_string_new("");
90 for (GList *cur = parts; cur; cur = cur->next)
91 g_string_append(path, cur->data);
93 return g_string_free(path, FALSE);
96 static gdouble _gis_tile_get_min_dist(
97 gdouble lat, gdouble lon, gdouble elev,
98 gdouble n, gdouble s, gdouble e, gdouble w)
100 gdouble tlat = lat > n ? n :
102 gdouble tlon = lon > e ? e :
104 gdouble telev = 0; // TODO: elevation at rlat,rlon
105 //if (lat == tlat && lon == tlon)
106 // return elev; /* Shortcut? */
108 lle2xyz( lat, lon, elev, a+0, a+1, a+2);
109 lle2xyz(tlat, tlon, telev, b+0, b+1, b+2);
113 static gboolean _gis_tile_precise(
114 gdouble lat, gdouble lon, gdouble elev,
115 gdouble n, gdouble s, gdouble e, gdouble w,
116 gdouble max_res, gint width, gint height)
118 gdouble min_dist = _gis_tile_get_min_dist(lat, lon, elev, n, s, e, w);
119 gdouble view_res = MPPX(min_dist);
121 gdouble lat_point = n < 0 ? n : s > 0 ? s : 0;
122 gdouble lon_dist = e - w;
123 gdouble tile_res = ll2m(lon_dist, lat_point)/width;
125 /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
127 gdouble scale = elev / min_dist;
129 //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
130 //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
131 // "eye=(%9.1f %9.1f %9.1f) "
132 // "elev=%9.1f / dist=%9.1f = %f",
133 // tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
135 // elev, min_dist, scale);
137 return tile_res < max_res ||
143 * @root: the root tile to split
144 * @res: a maximum resolution in meters per pixel to split tiles to
145 * @width: width in pixels of the image associated with the tile
146 * @height: height in pixels of the image associated with the tile
147 * @lat: latitude of the eye point
148 * @lon: longitude of the eye point
149 * @elev: elevation of the eye point
150 * @load_func: function used to load the image when a new tile is created
151 * @user_data: user data to past to the load function
153 * Recursively split a tile into children of appropriate detail. The resolution
154 * of the tile in pixels per meter is compared to the resolution which the tile
155 * is being drawn at on the screen. If the screen resolution is insufficient
156 * the tile is recursively subdivided until a sufficient resolution is
159 void gis_tile_update(GisTile *root,
160 gdouble res, gint width, gint height,
161 gdouble lat, gdouble lon, gdouble elev,
162 GisTileLoadFunc load_func, gpointer user_data)
164 root->atime = time(NULL);
165 //g_debug("GisTile: update - %p->atime = %u",
166 // root, (guint)root->atime);
167 const gdouble rows = G_N_ELEMENTS(root->children);
168 const gdouble cols = G_N_ELEMENTS(root->children[0]);
169 const gdouble lat_dist = root->edge.n - root->edge.s;
170 const gdouble lon_dist = root->edge.e - root->edge.w;
171 const gdouble lat_step = lat_dist / rows;
172 const gdouble lon_step = lon_dist / cols;
174 gis_tile_foreach_index(root, row, col) {
175 GisTile **child = &root->children[row][col];
176 const gdouble n = root->edge.n-(lat_step*(row+0));
177 const gdouble s = root->edge.n-(lat_step*(row+1));
178 const gdouble e = root->edge.w+(lon_step*(col+1));
179 const gdouble w = root->edge.w+(lon_step*(col+0));
180 if (!_gis_tile_precise(lat,lon,elev, n,s,e,w,
181 res,width/cols,height/rows)) {
183 *child = gis_tile_new(root, n,s,e,w);
184 load_func(*child, user_data);
186 gis_tile_update(*child,
189 load_func, user_data);
196 * @root: the root tile to search from
197 * @lat: target latitude
198 * @lon: target longitude
200 * Locate the subtile with the highest resolution which contains the given
203 * Returns: the child tile
205 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
207 gint rows = G_N_ELEMENTS(root->children);
208 gint cols = G_N_ELEMENTS(root->children[0]);
210 gdouble lat_step = (root->edge.n - root->edge.s) / rows;
211 gdouble lon_step = (root->edge.e - root->edge.w) / cols;
213 gdouble lat_offset = root->edge.n - lat;;
214 gdouble lon_offset = lon - root->edge.w;
216 gint row = lat_offset / lat_step;
217 gint col = lon_offset / lon_step;
219 if (lon == 180) col--;
220 if (lat == -90) row--;
222 //if (lon == 180 || lon == -180)
223 // g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
224 // lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
226 if (row < 0 || row >= rows || col < 0 || col >= cols)
228 else if (root->children[row][col] && root->children[row][col]->data)
229 return gis_tile_find(root->children[row][col], lat, lon);
236 * @root: the root tile to start garbage collection at
237 * @atime: most recent time at which tiles will be kept
238 * @free_func: function used to free the image when a new tile is collected
239 * @user_data: user data to past to the free function
241 * Garbage collect old tiles. This removes and deallocate tiles that have not
242 * been used since before @atime.
244 * Returns: a pointer to the original tile, or NULL if it was garbage collected
246 GisTile *gis_tile_gc(GisTile *root, time_t atime,
247 GisTileFreeFunc free_func, gpointer user_data)
251 gboolean has_children = FALSE;
253 gis_tile_foreach_index(root, x, y) {
254 root->children[x][y] = gis_tile_gc(
255 root->children[x][y], atime,
256 free_func, user_data);
257 if (root->children[x][y])
260 //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
261 // root, (guint)root->atime, (guint)atime);
262 if (!has_children && root->atime < atime && root->data) {
263 free_func(root, user_data);
264 g_object_unref(root);
270 /* Use GObject for this */
273 * @root: the root tile to free
274 * @free_func: function used to free the image when a new tile is collected
275 * @user_data: user data to past to the free function
277 * Recursively free a tile and all it's children.
279 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
284 gis_tile_foreach(root, child)
285 gis_tile_free(child, free_func, user_data);
287 free_func(root, user_data);
288 g_object_unref(root);
292 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
293 static void gis_tile_init(GisTile *tile)
297 static void gis_tile_class_init(GisTileClass *klass)