]> git.street.me.uk Git - andy/viking.git/blame - src/geonamessearch.c
[QA] minor doc improvment
[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 }
09a77f0d
HR
233 }
234 }
9f82a695 235 while ( gtk_tree_model_iter_next ( GTK_TREE_MODEL(store), &iter ) );
09a77f0d 236 }
9f82a695 237
09a77f0d
HR
238 if (selected_geonames)
239 {
240 gtk_widget_destroy ( dialog );
9f82a695 241 return selected_geonames;
09a77f0d
HR
242 }
243 a_dialog_error_msg(parent, _("Nothing was selected"));
244 }
245 gtk_widget_destroy ( dialog );
246 return NULL;
247}
248
d84ade77 249static GList *get_entries_from_file(gchar *file_name)
93c47137
HR
250{
251 gchar *text, *pat;
252 GMappedFile *mf;
253 gsize len;
09a77f0d 254 gboolean more = TRUE;
396ac020 255 gchar lat_buf[32], lon_buf[32], elev_buf[32];
93c47137 256 gchar *s;
09a77f0d
HR
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;
165a4fa9
HR
263 gchar *wikipedia_url = NULL;
264 gchar *thumbnail_url = NULL;
93c47137 265
396ac020 266 lat_buf[0] = lon_buf[0] = elev_buf[0] = '\0';
93c47137
HR
267
268 if ((mf = g_mapped_file_new(file_name, FALSE, NULL)) == NULL) {
269 g_critical(_("couldn't map temp file"));
73f09c15 270 return NULL;
93c47137
HR
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) {
09a77f0d 276 more = FALSE;
93c47137 277 }
09a77f0d
HR
278 found_entries = g_strsplit(text, "},", 0);
279 entry_runner = 0;
280 entry = found_entries[entry_runner];
281 while (entry)
282 {
283 more = TRUE;
165a4fa9 284 geoname = new_found_geoname();
2b19a255
RN
285 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_FEATURE_PATTERN))) {
286 pat += strlen(GEONAMES_FEATURE_PATTERN);
09a77f0d
HR
287 fragment_len = 0;
288 s = pat;
289 while (*pat != '"') {
290 fragment_len++;
291 pat++;
292 }
2b19a255 293 geoname->feature = g_strndup(s, fragment_len);
09a77f0d
HR
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 }
396ac020
RN
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 }
165a4fa9 323 if ((pat = g_strstr_len(entry, strlen(entry), GEONAMES_NAME_PATTERN))) {
09a77f0d
HR
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 }
165a4fa9
HR
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 }
09a77f0d
HR
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 {
165a4fa9 386 if (wikipedia_url) {
af98fde6
RN
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);
165a4fa9
HR
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 }
09a77f0d
HR
404 found_places = g_list_prepend(found_places, geoname);
405 }
406 entry_runner++;
407 entry = found_entries[entry_runner];
93c47137 408 }
09a77f0d 409 g_strfreev(found_entries);
165a4fa9 410 found_places = g_list_reverse(found_places);
316945d8 411 g_mapped_file_unref(mf);
165a4fa9
HR
412 return(found_places);
413}
414
415
120ab662 416void a_geonames_wikipedia_box ( VikWindow *vw, VikTrwLayer *vtl, struct LatLon maxmin[2] )
165a4fa9
HR
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
7700a91f
GB
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);
9e4edda0 431 uri = g_strdup_printf ( GEONAMES_WIKIPEDIA_URL_FMT, north, south, east, west, GEONAMES_LANG, GEONAMES_MAX_ENTRIES );
7700a91f
GB
432 g_free(north); north = NULL;
433 g_free(south); south = NULL;
434 g_free(east); east = NULL;
435 g_free(west); west = NULL;
e09b94fe 436 tmpname = a_download_uri_to_tmp_file ( uri, NULL );
165a4fa9
HR
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();
165a4fa9
HR
451 wiki_wp->visible = TRUE;
452 vik_coord_load_from_latlon(&(wiki_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &(wiki_geoname->ll));
396ac020 453 wiki_wp->altitude = wiki_geoname->elevation;
af98fde6
RN
454 vik_waypoint_set_comment(wiki_wp, wiki_geoname->cmt);
455 vik_waypoint_set_description(wiki_wp, wiki_geoname->desc);
f5c200d4
RN
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 }
165a4fa9
HR
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) {
7616220d 478 g_remove(tmpname);
165a4fa9
HR
479 g_free(tmpname);
480 }
165a4fa9 481}