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(GisTile *tile,
97 gdouble lat, gdouble lon, gdouble elev)
99 gdouble tlat = lat > tile->edge.n ? tile->edge.n :
100 lat < tile->edge.s ? tile->edge.s : lat;
101 gdouble tlon = lon > tile->edge.e ? tile->edge.e :
102 lon < tile->edge.w ? tile->edge.w : lon;
103 gdouble telev = 0; // TODO: elevation at rlat,rlon
104 //if (lat == tlat && lon == tlon)
105 // return elev; /* Shortcut? */
107 lle2xyz( lat, lon, elev, a+0, a+1, a+2);
108 lle2xyz(tlat, tlon, telev, b+0, b+1, b+2);
112 static gboolean _gis_tile_needs_split(GisTile *tile,
113 gdouble max_res, gint width, gint height,
114 gdouble lat, gdouble lon, gdouble elev)
116 gdouble lat_point = tile->edge.n < 0 ? tile->edge.n :
117 tile->edge.s > 0 ? tile->edge.s : 0;
118 gdouble min_dist = _gis_tile_get_min_dist(tile, lat, lon, elev);
119 gdouble view_res = MPPX(min_dist);
120 gdouble lon_dist = tile->edge.e - tile->edge.w;
121 gdouble tile_res = ll2m(lon_dist, lat_point)/width;
123 /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
125 gdouble scale = elev / min_dist;
127 //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
128 // "eye=(%9.1f %9.1f %9.1f) "
129 // "elev=%9.1f / dist=%9.1f = %f",
130 // tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
132 // elev, min_dist, scale);
134 if (tile_res < max_res)
136 return view_res < tile_res;
141 * @root: the root tile to split
142 * @res: a maximum resolution in meters per pixel to split tiles to
143 * @width: width in pixels of the image associated with the tile
144 * @height: height in pixels of the image associated with the tile
145 * @lat: latitude of the eye point
146 * @lon: longitude of the eye point
147 * @elev: elevation of the eye point
148 * @load_func: function used to load the image when a new tile is created
149 * @user_data: user data to past to the load function
151 * Recursively split a tile into children of appropriate detail. The resolution
152 * of the tile in pixels per meter is compared to the resolution which the tile
153 * is being drawn at on the screen. If the screen resolution is insufficient
154 * the tile is recursively subdivided until a sufficient resolution is
157 void gis_tile_update(GisTile *root,
158 gdouble res, gint width, gint height,
159 gdouble lat, gdouble lon, gdouble elev,
160 GisTileLoadFunc load_func, gpointer user_data)
162 root->atime = time(NULL);
163 //g_debug("GisTile: update - %p->atime = %u", root, (guint)root->atime);
164 gdouble lat_dist = root->edge.n - root->edge.s;
165 gdouble lon_dist = root->edge.e - root->edge.w;
166 if (_gis_tile_needs_split(root, res, width, height, lat, lon, elev)) {
167 gdouble lat_step = lat_dist / G_N_ELEMENTS(root->children);
168 gdouble lon_step = lon_dist / G_N_ELEMENTS(root->children[0]);
170 gis_tile_foreach_index(root, x, y) {
171 if (!root->children[x][y]) {
172 root->children[x][y] = gis_tile_new(root,
173 root->edge.n-(lat_step*(x+0)),
174 root->edge.n-(lat_step*(x+1)),
175 root->edge.w+(lon_step*(y+1)),
176 root->edge.w+(lon_step*(y+0)));
177 load_func(root->children[x][y], user_data);
179 gis_tile_update(root->children[x][y],
182 load_func, user_data);
189 * @root: the root tile to search from
190 * @lat: target latitude
191 * @lon: target longitude
193 * Locate the subtile with the highest resolution which contains the given
196 * Returns: the child tile
198 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
200 gint rows = G_N_ELEMENTS(root->children);
201 gint cols = G_N_ELEMENTS(root->children[0]);
203 gdouble lat_step = (root->edge.n - root->edge.s) / rows;
204 gdouble lon_step = (root->edge.e - root->edge.w) / cols;
206 gdouble lat_offset = root->edge.n - lat;;
207 gdouble lon_offset = lon - root->edge.w;
209 gint row = lat_offset / lat_step;
210 gint col = lon_offset / lon_step;
212 if (lon == 180) col--;
213 if (lat == -90) row--;
215 //if (lon == 180 || lon == -180)
216 // g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
217 // lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
219 if (row < 0 || row >= rows || col < 0 || col >= cols)
221 else if (root->children[row][col] && root->children[row][col]->data)
222 return gis_tile_find(root->children[row][col], lat, lon);
229 * @root: the root tile to start garbage collection at
230 * @atime: most recent time at which tiles will be kept
231 * @free_func: function used to free the image when a new tile is collected
232 * @user_data: user data to past to the free function
234 * Garbage collect old tiles. This removes and deallocate tiles that have not
235 * been used since before @atime.
237 * Returns: a pointer to the original tile, or NULL if it was garbage collected
239 GisTile *gis_tile_gc(GisTile *root, time_t atime,
240 GisTileFreeFunc free_func, gpointer user_data)
244 gboolean has_children = FALSE;
246 gis_tile_foreach_index(root, x, y) {
247 root->children[x][y] = gis_tile_gc(
248 root->children[x][y], atime,
249 free_func, user_data);
250 if (root->children[x][y])
253 //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
254 // root, (guint)root->atime, (guint)atime);
255 if (!has_children && root->atime < atime && root->data) {
256 free_func(root, user_data);
257 g_object_unref(root);
263 /* Use GObject for this */
266 * @root: the root tile to free
267 * @free_func: function used to free the image when a new tile is collected
268 * @user_data: user data to past to the free function
270 * Recursively free a tile and all it's children.
272 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
277 gis_tile_foreach(root, child)
278 gis_tile_free(child, free_func, user_data);
280 free_func(root, user_data);
281 g_object_unref(root);
285 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
286 static void gis_tile_init(GisTile *tile)
290 static void gis_tile_class_init(GisTileClass *klass)