From c12d43471d4932dabaa39249104c29f83b4181c0 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 27 Jan 2015 21:12:10 +0000 Subject: [PATCH] Allow starting Viking with command line parameters to set position, zoom and map type. --latitude --longitude --zoom --map e.g.: viking --latitude 51.4 --longitude -1.3 --zoom 12 --map 13 --- help/C/commandline.xml | 36 ++++++++++++++++++++++++++ help/C/viking.xml | 2 +- help/viking.xml | 52 +++++++++++++++++++++++++++++++++++++ src/main.c | 12 +++++++++ src/vikutils.c | 59 ++++++++++++++++++++++++++++++++++++++++++ src/vikutils.h | 2 ++ 6 files changed, 162 insertions(+), 1 deletion(-) diff --git a/help/C/commandline.xml b/help/C/commandline.xml index 2f1cdf2a..9fef9dca 100644 --- a/help/C/commandline.xml +++ b/help/C/commandline.xml @@ -57,10 +57,46 @@ This also enables some extra information features in the GUI itself, primarily o --version Show the version and then exit. + + N/A + --latitude + Set the initial position to the specified latitude in decimal degrees. + + + N/A + --longitude + Set the initial position to the specified longitude in decimal degrees. + + + -z + --zoom + Set the initial zoom level. The value is the OSM zoom level (0 - 22). + + + -m + --map + Add a map layer by specifying the map id. The value needs to match one of the internal ids or an id from the . + Specifying a value of 0 will use the configured map layer default. + + + + Internal Map Ids: + OSM Mapnik = 13 + OSM Cycle = 17 + Mapquest OSM = 19 + + + + +An example to open at a specified location with an OSM Mapnik map layer: +viking --latitude 51.4 --longitude -1.3 --zoom 12 --map 13 +If a file is also specified on the command line, the command line location and zoom parameters will take precendence. + + As a special combination when both -V and -d are both enabled at the same time, &appname; will not delete some of the temporary files created during the program run. diff --git a/help/C/viking.xml b/help/C/viking.xml index b72af780..29e86535 100644 --- a/help/C/viking.xml +++ b/help/C/viking.xml @@ -2891,7 +2891,7 @@ Accept: */* You can find more examples in the documentation part of the distribution. -
+
Map Source It is possible to add new map's sources. The file is ~/.viking/maps.xml for UNIX like systems, C:\Documents and Settings\username\.viking\maps.xml on Windows XP or C:\Users\username\.viking\maps.xml on Windows 7 onwards. An example of the file is in the distribution doc/examples/maps.xml. Further examples and values are online in the Maps Wiki diff --git a/help/viking.xml b/help/viking.xml index 7cc02634..4feb9018 100644 --- a/help/viking.xml +++ b/help/viking.xml @@ -91,6 +91,23 @@ and docbook-xsl in your Build-Depends control field. + + + degrees + + + degrees + + + + + ZoomLevelOSM + + + + MapId + + file @@ -165,6 +182,41 @@ and docbook-xsl in your Build-Depends control field. Show version. + + + + Set the initial position to the specified latitude in decimal degrees. + + + + + + Set the initial position to the specified longitude in decimal degrees. + + + + + + + Set the initial zoom level. The value is the OSM zoom level (0 - 22). + + + + + + + Add a map layer by specifying the map id. The value needs to match one of the internal ids or an id from the maps configuration extension (see below). + Specifying a value of 0 will use the configured map layer default. + + + Internal Map Ids: + OSM Mapnik = 13 + OSM Cycle = 17 + Mapquest OSM = 19 + + + + diff --git a/src/main.c b/src/main.c index c9c43eb3..84376f81 100644 --- a/src/main.c +++ b/src/main.c @@ -98,12 +98,22 @@ static int myXErrorHandler(Display *display, XErrorEvent *theEvent) } #endif +// Default values that won't actually get applied unless changed by command line parameter values +static gdouble latitude = 0.0; +static gdouble longitude = 0.0; +static gint zoom_level_osm = -1; +static gint map_id = -1; + /* Options */ static GOptionEntry entries[] = { { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL }, { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL }, { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL }, + { "latitude", 0, 0, G_OPTION_ARG_DOUBLE, &latitude, N_("Latitude in decimal degrees"), NULL }, + { "longitude", 0, 0, G_OPTION_ARG_DOUBLE, &longitude, N_("Longitude in decimal degrees"), NULL }, + { "zoom", 'z', 0, G_OPTION_ARG_INT, &zoom_level_osm, N_("Zoom Level (OSM). Value can be 0 - 22"), NULL }, + { "map", 'm', 0, G_OPTION_ARG_INT, &map_id, N_("Add a map layer by id value. Use 0 for the default map."), NULL }, { NULL } }; @@ -225,6 +235,8 @@ int main( int argc, char *argv[] ) vik_window_new_window_finish ( first_window ); + vu_command_line ( first_window, latitude, longitude, zoom_level_osm, map_id ); + gtk_main (); gdk_threads_leave (); diff --git a/src/vikutils.c b/src/vikutils.c index d4f56ecb..6c4aece7 100644 --- a/src/vikutils.c +++ b/src/vikutils.c @@ -769,3 +769,62 @@ gchar* vu_get_time_string ( time_t *time, const gchar *format, const VikCoord* v } return str; } + +/** + * vu_command_line: + * + * Apply any startup values that have been specified from the command line + * Values are defaulted in such a manner not to be applied when they haven't been specified + * + */ +void vu_command_line ( VikWindow *vw, gdouble latitude, gdouble longitude, gint zoom_osm_level, gint map_id ) +{ + if ( !vw ) + return; + + VikViewport *vvp = vik_window_viewport(vw); + + if ( latitude != 0.0 || longitude != 0.0 ) { + struct LatLon ll; + ll.lat = latitude; + ll.lon = longitude; + vik_viewport_set_center_latlon ( vvp, &ll, TRUE ); + } + + if ( zoom_osm_level >= 0 ) { + // Convert OSM zoom level into Viking zoom level + gdouble mpp = exp ( (17-zoom_osm_level) * log(2) ); + if ( mpp > 1.0 ) + mpp = round (mpp); + vik_viewport_set_zoom ( vvp, mpp ); + } + + if ( map_id >= 0 ) { + guint my_map_id = map_id; + if ( my_map_id == 0 ) + my_map_id = vik_maps_layer_get_default_map_type (); + + // Don't add map layer if one already exists + GList *vmls = vik_layers_panel_get_all_layers_of_type(vik_window_layers_panel(vw), VIK_LAYER_MAPS, TRUE); + int num_maps = g_list_length(vmls); + gboolean add_map = TRUE; + + for (int i = 0; i < num_maps; i++) { + VikMapsLayer *vml = (VikMapsLayer*)(vmls->data); + gint id = vik_maps_layer_get_map_type(vml); + if ( my_map_id == id ) { + add_map = FALSE; + break; + } + vmls = vmls->next; + } + + if ( add_map ) { + VikMapsLayer *vml = VIK_MAPS_LAYER ( vik_layer_create(VIK_LAYER_MAPS, vvp, FALSE) ); + vik_maps_layer_set_map_type ( vml, my_map_id ); + vik_layer_rename ( VIK_LAYER(vml), _("Map") ); + vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer(vik_window_layers_panel(vw)), VIK_LAYER(vml), TRUE ); + vik_layer_emit_update ( VIK_LAYER(vml) ); + } + } +} diff --git a/src/vikutils.h b/src/vikutils.h index 9fd3b7e0..d7994ebe 100644 --- a/src/vikutils.h +++ b/src/vikutils.h @@ -42,6 +42,8 @@ gchar* vu_get_tz_at_location ( const VikCoord* vc ); void vu_setup_lat_lon_tz_lookup (); void vu_finalize_lat_lon_tz_lookup (); +void vu_command_line ( VikWindow *vw, gdouble latitude, gdouble longitude, gint zoom_osm_level, gint map_id ); + G_END_DECLS #endif -- 2.39.5