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
31 GHashTable *loaded_dems = NULL;
34 static void loaded_dem_free ( LoadedDEM *ldem )
36 vik_dem_free ( ldem->dem );
43 g_hash_table_destroy ( loaded_dems );
46 /* To load a dem. if it was already loaded, will simply
47 * reference the one already loaded and return it.
49 VikDEM *a_dems_load(const gchar *filename)
53 /* dems init hash table */
55 loaded_dems = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) loaded_dem_free );
57 ldem = (LoadedDEM *) g_hash_table_lookup ( loaded_dems, filename );
62 VikDEM *dem = vik_dem_new_from_file ( filename );
65 ldem = g_malloc ( sizeof(LoadedDEM) );
68 g_hash_table_insert ( loaded_dems, g_strdup(filename), ldem );
73 void a_dems_unref(const gchar *filename)
75 LoadedDEM *ldem = (LoadedDEM *) g_hash_table_lookup ( loaded_dems, filename );
78 if ( ldem->ref_count == 0 )
79 g_hash_table_remove ( loaded_dems, filename );
82 /* to get a DEM that was already loaded.
83 * assumes that its in there already,
84 * although it could not be if earlier load failed.
86 VikDEM *a_dems_get(const gchar *filename)
88 LoadedDEM *ldem = g_hash_table_lookup ( loaded_dems, filename );
95 /* Load a string list (GList of strings) of dems. You have to use get to at them later.
96 * When updating a list as a parameter, this should be bfore freeing the list so
97 * the same DEMs won't be loaded & unloaded.
98 * Modifies the list to remove DEMs which did not load.
101 /* TODO: don't delete them when they don't exist.
102 * we need to warn the user, but we should keep them in the GList.
103 * we need to know that they weren't referenced though when we
104 * do the a_dems_list_free().
106 void a_dems_load_list ( GList **dems )
110 if ( ! a_dems_load((const gchar *) (iter->data)) ) {
111 GList *iter_temp = iter->next;
112 g_free ( iter->data );
113 (*dems) = g_list_remove_link ( (*dems), iter );
121 /* Takes a string list (GList of strings) of dems (filenames).
122 * Unrefs all the dems (i.e. "unloads" them), then frees the
123 * strings, the frees the list.
125 void a_dems_list_free ( GList *dems )
129 a_dems_unref ((const gchar *)iter->data);
130 g_free ( iter->data );
133 g_list_free ( dems );
136 GList *a_dems_list_copy ( GList *dems )
138 GList *rv = g_list_copy ( dems );
141 if ( ! a_dems_load((const gchar *) (iter->data)) ) {
142 GList *iter_temp = iter->next; /* delete link, don't bother strdup'ing and free'ing string */
143 rv = g_list_remove_link ( rv, iter );
146 iter->data = g_strdup((gchar *)iter->data); /* copy the string too. */
153 gint16 a_dems_list_get_elev_by_coord ( GList *dems, const VikCoord *coord )
155 static struct UTM utm_tmp;
156 static struct LatLon ll_tmp;
162 dem = a_dems_get ( (gchar *) iter->data );
164 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
165 vik_coord_to_latlon ( coord, &ll_tmp );
168 elev = vik_dem_get_east_north(dem, ll_tmp.lon, ll_tmp.lat);
169 if ( elev != VIK_DEM_INVALID_ELEVATION )
171 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
172 vik_coord_to_utm ( coord, &utm_tmp );
173 if ( utm_tmp.zone == dem->utm_zone &&
174 (elev = vik_dem_get_east_north(dem, utm_tmp.easting, utm_tmp.northing)) != VIK_DEM_INVALID_ELEVATION )
180 return VIK_DEM_INVALID_ELEVATION;
184 const VikCoord *coord;
185 VikDemInterpol method;
189 static gboolean get_elev_by_coord(gpointer key, LoadedDEM *ldem, CoordElev *ce)
191 VikDEM *dem = ldem->dem;
194 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
195 struct LatLon ll_tmp;
196 vik_coord_to_latlon (ce->coord, &ll_tmp );
197 lat = ll_tmp.lat * 3600;
198 lon = ll_tmp.lon * 3600;
199 } else if (dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS) {
200 static struct UTM utm_tmp;
201 if (utm_tmp.zone != dem->utm_zone)
203 vik_coord_to_utm (ce->coord, &utm_tmp);
204 lat = utm_tmp.northing;
205 lon = utm_tmp.easting;
209 switch (ce->method) {
210 case VIK_DEM_INTERPOL_NONE:
211 ce->elev = vik_dem_get_east_north(dem, lon, lat);
213 case VIK_DEM_INTERPOL_SIMPLE:
214 ce->elev = vik_dem_get_simple_interpol(dem, lon, lat);
216 case VIK_DEM_INTERPOL_BEST:
217 ce->elev = vik_dem_get_shepard_interpol(dem, lon, lat);
220 return (ce->elev != VIK_DEM_INVALID_ELEVATION);
223 /* TODO: keep a (sorted) linked list of DEMs and select the best resolution one */
224 gint16 a_dems_get_elev_by_coord ( const VikCoord *coord, VikDemInterpol method )
229 return VIK_DEM_INVALID_ELEVATION;
233 ce.elev = VIK_DEM_INVALID_ELEVATION;
235 if(!g_hash_table_find(loaded_dems, (GHRFunc)get_elev_by_coord, &ce))
236 return VIK_DEM_INVALID_ELEVATION;