2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * Lat/Lon plotting functions calcxy* are from GPSDrive
7 * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
9 * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
39 #include "vikwindow.h"
40 #include "vikviewport.h"
47 static gdouble EASTING_OFFSET = 500000.0;
49 static void viewport_class_init ( VikViewportClass *klass );
50 static void viewport_init ( VikViewport *vvp );
51 static void viewport_finalize ( GObject *gob );
52 static void viewport_utm_zone_check ( VikViewport *vvp );
54 static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 );
55 static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 );
56 double calcR (double lat);
58 static double Radius[181];
59 static void viewport_init_ra();
61 static GObjectClass *parent_class;
65 GtkDrawingArea drawing_area;
66 GdkPixmap *scr_buffer;
69 VikCoordMode coord_mode;
72 GdkPixbuf *alpha_pixbuf;
73 guint8 alpha_pixbuf_width;
74 guint8 alpha_pixbuf_height;
76 gdouble utm_zone_width;
77 gboolean one_utm_zone;
80 GdkColor background_color;
83 gboolean draw_centermark;
85 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
86 VikViewportDrawMode drawmode;
88 /* handy conversion factors which make google plotting extremely fast */
89 gdouble google_calcx_fact;
90 gdouble google_calcy_fact;
91 gdouble google_calcx_rev_fact;
92 gdouble google_calcy_rev_fact;
96 GdkPixmap *snapshot_buffer;
101 viewport_utm_zone_width ( VikViewport *vvp )
103 if ( vvp->coord_mode == VIK_COORD_UTM ) {
106 /* get latitude of screen bottom */
107 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
108 utm.northing -= vvp -> height * vvp -> ympp / 2;
109 a_coords_utm_to_latlon ( &utm, &ll );
112 ll.lon = (utm.zone - 1) * 6 - 180 ;
113 a_coords_latlon_to_utm ( &ll, &utm);
114 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
120 GType vik_viewport_get_type (void)
122 static GType vvp_type = 0;
126 static const GTypeInfo vvp_info =
128 sizeof (VikViewportClass),
129 NULL, /* base_init */
130 NULL, /* base_finalize */
131 (GClassInitFunc) viewport_class_init,
132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (VikViewport),
136 (GInstanceInitFunc) viewport_init,
138 vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
143 static void viewport_class_init ( VikViewportClass *klass )
146 GObjectClass *object_class;
148 object_class = G_OBJECT_CLASS (klass);
150 object_class->finalize = viewport_finalize;
152 parent_class = g_type_class_peek_parent (klass);
155 VikViewport *vik_viewport_new ()
157 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
161 static void viewport_init ( VikViewport *vvp )
167 ll.lat = a_vik_get_default_lat();
168 ll.lon = a_vik_get_default_long();
169 a_coords_latlon_to_utm ( &ll, &utm );
171 /* TODO: not static */
174 vvp->coord_mode = VIK_COORD_LATLON;
175 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
176 vvp->center.mode = VIK_COORD_LATLON;
177 vvp->center.north_south = ll.lat;
178 vvp->center.east_west = ll.lon;
179 vvp->center.utm_zone = (int)utm.zone;
180 vvp->center.utm_letter = utm.letter;
181 vvp->scr_buffer = NULL;
182 vvp->alpha_pixbuf = NULL;
183 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
184 vvp->utm_zone_width = 0.0;
185 vvp->background_gc = NULL;
186 vvp->scale_bg_gc = NULL;
187 vvp->draw_scale = TRUE;
188 vvp->draw_centermark = TRUE;
191 vvp->snapshot_buffer = NULL;
192 vvp->half_drawn = FALSE;
194 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
196 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
199 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
201 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
202 *rv = vvp->background_color;
206 /* returns pointer to internal static storage, changes next time function called, use quickly */
207 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
209 static gchar color[8];
210 g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->background_color.red/256),(int)(vvp->background_color.green/256),(int)(vvp->background_color.blue/256));
214 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
216 g_assert ( vvp->background_gc );
217 gdk_color_parse ( colorname, &(vvp->background_color) );
218 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
221 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
223 g_assert ( vvp->background_gc );
224 vvp->background_color = *color;
225 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
229 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
234 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
235 gdk_color_parse ( colorname, &color );
236 gdk_gc_set_rgb_fg_color ( rv, &color );
237 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
241 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
245 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
246 gdk_gc_set_rgb_fg_color ( rv, color );
247 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
251 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
254 vvp->height = height;
255 if ( vvp->scr_buffer )
256 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
257 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
259 /* TODO trigger: only if this is enabled !!! */
260 if ( vvp->snapshot_buffer )
261 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
262 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
266 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
268 return vvp->scr_buffer;
271 gboolean vik_viewport_configure ( VikViewport *vvp )
273 g_return_val_if_fail ( vvp != NULL, TRUE );
275 vvp->width = GTK_WIDGET(vvp)->allocation.width;
276 vvp->height = GTK_WIDGET(vvp)->allocation.height;
278 if ( vvp->scr_buffer )
279 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
281 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
283 /* TODO trigger: only if enabled! */
284 if ( vvp->snapshot_buffer )
285 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
287 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
290 /* this is down here so it can get a GC (necessary?) */
291 if ( ! vvp->background_gc )
293 vvp->background_gc = vik_viewport_new_gc ( vvp, "", 1 );
294 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */
296 if ( !vvp->scale_bg_gc) {
297 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
303 static void viewport_finalize ( GObject *gob )
305 VikViewport *vvp = VIK_VIEWPORT(gob);
307 g_return_if_fail ( vvp != NULL );
309 if ( vvp->scr_buffer )
310 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
312 if ( vvp->snapshot_buffer )
313 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
315 if ( vvp->alpha_pixbuf )
316 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
318 if ( vvp->background_gc )
319 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
321 if ( vvp->scale_bg_gc ) {
322 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
323 vvp->scale_bg_gc = NULL;
326 G_OBJECT_CLASS(parent_class)->finalize(gob);
329 void vik_viewport_clear ( VikViewport *vvp )
331 g_return_if_fail ( vvp != NULL );
332 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
335 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
337 vvp->draw_scale = draw_scale;
340 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
342 return vvp->draw_scale;
345 void vik_viewport_draw_scale ( VikViewport *vvp )
347 if ( vvp->draw_scale ) {
348 VikCoord left, right;
349 gdouble unit, base, diff, old_unit, old_diff, ratio;
350 gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
351 PangoFontDescription *pfd;
355 g_return_if_fail ( vvp != NULL );
357 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
358 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
360 vik_units_distance_t dist_units = a_vik_get_units_distance ();
361 switch (dist_units) {
362 case VIK_UNITS_DISTANCE_KILOMETRES:
363 base = vik_coord_diff ( &left, &right ); // in meters
365 case VIK_UNITS_DISTANCE_MILES:
366 // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
367 base = vik_coord_diff ( &left, &right ) * 0.00621371192;
370 base = 1; // Keep the compiler happy
371 g_critical("Houston, we've had a problem. distance=%d", dist_units);
373 ratio = (vvp->width/SCSIZE)/base;
376 diff = fabs(base-unit);
380 while (diff <= old_diff) {
383 unit = unit * (odd%2 ? 5 : 2);
384 diff = fabs(base-unit);
390 /* white background */
391 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
392 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
393 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
394 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
395 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
396 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
398 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
399 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
400 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
401 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
402 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
403 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
406 for (i=1; i<5; i++) {
407 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
408 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
409 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
410 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
414 for (i=1; i<10; i++) {
415 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
416 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
417 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
418 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
421 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
422 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
423 pango_layout_set_font_description (pl, pfd);
424 pango_font_description_free (pfd);
426 switch (dist_units) {
427 case VIK_UNITS_DISTANCE_KILOMETRES:
429 sprintf(s, "%d km", (int)unit/1000);
431 sprintf(s, "%d m", (int)unit);
434 case VIK_UNITS_DISTANCE_MILES:
435 // Handle units in 0.1 miles
437 sprintf(s, "%0.1f miles", unit/10.0);
439 else if ((int)unit == 10.0) {
440 sprintf(s, "1 mile");
443 sprintf(s, "%d miles", (int)(unit/10.0));
447 g_critical("Houston, we've had a problem. distance=%d", dist_units);
449 pango_layout_set_text(pl, s, -1);
450 vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
451 PAD + len + PAD, vvp->height - PAD - 10, pl);
457 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
459 vvp->draw_centermark = draw_centermark;
462 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
464 return vvp->draw_centermark;
467 void vik_viewport_draw_centermark ( VikViewport *vvp )
469 if ( !vvp->draw_centermark )
474 int center_x = vvp->width/2;
475 int center_y = vvp->height/2;
476 GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
478 /* white back ground */
479 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
480 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
481 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
482 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
483 /* black fore ground */
484 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
485 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
486 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
487 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
491 void vik_viewport_sync ( VikViewport *vvp )
493 g_return_if_fail ( vvp != NULL );
494 gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
497 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
501 g_return_if_fail ( vvp != NULL );
502 gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
508 x = vvp->width+x_off;
515 y = vvp->height+y_off;
518 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
519 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
522 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
524 g_return_if_fail ( vvp != NULL );
525 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
526 vvp->xmpp = vvp->ympp = xympp;
528 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
529 viewport_utm_zone_check(vvp);
532 /* or could do factor */
533 void vik_viewport_zoom_in ( VikViewport *vvp )
535 g_return_if_fail ( vvp != NULL );
536 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
541 viewport_utm_zone_check(vvp);
545 void vik_viewport_zoom_out ( VikViewport *vvp )
547 g_return_if_fail ( vvp != NULL );
548 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
553 viewport_utm_zone_check(vvp);
557 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
559 if ( vvp->xmpp == vvp->ympp )
564 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
569 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
574 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
576 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
578 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
579 viewport_utm_zone_check(vvp);
583 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
585 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
587 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
588 viewport_utm_zone_check(vvp);
593 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
595 g_return_val_if_fail ( vvp != NULL, NULL );
596 return &(vvp->center);
599 /* called every time we update coordinates/zoom */
600 static void viewport_utm_zone_check ( VikViewport *vvp )
602 if ( vvp->coord_mode == VIK_COORD_UTM )
606 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
607 a_coords_latlon_to_utm ( &ll, &utm );
608 if ( utm.zone != vvp->center.utm_zone )
609 *((struct UTM *)(&vvp->center)) = utm;
611 /* misc. stuff so we don't have to check later */
612 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
613 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
617 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
619 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
620 if ( vvp->coord_mode == VIK_COORD_UTM )
621 viewport_utm_zone_check ( vvp );
624 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
626 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
627 if ( vvp->coord_mode == VIK_COORD_UTM )
628 viewport_utm_zone_check ( vvp );
631 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
633 vvp->center = *coord;
634 if ( vvp->coord_mode == VIK_COORD_UTM )
635 viewport_utm_zone_check ( vvp );
638 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
640 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
642 /* get center, then just offset */
643 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
644 ul->mode = VIK_COORD_UTM;
647 ul->north_south += (vvp->ympp * vvp->height / 2);
648 ul->east_west -= (vvp->xmpp * vvp->width / 2);
649 br->north_south -= (vvp->ympp * vvp->height / 2);
650 br->east_west += (vvp->xmpp * vvp->width / 2);
653 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
655 if ( vvp->coord_mode == VIK_COORD_UTM ) {
656 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
657 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
662 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
664 if ( vvp->coord_mode == VIK_COORD_UTM ) {
666 g_assert ( vvp != NULL );
667 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
668 return coord.utm_zone;
673 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
675 if ( vvp->coord_mode == VIK_COORD_UTM ) {
677 g_assert ( vvp != NULL );
678 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
679 return coord.utm_zone;
685 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
687 g_return_if_fail ( vvp != NULL );
688 if ( vvp->coord_mode == VIK_COORD_UTM ) {
689 /* slightly optimized */
690 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
691 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
692 viewport_utm_zone_check ( vvp );
695 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
696 vik_viewport_set_center_coord ( vvp, &tmp );
700 gint vik_viewport_get_width( VikViewport *vvp )
702 g_return_val_if_fail ( vvp != NULL, 0 );
706 gint vik_viewport_get_height( VikViewport *vvp )
708 g_return_val_if_fail ( vvp != NULL, 0 );
712 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
714 if ( vvp->coord_mode == VIK_COORD_UTM ) {
716 struct UTM *utm = (struct UTM *) coord;
717 coord->mode = VIK_COORD_UTM;
719 g_return_if_fail ( vvp != NULL );
721 utm->zone = vvp->center.utm_zone;
722 utm->letter = vvp->center.utm_letter;
723 utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
724 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
725 utm->zone += zone_delta;
726 utm->easting -= zone_delta * vvp->utm_zone_width;
727 utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
728 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
729 coord->mode = VIK_COORD_LATLON;
730 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
731 calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width/2, vvp->height/2);
732 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
734 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
735 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
738 --> THIS IS JUNK HERE.
739 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
741 (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
742 DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
748 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
751 g_return_if_fail ( vvp != NULL );
753 if ( coord->mode != vvp->coord_mode )
755 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
756 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
760 if ( vvp->coord_mode == VIK_COORD_UTM ) {
761 struct UTM *center = (struct UTM *) &(vvp->center);
762 struct UTM *utm = (struct UTM *) coord;
763 if ( center->zone != utm->zone && vvp->one_utm_zone )
765 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
769 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
770 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
771 *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
772 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
773 struct LatLon *center = (struct LatLon *) &(vvp->center);
774 struct LatLon *ll = (struct LatLon *) coord;
776 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
777 calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width / 2, vvp->height / 2 );
779 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
780 /* FIXMERCATOR: Optimize */
781 *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
782 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
787 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
789 if ( *x1 > 20000 || *x1 < -20000 ) {
790 gdouble shrinkfactor = ABS(20000.0 / *x1);
791 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
792 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
793 } else if ( *y1 > 20000 || *y1 < -20000 ) {
794 gdouble shrinkfactor = ABS(20000.0 / *x1);
795 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
796 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
797 } else if ( *x2 > 20000 || *x2 < -20000 ) {
798 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
799 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
800 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
801 g_print("%f, %d, %d\n", shrinkfactor, *x2, *y2);
802 } else if ( *y2 > 20000 || *y2 < -20000 ) {
803 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
804 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
805 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
809 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
811 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
812 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
813 /*** clipping, yeah! ***/
814 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
815 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
819 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
821 if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
822 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
825 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
827 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
828 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
831 /* shouldn't use this -- slow -- change the alpha channel instead. */
832 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
833 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
835 gint real_dest_x = MAX(dest_x,0);
836 gint real_dest_y = MAX(dest_y,0);
839 return; /* don't waste your time */
841 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
843 if ( vvp->alpha_pixbuf )
844 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
845 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
846 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
847 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
850 w = MIN(w,vvp->width - dest_x);
851 h = MIN(h,vvp->height - dest_y);
853 /* check that we are drawing within boundaries. */
854 src_x += (real_dest_x - dest_x);
855 src_y += (real_dest_y - dest_y);
856 w -= (real_dest_x - dest_x);
857 h -= (real_dest_y - dest_y);
859 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
860 real_dest_x, real_dest_y, 0, 0, w, h );
863 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
865 /* draw pixbuf_tmp */
866 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
869 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
870 gint dest_x, gint dest_y, gint w, gint h )
872 gdk_draw_pixbuf ( vvp->scr_buffer,
873 // GTK_WIDGET(vvp)->style->black_gc,
876 src_x, src_y, dest_x, dest_y, w, h,
877 GDK_RGB_DITHER_NONE, 0, 0 );
880 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
882 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
886 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
888 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
891 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
894 return vvp->coord_mode;
897 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
899 g_return_if_fail ( vvp != NULL );
900 vvp->coord_mode = mode;
901 vik_coord_convert ( &(vvp->center), mode );
904 /* Thanks GPSDrive */
905 static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
908 gdouble dif, lat, lon;
909 double Ra = Radius[90+(gint)zero_lat];
911 px = (mapSizeX2 - x) * pixelfact_x;
912 py = (-mapSizeY2 + y) * pixelfact_y;
914 lat = zero_lat - py / Ra;
915 lat = zero_lat - py / Ra;
919 cos (lat * DEG2RAD));
921 dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
922 lat = lat - dif / 1.5;
926 cos (lat * DEG2RAD));
933 /* Thanks GPSDrive */
934 static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
938 gint mapSizeX = 2 * mapSizeX2;
939 gint mapSizeY = 2 * mapSizeY2;
941 g_assert ( lt >= -90.0 && lt <= 90.0 );
942 // lg *= rad2deg; // FIXME, optimize equations
944 Ra = Radius[90+(gint)lt];
946 cos (lt*DEG2RAD) * (lg - zero_long);
947 *y = Ra * (lt - zero_lat);
948 dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
949 *y = *y + dif / 1.85;
950 *x = *x / pixelfact_x;
951 *y = *y / pixelfact_y;
954 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
959 static void viewport_init_ra()
961 static gboolean done_before = FALSE;
965 for ( i = -90; i <= 90; i++)
966 Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
971 double calcR (double lat)
973 double a = 6378.137, r, sc, x, y, z;
974 double e2 = 0.081082 * 0.081082;
976 * the radius of curvature of an ellipsoidal Earth in the plane of the
977 * meridian is given by
979 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
982 * where a is the equatorial radius, b is the polar radius, and e is
983 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
985 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
986 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
987 * = 0.081082 Eccentricity
993 z = 1.0 - e2 * sc * sc;
1000 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1002 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1005 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1007 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1008 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1011 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1013 static GdkGCValues values;
1014 gdk_gc_get_values ( gc, &values );
1015 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1018 GdkFunction vik_gc_get_function ( GdkGC *gc )
1020 static GdkGCValues values;
1021 gdk_gc_get_values ( gc, &values );
1022 return values.function;
1025 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1027 vvp->drawmode = drawmode;
1028 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1029 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1031 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1035 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1037 return vvp->drawmode;
1040 /******** triggering *******/
1041 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1043 vp->trigger = trigger;
1046 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1051 void vik_viewport_snapshot_save ( VikViewport *vp )
1053 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1056 void vik_viewport_snapshot_load ( VikViewport *vp )
1058 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1061 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1063 vp->half_drawn = half_drawn;
1066 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1068 return vp->half_drawn;
1072 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1074 const gchar *name = NULL;
1075 VikWindow *vw = NULL;
1076 GtkWidget *mode_button;
1079 vw = VIK_WINDOW_FROM_WIDGET(vv);
1080 mode_button = vik_window_get_drawmode_button(vw, mode);
1081 label = gtk_bin_get_child(GTK_BIN(mode_button));
1083 name = gtk_label_get_text ( GTK_LABEL(label) );
1089 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1091 VikCoord tleft, tright, bleft, bright;
1093 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1094 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1095 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1096 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1098 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1099 vik_coord_convert(&tright, VIK_COORD_LATLON);
1100 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1101 vik_coord_convert(&bright, VIK_COORD_LATLON);
1103 *max_lat = MAX(tleft.north_south, tright.north_south);
1104 *min_lat = MIN(bleft.north_south, bright.north_south);
1105 *max_lon = MAX(tright.east_west, bright.east_west);
1106 *min_lon = MIN(tleft.east_west, bleft.east_west);