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
25 #include <sys/types.h>
29 #include <glib/gi18n.h>
36 #include "background.h"
37 #include "vikwaypoint.h"
39 #include "vikviewport.h"
40 #include "viktreeview.h"
42 #include "vikaggregatelayer.h"
43 #include "viklayerspanel.h"
44 #include "vikdemlayer.h"
45 #include "vikdemlayer_pixmap.h"
46 #include "vikmapslayer.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"
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
);
77 static VikLayerParamScale param_scales
[] = {
83 static gchar
*params_source
[] = {
84 "SRTM Global 90m (3 arcsec)",
85 #ifdef VIK_CONFIG_DEM24K
92 enum { DEM_SOURCE_SRTM
,
93 #ifdef VIK_CONFIG_DEM24K
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
, GDK_CURSOR_IS_PIXMAP
, &cursor_demdl
},
125 static gchar
*dem_colors
[] = {
231 static const guint DEM_N_COLORS
= sizeof(dem_colors
)/sizeof(dem_colors
[0]);
234 VikLayerInterface vik_dem_layer_interface
= {
239 sizeof(dem_tools
) / sizeof(dem_tools
[0]),
248 (VikLayerFuncCreate
) vik_dem_layer_create
,
249 (VikLayerFuncRealize
) NULL
,
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
{
289 guint8 line_thickness
;
294 GType
vik_dem_layer_get_type ()
296 static GType vdl_type
= 0;
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 );
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 ();
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
);
336 gboolean
dem_layer_set_param ( VikDEMLayer
*vdl
, guint16 id
, VikLayerParamData data
, VikViewport
*vp
)
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;
350 static VikLayerParamData
dem_layer_get_param ( VikDEMLayer
*vdl
, guint16 id
)
352 VikLayerParamData rv
;
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;
365 static void dem_layer_post_read ( VikLayer
*vl
, VikViewport
*vp
, gboolean from_file
)
367 VikDEMLayer
*vdl
= VIK_DEM_LAYER(vl
);
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
);
385 vdl
->gcs
= g_malloc(sizeof(GdkGC
*)*DEM_N_COLORS
);
386 /* make new gcs only if we need it (copy layer -> use old) */
389 vdl
->max_elev
= 1000.0;
390 vdl
->source
= DEM_SOURCE_SRTM
;
391 vdl
->line_thickness
= 3;
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
) )
432 else if ( (max_lon
> dem_northeast
.lon
&& min_lon
> dem_northeast
.lon
) ||
433 (max_lon
< dem_southwest
.lon
&& min_lon
< dem_southwest
.lon
) )
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.
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
;
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
);
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
;
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
)
512 elev
= column
->points
[y
];
514 if ( elev
!= VIK_DEM_INVALID_ELEVATION
&& elev
< vdl
->min_elev
)
516 if ( elev
!= VIK_DEM_INVALID_ELEVATION
&& elev
> vdl
->max_elev
)
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 );
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 );
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
;
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 */
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
);
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
);
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
)
599 elev
= column
->points
[y
];
600 if ( elev
!= VIK_DEM_INVALID_ELEVATION
&& elev
< vdl
->min_elev
)
602 if ( elev
!= VIK_DEM_INVALID_ELEVATION
&& elev
> vdl
->max_elev
)
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 );
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 );
622 /* return the continent for the specified lat, lon */
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
;
631 if (!srtm_continent
) {
634 srtm_continent
= g_hash_table_new(g_str_hash
, g_str_equal
);
635 s
= _srtm_continent_data
;
636 while (*s
!= (gchar
*)-1) {
639 g_hash_table_insert(srtm_continent
, (gpointer
) *s
, (gpointer
) continent
);
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
;
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
);
668 while ( dems_iter
) {
669 dem
= a_dems_get ( (const char *) (dems_iter
->data
) );
671 vik_dem_layer_draw_dem ( vdl
, vp
, dem
);
672 dems_iter
= dems_iter
->next
;
676 void vik_dem_layer_free ( VikDEMLayer
*vdl
)
679 if ( vdl
->gc
!= NULL
)
680 g_object_unref ( G_OBJECT(vdl
->gc
) );
682 if ( vdl
->color
!= NULL
)
683 g_free ( vdl
->color
);
686 for ( i
= 0; i
< DEM_N_COLORS
; i
++ )
687 g_object_unref ( vdl
->gcs
[i
] );
690 a_dems_list_free ( vdl
->files
);
693 static void dem_layer_update_gc ( VikDEMLayer
*vdl
, VikViewport
*vp
, const gchar
*color
)
696 g_free ( vdl
->color
);
698 vdl
->color
= g_strdup ( color
);
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 ();
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" );
718 /**************************************************************
719 **** SOURCES & DOWNLOADING
720 **************************************************************/
726 VikDEMLayer
*vdl
; /* NULL if not alive */
732 /**************************************************
734 **************************************************/
736 static void srtm_dem_download_thread ( DEMDownloadParams
*p
, gpointer threaddata
)
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"), p
->lat
, p
->lon
);
750 gchar
*src_fn
= g_strdup_printf("%s%s%s%c%02d%c%03d.hgt.zip",
754 (intlat
>= 0) ? 'N' : 'S',
756 (intlon
>= 0) ? 'E' : 'W',
759 static DownloadOptions options
= { NULL
, 0, a_check_map_file
};
760 a_ftp_download_get_url ( SRTM_FTP_SITE
, src_fn
, p
->dest
, &options
);
764 static gchar
*srtm_lat_lon_to_dest_fn ( gdouble lat
, gdouble lon
)
767 const gchar
*continent_dir
;
769 intlat
= (int)floor(lat
);
770 intlon
= (int)floor(lon
);
771 continent_dir
= srtm_continent_dir(intlat
, intlon
);
774 continent_dir
= "nowhere";
776 return g_strdup_printf("srtm3-%s%s%c%02d%c%03d.hgt.zip",
779 (intlat
>= 0) ? 'N' : 'S',
781 (intlon
>= 0) ? 'E' : 'W',
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];
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
)
800 g_snprintf(buf
, sizeof(buf
), SRTM_CACHE_TEMPLATE
,
804 (i
>= 0) ? 'N' : 'S',
806 (j
>= 0) ? 'E' : 'W',
808 if ( g_file_test(buf
, G_FILE_TEST_EXISTS
) == TRUE
) {
813 sw
.mode
= VIK_COORD_LATLON
;
814 ne
.north_south
= i
+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 /**************************************************
831 **************************************************/
833 #ifdef VIK_CONFIG_DEM24K
835 static void dem24k_dem_download_thread ( DEMDownloadParams
*p
, gpointer threaddata
)
838 gchar
*cmdline
= g_strdup_printf("%s %.03f %.03f",
839 DEM24K_DOWNLOAD_SCRIPT
,
842 /* FIX: don't use system, use execv or something. check for existence */
846 static gchar
*dem24k_lat_lon_to_dest_fn ( gdouble lat
, gdouble lon
)
848 return g_strdup_printf("dem24k/%d/%d/%.03f,%.03f.dem",
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];
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/",
869 if ( g_file_test(buf
, G_FILE_TEST_EXISTS
) == FALSE
)
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/",
877 if ( g_file_test(buf
, G_FILE_TEST_EXISTS
) == FALSE
)
879 g_snprintf(buf
, sizeof(buf
), "%sdem24k/%d/%d/%.03f,%.03f.dem",
885 if ( g_file_test(buf
, G_FILE_TEST_EXISTS
) == TRUE
) {
889 sw
.east_west
= j
-0.125;
890 sw
.mode
= VIK_COORD_LATLON
;
891 ne
.north_south
= i
+0.125;
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
);
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
);
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 ( g_file_test(full_path
, G_FILE_TEST_EXISTS
) == TRUE
) {
926 /* only load if file size is not 0 (not in progress) */
928 stat (full_path
, &sb
);
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_debug("%s: %s", __FUNCTION__
, duped_path
);
934 vik_layer_emit_update ( VIK_LAYER(vdl
) );
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
);
951 g_mutex_lock ( p
->mutex
);
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
);
963 static void free_dem_download_params ( DEMDownloadParams
*p
)
965 g_mutex_free ( p
->mutex
);
970 static gpointer
dem_layer_download_create ( VikWindow
*vw
, VikViewport
*vvp
)
976 static gboolean
dem_layer_download_release ( VikDEMLayer
*vdl
, GdkEventButton
*event
, VikViewport
*vvp
)
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
);
1001 full_path
= g_strdup_printf("%s%s", MAPS_CACHE_DIR
, dem_file
);
1003 g_debug("%s: %s", __FUNCTION__
, 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
);
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
);
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 */