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
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
38 #include <glib/gi18n.h>
45 #include "background.h"
46 #include "vikwaypoint.h"
48 #include "vikviewport.h"
49 #include "viktreeview.h"
51 #include "vikaggregatelayer.h"
52 #include "viklayerspanel.h"
53 #include "vikmapslayer.h"
54 #include "vikdemlayer.h"
60 #include "icons/icons.h"
62 #define MAPS_CACHE_DIR maps_layer_default_dir()
64 #define SRTM_CACHE_TEMPLATE "%ssrtm3-%s%s%c%02d%c%03d.hgt.zip"
65 #define SRTM_FTP_SITE "e0srp01u.ecs.nasa.gov"
66 #define SRTM_FTP_URI "/srtm/version2/SRTM3/"
68 #ifdef VIK_CONFIG_DEM24K
69 #define DEM24K_DOWNLOAD_SCRIPT "dem24k.pl"
73 static void dem_layer_marshall( VikDEMLayer *vdl, guint8 **data, gint *len );
74 static VikDEMLayer *dem_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
75 static gboolean dem_layer_set_param ( VikDEMLayer *vdl, guint16 id, VikLayerParamData data, VikViewport *vp );
76 static VikLayerParamData dem_layer_get_param ( VikDEMLayer *vdl, guint16 id );
77 static void dem_layer_update_gc ( VikDEMLayer *vdl, VikViewport *vp, const gchar *color );
78 static void dem_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file );
79 static void srtm_draw_existence ( VikViewport *vp );
81 #ifdef VIK_CONFIG_DEM24K
82 static void dem24k_draw_existence ( VikViewport *vp );
85 static VikLayerParamScale param_scales[] = {
91 static gchar *params_source[] = {
92 "SRTM Global 90m (3 arcsec)",
93 #ifdef VIK_CONFIG_DEM24K
100 enum { DEM_SOURCE_SRTM,
101 #ifdef VIK_CONFIG_DEM24K
107 static VikLayerParam dem_layer_params[] = {
108 { "files", VIK_LAYER_PARAM_STRING_LIST, VIK_LAYER_GROUP_NONE, N_("DEM Files:"), VIK_LAYER_WIDGET_FILELIST },
109 { "source", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Download Source:"), VIK_LAYER_WIDGET_RADIOGROUP_STATIC, params_source, NULL },
110 { "color", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Color:"), VIK_LAYER_WIDGET_ENTRY },
111 { "min_elev", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Min Elev:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
112 { "max_elev", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Max Elev:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
113 { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Line Thickness:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 },
117 enum { PARAM_FILES=0, PARAM_SOURCE, PARAM_COLOR, PARAM_MIN_ELEV, PARAM_MAX_ELEV, PARAM_LINE_THICKNESS, NUM_PARAMS };
119 static gpointer dem_layer_download_create ( VikWindow *vw, VikViewport *vvp);
120 static gboolean dem_layer_download_release ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp );
121 static gboolean dem_layer_download_click ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp );
123 static VikToolInterface dem_tools[] = {
124 { N_("DEM Download/Import"), (VikToolConstructorFunc) dem_layer_download_create, NULL, NULL, NULL,
125 (VikToolMouseFunc) dem_layer_download_click, NULL, (VikToolMouseFunc) dem_layer_download_release,
126 (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_demdl_pixbuf },
133 static gchar *dem_colors[] = {
239 static const guint DEM_N_COLORS = sizeof(dem_colors)/sizeof(dem_colors[0]);
242 VikLayerInterface vik_dem_layer_interface = {
247 sizeof(dem_tools) / sizeof(dem_tools[0]),
256 (VikLayerFuncCreate) vik_dem_layer_create,
257 (VikLayerFuncRealize) NULL,
259 (VikLayerFuncFree) vik_dem_layer_free,
261 (VikLayerFuncProperties) NULL,
262 (VikLayerFuncDraw) vik_dem_layer_draw,
263 (VikLayerFuncChangeCoordMode) NULL,
265 (VikLayerFuncSetMenuItemsSelection) NULL,
266 (VikLayerFuncGetMenuItemsSelection) NULL,
268 (VikLayerFuncAddMenuItems) NULL,
269 (VikLayerFuncSublayerAddMenuItems) NULL,
271 (VikLayerFuncSublayerRenameRequest) NULL,
272 (VikLayerFuncSublayerToggleVisible) NULL,
274 (VikLayerFuncMarshall) dem_layer_marshall,
275 (VikLayerFuncUnmarshall) dem_layer_unmarshall,
277 (VikLayerFuncSetParam) dem_layer_set_param,
278 (VikLayerFuncGetParam) dem_layer_get_param,
280 (VikLayerFuncReadFileData) NULL,
281 (VikLayerFuncWriteFileData) NULL,
283 (VikLayerFuncDeleteItem) NULL,
284 (VikLayerFuncCopyItem) NULL,
285 (VikLayerFuncPasteItem) NULL,
286 (VikLayerFuncFreeCopiedItem) NULL,
287 (VikLayerFuncDragDropRequest) NULL,
290 struct _VikDEMLayer {
297 guint8 line_thickness;
302 GType vik_dem_layer_get_type ()
304 static GType vdl_type = 0;
308 static const GTypeInfo vdl_info =
310 sizeof (VikDEMLayerClass),
311 NULL, /* base_init */
312 NULL, /* base_finalize */
313 NULL, /* class init */
314 NULL, /* class_finalize */
315 NULL, /* class_data */
316 sizeof (VikDEMLayer),
318 NULL /* instance init */
320 vdl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikDEMLayer", &vdl_info, 0 );
326 static void dem_layer_marshall( VikDEMLayer *vdl, guint8 **data, gint *len )
328 vik_layer_marshall_params ( VIK_LAYER(vdl), data, len );
331 static VikDEMLayer *dem_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
333 VikDEMLayer *rv = vik_dem_layer_new ();
336 /* TODO: share GCS between layers */
337 for ( i = 0; i < DEM_N_COLORS; i++ )
338 rv->gcs[i] = vik_viewport_new_gc ( vvp, dem_colors[i], rv->line_thickness );
340 vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp );
344 gboolean dem_layer_set_param ( VikDEMLayer *vdl, guint16 id, VikLayerParamData data, VikViewport *vp )
348 case PARAM_COLOR: if ( vdl->color ) g_free ( vdl->color ); vdl->color = g_strdup ( data.s ); break;
349 case PARAM_SOURCE: vdl->source = data.u; break;
350 case PARAM_MIN_ELEV: vdl->min_elev = data.d; break;
351 case PARAM_MAX_ELEV: vdl->max_elev = data.d; break;
352 case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vdl->line_thickness = data.u; break;
353 case PARAM_FILES: a_dems_load_list ( &(data.sl) ); a_dems_list_free ( vdl->files ); vdl->files = data.sl; break;
358 static VikLayerParamData dem_layer_get_param ( VikDEMLayer *vdl, guint16 id )
360 VikLayerParamData rv;
363 case PARAM_FILES: rv.sl = vdl->files; break;
364 case PARAM_SOURCE: rv.u = vdl->source; break;
365 case PARAM_COLOR: rv.s = vdl->color ? vdl->color : ""; break;
366 case PARAM_MIN_ELEV: rv.d = vdl->min_elev; break;
367 case PARAM_MAX_ELEV: rv.d = vdl->max_elev; break;
368 case PARAM_LINE_THICKNESS: rv.i = vdl->line_thickness; break;
373 static void dem_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file )
375 VikDEMLayer *vdl = VIK_DEM_LAYER(vl);
377 g_object_unref ( G_OBJECT(vdl->gc) );
379 vdl->gc = vik_viewport_new_gc ( vp, vdl->color, vdl->line_thickness );
382 VikDEMLayer *vik_dem_layer_new ( )
384 VikDEMLayer *vdl = VIK_DEM_LAYER ( g_object_new ( VIK_DEM_LAYER_TYPE, NULL ) );
386 vik_layer_init ( VIK_LAYER(vdl), VIK_LAYER_DEM );
393 vdl->gcs = g_malloc(sizeof(GdkGC *)*DEM_N_COLORS);
394 /* make new gcs only if we need it (copy layer -> use old) */
397 vdl->max_elev = 1000.0;
398 vdl->source = DEM_SOURCE_SRTM;
399 vdl->line_thickness = 3;
406 static void vik_dem_layer_draw_dem ( VikDEMLayer *vdl, VikViewport *vp, VikDEM *dem )
408 VikDEMColumn *column;
410 struct LatLon dem_northeast, dem_southwest;
411 gdouble max_lat, max_lon, min_lat, min_lon;
413 /**** Check if viewport and DEM data overlap ****/
415 /* get min, max lat/lon of viewport */
416 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
418 /* get min, max lat/lon of DEM data */
419 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
420 dem_northeast.lat = dem->max_north / 3600.0;
421 dem_northeast.lon = dem->max_east / 3600.0;
422 dem_southwest.lat = dem->min_north / 3600.0;
423 dem_southwest.lon = dem->min_east / 3600.0;
424 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
425 struct UTM dem_northeast_utm, dem_southwest_utm;
426 dem_northeast_utm.northing = dem->max_north;
427 dem_northeast_utm.easting = dem->max_east;
428 dem_southwest_utm.northing = dem->min_north;
429 dem_southwest_utm.easting = dem->min_east;
430 dem_northeast_utm.zone = dem_southwest_utm.zone = dem->utm_zone;
431 dem_northeast_utm.letter = dem_southwest_utm.letter = dem->utm_letter;
433 a_coords_utm_to_latlon(&dem_northeast_utm, &dem_northeast);
434 a_coords_utm_to_latlon(&dem_southwest_utm, &dem_southwest);
437 if ( (max_lat > dem_northeast.lat && min_lat > dem_northeast.lat) ||
438 (max_lat < dem_southwest.lat && min_lat < dem_southwest.lat) )
440 else if ( (max_lon > dem_northeast.lon && min_lon > dem_northeast.lon) ||
441 (max_lon < dem_southwest.lon && min_lon < dem_southwest.lon) )
443 /* else they overlap */
445 /**** End Overlap Check ****/
446 /* boxes to show where we have DEM instead of actually drawing the DEM.
447 * useful if we want to see what areas we have coverage for (if we want
448 * to get elevation data for a track) but don't want to cover the map.
452 /* draw a box if a DEM is loaded. in future I'd like to add an option for this
453 * this is useful if we want to see what areas we have dem for but don't want to
454 * cover the map (or maybe we just need translucent DEM?) */
456 VikCoord demne, demsw;
458 vik_coord_load_from_latlon(&demne, vik_viewport_get_coord_mode(vp), &dem_northeast);
459 vik_coord_load_from_latlon(&demsw, vik_viewport_get_coord_mode(vp), &dem_southwest);
461 vik_viewport_coord_to_screen ( vp, &demne, &x1, &y1 );
462 vik_viewport_coord_to_screen ( vp, &demsw, &x2, &y2 );
464 if ( x1 > vik_viewport_get_width(vp) ) x1=vik_viewport_get_width(vp);
465 if ( y2 > vik_viewport_get_height(vp) ) y2=vik_viewport_get_height(vp);
466 if ( x2 < 0 ) x2 = 0;
467 if ( y1 < 0 ) y1 = 0;
468 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
469 FALSE, x2, y1, x1-x2, y2-y1 );
474 if ( dem->horiz_units == VIK_DEM_HORIZ_LL_ARCSECONDS ) {
475 VikCoord tmp; /* TODO: don't use coord_load_from_latlon, especially if in latlon drawing mode */
477 gdouble max_lat_as, max_lon_as, min_lat_as, min_lon_as;
478 gdouble start_lat_as, end_lat_as, start_lon_as, end_lon_as;
480 gdouble start_lat, end_lat, start_lon, end_lon;
482 struct LatLon counter;
484 guint x, y, start_x, start_y;
488 guint skip_factor = ceil ( vik_viewport_get_xmpp(vp) / 40 ); /* todo: smarter calculation. */
490 gdouble nscale_deg = dem->north_scale / ((gdouble) 3600);
491 gdouble escale_deg = dem->east_scale / ((gdouble) 3600);
493 max_lat_as = max_lat * 3600;
494 min_lat_as = min_lat * 3600;
495 max_lon_as = max_lon * 3600;
496 min_lon_as = min_lon * 3600;
498 start_lat_as = MAX(min_lat_as, dem->min_north);
499 end_lat_as = MIN(max_lat_as, dem->max_north);
500 start_lon_as = MAX(min_lon_as, dem->min_east);
501 end_lon_as = MIN(max_lon_as, dem->max_east);
503 start_lat = floor(start_lat_as / dem->north_scale) * nscale_deg;
504 end_lat = ceil (end_lat_as / dem->north_scale) * nscale_deg;
505 start_lon = floor(start_lon_as / dem->east_scale) * escale_deg;
506 end_lon = ceil (end_lon_as / dem->east_scale) * escale_deg;
508 vik_dem_east_north_to_xy ( dem, start_lon_as, start_lat_as, &start_x, &start_y );
510 /* verify sane elev interval */
511 if ( vdl->max_elev <= vdl->min_elev )
512 vdl->max_elev = vdl->min_elev + 1;
514 for ( x=start_x, counter.lon = start_lon; counter.lon <= end_lon; counter.lon += escale_deg * skip_factor, x += skip_factor ) {
515 if ( x > 0 && x < dem->n_columns ) {
516 column = g_ptr_array_index ( dem->columns, x );
517 for ( y=start_y, counter.lat = start_lat; counter.lat <= end_lat; counter.lat += nscale_deg * skip_factor, y += skip_factor ) {
518 if ( y > column->n_points )
520 elev = column->points[y];
522 if ( elev != VIK_DEM_INVALID_ELEVATION && elev < vdl->min_elev )
524 if ( elev != VIK_DEM_INVALID_ELEVATION && elev > vdl->max_elev )
531 vik_coord_load_from_latlon(&tmp, vik_viewport_get_coord_mode(vp), &counter);
532 vik_viewport_coord_to_screen(vp, &tmp, &a, &b);
533 if ( elev == VIK_DEM_INVALID_ELEVATION )
534 ; /* don't draw it */
535 else if ( elev <= 0 )
536 vik_viewport_draw_rectangle(vp, vdl->gcs[0], TRUE, a-2, b-2, 4, 4 );
538 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 );
543 } else if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
544 gdouble max_nor, max_eas, min_nor, min_eas;
545 gdouble start_nor, start_eas, end_nor, end_eas;
549 guint x, y, start_x, start_y;
551 VikCoord tmp; /* TODO: don't use coord_load_from_latlon, especially if in latlon drawing mode */
554 guint skip_factor = ceil ( vik_viewport_get_xmpp(vp) / 10 ); /* todo: smarter calculation. */
556 VikCoord tleft, tright, bleft, bright;
558 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
559 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
560 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
561 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &bright );
564 vik_coord_convert(&tleft, VIK_COORD_UTM);
565 vik_coord_convert(&tright, VIK_COORD_UTM);
566 vik_coord_convert(&bleft, VIK_COORD_UTM);
567 vik_coord_convert(&bright, VIK_COORD_UTM);
569 max_nor = MAX(tleft.north_south, tright.north_south);
570 min_nor = MIN(bleft.north_south, bright.north_south);
571 max_eas = MAX(bright.east_west, tright.east_west);
572 min_eas = MIN(bleft.east_west, tleft.east_west);
574 start_nor = MAX(min_nor, dem->min_north);
575 end_nor = MIN(max_nor, dem->max_north);
576 if ( tleft.utm_zone == dem->utm_zone && bleft.utm_zone == dem->utm_zone
577 && (tleft.utm_letter >= 'N') == (dem->utm_letter >= 'N')
578 && (bleft.utm_letter >= 'N') == (dem->utm_letter >= 'N') ) /* if the utm zones/hemispheres are different, min_eas will be bogus */
579 start_eas = MAX(min_eas, dem->min_east);
581 start_eas = dem->min_east;
582 if ( tright.utm_zone == dem->utm_zone && bright.utm_zone == dem->utm_zone
583 && (tright.utm_letter >= 'N') == (dem->utm_letter >= 'N')
584 && (bright.utm_letter >= 'N') == (dem->utm_letter >= 'N') ) /* if the utm zones/hemispheres are different, min_eas will be bogus */
585 end_eas = MIN(max_eas, dem->max_east);
587 end_eas = dem->max_east;
589 start_nor = floor(start_nor / dem->north_scale) * dem->north_scale;
590 end_nor = ceil (end_nor / dem->north_scale) * dem->north_scale;
591 start_eas = floor(start_eas / dem->east_scale) * dem->east_scale;
592 end_eas = ceil (end_eas / dem->east_scale) * dem->east_scale;
594 vik_dem_east_north_to_xy ( dem, start_eas, start_nor, &start_x, &start_y );
596 /* TODO: why start_x and start_y are -1 -- rounding error from above? */
598 counter.zone = dem->utm_zone;
599 counter.letter = dem->utm_letter;
601 for ( x=start_x, counter.easting = start_eas; counter.easting <= end_eas; counter.easting += dem->east_scale * skip_factor, x += skip_factor ) {
602 if ( x > 0 && x < dem->n_columns ) {
603 column = g_ptr_array_index ( dem->columns, x );
604 for ( y=start_y, counter.northing = start_nor; counter.northing <= end_nor; counter.northing += dem->north_scale * skip_factor, y += skip_factor ) {
605 if ( y > column->n_points )
607 elev = column->points[y];
608 if ( elev != VIK_DEM_INVALID_ELEVATION && elev < vdl->min_elev )
610 if ( elev != VIK_DEM_INVALID_ELEVATION && elev > vdl->max_elev )
615 vik_coord_load_from_utm(&tmp, vik_viewport_get_coord_mode(vp), &counter);
616 vik_viewport_coord_to_screen(vp, &tmp, &a, &b);
617 if ( elev == VIK_DEM_INVALID_ELEVATION )
618 ; /* don't draw it */
619 else if ( elev <= 0 )
620 vik_viewport_draw_rectangle(vp, vdl->gcs[0], TRUE, a-2, b-2, 4, 4 );
622 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 );
630 /* return the continent for the specified lat, lon */
632 static const gchar *srtm_continent_dir ( gint lat, gint lon )
634 extern const char *_srtm_continent_data[];
635 static GHashTable *srtm_continent = NULL;
636 const gchar *continent;
639 if (!srtm_continent) {
642 srtm_continent = g_hash_table_new(g_str_hash, g_str_equal);
643 s = _srtm_continent_data;
644 while (*s != (gchar *)-1) {
647 g_hash_table_insert(srtm_continent, (gpointer) *s, (gpointer) continent);
653 g_snprintf(name, sizeof(name), "%c%02d%c%03d",
654 (lat >= 0) ? 'N' : 'S', ABS(lat),
655 (lon >= 0) ? 'E' : 'W', ABS(lon));
657 return(g_hash_table_lookup(srtm_continent, name));
660 void vik_dem_layer_draw ( VikDEMLayer *vdl, gpointer data )
662 VikViewport *vp = (VikViewport *) data;
663 GList *dems_iter = vdl->files;
667 /* search for SRTM3 90m */
669 if ( vdl->source == DEM_SOURCE_SRTM )
670 srtm_draw_existence ( vp );
671 #ifdef VIK_CONFIG_DEM24K
672 else if ( vdl->source == DEM_SOURCE_DEM24K )
673 dem24k_draw_existence ( vp );
676 while ( dems_iter ) {
677 dem = a_dems_get ( (const char *) (dems_iter->data) );
679 vik_dem_layer_draw_dem ( vdl, vp, dem );
680 dems_iter = dems_iter->next;
684 void vik_dem_layer_free ( VikDEMLayer *vdl )
687 if ( vdl->gc != NULL )
688 g_object_unref ( G_OBJECT(vdl->gc) );
690 if ( vdl->color != NULL )
691 g_free ( vdl->color );
694 for ( i = 0; i < DEM_N_COLORS; i++ )
695 g_object_unref ( vdl->gcs[i] );
698 a_dems_list_free ( vdl->files );
701 static void dem_layer_update_gc ( VikDEMLayer *vdl, VikViewport *vp, const gchar *color )
704 g_free ( vdl->color );
706 vdl->color = g_strdup ( color );
709 g_object_unref ( G_OBJECT(vdl->gc) );
711 vdl->gc = vik_viewport_new_gc ( vp, vdl->color, vdl->line_thickness );
714 VikDEMLayer *vik_dem_layer_create ( VikViewport *vp )
716 VikDEMLayer *vdl = vik_dem_layer_new ();
719 /* TODO: share GCS between layers */
720 for ( i = 0; i < DEM_N_COLORS; i++ )
721 vdl->gcs[i] = vik_viewport_new_gc ( vp, dem_colors[i], vdl->line_thickness );
723 dem_layer_update_gc ( vdl, vp, "red" );
726 /**************************************************************
727 **** SOURCES & DOWNLOADING
728 **************************************************************/
734 VikDEMLayer *vdl; /* NULL if not alive */
740 /**************************************************
742 **************************************************/
744 static void srtm_dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
747 const gchar *continent_dir;
749 intlat = (int)floor(p->lat);
750 intlon = (int)floor(p->lon);
751 continent_dir = srtm_continent_dir(intlat, intlon);
753 if (!continent_dir) {
754 g_warning(N_("No SRTM data available for %f, %f"), p->lat, p->lon);
758 gchar *src_fn = g_strdup_printf("%s%s%s%c%02d%c%03d.hgt.zip",
762 (intlat >= 0) ? 'N' : 'S',
764 (intlon >= 0) ? 'E' : 'W',
767 static DownloadOptions options = { NULL, 0, a_check_map_file };
768 a_ftp_download_get_url ( SRTM_FTP_SITE, src_fn, p->dest, &options );
772 static gchar *srtm_lat_lon_to_dest_fn ( gdouble lat, gdouble lon )
775 const gchar *continent_dir;
777 intlat = (int)floor(lat);
778 intlon = (int)floor(lon);
779 continent_dir = srtm_continent_dir(intlat, intlon);
782 continent_dir = "nowhere";
784 return g_strdup_printf("srtm3-%s%s%c%02d%c%03d.hgt.zip",
787 (intlat >= 0) ? 'N' : 'S',
789 (intlon >= 0) ? 'E' : 'W',
794 /* TODO: generalize */
795 static void srtm_draw_existence ( VikViewport *vp )
797 gdouble max_lat, max_lon, min_lat, min_lon;
798 gchar buf[strlen(MAPS_CACHE_DIR)+strlen(SRTM_CACHE_TEMPLATE)+30];
801 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
803 for (i = floor(min_lat); i <= floor(max_lat); i++) {
804 for (j = floor(min_lon); j <= floor(max_lon); j++) {
805 const gchar *continent_dir;
806 if ((continent_dir = srtm_continent_dir(i, j)) == NULL)
808 g_snprintf(buf, sizeof(buf), SRTM_CACHE_TEMPLATE,
812 (i >= 0) ? 'N' : 'S',
814 (j >= 0) ? 'E' : 'W',
816 if ( g_file_test(buf, G_FILE_TEST_EXISTS ) == TRUE ) {
821 sw.mode = VIK_COORD_LATLON;
822 ne.north_south = i+1;
824 ne.mode = VIK_COORD_LATLON;
825 vik_viewport_coord_to_screen ( vp, &sw, &x1, &y1 );
826 vik_viewport_coord_to_screen ( vp, &ne, &x2, &y2 );
827 if ( x1 < 0 ) x1 = 0;
828 if ( y2 < 0 ) y2 = 0;
829 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
830 FALSE, x1, y2, x2-x1, y1-y2 );
837 /**************************************************
839 **************************************************/
841 #ifdef VIK_CONFIG_DEM24K
843 static void dem24k_dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
846 gchar *cmdline = g_strdup_printf("%s %.03f %.03f",
847 DEM24K_DOWNLOAD_SCRIPT,
850 /* FIX: don't use system, use execv or something. check for existence */
854 static gchar *dem24k_lat_lon_to_dest_fn ( gdouble lat, gdouble lon )
856 return g_strdup_printf("dem24k/%d/%d/%.03f,%.03f.dem",
863 /* TODO: generalize */
864 static void dem24k_draw_existence ( VikViewport *vp )
866 gdouble max_lat, max_lon, min_lat, min_lon;
867 gchar buf[strlen(MAPS_CACHE_DIR)+40];
870 vik_viewport_get_min_max_lat_lon ( vp, &min_lat, &max_lat, &min_lon, &max_lon );
872 for (i = floor(min_lat*8)/8; i <= floor(max_lat*8)/8; i+=0.125) {
873 /* check lat dir first -- faster */
874 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/",
877 if ( g_file_test(buf, G_FILE_TEST_EXISTS) == FALSE )
879 for (j = floor(min_lon*8)/8; j <= floor(max_lon*8)/8; j+=0.125) {
880 /* check lon dir first -- faster */
881 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/%d/",
885 if ( g_file_test(buf, G_FILE_TEST_EXISTS) == FALSE )
887 g_snprintf(buf, sizeof(buf), "%sdem24k/%d/%d/%.03f,%.03f.dem",
893 if ( g_file_test(buf, G_FILE_TEST_EXISTS ) == TRUE ) {
897 sw.east_west = j-0.125;
898 sw.mode = VIK_COORD_LATLON;
899 ne.north_south = i+0.125;
901 ne.mode = VIK_COORD_LATLON;
902 vik_viewport_coord_to_screen ( vp, &sw, &x1, &y1 );
903 vik_viewport_coord_to_screen ( vp, &ne, &x2, &y2 );
904 if ( x1 < 0 ) x1 = 0;
905 if ( y2 < 0 ) y2 = 0;
906 vik_viewport_draw_rectangle ( vp, GTK_WIDGET(vp)->style->black_gc,
907 FALSE, x1, y2, x2-x1, y1-y2 );
914 /**************************************************
915 * SOURCES -- DOWNLOADING & IMPORTING TOOL *
916 **************************************************
919 static void weak_ref_cb ( gpointer ptr, GObject * dead_vdl )
921 DEMDownloadParams *p = ptr;
922 g_mutex_lock ( p->mutex );
924 g_mutex_unlock ( p->mutex );
927 /* Try to add file full_path.
928 * full_path will be copied.
929 * returns FALSE if file does not exists, TRUE otherwise.
931 static gboolean dem_layer_add_file ( VikDEMLayer *vdl, const gchar *full_path )
933 if ( g_file_test(full_path, G_FILE_TEST_EXISTS ) == TRUE ) {
934 /* only load if file size is not 0 (not in progress) */
936 stat (full_path, &sb);
938 gchar *duped_path = g_strdup(full_path);
939 vdl->files = g_list_prepend ( vdl->files, duped_path );
940 a_dems_load ( duped_path );
941 g_debug("%s: %s", __FUNCTION__, duped_path);
942 vik_layer_emit_update ( VIK_LAYER(vdl) );
949 static void dem_download_thread ( DEMDownloadParams *p, gpointer threaddata )
951 if ( p->source == DEM_SOURCE_SRTM )
952 srtm_dem_download_thread ( p, threaddata );
953 #ifdef VIK_CONFIG_DEM24K
954 else if ( p->source == DEM_SOURCE_DEM24K )
955 dem24k_dem_download_thread ( p, threaddata );
959 g_mutex_lock ( p->mutex );
961 g_object_weak_unref ( G_OBJECT(p->vdl), weak_ref_cb, p );
963 if ( dem_layer_add_file ( p->vdl, p->dest ) )
964 vik_layer_emit_update ( VIK_LAYER(p->vdl) );
966 g_mutex_unlock ( p->mutex );
971 static void free_dem_download_params ( DEMDownloadParams *p )
973 g_mutex_free ( p->mutex );
978 static gpointer dem_layer_download_create ( VikWindow *vw, VikViewport *vvp)
984 static gboolean dem_layer_download_release ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp )
990 gchar *dem_file = NULL;
992 if ( vdl->source == DEM_SOURCE_NONE )
993 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vdl), _("No download source selected. Edit layer properties.") );
995 vik_viewport_screen_to_coord ( vvp, event->x, event->y, &coord );
996 vik_coord_to_latlon ( &coord, &ll );
999 if ( vdl->source == DEM_SOURCE_SRTM )
1000 dem_file = srtm_lat_lon_to_dest_fn ( ll.lat, ll.lon );
1001 #ifdef VIK_CONFIG_DEM24K
1002 else if ( vdl->source == DEM_SOURCE_DEM24K )
1003 dem_file = dem24k_lat_lon_to_dest_fn ( ll.lat, ll.lon );
1009 full_path = g_strdup_printf("%s%s", MAPS_CACHE_DIR, dem_file );
1011 g_debug("%s: %s", __FUNCTION__, full_path);
1013 // TODO: check if already in filelist
1015 if ( ! dem_layer_add_file(vdl, full_path) ) {
1016 gchar *tmp = g_strdup_printf ( _("Downloading DEM %s"), dem_file );
1017 DEMDownloadParams *p = g_malloc(sizeof(DEMDownloadParams));
1018 p->dest = g_strdup(full_path);
1022 p->mutex = g_mutex_new();
1023 p->source = vdl->source;
1024 g_object_weak_ref(G_OBJECT(p->vdl), weak_ref_cb, p );
1026 a_background_thread ( VIK_GTK_WINDOW_FROM_LAYER(vdl), tmp,
1027 (vik_thr_func) dem_download_thread, p,
1028 (vik_thr_free_func) free_dem_download_params, NULL, 1 );
1031 g_free ( dem_file );
1032 g_free ( full_path );
1037 static gboolean dem_layer_download_click ( VikDEMLayer *vdl, GdkEventButton *event, VikViewport *vvp )
1039 /* choose & keep track of cache dir
1040 * download in background thread
1041 * download over area */