Contents of /trunk/src/map-tool.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (show annotations)
Wed Sep 2 12:25:51 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 17751 byte(s)
Relief info
1 /*
2 * Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of GPXView.
5 *
6 * GPXView is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GPXView is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * http://topo.geofabrik.de/relief/${z}/${x}/${y}.png 8-15
22 * http://topo.geofabrik.de/trail/${z}/${x}/${y}.png 8-15
23 */
24
25 #include "gpxview.h"
26 #include "converter.h"
27 #include <math.h> // for isnan
28
29 #ifdef ENABLE_OSM_GPS_MAP
30 #include "osm-gps-map.h"
31 #include "osm-gps-map-osd-classic.h"
32 #endif
33
34 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
35 #include <gdk/gdkx.h>
36 #include <X11/Xatom.h>
37 #endif
38
39 // #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENSTREETMAP
40 #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENCYCLEMAP
41 // #define MAP_SOURCE OSM_GPS_MAP_SOURCE_GOOGLE_STREET
42 #define GPS_DEFAULT_ZOOM 13
43
44 #define PROXY_KEY "/system/http_proxy/"
45
46 static const char *get_proxy_uri(appdata_t *appdata) {
47 static char proxy_buffer[64] = "";
48
49 /* use environment settings if preset */
50 const char *proxy = g_getenv("http_proxy");
51 if(proxy) {
52 printf("http_proxy: %s\n", proxy);
53 return proxy;
54 }
55
56 /* ------------- get proxy settings -------------------- */
57 if(gconf_client_get_bool(appdata->gconf_client,
58 PROXY_KEY "use_http_proxy", NULL)) {
59
60 /* we can savely ignore things like "ignore_hosts" since we */
61 /* are pretty sure not inside the net of one of our map renderers */
62 /* (unless the user works at google :-) */
63
64 /* get basic settings */
65 char *host =
66 gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL);
67 if(host) {
68 int port =
69 gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL);
70
71 snprintf(proxy_buffer, sizeof(proxy_buffer),
72 "http://%s:%u", host, port);
73
74 g_free(host);
75 }
76 return proxy_buffer;
77 }
78
79 return NULL;
80 }
81
82 static void
83 cb_map_gps(osd_button_t but, map_context_t *context) {
84 if(but == OSD_GPS) {
85 pos_t *refpos = get_pos(context->appdata);
86 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
87 gint zoom;
88 g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
89 if(zoom < 10)
90 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
91 refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
92 else
93 osm_gps_map_set_center(OSM_GPS_MAP(context->widget),
94 refpos->lat, refpos->lon);
95
96 /* re-enable centering */
97 g_object_set(context->widget, "auto-center", TRUE, NULL);
98 } else {
99 /* no coordinates given: display the entire world */
100 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
101 0.0, 0.0, 1);
102 }
103 }
104 }
105
106 static int dist2pixel(map_context_t *context, float km, float lat) {
107 return 1000.0*km/osm_gps_map_get_scale(OSM_GPS_MAP(context->widget));
108 }
109
110 static gboolean map_gps_update(gpointer data) {
111 map_context_t *context = (map_context_t*)data;
112
113 /* get reference position ... */
114 pos_t *refpos = get_pos(context->appdata);
115 gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
116
117 /* ... and enable "goto" button if it's valid */
118 osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),
119 OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);
120
121 if(ok) {
122 float heading = NAN;
123 int radius = 0;
124
125 if(context->appdata->use_gps) {
126 heading = gps_get_heading(context->appdata);
127
128 /* get error */
129 float eph = gps_get_eph(context->appdata);
130 if(!isnan(eph))
131 radius = dist2pixel(context, eph/1000, refpos->lat);
132 }
133
134 g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
135 osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
136 refpos->lat, refpos->lon, heading);
137 } else
138 osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
139
140 return TRUE;
141 }
142
143 static gboolean on_map_configure(GtkWidget *widget,
144 GdkEventConfigure *event,
145 map_context_t *context) {
146
147 if(!context->map_complete) {
148
149 /* set default values if they are invalid */
150 if(!context->appdata->map.zoom ||
151 isnan(context->appdata->map.pos.lat) ||
152 isnan(context->appdata->map.pos.lon)) {
153 printf("no valid map position found\n");
154
155 pos_t *refpos = get_pos(context->appdata);
156 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
157 /* use gps position if present */
158 context->appdata->map.pos = *refpos;
159 context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
160 } else {
161 /* use world map otherwise */
162 context->appdata->map.pos.lat = 0.0;
163 context->appdata->map.pos.lon = 0.0;
164 context->appdata->map.zoom = 1;
165 }
166 }
167
168 /* jump to initial position */
169 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
170 context->appdata->map.pos.lat,
171 context->appdata->map.pos.lon,
172 context->appdata->map.zoom);
173 context->map_complete = TRUE;
174 }
175
176 return FALSE;
177 }
178
179 static void map_draw_cachelist(GtkWidget *map, cache_t *cache) {
180 while(cache) {
181 GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
182
183 osm_gps_map_add_image(OSM_GPS_MAP(map),
184 cache->pos.lat, cache->pos.lon, icon);
185
186 cache = cache->next;
187 }
188 }
189
190 static void
191 map_cachelist_nearest(cache_t *cache, pos_t *pos,
192 cache_t **result, float *distance) {
193 while(cache) {
194 float dist =
195 pow(cache->pos.lat - pos->lat, 2) +
196 pow(cache->pos.lon - pos->lon, 2);
197
198 if(!(dist > *distance)) {
199 *result = cache;
200 *distance = dist;
201 }
202
203 cache = cache->next;
204 }
205 }
206
207 static cache_t *map_closest(map_context_t *context, pos_t *pos) {
208 cache_t *result = NULL;
209 float distance = NAN;
210
211 #ifdef USE_MAEMO
212 if(!context->appdata->cur_gpx) {
213 #endif
214 /* search all geocaches */
215 gpx_t *gpx = context->appdata->gpx;
216 while(gpx) {
217 map_cachelist_nearest(gpx->cache, pos, &result, &distance);
218 gpx = gpx->next;
219 }
220 #ifdef USE_MAEMO
221 } else {
222 map_cachelist_nearest(context->appdata->cur_gpx->cache,
223 pos, &result, &distance);
224 }
225 #endif
226
227 return result;
228 }
229
230 /* translate between osm-gps-map positions and gpxview ones */
231 pos_t coord2pos(coord_t coo) {
232 pos_t pos;
233 pos.lat = rad2deg(coo.rlat);
234 pos.lon = rad2deg(coo.rlon);
235 return pos;
236 }
237
238 #define CLICK_FUZZ (24)
239
240 static gboolean
241 on_map_button_press_event(GtkWidget *widget,
242 GdkEventButton *event, map_context_t *context) {
243 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
244
245 /* check if we actually clicked parts of the OSD */
246 if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)
247 return FALSE;
248
249 /* got a press event without release event? eat it! */
250 if(context->press_on != NULL) {
251 printf("PRESS: already\n");
252 return FALSE;
253 }
254
255 pos_t pos =
256 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
257
258 cache_t *nearest = map_closest(context, &pos);
259 if(nearest) {
260 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
261 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
262 context->press_on = nearest;
263 }
264
265 return FALSE;
266 }
267
268 static void
269 cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {
270 /* convert the pixbuf into something cairo can handle */
271
272 // Create a new ImageSurface
273 cairo_surface_t *image_surface =
274 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
275 gdk_pixbuf_get_width(buf),
276 gdk_pixbuf_get_height(buf));
277
278 // Create the new Context for the ImageSurface
279 cairo_t *context = cairo_create(image_surface);
280
281 // Draw the image on the new Context
282 gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0);
283 cairo_paint(context);
284
285 // now draw this onto the original context
286 cairo_set_source_surface(cr, image_surface, x, y);
287
288 cairo_paint(cr);
289 }
290
291 #ifndef BIG_BALLOONS
292 #define LINE_SKIP 7
293 #else
294 #define LINE_SKIP 12
295 #endif
296
297 static void
298 balloon_draw_cb(cairo_t *cr, OsmGpsMapRect_t *rect, gpointer data) {
299 cache_t *cache = (cache_t*)data;
300
301 #if 0
302 /* draw pink background to check clipping */
303 cairo_rectangle (cr, rect->x-20, rect->y-20, rect->w+40, rect->h+40);
304 cairo_set_source_rgba (cr, 1, 0, 0, 0.3);
305 cairo_fill_preserve (cr);
306 cairo_set_line_width (cr, 0);
307 cairo_stroke (cr);
308 #endif
309
310 /* leave a little border top and left */
311 gint x = rect->x, y = rect->y;
312
313 /* draw the cache type icon ... */
314 GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
315 cairo_draw_pixbuf(cr, icon, x, y);
316
317 /* ... and right of it the waypoint id */
318 cairo_text_extents_t extents;
319
320 if(cache->id) {
321 cairo_select_font_face (cr, "Sans",
322 CAIRO_FONT_SLANT_NORMAL,
323 CAIRO_FONT_WEIGHT_BOLD);
324
325 #ifndef BIG_BALLOONS
326 cairo_set_font_size (cr, 20.0);
327 #else
328 cairo_set_font_size (cr, 36.0);
329 #endif
330
331 cairo_text_extents (cr, cache->id, &extents);
332
333 /* display id right of icon vertically centered */
334 x += gdk_pixbuf_get_width(icon) + 5;
335 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
336 cairo_move_to (cr, x, y);
337 cairo_set_source_rgba (cr, 0, 0, 0, 1);
338 cairo_show_text (cr, cache->id);
339 cairo_stroke (cr);
340
341 y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP;
342 } else
343 y += gdk_pixbuf_get_height(icon);
344
345 /* return to the left border and below icon/text */
346 x = rect->x;
347
348 /* everything from here uses the same font */
349 cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
350 CAIRO_FONT_WEIGHT_NORMAL);
351 #ifndef BIG_BALLOONS
352 cairo_set_font_size (cr, 14.0);
353 #else
354 cairo_set_font_size (cr, 22.0);
355 #endif
356
357 if(cache->name) {
358 /* draw cache name */
359 cairo_text_extents (cr, cache->name, &extents);
360 y += extents.height;
361 cairo_move_to (cr, x, y);
362 cairo_set_source_rgba (cr, 0, 0, 0, 1);
363 cairo_show_text (cr, cache->name);
364 cairo_stroke (cr);
365
366 /* return to the left border and below text */
367 y += LINE_SKIP;
368 x = rect->x;
369 }
370
371 if(cache->terrain) {
372 /* draw cache rating */
373 const char *terrain = "Terrain:";
374 icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2));
375 cairo_text_extents (cr, _(terrain), &extents);
376 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
377
378 /* draw "Terrain:" string */
379 cairo_move_to (cr, x, y);
380 cairo_set_source_rgba (cr, 0, 0, 0, 1);
381 cairo_show_text (cr, _(terrain));
382 cairo_stroke (cr);
383 x += extents.width + 2;
384
385 /* draw terrain stars */
386 cairo_draw_pixbuf(cr, icon, x, y -
387 (gdk_pixbuf_get_height(icon) + extents.height)/2);
388
389 x += gdk_pixbuf_get_width(icon) + LINE_SKIP;
390 y -= (gdk_pixbuf_get_height(icon) + extents.height)/2;
391 }
392
393 if(cache->difficulty) {
394 const char *difficulty = "Difficulty:";
395 cairo_text_extents (cr, _(difficulty), &extents);
396 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
397
398 /* draw "Difficulty:" string */
399 cairo_move_to (cr, x, y);
400 cairo_set_source_rgba (cr, 0, 0, 0, 1);
401 cairo_show_text (cr, _(difficulty));
402 cairo_stroke (cr);
403 x += extents.width + 2;
404
405 icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2));
406 cairo_draw_pixbuf(cr, icon, x, y -
407 (gdk_pixbuf_get_height(icon) + extents.height)/2);
408 }
409 }
410
411 static gboolean
412 on_map_button_release_event(GtkWidget *widget,
413 GdkEventButton *event, map_context_t *context) {
414 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
415
416 if(context->press_on) {
417 coord_t coo;
418 coo = osm_gps_map_get_co_ordinates(map, event->x, event->y);
419
420 pos_t pos =
421 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
422
423 cache_t *nearest = map_closest(context, &pos);
424 if(nearest && nearest == context->press_on) {
425 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
426 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) {
427
428 osm_gps_map_draw_balloon(map, nearest->pos.lat, nearest->pos.lon,
429 balloon_draw_cb, nearest);
430 }
431 }
432 context->press_on = NULL;
433 } else {
434 /* save new map position */
435 gfloat lat, lon;
436 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
437 context->appdata->map.pos.lat = lat;
438 context->appdata->map.pos.lon = lon;
439 }
440
441 return FALSE;
442 }
443
444 static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
445 appdata_t *appdata = context->appdata;
446
447 printf("destroy map window\n");
448
449 /* save map parameters */
450 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
451 gint zoom;
452 g_object_get(map, "zoom", &zoom, NULL);
453 context->appdata->map.zoom = zoom;
454
455 gfloat lat, lon;
456 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
457 context->appdata->map.pos.lat = lat;
458 context->appdata->map.pos.lon = lon;
459
460 gint source;
461 g_object_get(map, "map-source", &source, NULL);
462 context->appdata->map.source = source;
463
464 #if MAEMO_VERSION_MAJOR == 5
465 /* restore cur_view */
466 context->appdata->cur_view = context->old_view;
467 #endif
468
469 gtk_timeout_remove(context->handler_id);
470
471 g_free(context);
472 appdata->map.context = NULL;
473 }
474
475 #if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__)
476 /* get access to zoom buttons */
477 static void
478 on_window_realize(GtkWidget *widget, gpointer data) {
479 if (widget->window) {
480 unsigned char value = 1;
481 Atom hildon_zoom_key_atom =
482 gdk_x11_get_xatom_by_name("_HILDON_ZOOM_KEY_ATOM"),
483 integer_atom = gdk_x11_get_xatom_by_name("INTEGER");
484 Display *dpy =
485 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(widget->window));
486 Window w = GDK_WINDOW_XID(widget->window);
487
488 XChangeProperty(dpy, w, hildon_zoom_key_atom,
489 integer_atom, 8, PropModeReplace, &value, 1);
490 }
491 }
492 #endif
493
494 void map(appdata_t *appdata) {
495 map_context_t *context = NULL;
496
497 /* if the map window already exists, just raise it */
498 if(appdata->map.context) {
499 gtk_window_present(GTK_WINDOW(appdata->map.context->window));
500 return;
501 }
502
503 context = appdata->map.context = g_new0(map_context_t, 1);
504 context->appdata = appdata;
505 context->map_complete = FALSE;
506
507 /* cleanup old (pre 0.8.7) path if it exists */
508 char *old_path = g_strdup_printf("%s/map/", appdata->image_path);
509 if(g_file_test(old_path, G_FILE_TEST_IS_DIR)) {
510 printf("old file path %s exists\n", old_path);
511 rmdir_recursive(old_path);
512 }
513
514 /* It is recommanded that all applications share these same */
515 /* map path, so data is only cached once. The path should be: */
516 /* ~/.osm-gps-map on standard PC (users home) */
517 /* /home/user/.osm-gps-map on Maemo5 (ext3 on internal card) */
518 /* /media/mmc2/osm-gps-map on Maemo4 (vfat on internal card) */
519 #if !defined(USE_MAEMO)
520 char *p = getenv("HOME");
521 if(!p) p = "/tmp";
522 char *path = g_strdup_printf("%s/.osm-gps-map", p);
523 #else
524 #if MAEMO_VERSION_MAJOR == 5
525 char *path = g_strdup("/home/user/.osm-gps-map");
526 #else
527 char *path = g_strdup("/media/mmc2/osm-gps-map");
528 #endif
529 #endif
530
531 const char *proxy = get_proxy_uri(appdata);
532
533 gint source = context->appdata->map.source;
534 if(!source) source = MAP_SOURCE;
535
536 context->widget = g_object_new(OSM_TYPE_GPS_MAP,
537 "map-source", source,
538 "tile-cache", path,
539 "auto-center", FALSE,
540 "record-trip-history", FALSE,
541 "show-trip-history", FALSE,
542 proxy?"proxy-uri":NULL, proxy,
543 NULL);
544
545 g_free(path);
546
547 osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget));
548
549 char *name = NULL;
550 #ifdef USE_MAEMO
551 if(!appdata->cur_gpx) {
552 #endif
553 /* draw all geocaches */
554 gpx_t *gpx = appdata->gpx;
555 while(gpx) {
556 map_draw_cachelist(context->widget, gpx->cache);
557 gpx = gpx->next;
558 }
559 name = g_strdup(_("all geocaches"));
560 #ifdef USE_MAEMO
561 } else {
562 map_draw_cachelist(context->widget, appdata->cur_gpx->cache);
563 name = g_strdup(appdata->cur_gpx->name);
564 }
565 #endif
566
567 char *title = g_strdup_printf(_("Map - %s"), name);
568 g_free(name);
569
570 #ifdef USE_MAEMO
571 #ifdef USE_STACKABLE_WINDOW
572 context->window = hildon_stackable_window_new();
573 /* try to enable the zoom buttons. don't do this on x86 as it breaks */
574 /* at runtime with cygwin x */
575 #ifndef __i386__
576 g_signal_connect(G_OBJECT(context->window), "realize",
577 G_CALLBACK(on_window_realize), NULL);
578 #endif // MAEMO_VERSION
579 #else
580 context->window = hildon_window_new();
581 #endif
582 #else
583 context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
584 #endif
585
586 gtk_window_set_title(GTK_WINDOW(context->window), title);
587
588 #ifndef USE_MAEMO
589 gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
590 #endif
591
592 g_free(title);
593
594 g_signal_connect(G_OBJECT(context->widget), "configure-event",
595 G_CALLBACK(on_map_configure), context);
596
597 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
598 G_CALLBACK(on_map_button_press_event), context);
599
600 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
601 G_CALLBACK(on_map_button_release_event), context);
602
603 /* install handler for timed updates of the gps button */
604 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
605
606 #if MAEMO_VERSION_MAJOR == 5
607 /* prevent some of the main screen things */
608 context->old_view = appdata->cur_view;
609 appdata->cur_view = NULL;
610 #endif
611
612 g_signal_connect(G_OBJECT(context->window), "destroy",
613 G_CALLBACK(on_window_destroy), context);
614
615 gtk_container_add(GTK_CONTAINER(context->window), context->widget);
616 gtk_widget_show_all(GTK_WIDGET(context->window));
617 }