1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Format 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 Format Public License for more details.
17 * You should have received a copy of the GNU Format 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
27 #include "vikwebtoolformat.h"
36 static GObjectClass *parent_class;
38 static void webtool_format_finalize ( GObject *gob );
40 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp );
41 static gchar *webtool_format_get_url ( VikWebtool *vw, VikWindow *vwindow );
43 typedef struct _VikWebtoolFormatPrivate VikWebtoolFormatPrivate;
45 struct _VikWebtoolFormatPrivate
48 gchar *url_format_code;
51 #define WEBTOOL_FORMAT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
52 VIK_WEBTOOL_FORMAT_TYPE, \
53 VikWebtoolFormatPrivate))
55 G_DEFINE_TYPE (VikWebtoolFormat, vik_webtool_format, VIK_WEBTOOL_TYPE)
66 webtool_format_set_property (GObject *object,
71 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
72 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
74 switch (property_id) {
77 priv->url = g_value_dup_string (value);
78 g_debug ("VikWebtoolFormat.url: %s", priv->url);
80 case PROP_URL_FORMAT_CODE:
81 g_free ( priv->url_format_code );
82 priv->url_format_code = g_value_dup_string ( value );
83 g_debug ( "VikWebtoolFormat.url_format_code: %s", priv->url_format_code );
87 /* We don't have any other property... */
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 webtool_format_get_property (GObject *object,
99 VikWebtoolFormat *self = VIK_WEBTOOL_FORMAT (object);
100 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
102 switch (property_id) {
104 g_value_set_string (value, priv->url);
107 case PROP_URL_FORMAT_CODE:
108 g_value_set_string ( value, priv->url_format_code );
112 /* We don't have any other property... */
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 vik_webtool_format_class_init ( VikWebtoolFormatClass *klass )
121 GObjectClass *gobject_class;
122 VikWebtoolClass *base_class;
125 gobject_class = G_OBJECT_CLASS (klass);
127 gobject_class->finalize = webtool_format_finalize;
128 gobject_class->set_property = webtool_format_set_property;
129 gobject_class->get_property = webtool_format_get_property;
131 pspec = g_param_spec_string ("url",
133 "Set the template url",
134 VIKING_URL /* default value */,
135 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
136 g_object_class_install_property (gobject_class,
140 pspec = g_param_spec_string ("url_format_code",
141 "Template URL Format Code",
142 "Set the template URL format code",
143 "AOZ", // default value Lat, Long, Zoom
144 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
145 g_object_class_install_property (gobject_class,
146 PROP_URL_FORMAT_CODE,
149 parent_class = g_type_class_peek_parent (klass);
151 base_class = VIK_WEBTOOL_CLASS ( klass );
152 base_class->get_url = webtool_format_get_url;
154 klass->mpp_to_zoom = webtool_format_mpp_to_zoom;
156 g_type_class_add_private (klass, sizeof (VikWebtoolFormatPrivate));
159 VikWebtoolFormat *vik_webtool_format_new ()
161 return VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE, NULL ) );
164 VikWebtoolFormat *vik_webtool_format_new_with_members ( const gchar *label,
166 const gchar *url_format_code )
168 VikWebtoolFormat *result = VIK_WEBTOOL_FORMAT ( g_object_new ( VIK_WEBTOOL_FORMAT_TYPE,
171 "url_format_code", url_format_code,
178 vik_webtool_format_init ( VikWebtoolFormat *self )
180 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
182 priv->url_format_code = NULL;
185 static void webtool_format_finalize ( GObject *gob )
187 VikWebtoolFormatPrivate *priv = WEBTOOL_FORMAT_GET_PRIVATE ( gob );
188 g_free ( priv->url ); priv->url = NULL;
189 g_free ( priv->url_format_code ); priv->url_format_code = NULL;
190 G_OBJECT_CLASS(parent_class)->finalize(gob);
193 static guint8 webtool_format_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
194 return map_utils_mpp_to_zoom_level ( mpp );
197 #define MAX_NUMBER_CODES 7
199 static gchar *webtool_format_get_url ( VikWebtool *self, VikWindow *vw )
201 VikWebtoolFormatPrivate *priv = NULL;
202 priv = WEBTOOL_FORMAT_GET_PRIVATE (self);
204 VikViewport *viewport = vik_window_viewport ( vw );
206 // Get top left and bottom right lat/lon pairs from the viewport
207 gdouble min_lat, max_lat, min_lon, max_lon;
208 gchar sminlon[G_ASCII_DTOSTR_BUF_SIZE];
209 gchar smaxlon[G_ASCII_DTOSTR_BUF_SIZE];
210 gchar sminlat[G_ASCII_DTOSTR_BUF_SIZE];
211 gchar smaxlat[G_ASCII_DTOSTR_BUF_SIZE];
212 vik_viewport_get_min_max_lat_lon ( viewport, &min_lat, &max_lat, &min_lon, &max_lon );
214 // Cannot simply use g_strdup_printf and gdouble due to locale.
215 // As we compute an URL, we have to think in C locale.
216 g_ascii_dtostr (sminlon, G_ASCII_DTOSTR_BUF_SIZE, min_lon);
217 g_ascii_dtostr (smaxlon, G_ASCII_DTOSTR_BUF_SIZE, max_lon);
218 g_ascii_dtostr (sminlat, G_ASCII_DTOSTR_BUF_SIZE, min_lat);
219 g_ascii_dtostr (smaxlat, G_ASCII_DTOSTR_BUF_SIZE, max_lat);
222 const VikCoord *coord = vik_viewport_get_center ( viewport );
224 vik_coord_to_latlon ( coord, &ll );
226 gchar scenterlat[G_ASCII_DTOSTR_BUF_SIZE];
227 gchar scenterlon[G_ASCII_DTOSTR_BUF_SIZE];
228 g_ascii_dtostr (scenterlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
229 g_ascii_dtostr (scenterlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
231 guint8 zoom = 17; // A zoomed in default
232 // zoom - ideally x & y factors need to be the same otherwise use the default
233 if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
234 zoom = map_utils_mpp_to_zoom_level ( vik_viewport_get_zoom ( viewport ) );
236 gchar szoom[G_ASCII_DTOSTR_BUF_SIZE];
237 g_snprintf ( szoom, G_ASCII_DTOSTR_BUF_SIZE, "%d", zoom );
240 if ( priv->url_format_code )
241 len = strlen ( priv->url_format_code );
242 if ( len > MAX_NUMBER_CODES )
243 len = MAX_NUMBER_CODES;
245 gchar* values[MAX_NUMBER_CODES];
247 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
251 for ( i = 0; i < len; i++ ) {
252 switch ( g_ascii_toupper ( priv->url_format_code[i] ) ) {
253 case 'L': values[i] = g_strdup ( sminlon ); break;
254 case 'R': values[i] = g_strdup ( smaxlon ); break;
255 case 'B': values[i] = g_strdup ( sminlat ); break;
256 case 'T': values[i] = g_strdup ( smaxlat ); break;
257 case 'A': values[i] = g_strdup ( scenterlat ); break;
258 case 'O': values[i] = g_strdup ( scenterlon ); break;
259 case 'Z': values[i] = g_strdup ( szoom ); break;
264 gchar *url = g_strdup_printf ( priv->url, values[0], values[1], values[2], values[3], values[4], values[5], values[6] );
266 for ( i = 0; i < MAX_NUMBER_CODES; i++ ) {
267 if ( values[i] != '\0' )
268 g_free ( values[i] );
271 g_debug ("%s %s", __FUNCTION__, url);
275 guint8 vik_webtool_format_mpp_to_zoom (VikWebtool *self, gdouble mpp)
277 return VIK_WEBTOOL_FORMAT_GET_CLASS( self )->mpp_to_zoom( self, mpp );