2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "background.h"
32 GHashTable *loaded_dems = NULL;
35 static void loaded_dem_free ( LoadedDEM *ldem )
37 vik_dem_free ( ldem->dem );
44 g_hash_table_destroy ( loaded_dems );
47 /* To load a dem. if it was already loaded, will simply
48 * reference the one already loaded and return it.
50 VikDEM *a_dems_load(const gchar *filename)
54 /* dems init hash table */
56 loaded_dems = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) loaded_dem_free );
58 ldem = (LoadedDEM *) g_hash_table_lookup ( loaded_dems, filename );
63 VikDEM *dem = vik_dem_new_from_file ( filename );
66 ldem = g_malloc ( sizeof(LoadedDEM) );
69 g_hash_table_insert ( loaded_dems, g_strdup(filename), ldem );
74 void a_dems_unref(const gchar *filename)
76 LoadedDEM *ldem = (LoadedDEM *) g_hash_table_lookup ( loaded_dems, filename );
78 /* This is fine - probably means the loaded list was aborted / not completed for some reason */
82 if ( ldem->ref_count == 0 )
83 g_hash_table_remove ( loaded_dems, filename );
86 /* to get a DEM that was already loaded.
87 * assumes that its in there already,
88 * although it could not be if earlier load failed.
90 VikDEM *a_dems_get(const gchar *filename)
92 LoadedDEM *ldem = g_hash_table_lookup ( loaded_dems, filename );
99 /* Load a string list (GList of strings) of dems. You have to use get to at them later.
100 * When updating a list as a parameter, this should be bfore freeing the list so
101 * the same DEMs won't be loaded & unloaded.
102 * Modifies the list to remove DEMs which did not load.
105 /* TODO: don't delete them when they don't exist.
106 * we need to warn the user, but we should keep them in the GList.
107 * we need to know that they weren't referenced though when we
108 * do the a_dems_list_free().
110 int a_dems_load_list ( GList **dems, gpointer threaddata )
114 const guint dem_total = g_list_length ( *dems );
116 if ( ! a_dems_load((const gchar *) (iter->data)) ) {
117 GList *iter_temp = iter->next;
118 g_free ( iter->data );
119 (*dems) = g_list_remove_link ( (*dems), iter );
124 /* When running a thread - inform of progress */
127 /* NB Progress also detects abort request via the returned value */
128 int result = a_background_thread_progress ( threaddata, ((gdouble)dem_count) / dem_total );
130 return -1; /* Abort thread */
136 /* Takes a string list (GList of strings) of dems (filenames).
137 * Unrefs all the dems (i.e. "unloads" them), then frees the
138 * strings, the frees the list.
140 void a_dems_list_free ( GList *dems )
144 a_dems_unref ((const gchar *)iter->data);
145 g_free ( iter->data );
148 g_list_free ( dems );
151 GList *a_dems_list_copy ( GList *dems )
153 GList *rv = g_list_copy ( dems );
156 if ( ! a_dems_load((const gchar *) (iter->data)) ) {
157 GList *iter_temp = iter->next; /* delete link, don't bother strdup'ing and free'ing string */
158 rv = g_list_remove_link ( rv, iter );
161 iter->data = g_strdup((gchar *)iter->data); /* copy the string too. */
168 gint16 a_dems_list_get_elev_by_coord ( GList *dems, const VikCoord *coord )
170 static struct UTM utm_tmp;
171 static struct LatLon ll_tmp;
177 dem = a_dems_get ( (gchar *) iter->data );
179 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
180 vik_coord_to_latlon ( coord, &ll_tmp );
183 elev = vik_dem_get_east_north(dem, ll_tmp.lon, ll_tmp.lat);
184 if ( elev != VIK_DEM_INVALID_ELEVATION )
186 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
187 vik_coord_to_utm ( coord, &utm_tmp );
188 if ( utm_tmp.zone == dem->utm_zone &&
189 (elev = vik_dem_get_east_north(dem, utm_tmp.easting, utm_tmp.northing)) != VIK_DEM_INVALID_ELEVATION )
195 return VIK_DEM_INVALID_ELEVATION;
199 const VikCoord *coord;
200 VikDemInterpol method;
204 static gboolean get_elev_by_coord(gpointer key, LoadedDEM *ldem, CoordElev *ce)
206 VikDEM *dem = ldem->dem;
209 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
210 struct LatLon ll_tmp;
211 vik_coord_to_latlon (ce->coord, &ll_tmp );
212 lat = ll_tmp.lat * 3600;
213 lon = ll_tmp.lon * 3600;
214 } else if (dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS) {
215 static struct UTM utm_tmp;
216 if (utm_tmp.zone != dem->utm_zone)
218 vik_coord_to_utm (ce->coord, &utm_tmp);
219 lat = utm_tmp.northing;
220 lon = utm_tmp.easting;
224 switch (ce->method) {
225 case VIK_DEM_INTERPOL_NONE:
226 ce->elev = vik_dem_get_east_north(dem, lon, lat);
228 case VIK_DEM_INTERPOL_SIMPLE:
229 ce->elev = vik_dem_get_simple_interpol(dem, lon, lat);
231 case VIK_DEM_INTERPOL_BEST:
232 ce->elev = vik_dem_get_shepard_interpol(dem, lon, lat);
235 return (ce->elev != VIK_DEM_INVALID_ELEVATION);
238 /* TODO: keep a (sorted) linked list of DEMs and select the best resolution one */
239 gint16 a_dems_get_elev_by_coord ( const VikCoord *coord, VikDemInterpol method )
244 return VIK_DEM_INVALID_ELEVATION;
248 ce.elev = VIK_DEM_INVALID_ELEVATION;
250 if(!g_hash_table_find(loaded_dems, (GHRFunc)get_elev_by_coord, &ce))
251 return VIK_DEM_INVALID_ELEVATION;