]> git.street.me.uk Git - andy/viking.git/blob - src/geonamessearch.c
Set the generate image file filter according to the file type selected.
[andy/viking.git] / src / geonamessearch.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2009, Hein Ragas
5  * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
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  *
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"
34 #include "util.h"
35 #include "geonamessearch.h"
36
37 /* Compatibility */
38 #if ! GLIB_CHECK_VERSION(2,22,0)
39 #define g_mapped_file_unref g_mapped_file_free
40 #endif
41
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")
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
50
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"
52
53 #define GEONAMES_FEATURE_PATTERN "\"feature\": \""
54 #define GEONAMES_LONGITUDE_PATTERN "\"lng\": "
55 #define GEONAMES_NAME_PATTERN "\"name\": \""
56 #define GEONAMES_LATITUDE_PATTERN "\"lat\": "
57 #define GEONAMES_ELEVATION_PATTERN "\"elevation\": "
58 #define GEONAMES_TITLE_PATTERN "\"title\": \""
59 #define GEONAMES_WIKIPEDIAURL_PATTERN "\"wikipediaUrl\": \""
60 #define GEONAMES_THUMBNAILIMG_PATTERN "\"thumbnailImg\": \""
61 #define GEONAMES_SEARCH_NOT_FOUND "not understand the location"
62
63 /* found_geoname: Type to contain data returned from GeoNames.org */
64
65 typedef struct {
66   gchar *name;
67   gchar *feature;
68   struct LatLon ll;
69   gdouble elevation;
70   gchar *cmt;
71   gchar *desc;
72 } found_geoname;
73
74 static found_geoname *new_found_geoname()
75 {
76   found_geoname *ret;
77
78   ret = (found_geoname *)g_malloc(sizeof(found_geoname));
79   ret->name = NULL;
80   ret->feature = NULL;
81   ret->cmt = NULL;
82   ret->desc = NULL;
83   ret->ll.lat = 0.0;
84   ret->ll.lon = 0.0;
85   ret->elevation = VIK_DEFAULT_ALTITUDE;
86   return ret;
87 }
88
89 static found_geoname *copy_found_geoname(found_geoname *src)
90 {
91   found_geoname *dest = new_found_geoname();
92   dest->name = g_strdup(src->name);
93   dest->feature = g_strdup(src->feature);
94   dest->ll.lat = src->ll.lat;
95   dest->ll.lon = src->ll.lon;
96   dest->elevation = src->elevation;
97   dest->cmt = g_strdup(src->cmt);
98   dest->desc = g_strdup(src->desc);
99   return(dest);
100 }
101
102 static void free_list_geonames(found_geoname *geoname, gpointer userdata)
103 {
104   g_free(geoname->name);
105   g_free(geoname->feature);
106   g_free(geoname->cmt);
107   g_free(geoname->desc);
108 }
109
110 static void free_geoname_list(GList *found_places)
111 {
112   g_list_foreach(found_places, (GFunc)free_list_geonames, NULL);
113   g_list_free(found_places);
114 }
115
116 static 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!"));
124   gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), search_label, FALSE, FALSE, 5 );
125   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
126   gtk_widget_show_all(dialog);
127
128   gtk_dialog_run ( GTK_DIALOG(dialog) );
129   gtk_widget_destroy(dialog);
130 }
131
132 static GList *a_select_geoname_from_list(GtkWindow *parent, GList *geonames, gboolean multiple_selection_allowed, const gchar *title, const gchar *msg)
133 {
134   GtkTreeIter iter;
135   GtkCellRenderer *renderer;
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);
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
156   GtkWidget *label = gtk_label_new ( msg );
157   GtkTreeStore *store = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
158
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);
165     gtk_tree_store_set(store, &iter, 0, geoname->name, 1, geoname->feature, 2, latlon_string, -1);
166     geoname_runner = g_list_next(geoname_runner);
167     g_free(latlon_string);
168   }
169
170   view = gtk_tree_view_new();
171   renderer = gtk_cell_renderer_text_new();
172   column_runner = 0;
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
181   column_runner++;
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
186   column_runner++;
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
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
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
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);
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
207   if ( response_w )
208     gtk_widget_grab_focus ( response_w );
209
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
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           }
233         }
234       }
235       while ( gtk_tree_model_iter_next ( GTK_TREE_MODEL(store), &iter ) );
236     }
237
238     if (selected_geonames)
239     { 
240       gtk_widget_destroy ( dialog );
241       return selected_geonames;
242     }
243     a_dialog_error_msg(parent, _("Nothing was selected"));
244   }
245   gtk_widget_destroy ( dialog );
246   return NULL;
247 }
248
249 static GList *get_entries_from_file(gchar *file_name)
250 {
251   gchar *text, *pat;
252   GMappedFile *mf;
253   gsize len;
254   gboolean more = TRUE;
255   gchar lat_buf[32], lon_buf[32], elev_buf[32];
256   gchar *s;
257   gint fragment_len;
258   GList *found_places = NULL;
259   found_geoname *geoname = NULL;
260   gchar **found_entries;
261   gchar *entry;
262   int entry_runner;
263   gchar *wikipedia_url = NULL;
264   gchar *thumbnail_url = NULL;
265
266   lat_buf[0] = lon_buf[0] = elev_buf[0] = '\0';
267
268   if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
269     g_critical(_("couldn't map temp file"));
270     return NULL;
271   }
272   len = g_mapped_file_get_length(mf);
273   text = g_mapped_file_get_contents(mf);
274
275   if (g_strstr_len(text, len, GEONAMES_SEARCH_NOT_FOUND) != NULL) {
276     more = FALSE;
277   }
278   found_entries = g_strsplit(text, "},", 0);
279   entry_runner = 0;
280   entry = found_entries[entry_runner];
281   while (entry)
282   {
283     more = TRUE;
284     geoname = new_found_geoname();
285     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_FEATURE_PATTERN))) {
286       pat += strlen(GEONAMES_FEATURE_PATTERN);
287       fragment_len = 0;
288       s = pat;
289       while (*pat != '"') {
290         fragment_len++;
291         pat++;
292       }
293       geoname->feature = g_strndup(s, fragment_len);
294     }
295     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LONGITUDE_PATTERN)) == NULL) {
296       more = FALSE;
297     }
298     else {
299       pat += strlen(GEONAMES_LONGITUDE_PATTERN);
300       s = lon_buf;
301       if (*pat == '-')
302         *s++ = *pat++;
303       while ((s < (lon_buf + sizeof(lon_buf))) && (pat < (text + len)) &&
304               (g_ascii_isdigit(*pat) || (*pat == '.')))
305         *s++ = *pat++;
306       *s = '\0';
307       if ((pat >= (text + len)) || (lon_buf[0] == '\0')) {
308         more = FALSE;
309       }
310       geoname->ll.lon = g_ascii_strtod(lon_buf, NULL);
311     }
312     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_ELEVATION_PATTERN))) {
313       pat += strlen(GEONAMES_ELEVATION_PATTERN);
314       s = elev_buf;
315       if (*pat == '-')
316         *s++ = *pat++;
317       while ((s < (elev_buf + sizeof(elev_buf))) && (pat < (text + len)) &&
318               (g_ascii_isdigit(*pat) || (*pat == '.')))
319         *s++ = *pat++;
320       *s = '\0';
321       geoname->elevation = g_ascii_strtod(elev_buf, NULL);
322     }
323     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_NAME_PATTERN))) {
324       pat += strlen(GEONAMES_NAME_PATTERN);
325       fragment_len = 0;
326       s = pat;
327       while (*pat != '"') {
328         fragment_len++;
329         pat++;
330       }
331       geoname -> name = g_strndup(s, fragment_len);
332     }
333     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_TITLE_PATTERN))) {
334       pat += strlen(GEONAMES_TITLE_PATTERN);
335       fragment_len = 0;
336       s = pat;
337       while (*pat != '"') {
338         fragment_len++;
339         pat++;
340       }
341       geoname -> name = g_strndup(s, fragment_len);
342     }
343     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_WIKIPEDIAURL_PATTERN))) {
344       pat += strlen(GEONAMES_WIKIPEDIAURL_PATTERN);
345       fragment_len = 0;
346       s = pat;
347       while (*pat != '"') {
348         fragment_len++;
349         pat++;
350       }
351       wikipedia_url = g_strndup(s, fragment_len);
352     }
353     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_THUMBNAILIMG_PATTERN))) {
354       pat += strlen(GEONAMES_THUMBNAILIMG_PATTERN);
355       fragment_len = 0;
356       s = pat;
357       while (*pat != '"') {
358         fragment_len++;
359         pat++;
360       }
361       thumbnail_url = g_strndup(s, fragment_len);
362     }
363     if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_LATITUDE_PATTERN)) == NULL) {
364       more = FALSE;
365     }
366     else {
367       pat += strlen(GEONAMES_LATITUDE_PATTERN);
368       s = lat_buf;
369       if (*pat == '-')
370         *s++ = *pat++;
371       while ((s < (lat_buf + sizeof(lat_buf))) && (pat < (text + len)) &&
372               (g_ascii_isdigit(*pat) || (*pat == '.')))
373         *s++ = *pat++;
374       *s = '\0';
375       if ((pat >= (text + len)) || (lat_buf[0] == '\0')) {
376         more = FALSE;
377       }
378       geoname->ll.lat = g_ascii_strtod(lat_buf, NULL);
379     }
380     if (!more) {
381       if (geoname) {
382         g_free(geoname);
383       }
384     }
385     else {
386       if (wikipedia_url) {
387         // Really we should support the GPX URL tag and then put that in there...
388         geoname->cmt = g_strdup_printf("http://%s", wikipedia_url);
389         if (thumbnail_url) {
390           geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\"><img src=\"%s\" border=\"0\"/></a>", wikipedia_url, thumbnail_url);
391         }
392         else {
393           geoname -> desc = g_strdup_printf("<a href=\"http://%s\" target=\"_blank\">%s</a>", wikipedia_url, geoname->name);
394         }
395       }
396       if (wikipedia_url) {
397         g_free(wikipedia_url);
398         wikipedia_url = NULL;
399       }
400       if (thumbnail_url) {
401         g_free(thumbnail_url);
402         thumbnail_url = NULL;
403       }
404       found_places = g_list_prepend(found_places, geoname);
405     }
406     entry_runner++;
407     entry = found_entries[entry_runner];
408   }
409   g_strfreev(found_entries);
410   found_places = g_list_reverse(found_places);
411   g_mapped_file_unref(mf);
412   return(found_places);
413 }
414
415
416 void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon maxmin[2] )
417 {
418   gchar *uri;
419   gchar *tmpname;
420   GList *wiki_places;
421   GList *selected;
422   GList *wp_runner;
423   VikWaypoint *wiki_wp;
424   found_geoname *wiki_geoname;
425
426   /* encode doubles in a C locale */
427   gchar *north = a_coords_dtostr(maxmin[0].lat);
428   gchar *south = a_coords_dtostr(maxmin[1].lat);
429   gchar *east = a_coords_dtostr(maxmin[0].lon);
430   gchar *west = a_coords_dtostr(maxmin[1].lon);
431   uri = g_strdup_printf ( GEONAMES_WIKIPEDIA_URL_FMT, north, south, east, west, GEONAMES_LANG, GEONAMES_MAX_ENTRIES );
432   g_free(north); north = NULL;
433   g_free(south); south = NULL;
434   g_free(east);  east = NULL;
435   g_free(west);  west = NULL;
436   tmpname = a_download_uri_to_tmp_file ( uri, NULL );
437   if (!tmpname) {
438     none_found(vw);
439     return;
440   }
441   wiki_places = get_entries_from_file(tmpname);
442   if (g_list_length(wiki_places) == 0) {
443     none_found(vw);
444     return;
445   }
446   selected = a_select_geoname_from_list(VIK_GTK_WINDOW_FROM_WIDGET(vw), wiki_places, TRUE, "Select articles", "Select the articles you want to add.");
447   wp_runner = selected;
448   while (wp_runner) {
449     wiki_geoname = (found_geoname *)wp_runner->data;
450     wiki_wp = vik_waypoint_new();
451     wiki_wp->visible = TRUE;
452     vik_coord_load_from_latlon(&(wiki_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &(wiki_geoname->ll));
453     wiki_wp->altitude = wiki_geoname->elevation;
454     vik_waypoint_set_comment(wiki_wp, wiki_geoname->cmt);
455     vik_waypoint_set_description(wiki_wp, wiki_geoname->desc);
456     // Use the featue type to generate a suitable waypoint icon
457     //  http://www.geonames.org/wikipedia/wikipedia_features.html
458     // Only a few values supported as only a few symbols make sense
459     if ( wiki_geoname->feature ) {
460       if ( !strcmp (wiki_geoname->feature, "city") )
461         vik_waypoint_set_symbol(wiki_wp, "city (medium)");
462       if ( !strcmp (wiki_geoname->feature, "edu") )
463         vik_waypoint_set_symbol(wiki_wp, "school");
464       if ( !strcmp (wiki_geoname->feature, "airport") )
465         vik_waypoint_set_symbol(wiki_wp, "airport");
466       if ( !strcmp (wiki_geoname->feature, "mountain") )
467         vik_waypoint_set_symbol(wiki_wp, "summit");
468       if ( !strcmp (wiki_geoname->feature, "forest") )
469         vik_waypoint_set_symbol(wiki_wp, "forest");
470     }
471     vik_trw_layer_filein_add_waypoint ( vtl, wiki_geoname->name, wiki_wp );
472     wp_runner = g_list_next(wp_runner);
473   }
474   free_geoname_list(wiki_places);
475   free_geoname_list(selected);
476   g_free(uri);
477   if (tmpname) {
478     g_remove(tmpname);
479     g_free(tmpname);
480   }
481 }