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
27 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
34 #include "vikwindow.h"
35 #include "vikviewport.h"
42 static gdouble EASTING_OFFSET = 500000.0;
44 static void viewport_class_init ( VikViewportClass *klass );
45 static void viewport_init ( VikViewport *vvp );
46 static void viewport_finalize ( GObject *gob );
47 static void viewport_utm_zone_check ( VikViewport *vvp );
49 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 );
50 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 );
51 double calcR (double lat);
53 static double Radius[181];
54 static void viewport_init_ra();
56 static GObjectClass *parent_class;
60 GtkDrawingArea drawing_area;
61 GdkPixmap *scr_buffer;
64 VikCoordMode coord_mode;
67 GdkPixbuf *alpha_pixbuf;
68 guint8 alpha_pixbuf_width;
69 guint8 alpha_pixbuf_height;
71 gdouble utm_zone_width;
72 gboolean one_utm_zone;
75 GdkColor background_color;
78 gboolean draw_centermark;
80 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
81 VikViewportDrawMode drawmode;
83 /* handy conversion factors which make google plotting extremely fast */
84 gdouble google_calcx_fact;
85 gdouble google_calcy_fact;
86 gdouble google_calcx_rev_fact;
87 gdouble google_calcy_rev_fact;
91 GdkPixmap *snapshot_buffer;
96 viewport_utm_zone_width ( VikViewport *vvp )
98 if ( vvp->coord_mode == VIK_COORD_UTM ) {
101 /* get latitude of screen bottom */
102 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
103 utm.northing -= vvp -> height * vvp -> ympp / 2;
104 a_coords_utm_to_latlon ( &utm, &ll );
107 ll.lon = (utm.zone - 1) * 6 - 180 ;
108 a_coords_latlon_to_utm ( &ll, &utm);
109 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
115 GType vik_viewport_get_type (void)
117 static GType vvp_type = 0;
121 static const GTypeInfo vvp_info =
123 sizeof (VikViewportClass),
124 NULL, /* base_init */
125 NULL, /* base_finalize */
126 (GClassInitFunc) viewport_class_init,
127 NULL, /* class_finalize */
128 NULL, /* class_data */
129 sizeof (VikViewport),
131 (GInstanceInitFunc) viewport_init,
133 vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
138 static void viewport_class_init ( VikViewportClass *klass )
141 GObjectClass *object_class;
143 object_class = G_OBJECT_CLASS (klass);
145 object_class->finalize = viewport_finalize;
147 parent_class = g_type_class_peek_parent (klass);
150 VikViewport *vik_viewport_new ()
152 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
156 static void viewport_init ( VikViewport *vvp )
160 /* TODO: not static */
163 vvp->coord_mode = VIK_COORD_LATLON;
164 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
165 vvp->center.mode = VIK_COORD_LATLON;
166 vvp->center.north_south = 40.714490;
167 vvp->center.east_west = -74.007130;
168 vvp->center.utm_zone = 31;
169 vvp->center.utm_letter = 'N';
170 vvp->scr_buffer = NULL;
171 vvp->alpha_pixbuf = NULL;
172 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
173 vvp->utm_zone_width = 0.0;
174 vvp->background_gc = NULL;
175 vvp->scale_bg_gc = NULL;
176 vvp->draw_scale = TRUE;
177 vvp->draw_centermark = TRUE;
180 vvp->snapshot_buffer = NULL;
181 vvp->half_drawn = FALSE;
183 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
185 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
188 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
190 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
191 *rv = vvp->background_color;
195 /* returns pointer to internal static storage, changes next time function called, use quickly */
196 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
198 static gchar color[8];
199 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));
203 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
205 g_assert ( vvp->background_gc );
206 gdk_color_parse ( colorname, &(vvp->background_color) );
207 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
210 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
212 g_assert ( vvp->background_gc );
213 vvp->background_color = *color;
214 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
218 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
223 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
224 gdk_color_parse ( colorname, &color );
225 gdk_gc_set_rgb_fg_color ( rv, &color );
226 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
230 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
234 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
235 gdk_gc_set_rgb_fg_color ( rv, color );
236 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
240 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
243 vvp->height = height;
244 if ( vvp->scr_buffer )
245 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
246 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
248 /* TODO trigger: only if this is enabled !!! */
249 if ( vvp->snapshot_buffer )
250 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
251 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
255 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
257 return vvp->scr_buffer;
260 gboolean vik_viewport_configure ( VikViewport *vvp )
262 g_return_val_if_fail ( vvp != NULL, TRUE );
264 vvp->width = GTK_WIDGET(vvp)->allocation.width;
265 vvp->height = GTK_WIDGET(vvp)->allocation.height;
267 if ( vvp->scr_buffer )
268 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
270 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
272 /* TODO trigger: only if enabled! */
273 if ( vvp->snapshot_buffer )
274 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
276 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
279 /* this is down here so it can get a GC (necessary?) */
280 if ( ! vvp->background_gc )
282 vvp->background_gc = vik_viewport_new_gc ( vvp, "", 1 );
283 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */
285 if ( !vvp->scale_bg_gc) {
286 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
292 static void viewport_finalize ( GObject *gob )
294 VikViewport *vvp = VIK_VIEWPORT(gob);
296 g_return_if_fail ( vvp != NULL );
298 if ( vvp->scr_buffer )
299 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
301 if ( vvp->snapshot_buffer )
302 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
304 if ( vvp->alpha_pixbuf )
305 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
307 if ( vvp->background_gc )
308 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
310 if ( vvp->scale_bg_gc ) {
311 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
312 vvp->scale_bg_gc = NULL;
315 G_OBJECT_CLASS(parent_class)->finalize(gob);
318 void vik_viewport_clear ( VikViewport *vvp )
320 g_return_if_fail ( vvp != NULL );
321 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
324 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
326 vvp->draw_scale = draw_scale;
329 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
331 return vvp->draw_scale;
334 void vik_viewport_draw_scale ( VikViewport *vvp )
336 if ( vvp->draw_scale ) {
337 VikCoord left, right;
338 gdouble unit, base, diff, old_unit, old_diff, ratio;
339 gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
340 PangoFontDescription *pfd;
344 g_return_if_fail ( vvp != NULL );
346 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
347 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
349 base = vik_coord_diff ( &left, &right ); // in meters
350 ratio = (vvp->width/SCSIZE)/base;
353 diff = fabs(base-unit);
357 while (diff <= old_diff) {
360 unit = unit * (odd%2 ? 5 : 2);
361 diff = fabs(base-unit);
367 /* white background */
368 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
369 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
370 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
371 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
372 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
373 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
375 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
376 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
377 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
378 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
379 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
380 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
383 for (i=1; i<5; i++) {
384 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
385 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
386 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
387 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
391 for (i=1; i<10; i++) {
392 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
393 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
394 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
395 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
398 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
399 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
400 pango_layout_set_font_description (pl, pfd);
401 pango_font_description_free (pfd);
404 sprintf(s, "%d km", (int)unit/1000);
406 sprintf(s, "%d m", (int)unit);
408 pango_layout_set_text(pl, s, -1);
409 vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
410 PAD + len + PAD, vvp->height - PAD - 10, pl);
414 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
416 vvp->draw_centermark = draw_centermark;
419 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
421 return vvp->draw_centermark;
424 void vik_viewport_draw_centermark ( VikViewport *vvp )
426 if ( !vvp->draw_centermark )
431 int center_x = vvp->width/2;
432 int center_y = vvp->height/2;
433 GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
435 /* white back ground */
436 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
437 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
438 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
439 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
440 /* black fore ground */
441 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
442 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
443 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
444 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
448 void vik_viewport_sync ( VikViewport *vvp )
450 g_return_if_fail ( vvp != NULL );
451 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);
454 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
458 g_return_if_fail ( vvp != NULL );
459 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);
465 x = vvp->width+x_off;
472 y = vvp->height+y_off;
475 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
476 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
479 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
481 g_return_if_fail ( vvp != NULL );
482 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
483 vvp->xmpp = vvp->ympp = xympp;
485 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
486 viewport_utm_zone_check(vvp);
489 /* or could do factor */
490 void vik_viewport_zoom_in ( VikViewport *vvp )
492 g_return_if_fail ( vvp != NULL );
493 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
498 viewport_utm_zone_check(vvp);
502 void vik_viewport_zoom_out ( VikViewport *vvp )
504 g_return_if_fail ( vvp != NULL );
505 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
510 viewport_utm_zone_check(vvp);
514 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
516 if ( vvp->xmpp == vvp->ympp )
521 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
526 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
531 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
533 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
535 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
536 viewport_utm_zone_check(vvp);
540 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
542 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
544 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
545 viewport_utm_zone_check(vvp);
550 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
552 g_return_val_if_fail ( vvp != NULL, NULL );
553 return &(vvp->center);
556 /* called every time we update coordinates/zoom */
557 static void viewport_utm_zone_check ( VikViewport *vvp )
559 if ( vvp->coord_mode == VIK_COORD_UTM )
563 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
564 a_coords_latlon_to_utm ( &ll, &utm );
565 if ( utm.zone != vvp->center.utm_zone )
566 *((struct UTM *)(&vvp->center)) = utm;
568 /* misc. stuff so we don't have to check later */
569 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
570 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
574 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
576 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
577 if ( vvp->coord_mode == VIK_COORD_UTM )
578 viewport_utm_zone_check ( vvp );
581 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
583 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
584 if ( vvp->coord_mode == VIK_COORD_UTM )
585 viewport_utm_zone_check ( vvp );
588 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
590 vvp->center = *coord;
591 if ( vvp->coord_mode == VIK_COORD_UTM )
592 viewport_utm_zone_check ( vvp );
595 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
597 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
599 /* get center, then just offset */
600 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
601 ul->mode = VIK_COORD_UTM;
604 ul->north_south += (vvp->ympp * vvp->height / 2);
605 ul->east_west -= (vvp->xmpp * vvp->width / 2);
606 br->north_south -= (vvp->ympp * vvp->height / 2);
607 br->east_west += (vvp->xmpp * vvp->width / 2);
610 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
612 if ( vvp->coord_mode == VIK_COORD_UTM ) {
613 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
614 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
619 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
621 if ( vvp->coord_mode == VIK_COORD_UTM ) {
623 g_assert ( vvp != NULL );
624 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
625 return coord.utm_zone;
630 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
632 if ( vvp->coord_mode == VIK_COORD_UTM ) {
634 g_assert ( vvp != NULL );
635 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
636 return coord.utm_zone;
642 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
644 g_return_if_fail ( vvp != NULL );
645 if ( vvp->coord_mode == VIK_COORD_UTM ) {
646 /* slightly optimized */
647 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
648 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
649 viewport_utm_zone_check ( vvp );
652 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
653 vik_viewport_set_center_coord ( vvp, &tmp );
657 gint vik_viewport_get_width( VikViewport *vvp )
659 g_return_val_if_fail ( vvp != NULL, 0 );
663 gint vik_viewport_get_height( VikViewport *vvp )
665 g_return_val_if_fail ( vvp != NULL, 0 );
669 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
671 if ( vvp->coord_mode == VIK_COORD_UTM ) {
673 struct UTM *utm = (struct UTM *) coord;
674 coord->mode = VIK_COORD_UTM;
676 g_return_if_fail ( vvp != NULL );
678 utm->zone = vvp->center.utm_zone;
679 utm->letter = vvp->center.utm_letter;
680 utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
681 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
682 utm->zone += zone_delta;
683 utm->easting -= zone_delta * vvp->utm_zone_width;
684 utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
685 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
686 coord->mode = VIK_COORD_LATLON;
687 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
688 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);
689 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
691 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
692 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
695 --> THIS IS JUNK HERE.
696 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
698 (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
699 DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
705 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
708 g_return_if_fail ( vvp != NULL );
710 if ( coord->mode != vvp->coord_mode )
712 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
713 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
717 if ( vvp->coord_mode == VIK_COORD_UTM ) {
718 struct UTM *center = (struct UTM *) &(vvp->center);
719 struct UTM *utm = (struct UTM *) coord;
720 if ( center->zone != utm->zone && vvp->one_utm_zone )
722 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
726 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
727 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
728 *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
729 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
730 struct LatLon *center = (struct LatLon *) &(vvp->center);
731 struct LatLon *ll = (struct LatLon *) coord;
733 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
734 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 );
736 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
737 /* FIXMERCATOR: Optimize */
738 *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
739 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
744 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
746 if ( *x1 > 20000 || *x1 < -20000 ) {
747 gdouble shrinkfactor = ABS(20000.0 / *x1);
748 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
749 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
750 } else if ( *y1 > 20000 || *y1 < -20000 ) {
751 gdouble shrinkfactor = ABS(20000.0 / *x1);
752 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
753 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
754 } else if ( *x2 > 20000 || *x2 < -20000 ) {
755 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
756 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
757 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
758 g_print("%f, %d, %d\n", shrinkfactor, *x2, *y2);
759 } else if ( *y2 > 20000 || *y2 < -20000 ) {
760 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
761 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
762 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
766 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
768 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
769 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
770 /*** clipping, yeah! ***/
771 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
772 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
776 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
778 if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
779 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
782 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
784 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
785 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
788 /* shouldn't use this -- slow -- change the alpha channel instead. */
789 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
790 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
792 gint real_dest_x = MAX(dest_x,0);
793 gint real_dest_y = MAX(dest_y,0);
796 return; /* don't waste your time */
798 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
800 if ( vvp->alpha_pixbuf )
801 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
802 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
803 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
804 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
807 w = MIN(w,vvp->width - dest_x);
808 h = MIN(h,vvp->height - dest_y);
810 /* check that we are drawing within boundaries. */
811 src_x += (real_dest_x - dest_x);
812 src_y += (real_dest_y - dest_y);
813 w -= (real_dest_x - dest_x);
814 h -= (real_dest_y - dest_y);
816 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
817 real_dest_x, real_dest_y, 0, 0, w, h );
820 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
822 /* draw pixbuf_tmp */
823 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
826 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
827 gint dest_x, gint dest_y, gint w, gint h )
829 gdk_draw_pixbuf ( vvp->scr_buffer,
830 // GTK_WIDGET(vvp)->style->black_gc,
833 src_x, src_y, dest_x, dest_y, w, h,
834 GDK_RGB_DITHER_NONE, 0, 0 );
837 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
839 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
843 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
845 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
848 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
851 return vvp->coord_mode;
854 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
856 g_return_if_fail ( vvp != NULL );
857 vvp->coord_mode = mode;
858 vik_coord_convert ( &(vvp->center), mode );
861 /* Thanks GPSDrive */
862 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 )
865 gdouble dif, lat, lon;
866 double Ra = Radius[90+(gint)zero_lat];
868 px = (mapSizeX2 - x) * pixelfact_x;
869 py = (-mapSizeY2 + y) * pixelfact_y;
871 lat = zero_lat - py / Ra;
872 lat = zero_lat - py / Ra;
876 cos (lat * DEG2RAD));
878 dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
879 lat = lat - dif / 1.5;
883 cos (lat * DEG2RAD));
890 /* Thanks GPSDrive */
891 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 )
895 gint mapSizeX = 2 * mapSizeX2;
896 gint mapSizeY = 2 * mapSizeY2;
898 g_assert ( lt >= -90.0 && lt <= 90.0 );
899 // lg *= rad2deg; // FIXME, optimize equations
901 Ra = Radius[90+(gint)lt];
903 cos (lt*DEG2RAD) * (lg - zero_long);
904 *y = Ra * (lt - zero_lat);
905 dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
906 *y = *y + dif / 1.85;
907 *x = *x / pixelfact_x;
908 *y = *y / pixelfact_y;
911 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
916 static void viewport_init_ra()
918 static gboolean done_before = FALSE;
922 for ( i = -90; i <= 90; i++)
923 Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
928 double calcR (double lat)
930 double a = 6378.137, r, sc, x, y, z;
931 double e2 = 0.081082 * 0.081082;
933 * the radius of curvature of an ellipsoidal Earth in the plane of the
934 * meridian is given by
936 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
939 * where a is the equatorial radius, b is the polar radius, and e is
940 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
942 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
943 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
944 * = 0.081082 Eccentricity
950 z = 1.0 - e2 * sc * sc;
957 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
959 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
962 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
964 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
965 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
968 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
970 static GdkGCValues values;
971 gdk_gc_get_values ( gc, &values );
972 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
975 GdkFunction vik_gc_get_function ( GdkGC *gc )
977 static GdkGCValues values;
978 gdk_gc_get_values ( gc, &values );
979 return values.function;
982 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
984 vvp->drawmode = drawmode;
985 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
986 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
988 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
992 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
994 return vvp->drawmode;
997 /******** triggering *******/
998 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1000 vp->trigger = trigger;
1003 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1008 void vik_viewport_snapshot_save ( VikViewport *vp )
1010 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1013 void vik_viewport_snapshot_load ( VikViewport *vp )
1015 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1018 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1020 vp->half_drawn = half_drawn;
1023 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1025 return vp->half_drawn;
1029 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1031 const gchar *name = NULL;
1032 VikWindow *vw = NULL;
1033 GtkWidget *mode_button;
1036 vw = VIK_WINDOW_FROM_WIDGET(vv);
1037 mode_button = vik_window_get_drawmode_button(vw, mode);
1038 label = gtk_bin_get_child(GTK_BIN(mode_button));
1040 name = gtk_label_get_text ( GTK_LABEL(label) );
1046 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1048 VikCoord tleft, tright, bleft, bright;
1050 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1051 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1052 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1053 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1055 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1056 vik_coord_convert(&tright, VIK_COORD_LATLON);
1057 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1058 vik_coord_convert(&bright, VIK_COORD_LATLON);
1060 *max_lat = MAX(tleft.north_south, tright.north_south);
1061 *min_lat = MIN(bleft.north_south, bright.north_south);
1062 *max_lon = MAX(tright.east_west, bright.east_west);
1063 *min_lon = MIN(tleft.east_west, bleft.east_west);