Mark many strings translatable
[viking.git] / src / vikdemlayer.c
blob5a2b07ae46d8a0b491af41e308e12775cb730415
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <math.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <glib/gi18n.h>
31 #include "config.h"
32 #include "globals.h"
33 #include "coords.h"
34 #include "vikcoord.h"
35 #include "download.h"
36 #include "background.h"
37 #include "vikwaypoint.h"
38 #include "viktrack.h"
39 #include "vikviewport.h"
40 #include "viktreeview.h"
41 #include "viklayer.h"
42 #include "vikaggregatelayer.h"
43 #include "viklayerspanel.h"
44 #include "vikdemlayer.h"
45 #include "vikdemlayer_pixmap.h"
46 #include "vikmapslayer.h"
47 #include "dialog.h"
49 #include "dem.h"
50 #include "dems.h"
52 #include "icons/icons.h"
54 #define MAPS_CACHE_DIR maps_layer_default_dir()
56 #define SRTM_CACHE_TEMPLATE "%ssrtm3-%s%s%c%02d%c%03d.hgt.zip"
57 #define SRTM_FTP_SITE "e0srp01u.ecs.nasa.gov"
58 #define SRTM_FTP_URI "/srtm/version2/SRTM3/"
60 #ifdef VIK_CONFIG_DEM24K
61 #define DEM24K_DOWNLOAD_SCRIPT "dem24k.pl"
62 #endif
65 static void dem_layer_marshall( VikDEMLayer *vdl, guint8 **data, gint *len );
66 static VikDEMLayer *dem_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
67 static gboolean dem_layer_set_param ( VikDEMLayer *vdl, guint16 id, VikLayerParamData data, VikViewport *vp );
68 static VikLayerParamData dem_layer_get_param ( VikDEMLayer *vdl, guint16 id );
69 static void dem_layer_update_gc ( VikDEMLayer *vdl, VikViewport *vp, const gchar *color );
70 static void dem_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file );
71 static void srtm_draw_existence ( VikViewport *vp );
73 #ifdef VIK_CONFIG_DEM24K
74 static void dem24k_draw_existence ( VikViewport *vp );
75 #endif
77 static VikLayerParamScale param_scales[] = {
78 { 1, 10000, 10, 1 },
79 { 1, 10000, 10, 1 },
80 { 1, 10, 1, 0 },
83 static gchar *params_source[] = {
84 "SRTM Global 90m (3 arcsec)",
85 #ifdef VIK_CONFIG_DEM24K
86 "USA 10m (USGS 24k)",
87 #endif
88 "None",
89 NULL
92 enum { DEM_SOURCE_SRTM,
93 #ifdef VIK_CONFIG_DEM24K
94 DEM_SOURCE_DEM24K,
95 #endif
96 DEM_SOURCE_NONE,
99 static VikLayerParam dem_layer_params[] = {
100 { "files", VIK_LAYER_PARAM_STRING_LIST, VIK_LAYER_GROUP_NONE, N_("DEM Files:"), VIK_LAYER_WIDGET_FILELIST },
101 { "source", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Download Source:"), VIK_LAYER_WIDGET_RADIOGROUP_STATIC, params_source, NULL },
102 { "color", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Color:"), VIK_LAYER_WIDGET_ENTRY },
103 { "min_elev", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Min Elev:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
104 { "max_elev", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Max Elev:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
105 { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Line Thickness:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 },
109 enum { PARAM_FILES=0, PARAM_SOURCE, PARAM_COLOR, PARAM_MIN_ELEV, PARAM_MAX_ELEV, PARAM_LINE_THICKNESS, NUM_PARAMS };
111 static gpointer dem_layer_download_create ( VikWindow *vw, VikViewport *vvp);
112 static gboolean dem_layer_download_release ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp );
113 static gboolean dem_layer_download_click ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp );
115 static VikToolInterface dem_tools[] = {
116 { N_("DEM Download/Import"), (VikToolConstructorFunc) dem_layer_download_create, NULL, NULL, NULL,
117 (VikToolMouseFunc) dem_layer_download_click, NULL, (VikToolMouseFunc) dem_layer_download_release,
118 (VikToolKeyFunc) NULL, &cursor_demdl },
125 static gchar *dem_colors[] = {
126 "#0000FF",
127 "#9b793c",
128 "#9c7d40",
129 "#9d8144",
130 "#9e8549",
131 "#9f894d",
132 "#a08d51",
133 "#a29156",
134 "#a3955a",
135 "#a4995e",
136 "#a69d63",
137 "#a89f65",
138 "#aaa267",
139 "#ada569",
140 "#afa76b",
141 "#b1aa6d",
142 "#b4ad6f",
143 "#b6b071",
144 "#b9b373",
145 "#bcb676",
146 "#beb978",
147 "#c0bc7a",
148 "#c2c07d",
149 "#c4c37f",
150 "#c6c681",
151 "#c8ca84",
152 "#cacd86",
153 "#ccd188",
154 "#cfd58b",
155 "#c2ce84",
156 "#b5c87e",
157 "#a9c278",
158 "#9cbb71",
159 "#8fb56b",
160 "#83af65",
161 "#76a95e",
162 "#6aa358",
163 "#5e9d52",
164 "#63a055",
165 "#69a458",
166 "#6fa85c",
167 "#74ac5f",
168 "#7ab063",
169 "#80b467",
170 "#86b86a",
171 "#8cbc6e",
172 "#92c072",
173 "#94c175",
174 "#97c278",
175 "#9ac47c",
176 "#9cc57f",
177 "#9fc682",
178 "#a2c886",
179 "#a4c989",
180 "#a7cb8d",
181 "#aacd91",
182 "#afce99",
183 "#b5d0a1",
184 "#bbd2aa",
185 "#c0d3b2",
186 "#c6d5ba",
187 "#ccd7c3",
188 "#d1d9cb",
189 "#d7dbd4",
190 "#DDDDDD",
191 "#e0e0e0",
192 "#e4e4e4",
193 "#e8e8e8",
194 "#ebebeb",
195 "#efefef",
196 "#f3f3f3",
197 "#f7f7f7",
198 "#fbfbfb",
199 "#ffffff"
203 "#9b793c",
204 "#9e8549",
205 "#a29156",
206 "#a69d63",
207 "#ada569",
208 "#b4ad6f",
209 "#bcb676",
210 "#c2c07d",
211 "#c8ca84",
212 "#cfd58b",
213 "#a9c278",
214 "#83af65",
215 "#5e9d52",
216 "#6fa85c",
217 "#80b467",
218 "#92c072",
219 "#9ac47c",
220 "#a2c886",
221 "#aacd91",
222 "#bbd2aa",
223 "#ccd7c3",
224 "#DDDDDD",
225 "#e8e8e8",
226 "#f3f3f3",
227 "#FFFFFF"
231 static const guint DEM_N_COLORS = sizeof(dem_colors)/sizeof(dem_colors[0]);
234 VikLayerInterface vik_dem_layer_interface = {
235 "DEM",
236 &demlayer_pixbuf,
238 dem_tools,
239 sizeof(dem_tools) / sizeof(dem_tools[0]),
241 dem_layer_params,
242 NUM_PARAMS,
243 NULL,
246 VIK_MENU_ITEM_ALL,
248 (VikLayerFuncCreate) vik_dem_layer_create,
249 (VikLayerFuncRealize) NULL,
250 dem_layer_post_read,
251 (VikLayerFuncFree) vik_dem_layer_free,
253 (VikLayerFuncProperties) NULL,
254 (VikLayerFuncDraw) vik_dem_layer_draw,
255 (VikLayerFuncChangeCoordMode) NULL,
257 (VikLayerFuncSetMenuItemsSelection) NULL,
258 (VikLayerFuncGetMenuItemsSelection) NULL,
260 (VikLayerFuncAddMenuItems) NULL,
261 (VikLayerFuncSublayerAddMenuItems) NULL,
263 (VikLayerFuncSublayerRenameRequest) NULL,
264 (VikLayerFuncSublayerToggleVisible) NULL,
266 (VikLayerFuncMarshall) dem_layer_marshall,
267 (VikLayerFuncUnmarshall) dem_layer_unmarshall,
269 (VikLayerFuncSetParam) dem_layer_set_param,
270 (VikLayerFuncGetParam) dem_layer_get_param,
272 (VikLayerFuncReadFileData) NULL,
273 (VikLayerFuncWriteFileData) NULL,
275 (VikLayerFuncDeleteItem) NULL,
276 (VikLayerFuncCopyItem) NULL,
277 (VikLayerFuncPasteItem) NULL,
278 (VikLayerFuncFreeCopiedItem) NULL,
279 (VikLayerFuncDragDropRequest) NULL,
282 struct _VikDEMLayer {
283 VikLayer vl;
284 GdkGC *gc;
285 GdkGC **gcs;
286 GList *files;
287 gdouble min_elev;
288 gdouble max_elev;
289 guint8 line_thickness;
290 gchar *color;
291 guint source;
294 GType vik_dem_layer_get_type ()
296 static GType vdl_type = 0;
298 if (!vdl_type)
300 static const GTypeInfo vdl_info =
302 sizeof (VikDEMLayerClass),
303 NULL, /* base_init */
304 NULL, /* base_finalize */
305 NULL, /* class init */
306 NULL, /* class_finalize */
307 NULL, /* class_data */
308 sizeof (VikDEMLayer),
310 NULL /* instance init */
312 vdl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikDEMLayer", &vdl_info, 0 );
315 return vdl_type;
318 static void dem_layer_marshall( VikDEMLayer *vdl, guint8 **data, gint *len )
320 vik_layer_marshall_params ( VIK_LAYER(vdl), data, len );
323 static VikDEMLayer *dem_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
325 VikDEMLayer *rv = vik_dem_layer_new ();
326 gint i;
328 /* TODO: share GCS between layers */
329 for ( i = 0; i < DEM_N_COLORS; i++ )
330 rv->gcs[i] = vik_viewport_new_gc ( vvp, dem_colors[i], rv->line_thickness );
332 vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp );
333 return rv;
336 gboolean dem_layer_set_param ( VikDEMLayer *vdl, guint16 id, VikLayerParamData data, VikViewport *vp )
338 switch ( id )
340 case PARAM_COLOR: if ( vdl->color ) g_free ( vdl->color ); vdl->color = g_strdup ( data.s ); break;
341 case PARAM_SOURCE: vdl->source = data.u; break;
342 case PARAM_MIN_ELEV: vdl->min_elev = data.d; break;
343 case PARAM_MAX_ELEV: vdl->max_elev = data.d; break;
344 case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vdl->line_thickness = data.u; break;
345 case PARAM_FILES: a_dems_load_list ( &(data.sl) ); a_dems_list_free ( vdl->files ); vdl->files = data.sl; break;
347 return TRUE;
350 static VikLayerParamData dem_layer_get_param ( VikDEMLayer *vdl, guint16 id )
352 VikLayerParamData rv;
353 switch ( id )
355 case PARAM_FILES: rv.sl = vdl->files; break;
356 case PARAM_SOURCE: rv.u = vdl->source; break;
357 case PARAM_COLOR: rv.s = vdl->color ? vdl->color : ""; break;
358 case PARAM_MIN_ELEV: rv.d = vdl->min_elev; break;
359 case PARAM_MAX_ELEV: rv.d = vdl->max_elev; break;
360 case PARAM_LINE_THICKNESS: rv.i = vdl->line_thickness; break;
362 return rv;
365 static void dem_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file )
367 VikDEMLayer *vdl = VIK_DEM_LAYER(vl);
368 if ( vdl->gc )
369 g_object_unref ( G_OBJECT(vdl->gc) );
371 vdl->gc = vik_viewport_new_gc ( vp, vdl->color, vdl->line_thickness );
374 VikDEMLayer *vik_dem_layer_new ( )
376 VikDEMLayer *vdl = VIK_DEM_LAYER ( g_object_new ( VIK_DEM_LAYER_TYPE, NULL ) );
378 vik_layer_init ( VIK_LAYER(vdl), VIK_LAYER_DEM );
380 vdl->files = NULL;
383 vdl->gc = NULL;
385 vdl->gcs = g_malloc(sizeof(GdkGC *)*DEM_N_COLORS);
386 /* make new gcs only if we need it (copy layer -> use old) */
388 vdl->min_elev = 0.0;
389 vdl->max_elev = 1000.0;
390 vdl->source = DEM_SOURCE_SRTM;
391 vdl->line_thickness = 3;
392 vdl->color = NULL;
393 return vdl;
398 static void vik_dem_layer_draw_dem ( VikDEMLayer *vdl, VikViewport *vp, VikDEM *dem )
400 VikDEMColumn *column;
402 struct LatLon dem_northeast, dem_southwest;
403 gdouble max_lat, max_lon, min_lat, min_lon;
405 /**** Check if viewport and DEM data overlap ****/
407 /* get min, max lat/lon of viewport */
408 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
410 /* get min, max lat/lon of DEM data */
411 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
412 dem_northeast.lat = dem->max_north / 3600.0;
413 dem_northeast.lon = dem->max_east / 3600.0;
414 dem_southwest.lat = dem->min_north / 3600.0;
415 dem_southwest.lon = dem->min_east / 3600.0;
416 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
417 struct UTM dem_northeast_utm, dem_southwest_utm;
418 dem_northeast_utm.northing = dem->max_north;
419 dem_northeast_utm.easting = dem->max_east;
420 dem_southwest_utm.northing = dem->min_north;
421 dem_southwest_utm.easting = dem->min_east;
422 dem_northeast_utm.zone = dem_southwest_utm.zone = dem->utm_zone;
423 dem_northeast_utm.letter = dem_southwest_utm.letter = dem->utm_letter;
425 a_coords_utm_to_latlon(&dem_northeast_utm, &dem_northeast);
426 a_coords_utm_to_latlon(&dem_southwest_utm, &dem_southwest);
429 if ( (max_lat > dem_northeast.lat && min_lat > dem_northeast.lat) ||
430 (max_lat < dem_southwest.lat && min_lat < dem_southwest.lat) )
431 return;
432 else if ( (max_lon > dem_northeast.lon && min_lon > dem_northeast.lon) ||
433 (max_lon < dem_southwest.lon && min_lon < dem_southwest.lon) )
434 return;
435 /* else they overlap */
437 /**** End Overlap Check ****/
438 /* boxes to show where we have DEM instead of actually drawing the DEM.
439 * useful if we want to see what areas we have coverage for (if we want
440 * to get elevation data for a track) but don't want to cover the map.
443 #if 0
444 /* draw a box if a DEM is loaded. in future I'd like to add an option for this
445 * this is useful if we want to see what areas we have dem for but don't want to
446 * cover the map (or maybe we just need translucent DEM?) */
448 VikCoord demne, demsw;
449 gint x1, y1, x2, y2;
450 vik_coord_load_from_latlon(&demne, vik_viewport_get_coord_mode(vp), &dem_northeast);
451 vik_coord_load_from_latlon(&demsw, vik_viewport_get_coord_mode(vp), &dem_southwest);
453 vik_viewport_coord_to_screen ( vp, &demne, &x1, &y1 );
454 vik_viewport_coord_to_screen ( vp, &demsw, &x2, &y2 );
456 if ( x1 > vik_viewport_get_width(vp) ) x1=vik_viewport_get_width(vp);
457 if ( y2 > vik_viewport_get_height(vp) ) y2=vik_viewport_get_height(vp);
458 if ( x2 < 0 ) x2 = 0;
459 if ( y1 < 0 ) y1 = 0;
460 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
461 FALSE, x2, y1, x1-x2, y2-y1 );
462 return;
464 #endif
466 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
467 VikCoord tmp; /* TODO: don't use coord_load_from_latlon, especially if in latlon drawing mode */
469 gdouble max_lat_as, max_lon_as, min_lat_as, min_lon_as;
470 gdouble start_lat_as, end_lat_as, start_lon_as, end_lon_as;
472 gdouble start_lat, end_lat, start_lon, end_lon;
474 struct LatLon counter;
476 guint x, y, start_x, start_y;
478 gint16 elev;
480 guint skip_factor = ceil ( vik_viewport_get_xmpp(vp) / 40 ); /* todo: smarter calculation. */
482 gdouble nscale_deg = dem->north_scale / ((gdouble) 3600);
483 gdouble escale_deg = dem->east_scale / ((gdouble) 3600);
485 max_lat_as = max_lat * 3600;
486 min_lat_as = min_lat * 3600;
487 max_lon_as = max_lon * 3600;
488 min_lon_as = min_lon * 3600;
490 start_lat_as = MAX(min_lat_as, dem->min_north);
491 end_lat_as = MIN(max_lat_as, dem->max_north);
492 start_lon_as = MAX(min_lon_as, dem->min_east);
493 end_lon_as = MIN(max_lon_as, dem->max_east);
495 start_lat = floor(start_lat_as / dem->north_scale) * nscale_deg;
496 end_lat = ceil (end_lat_as / dem->north_scale) * nscale_deg;
497 start_lon = floor(start_lon_as / dem->east_scale) * escale_deg;
498 end_lon = ceil (end_lon_as / dem->east_scale) * escale_deg;
500 vik_dem_east_north_to_xy ( dem, start_lon_as, start_lat_as, &start_x, &start_y );
502 /* verify sane elev interval */
503 if ( vdl->max_elev <= vdl->min_elev )
504 vdl->max_elev = vdl->min_elev + 1;
506 for ( x=start_x, counter.lon = start_lon; counter.lon <= end_lon; counter.lon += escale_deg * skip_factor, x += skip_factor ) {
507 if ( x > 0 && x < dem->n_columns ) {
508 column = g_ptr_array_index ( dem->columns, x );
509 for ( y=start_y, counter.lat = start_lat; counter.lat <= end_lat; counter.lat += nscale_deg * skip_factor, y += skip_factor ) {
510 if ( y > column->n_points )
511 break;
512 elev = column->points[y];
514 if ( elev != VIK_DEM_INVALID_ELEVATION && elev < vdl->min_elev )
515 elev=vdl->min_elev;
516 if ( elev != VIK_DEM_INVALID_ELEVATION && elev > vdl->max_elev )
517 elev=vdl->max_elev;
521 gint a, b;
523 vik_coord_load_from_latlon(&tmp, vik_viewport_get_coord_mode(vp), &counter);
524 vik_viewport_coord_to_screen(vp, &tmp, &a, &b);
525 if ( elev == VIK_DEM_INVALID_ELEVATION )
526 ; /* don't draw it */
527 else if ( elev <= 0 )
528 vik_viewport_draw_rectangle(vp, vdl->gcs[0], TRUE, a-2, b-2, 4, 4 );
529 else
530 vik_viewport_draw_rectangle(vp, vdl->gcs[(gint)floor((elev - vdl->min_elev)/(vdl->max_elev - vdl->min_elev)*(DEM_N_COLORS-2))+1], TRUE, a-2, b-2, 4, 4 );
532 } /* for y= */
534 } /* for x= */
535 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
536 gdouble max_nor, max_eas, min_nor, min_eas;
537 gdouble start_nor, start_eas, end_nor, end_eas;
539 gint16 elev;
541 guint x, y, start_x, start_y;
543 VikCoord tmp; /* TODO: don't use coord_load_from_latlon, especially if in latlon drawing mode */
544 struct UTM counter;
546 guint skip_factor = ceil ( vik_viewport_get_xmpp(vp) / 10 ); /* todo: smarter calculation. */
548 VikCoord tleft, tright, bleft, bright;
550 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
551 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
552 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
553 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &bright );
556 vik_coord_convert(&tleft, VIK_COORD_UTM);
557 vik_coord_convert(&tright, VIK_COORD_UTM);
558 vik_coord_convert(&bleft, VIK_COORD_UTM);
559 vik_coord_convert(&bright, VIK_COORD_UTM);
561 max_nor = MAX(tleft.north_south, tright.north_south);
562 min_nor = MIN(bleft.north_south, bright.north_south);
563 max_eas = MAX(bright.east_west, tright.east_west);
564 min_eas = MIN(bleft.east_west, tleft.east_west);
566 start_nor = MAX(min_nor, dem->min_north);
567 end_nor = MIN(max_nor, dem->max_north);
568 if ( tleft.utm_zone == dem->utm_zone && bleft.utm_zone == dem->utm_zone
569 && (tleft.utm_letter >= 'N') == (dem->utm_letter >= 'N')
570 && (bleft.utm_letter >= 'N') == (dem->utm_letter >= 'N') ) /* if the utm zones/hemispheres are different, min_eas will be bogus */
571 start_eas = MAX(min_eas, dem->min_east);
572 else
573 start_eas = dem->min_east;
574 if ( tright.utm_zone == dem->utm_zone && bright.utm_zone == dem->utm_zone
575 && (tright.utm_letter >= 'N') == (dem->utm_letter >= 'N')
576 && (bright.utm_letter >= 'N') == (dem->utm_letter >= 'N') ) /* if the utm zones/hemispheres are different, min_eas will be bogus */
577 end_eas = MIN(max_eas, dem->max_east);
578 else
579 end_eas = dem->max_east;
581 start_nor = floor(start_nor / dem->north_scale) * dem->north_scale;
582 end_nor = ceil (end_nor / dem->north_scale) * dem->north_scale;
583 start_eas = floor(start_eas / dem->east_scale) * dem->east_scale;
584 end_eas = ceil (end_eas / dem->east_scale) * dem->east_scale;
586 vik_dem_east_north_to_xy ( dem, start_eas, start_nor, &start_x, &start_y );
588 /* TODO: why start_x and start_y are -1 -- rounding error from above? */
590 counter.zone = dem->utm_zone;
591 counter.letter = dem->utm_letter;
593 for ( x=start_x, counter.easting = start_eas; counter.easting <= end_eas; counter.easting += dem->east_scale * skip_factor, x += skip_factor ) {
594 if ( x > 0 && x < dem->n_columns ) {
595 column = g_ptr_array_index ( dem->columns, x );
596 for ( y=start_y, counter.northing = start_nor; counter.northing <= end_nor; counter.northing += dem->north_scale * skip_factor, y += skip_factor ) {
597 if ( y > column->n_points )
598 continue;
599 elev = column->points[y];
600 if ( elev != VIK_DEM_INVALID_ELEVATION && elev < vdl->min_elev )
601 elev=vdl->min_elev;
602 if ( elev != VIK_DEM_INVALID_ELEVATION && elev > vdl->max_elev )
603 elev=vdl->max_elev;
606 gint a, b;
607 vik_coord_load_from_utm(&tmp, vik_viewport_get_coord_mode(vp), &counter);
608 vik_viewport_coord_to_screen(vp, &tmp, &a, &b);
609 if ( elev == VIK_DEM_INVALID_ELEVATION )
610 ; /* don't draw it */
611 else if ( elev <= 0 )
612 vik_viewport_draw_rectangle(vp, vdl->gcs[0], TRUE, a-2, b-2, 4, 4 );
613 else
614 vik_viewport_draw_rectangle(vp, vdl->gcs[(gint)floor((elev - vdl->min_elev)/(vdl->max_elev - vdl->min_elev)*(DEM_N_COLORS-2))+1], TRUE, a-2, b-2, 4, 4 );
616 } /* for y= */
618 } /* for x= */
622 /* return the continent for the specified lat, lon */
623 /* TODO */
624 static const gchar *srtm_continent_dir ( gint lat, gint lon )
626 extern const char *_srtm_continent_data[];
627 static GHashTable *srtm_continent = NULL;
628 const gchar *continent;
629 gchar name[16];
631 if (!srtm_continent) {
632 const gchar **s;
634 srtm_continent = g_hash_table_new(g_str_hash, g_str_equal);
635 s = _srtm_continent_data;
636 while (*s != (gchar *)-1) {
637 continent = *s++;
638 while (*s) {
639 g_hash_table_insert(srtm_continent, *s, continent);
640 s++;
642 s++;
645 g_snprintf(name, sizeof(name), "%c%02d%c%03d",
646 (lat >= 0) ? 'N' : 'S', ABS(lat),
647 (lon >= 0) ? 'E' : 'W', ABS(lon));
649 return(g_hash_table_lookup(srtm_continent, name));
652 void vik_dem_layer_draw ( VikDEMLayer *vdl, gpointer data )
654 VikViewport *vp = (VikViewport *) data;
655 GList *dems_iter = vdl->files;
656 VikDEM *dem;
659 /* search for SRTM3 90m */
661 if ( vdl->source == DEM_SOURCE_SRTM )
662 srtm_draw_existence ( vp );
663 #ifdef VIK_CONFIG_DEM24K
664 else if ( vdl->source == DEM_SOURCE_DEM24K )
665 dem24k_draw_existence ( vp );
666 #endif
668 while ( dems_iter ) {
669 dem = a_dems_get ( (const char *) (dems_iter->data) );
670 if ( dem )
671 vik_dem_layer_draw_dem ( vdl, vp, dem );
672 dems_iter = dems_iter->next;
676 void vik_dem_layer_free ( VikDEMLayer *vdl )
678 gint i;
679 if ( vdl->gc != NULL )
680 g_object_unref ( G_OBJECT(vdl->gc) );
682 if ( vdl->color != NULL )
683 g_free ( vdl->color );
685 if ( vdl->gcs )
686 for ( i = 0; i < DEM_N_COLORS; i++ )
687 g_object_unref ( vdl->gcs[i] );
688 g_free ( vdl->gcs );
690 a_dems_list_free ( vdl->files );
693 static void dem_layer_update_gc ( VikDEMLayer *vdl, VikViewport *vp, const gchar *color )
695 if ( vdl->color )
696 g_free ( vdl->color );
698 vdl->color = g_strdup ( color );
700 if ( vdl->gc )
701 g_object_unref ( G_OBJECT(vdl->gc) );
703 vdl->gc = vik_viewport_new_gc ( vp, vdl->color, vdl->line_thickness );
706 VikDEMLayer *vik_dem_layer_create ( VikViewport *vp )
708 VikDEMLayer *vdl = vik_dem_layer_new ();
709 gint i;
711 /* TODO: share GCS between layers */
712 for ( i = 0; i < DEM_N_COLORS; i++ )
713 vdl->gcs[i] = vik_viewport_new_gc ( vp, dem_colors[i], vdl->line_thickness );
715 dem_layer_update_gc ( vdl, vp, "red" );
716 return vdl;
718 /**************************************************************
719 **** SOURCES & DOWNLOADING
720 **************************************************************/
721 typedef struct {
722 gchar *dest;
723 gdouble lat, lon;
725 GMutex *mutex;
726 VikDEMLayer *vdl; /* NULL if not alive */
728 guint source;
729 } DEMDownloadParams;
732 /**************************************************
733 * SOURCE: SRTM *
734 **************************************************/
736 static void srtm_dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
738 gint intlat, intlon;
739 const gchar *continent_dir;
741 intlat = (int)floor(p->lat);
742 intlon = (int)floor(p->lon);
743 continent_dir = srtm_continent_dir(intlat, intlon);
745 if (!continent_dir) {
746 g_warning(N_("No SRTM data available for %f, %f\n"), p->lat, p->lon);
747 return;
750 gchar *src_fn = g_strdup_printf("%s%s%s%c%02d%c%03d.hgt.zip",
751 SRTM_FTP_URI,
752 continent_dir,
753 G_DIR_SEPARATOR_S,
754 (intlat >= 0) ? 'N' : 'S',
755 ABS(intlat),
756 (intlon >= 0) ? 'E' : 'W',
757 ABS(intlon) );
759 DownloadOptions options = { NULL, 0 };
760 a_ftp_download_get_url ( SRTM_FTP_SITE, src_fn, p->dest, &options );
761 g_free ( src_fn );
764 static gchar *srtm_lat_lon_to_dest_fn ( gdouble lat, gdouble lon )
766 gint intlat, intlon;
767 const gchar *continent_dir;
769 intlat = (int)floor(lat);
770 intlon = (int)floor(lon);
771 continent_dir = srtm_continent_dir(intlat, intlon);
773 if (!continent_dir)
774 continent_dir = "nowhere";
776 return g_strdup_printf("srtm3-%s%s%c%02d%c%03d.hgt.zip",
777 continent_dir,
778 G_DIR_SEPARATOR_S,
779 (intlat >= 0) ? 'N' : 'S',
780 ABS(intlat),
781 (intlon >= 0) ? 'E' : 'W',
782 ABS(intlon) );
786 /* TODO: generalize */
787 static void srtm_draw_existence ( VikViewport *vp )
789 gdouble max_lat, max_lon, min_lat, min_lon;
790 gchar buf[strlen(MAPS_CACHE_DIR)+strlen(SRTM_CACHE_TEMPLATE)+30];
791 gint i, j;
793 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
795 for (i = floor(min_lat); i <= floor(max_lat); i++) {
796 for (j = floor(min_lon); j <= floor(max_lon); j++) {
797 const gchar *continent_dir;
798 if ((continent_dir = srtm_continent_dir(i, j)) == NULL)
799 continue;
800 g_snprintf(buf, sizeof(buf), SRTM_CACHE_TEMPLATE,
801 MAPS_CACHE_DIR,
802 continent_dir,
803 G_DIR_SEPARATOR_S,
804 (i >= 0) ? 'N' : 'S',
805 ABS(i),
806 (j >= 0) ? 'E' : 'W',
807 ABS(j) );
808 if ( access(buf, F_OK ) == 0 ) {
809 VikCoord ne, sw;
810 gint x1, y1, x2, y2;
811 sw.north_south = i;
812 sw.east_west = j;
813 sw.mode = VIK_COORD_LATLON;
814 ne.north_south = i+1;
815 ne.east_west = j+1;
816 ne.mode = VIK_COORD_LATLON;
817 vik_viewport_coord_to_screen ( vp, &sw, &x1, &y1 );
818 vik_viewport_coord_to_screen ( vp, &ne, &x2, &y2 );
819 if ( x1 < 0 ) x1 = 0;
820 if ( y2 < 0 ) y2 = 0;
821 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
822 FALSE, x1, y2, x2-x1, y1-y2 );
829 /**************************************************
830 * SOURCE: USGS 24K *
831 **************************************************/
833 #ifdef VIK_CONFIG_DEM24K
835 static void dem24k_dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
837 /* TODO: dest dir */
838 gchar *cmdline = g_strdup_printf("%s %.03f %.03f",
839 DEM24K_DOWNLOAD_SCRIPT,
840 floor(p->lat*8)/8,
841 ceil(p->lon*8)/8 );
842 /* FIX: don't use system, use execv or something. check for existence */
843 system(cmdline);
846 static gchar *dem24k_lat_lon_to_dest_fn ( gdouble lat, gdouble lon )
848 return g_strdup_printf("dem24k/%d/%d/%.03f,%.03f.dem",
849 (gint) lat,
850 (gint) lon,
851 floor(lat*8)/8,
852 ceil(lon*8)/8);
855 /* TODO: generalize */
856 static void dem24k_draw_existence ( VikViewport *vp )
858 gdouble max_lat, max_lon, min_lat, min_lon;
859 gchar buf[strlen(MAPS_CACHE_DIR)+40];
860 gdouble i, j;
862 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
864 for (i = floor(min_lat*8)/8; i <= floor(max_lat*8)/8; i+=0.125) {
865 /* check lat dir first -- faster */
866 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/",
867 MAPS_CACHE_DIR,
868 (gint) i );
869 if ( access(buf, F_OK) != 0 )
870 continue;
871 for (j = floor(min_lon*8)/8; j <= floor(max_lon*8)/8; j+=0.125) {
872 /* check lon dir first -- faster */
873 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/%d/",
874 MAPS_CACHE_DIR,
875 (gint) i,
876 (gint) j );
877 if ( access(buf, F_OK) != 0 )
878 continue;
879 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/%d/%.03f,%.03f.dem",
880 MAPS_CACHE_DIR,
881 (gint) i,
882 (gint) j,
883 floor(i*8)/8,
884 floor(j*8)/8 );
885 if ( access(buf, F_OK ) == 0 ) {
886 VikCoord ne, sw;
887 gint x1, y1, x2, y2;
888 sw.north_south = i;
889 sw.east_west = j-0.125;
890 sw.mode = VIK_COORD_LATLON;
891 ne.north_south = i+0.125;
892 ne.east_west = j;
893 ne.mode = VIK_COORD_LATLON;
894 vik_viewport_coord_to_screen ( vp, &sw, &x1, &y1 );
895 vik_viewport_coord_to_screen ( vp, &ne, &x2, &y2 );
896 if ( x1 < 0 ) x1 = 0;
897 if ( y2 < 0 ) y2 = 0;
898 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
899 FALSE, x1, y2, x2-x1, y1-y2 );
904 #endif
906 /**************************************************
907 * SOURCES -- DOWNLOADING & IMPORTING TOOL *
908 **************************************************
911 static void weak_ref_cb ( gpointer ptr, GObject * dead_vdl )
913 DEMDownloadParams *p = ptr;
914 g_mutex_lock ( p->mutex );
915 p->vdl = NULL;
916 g_mutex_unlock ( p->mutex );
919 /* Try to add file full_path.
920 * full_path will be copied.
921 * returns FALSE if file does not exists, TRUE otherwise.
923 static gboolean dem_layer_add_file ( VikDEMLayer *vdl, const gchar *full_path )
925 if ( access(full_path, F_OK ) == 0 ) {
926 /* only load if file size is not 0 (not in progress */
927 struct stat sb;
928 stat (full_path, &sb);
929 if ( sb.st_size ) {
930 gchar *duped_path = g_strdup(full_path);
931 vdl->files = g_list_prepend ( vdl->files, duped_path );
932 a_dems_load ( duped_path );
933 g_warning(duped_path);
934 vik_layer_emit_update ( VIK_LAYER(vdl) );
936 return TRUE;
937 } else
938 return FALSE;
941 static void dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
943 if ( p->source == DEM_SOURCE_SRTM )
944 srtm_dem_download_thread ( p, threaddata );
945 #ifdef VIK_CONFIG_DEM24K
946 else if ( p->source == DEM_SOURCE_DEM24K )
947 dem24k_dem_download_thread ( p, threaddata );
948 #endif
950 gdk_threads_enter();
951 g_mutex_lock ( p->mutex );
952 if ( p->vdl ) {
953 g_object_weak_unref ( G_OBJECT(p->vdl), weak_ref_cb, p );
955 if ( dem_layer_add_file ( p->vdl, p->dest ) )
956 vik_layer_emit_update ( VIK_LAYER(p->vdl) );
958 g_mutex_unlock ( p->mutex );
959 gdk_threads_leave();
963 static void free_dem_download_params ( DEMDownloadParams *p )
965 g_mutex_free ( p->mutex );
966 g_free ( p->dest );
967 g_free ( p );
970 static gpointer dem_layer_download_create ( VikWindow *vw, VikViewport *vvp)
972 return vvp;
976 static gboolean dem_layer_download_release ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp )
978 VikCoord coord;
979 struct LatLon ll;
981 gchar *full_path;
982 gchar *dem_file = NULL;
984 if ( vdl->source == DEM_SOURCE_NONE )
985 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vdl), _("No download source selected. Edit layer properties.") );
987 vik_viewport_screen_to_coord ( vvp, event->x, event->y, &coord );
988 vik_coord_to_latlon ( &coord, &ll );
991 if ( vdl->source == DEM_SOURCE_SRTM )
992 dem_file = srtm_lat_lon_to_dest_fn ( ll.lat, ll.lon );
993 #ifdef VIK_CONFIG_DEM24K
994 else if ( vdl->source == DEM_SOURCE_DEM24K )
995 dem_file = dem24k_lat_lon_to_dest_fn ( ll.lat, ll.lon );
996 #endif
998 if ( ! dem_file )
999 return TRUE;
1001 full_path = g_strdup_printf("%s%s", MAPS_CACHE_DIR, dem_file );
1003 g_warning(full_path);
1005 // TODO: check if already in filelist
1007 if ( ! dem_layer_add_file(vdl, full_path) ) {
1008 gchar *tmp = g_strdup_printf ( _("Downloading DEM %s"), dem_file );
1009 DEMDownloadParams *p = g_malloc(sizeof(DEMDownloadParams));
1010 p->dest = g_strdup(full_path);
1011 p->lat = ll.lat;
1012 p->lon = ll.lon;
1013 p->vdl = vdl;
1014 p->mutex = g_mutex_new();
1015 p->source = vdl->source;
1016 g_object_weak_ref(G_OBJECT(p->vdl), weak_ref_cb, p );
1018 a_background_thread ( VIK_GTK_WINDOW_FROM_LAYER(vdl), tmp,
1019 (vik_thr_func) dem_download_thread, p,
1020 (vik_thr_free_func) free_dem_download_params, NULL, 1 );
1023 g_free ( dem_file );
1024 g_free ( full_path );
1026 return TRUE;
1029 static gboolean dem_layer_download_click ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp )
1031 /* choose & keep track of cache dir
1032 * download in background thread
1033 * download over area */
1034 return TRUE;