]> git.street.me.uk Git - andy/viking.git/blame - src/geonamessearch.c
Use the last selected date when initializing the date search.
[andy/viking.git] / src / geonamessearch.c
CommitLineData
93c47137
HR
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a889d671 4 * Copyright (C) 2009, Hein Ragas
2b19a255 5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
93c47137
HR
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
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 General Public License for more details.
16 *
17 * You should have received a copy of the GNU General 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
20 *
93c47137
HR
21 */
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <glib.h>
29#include <glib/gstdio.h>
30#include <glib/gprintf.h>
31#include <glib/gi18n.h>
32
33#include "viking.h"
ba4a5e11 34#include "util.h"
317aa38d 35#include "geonamessearch.h"
93c47137 36
316945d8
GB
37/* Compatibility */
38#if ! GLIB_CHECK_VERSION(2,22,0)
39#define g_mapped_file_unref g_mapped_file_free
40#endif
41
ba1fd74e
RN
42/**
43 * See http://www.geonames.org/export/wikipedia-webservice.html#wikipediaBoundingBox
44 */
45// Translators may wish to change this setting as appropriate to get Wikipedia articles in that language
46#define GEONAMES_LANG N_("en")
9e4edda0
RN
47// TODO - offer configuration of this value somewhere
48// ATM decided it's not essential enough to warrant putting in the preferences
49#define GEONAMES_MAX_ENTRIES 20
ba1fd74e 50
9e4edda0 51#define GEONAMES_WIKIPEDIA_URL_FMT "http://ws.geonames.org/wikipediaBoundingBoxJSON?formatted=true&north=%s&south=%s&east=%s&west=%s&lang=%s&maxRows=%d"
ba1fd74e 52
2b19a255 53#define GEONAMES_FEATURE_PATTERN "\"feature\": \""
09a77f0d
HR
54#define GEONAMES_LONGITUDE_PATTERN "\"lng\": "
55#define GEONAMES_NAME_PATTERN "\"name\": \""
56#define GEONAMES_LATITUDE_PATTERN "\"lat\": "
396ac020 57#define GEONAMES_ELEVATION_PATTERN "\"elevation\": "
165a4fa9
HR
58#define GEONAMES_TITLE_PATTERN "\"title\": \""
59#define GEONAMES_WIKIPEDIAURL_PATTERN "\"wikipediaUrl\": \""
60#define GEONAMES_THUMBNAILIMG_PATTERN "\"thumbnailImg\": \""
93c47137
HR
61#define GEONAMES_SEARCH_NOT_FOUND "not understand the location"
62
165a4fa9
HR
63/* found_geoname: Type to contain data returned from GeoNames.org */
64
09a77f0d
HR
65typedef struct {
66 gchar *name;
2b19a255 67 gchar *feature;
09a77f0d 68 struct LatLon ll;
396ac020 69 gdouble elevation;
af98fde6 70 gchar *cmt;
165a4fa9 71 gchar *desc;
09a77f0d
HR
72} found_geoname;
73
9060b2f7 74static found_geoname *new_found_geoname()
165a4fa9
HR
75{
76 found_geoname *ret;
77
78 ret = (found_geoname *)g_malloc(sizeof(found_geoname));
79 ret->name = NULL;
2b19a255 80 ret->feature = NULL;
af98fde6 81 ret->cmt = NULL;
165a4fa9
HR
82 ret->desc = NULL;
83 ret->ll.lat = 0.0;
84 ret->ll.lon = 0.0;
396ac020
RN
85 ret->elevation = VIK_DEFAULT_ALTITUDE;
86 return ret;
165a4fa9
HR
87}
88
9060b2f7 89static found_geoname *copy_found_geoname(found_geoname *src)
09a77f0d 90{
165a4fa9 91 found_geoname *dest = new_found_geoname();
09a77f0d 92 dest->name = g_strdup(src->name);
2b19a255 93 dest->feature = g_strdup(src->feature);
09a77f0d
HR
94 dest->ll.lat = src->ll.lat;
95 dest->ll.lon = src->ll.lon;
396ac020 96 dest->elevation = src->elevation;
af98fde6 97 dest->cmt = g_strdup(src->cmt);
165a4fa9 98 dest->desc = g_strdup(src->desc);
09a77f0d
HR
99 return(dest);
100}
101
165a4fa9
HR
102static void free_list_geonames(found_geoname *geoname, gpointer userdata)
103{
104 g_free(geoname->name);
2b19a255 105 g_free(geoname->feature);
af98fde6 106 g_free(geoname->cmt);
165a4fa9
HR
107 g_free(geoname->desc);
108}
109
9060b2f7 110static void free_geoname_list(GList *found_places)
165a4fa9
HR
111{
112 g_list_foreach(found_places, (GFunc)free_list_geonames, NULL);
113 g_list_free(found_places);
114}
115
165a4fa9
HR
116static void none_found(VikWindow *vw)
117{
118 GtkWidget *dialog = NULL;
119
120 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
121 gtk_window_set_title(GTK_WINDOW(dialog), _("Search"));
122
123 GtkWidget *search_label = gtk_label_new(_("No entries found!"));
9b082b39 124 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), search_label, FALSE, FALSE, 5 );
3a256c3c 125 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
165a4fa9
HR
126 gtk_widget_show_all(dialog);
127
128 gtk_dialog_run ( GTK_DIALOG(dialog) );
129 gtk_widget_destroy(dialog);
130}
131
9060b2f7 132static GList *a_select_geoname_from_list(GtkWindow *parent, GList *geonames, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg)
09a77f0d
HR
133{
134 GtkTreeIter iter;
135 GtkCellRenderer *renderer;
09a77f0d
HR
136 GtkWidget *view;
137 found_geoname *geoname;
138 gchar *latlon_string;
139 int column_runner;
140
141 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
142 parent,
143 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
144 GTK_STOCK_CANCEL,
145 GTK_RESPONSE_REJECT,
146 GTK_STOCK_OK,
147 GTK_RESPONSE_ACCEPT,
148 NULL);
3a256c3c
RN
149 /* When something is selected then OK */
150 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
151 GtkWidget *response_w = NULL;
152#if GTK_CHECK_VERSION (2, 20, 0)
153 /* Default to not apply - as initially nothing is selected! */
154 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
155#endif
09a77f0d 156 GtkWidget *label = gtk_label_new ( msg );
9f82a695
RN
157 GtkTreeStore *store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
158
09a77f0d
HR
159 GList *geoname_runner = geonames;
160 while (geoname_runner)
161 {
162 geoname = (found_geoname *)geoname_runner->data;
163 latlon_string = g_strdup_printf("(%f,%f)", geoname->ll.lat, geoname->ll.lon);
164 gtk_tree_store_append(store, &iter, NULL);
9f82a695 165 gtk_tree_store_set(store, &iter, 0, geoname->name, 1, geoname->feature, 2, latlon_string, -1);
09a77f0d
HR
166 geoname_runner = g_list_next(geoname_runner);
167 g_free(latlon_string);
168 }
9f82a695 169
09a77f0d
HR
170 view = gtk_tree_view_new();
171 renderer = gtk_cell_renderer_text_new();
172 column_runner = 0;
9f82a695
RN
173 GtkTreeViewColumn *column;
174 // NB could allow columns to be shifted around by doing this after each new
175 // gtk_tree_view_column_set_reorderable ( column, TRUE );
176 // However I don't think is that useful, so I haven't put it in
177 column = gtk_tree_view_column_new_with_attributes( _("Name"), renderer, "text", column_runner, NULL);
178 gtk_tree_view_column_set_sort_column_id (column, column_runner);
179 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
180
09a77f0d 181 column_runner++;
9f82a695
RN
182 column = gtk_tree_view_column_new_with_attributes( _("Feature"), renderer, "text", column_runner, NULL);
183 gtk_tree_view_column_set_sort_column_id (column, column_runner);
184 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
185
09a77f0d 186 column_runner++;
9f82a695
RN
187 column = gtk_tree_view_column_new_with_attributes( _("Lat/Lon"), renderer, "text", column_runner, NULL);
188 gtk_tree_view_column_set_sort_column_id (column, column_runner);
189 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
190
09a77f0d
HR
191 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
192 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
193 multiple_selection_allowed ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE );
194 g_object_unref(store);
195
9e4edda0
RN
196 GtkWidget *scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
197 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
198 gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
199
9b082b39
RN
200 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
201 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0);
9e4edda0
RN
202
203 // Ensure a reasonable number of items are shown, but let the width be automatically sized
204 gtk_widget_set_size_request ( dialog, -1, 400) ;
205 gtk_widget_show_all ( dialog );
206
3a256c3c
RN
207 if ( response_w )
208 gtk_widget_grab_focus ( response_w );
9e4edda0 209
09a77f0d
HR
210 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT )
211 {
212 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
213 GList *selected_geonames = NULL;
214
9f82a695
RN
215 // Possibily not the fastest method but we don't have thousands of entries to process...
216 if ( gtk_tree_model_get_iter_first( GTK_TREE_MODEL(store), &iter) ) {
217 do {
218 if ( gtk_tree_selection_iter_is_selected ( selection, &iter ) ) {
219 // For every selected item,
220 // compare the name from the displayed view to every geoname entry to find the geoname this selection represents
221 gchar* name;
222 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter, 0, &name, -1 );
223 // I believe the name of these items to be always unique
224 geoname_runner = geonames;
225 while ( geoname_runner ) {
226 if ( !strcmp ( ((found_geoname*)geoname_runner->data)->name, name ) ) {
227 found_geoname *copied = copy_found_geoname(geoname_runner->data);
228 selected_geonames = g_list_prepend(selected_geonames, copied);
229 break;
230 }
231 geoname_runner = g_list_next(geoname_runner);
232 }
644eea0e 233 g_free ( name );
09a77f0d
HR
234 }
235 }
9f82a695 236 while ( gtk_tree_model_iter_next ( GTK_TREE_MODEL(store), &iter ) );
09a77f0d 237 }
9f82a695 238
09a77f0d
HR
239 if (selected_geonames)
240 {
241 gtk_widget_destroy ( dialog );
9f82a695 242 return selected_geonames;
09a77f0d
HR
243 }
244 a_dialog_error_msg(parent, _("Nothing was selected"));
245 }
246 gtk_widget_destroy ( dialog );
247 return NULL;
248}
249
d84ade77 250static GList *get_entries_from_file(gchar *file_name)
93c47137
HR
251{
252 gchar *text, *pat;
253 GMappedFile *mf;
254 gsize len;
09a77f0d 255 gboolean more = TRUE;
396ac020 256 gchar lat_buf[32], lon_buf[32], elev_buf[32];
93c47137 257 gchar *s;
09a77f0d
HR
258 gint fragment_len;
259 GList *found_places = NULL;
260 found_geoname *geoname = NULL;
261 gchar **found_entries;
262 gchar *entry;
263 int entry_runner;
165a4fa9
HR
264 gchar *wikipedia_url = NULL;
265 gchar *thumbnail_url = NULL;
93c47137 266
396ac020 267 lat_buf[0] = lon_buf[0] = elev_buf[0] = '\0';
93c47137
HR
268
269 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
270 g_critical(_("couldn't map temp file"));
73f09c15 271 return NULL;
93c47137
HR
272 }
273 len = g_mapped_file_get_length(mf);
274 text = g_mapped_file_get_contents(mf);
275
276 if (g_strstr_len(text, len, GEONAMES_SEARCH_NOT_FOUND) != NULL) {
09a77f0d 277 more = FALSE;
93c47137 278 }
09a77f0d
HR
279 found_entries = g_strsplit(text, "},", 0);
280 entry_runner = 0;
281 entry = found_entries[entry_runner];
282 while (entry)
283 {
284 more = TRUE;
165a4fa9 285 geoname = new_found_geoname();
2b19a255
RN
286 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_FEATURE_PATTERN))) {
287 pat += strlen(GEONAMES_FEATURE_PATTERN);
09a77f0d
HR
288 fragment_len = 0;
289 s = pat;
290 while (*pat != '"') {
291 fragment_len++;
292 pat++;
293 }
2b19a255 294 geoname->feature = g_strndup(s, fragment_len);
09a77f0d
HR
295 }
296 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LONGITUDE_PATTERN)) == NULL) {
297 more = FALSE;
298 }
299 else {
300 pat += strlen(GEONAMES_LONGITUDE_PATTERN);
301 s = lon_buf;
302 if (*pat == '-')
303 *s++ = *pat++;
304 while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
305 (g_ascii_isdigit(*pat) || (*pat == '.')))
306 *s++ = *pat++;
307 *s = '\0';
308 if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
309 more = FALSE;
310 }
311 geoname->ll.lon = g_ascii_strtod(lon_buf, NULL);
312 }
396ac020
RN
313 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_ELEVATION_PATTERN))) {
314 pat += strlen(GEONAMES_ELEVATION_PATTERN);
315 s = elev_buf;
316 if (*pat == '-')
317 *s++ = *pat++;
318 while ((s < (elev_buf + sizeof(elev_buf))) && (pat < (text + len)) &&
319 (g_ascii_isdigit(*pat) || (*pat == '.')))
320 *s++ = *pat++;
321 *s = '\0';
322 geoname->elevation = g_ascii_strtod(elev_buf, NULL);
323 }
165a4fa9 324 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_NAME_PATTERN))) {
09a77f0d
HR
325 pat += strlen(GEONAMES_NAME_PATTERN);
326 fragment_len = 0;
327 s = pat;
328 while (*pat != '"') {
329 fragment_len++;
330 pat++;
331 }
332 geoname -> name = g_strndup(s, fragment_len);
333 }
165a4fa9
HR
334 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_TITLE_PATTERN))) {
335 pat += strlen(GEONAMES_TITLE_PATTERN);
336 fragment_len = 0;
337 s = pat;
338 while (*pat != '"') {
339 fragment_len++;
340 pat++;
341 }
342 geoname -> name = g_strndup(s, fragment_len);
343 }
344 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_WIKIPEDIAURL_PATTERN))) {
345 pat += strlen(GEONAMES_WIKIPEDIAURL_PATTERN);
346 fragment_len = 0;
347 s = pat;
348 while (*pat != '"') {
349 fragment_len++;
350 pat++;
351 }
352 wikipedia_url = g_strndup(s, fragment_len);
353 }
354 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_THUMBNAILIMG_PATTERN))) {
355 pat += strlen(GEONAMES_THUMBNAILIMG_PATTERN);
356 fragment_len = 0;
357 s = pat;
358 while (*pat != '"') {
359 fragment_len++;
360 pat++;
361 }
362 thumbnail_url = g_strndup(s, fragment_len);
363 }
09a77f0d
HR
364 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LATITUDE_PATTERN)) == NULL) {
365 more = FALSE;
366 }
367 else {
368 pat += strlen(GEONAMES_LATITUDE_PATTERN);
369 s = lat_buf;
370 if (*pat == '-')
371 *s++ = *pat++;
372 while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
373 (g_ascii_isdigit(*pat) || (*pat == '.')))
374 *s++ = *pat++;
375 *s = '\0';
376 if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
377 more = FALSE;
378 }
379 geoname->ll.lat = g_ascii_strtod(lat_buf, NULL);
380 }
381 if (!more) {
382 if (geoname) {
383 g_free(geoname);
384 }
385 }
386 else {
165a4fa9 387 if (wikipedia_url) {
af98fde6
RN
388 // Really we should support the GPX URL tag and then put that in there...
389 geoname->cmt = g_strdup_printf("http://%s", wikipedia_url);
165a4fa9
HR
390 if (thumbnail_url) {
391 geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\"><img src=\"%s\" border=\"0\"/></a>", wikipedia_url, thumbnail_url);
392 }
393 else {
394 geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\">%s</a>", wikipedia_url, geoname->name);
395 }
396 }
397 if (wikipedia_url) {
398 g_free(wikipedia_url);
399 wikipedia_url = NULL;
400 }
401 if (thumbnail_url) {
402 g_free(thumbnail_url);
403 thumbnail_url = NULL;
404 }
09a77f0d
HR
405 found_places = g_list_prepend(found_places, geoname);
406 }
407 entry_runner++;
408 entry = found_entries[entry_runner];
93c47137 409 }
09a77f0d 410 g_strfreev(found_entries);
165a4fa9 411 found_places = g_list_reverse(found_places);
316945d8 412 g_mapped_file_unref(mf);
165a4fa9
HR
413 return(found_places);
414}
415
416
120ab662 417void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon maxmin[2] )
165a4fa9
HR
418{
419 gchar *uri;
420 gchar *tmpname;
421 GList *wiki_places;
422 GList *selected;
423 GList *wp_runner;
424 VikWaypoint *wiki_wp;
425 found_geoname *wiki_geoname;
426
7700a91f
GB
427 /* encode doubles in a C locale */
428 gchar *north = a_coords_dtostr(maxmin[0].lat);
429 gchar *south = a_coords_dtostr(maxmin[1].lat);
430 gchar *east = a_coords_dtostr(maxmin[0].lon);
431 gchar *west = a_coords_dtostr(maxmin[1].lon);
9e4edda0 432 uri = g_strdup_printf ( GEONAMES_WIKIPEDIA_URL_FMT, north, south, east, west, GEONAMES_LANG, GEONAMES_MAX_ENTRIES );
7700a91f
GB
433 g_free(north); north = NULL;
434 g_free(south); south = NULL;
435 g_free(east); east = NULL;
436 g_free(west); west = NULL;
e09b94fe 437 tmpname = a_download_uri_to_tmp_file ( uri, NULL );
165a4fa9
HR
438 if (!tmpname) {
439 none_found(vw);
440 return;
441 }
442 wiki_places = get_entries_from_file(tmpname);
443 if (g_list_length(wiki_places) == 0) {
444 none_found(vw);
445 return;
446 }
447 selected = a_select_geoname_from_list(VIK_GTK_WINDOW_FROM_WIDGET(vw), wiki_places, TRUE, "Select articles", "Select the articles you want to add.");
448 wp_runner = selected;
449 while (wp_runner) {
450 wiki_geoname = (found_geoname *)wp_runner->data;
451 wiki_wp = vik_waypoint_new();
165a4fa9
HR
452 wiki_wp->visible = TRUE;
453 vik_coord_load_from_latlon(&(wiki_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &(wiki_geoname->ll));
396ac020 454 wiki_wp->altitude = wiki_geoname->elevation;
af98fde6
RN
455 vik_waypoint_set_comment(wiki_wp, wiki_geoname->cmt);
456 vik_waypoint_set_description(wiki_wp, wiki_geoname->desc);
f5c200d4
RN
457 // Use the featue type to generate a suitable waypoint icon
458 // http://www.geonames.org/wikipedia/wikipedia_features.html
459 // Only a few values supported as only a few symbols make sense
460 if ( wiki_geoname->feature ) {
461 if ( !strcmp (wiki_geoname->feature, "city") )
462 vik_waypoint_set_symbol(wiki_wp, "city (medium)");
463 if ( !strcmp (wiki_geoname->feature, "edu") )
464 vik_waypoint_set_symbol(wiki_wp, "school");
465 if ( !strcmp (wiki_geoname->feature, "airport") )
466 vik_waypoint_set_symbol(wiki_wp, "airport");
467 if ( !strcmp (wiki_geoname->feature, "mountain") )
468 vik_waypoint_set_symbol(wiki_wp, "summit");
469 if ( !strcmp (wiki_geoname->feature, "forest") )
470 vik_waypoint_set_symbol(wiki_wp, "forest");
471 }
165a4fa9
HR
472 vik_trw_layer_filein_add_waypoint ( vtl, wiki_geoname->name, wiki_wp );
473 wp_runner = g_list_next(wp_runner);
474 }
475 free_geoname_list(wiki_places);
476 free_geoname_list(selected);
477 g_free(uri);
478 if (tmpname) {
7616220d 479 g_remove(tmpname);
165a4fa9
HR
480 g_free(tmpname);
481 }
165a4fa9 482}