]> git.street.me.uk Git - andy/viking.git/blob - src/vikviewport.c
Merge remote branch 'rnorris/unitPref'
[andy/viking.git] / src / vikviewport.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * Lat/Lon plotting functions calcxy* are from GPSDrive
7  * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
8  *
9  * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
10  *
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.
15  *
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.
20  *
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
24  *
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
31
32 #include <gtk/gtk.h>
33 #ifdef HAVE_MATH_H
34 #include <math.h>
35 #endif
36
37 #include "coords.h"
38 #include "vikcoord.h"
39 #include "vikwindow.h"
40 #include "vikviewport.h"
41
42 #include "mapcoord.h"
43
44 /* for ALTI_TO_MPP */
45 #include "globals.h"
46
47 static gdouble EASTING_OFFSET = 500000.0;
48
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 );
53
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);
57
58 static double Radius[181];
59 static void viewport_init_ra();
60
61 static GObjectClass *parent_class;
62
63
64 struct _VikViewport {
65   GtkDrawingArea drawing_area;
66   GdkPixmap *scr_buffer;
67   gint width, height;
68   VikCoord center;
69   VikCoordMode coord_mode;
70   gdouble xmpp, ympp;
71
72   GdkPixbuf *alpha_pixbuf;
73   guint8 alpha_pixbuf_width;
74   guint8 alpha_pixbuf_height;
75
76   gdouble utm_zone_width;
77   gboolean one_utm_zone;
78
79   GdkGC *background_gc;
80   GdkColor background_color;
81   GdkGC *scale_bg_gc;
82   gboolean draw_scale;
83   gboolean draw_centermark;
84
85   /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
86   VikViewportDrawMode drawmode;
87
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;
93
94   /* trigger stuff */
95   gpointer trigger;
96   GdkPixmap *snapshot_buffer;
97   gboolean half_drawn;
98 };
99
100 static gdouble
101 viewport_utm_zone_width ( VikViewport *vvp )
102 {
103   if ( vvp->coord_mode == VIK_COORD_UTM ) {
104     struct LatLon ll;
105
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 );
110
111     /* boundary */
112     ll.lon = (utm.zone - 1) * 6 - 180 ;
113     a_coords_latlon_to_utm ( &ll, &utm);
114     return fabs ( utm.easting - EASTING_OFFSET ) * 2;
115   } else
116     return 0.0;
117 }
118
119
120 GType vik_viewport_get_type (void)
121 {
122   static GType vvp_type = 0;
123
124   if (!vvp_type)
125   {
126     static const GTypeInfo vvp_info = 
127     {
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),
135       0,
136       (GInstanceInitFunc) viewport_init,
137     };
138     vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
139   }
140   return vvp_type;
141 }
142
143 static void viewport_class_init ( VikViewportClass *klass )
144 {
145   /* Destructor */
146   GObjectClass *object_class;
147
148   object_class = G_OBJECT_CLASS (klass);
149
150   object_class->finalize = viewport_finalize;
151
152   parent_class = g_type_class_peek_parent (klass);
153 }
154
155 VikViewport *vik_viewport_new ()
156 {
157   VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
158   return vv;
159 }
160
161 static void viewport_init ( VikViewport *vvp )
162 {
163   viewport_init_ra();
164
165   /* TODO: not static */
166   vvp->xmpp = 4.0;
167   vvp->ympp = 4.0;
168   vvp->coord_mode = VIK_COORD_LATLON;
169   vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
170   vvp->center.mode = VIK_COORD_LATLON;
171   vvp->center.north_south = 40.714490;
172   vvp->center.east_west = -74.007130;
173   vvp->center.utm_zone = 31;
174   vvp->center.utm_letter = 'N';
175   vvp->scr_buffer = NULL;
176   vvp->alpha_pixbuf = NULL;
177   vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
178   vvp->utm_zone_width = 0.0;
179   vvp->background_gc = NULL;
180   vvp->scale_bg_gc = NULL;
181   vvp->draw_scale = TRUE;
182   vvp->draw_centermark = TRUE;
183
184   vvp->trigger = NULL;
185   vvp->snapshot_buffer = NULL;
186   vvp->half_drawn = FALSE;
187
188   g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
189
190   GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
191 }
192
193 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
194 {
195   GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
196   *rv = vvp->background_color;
197   return rv;
198 }
199
200 /* returns pointer to internal static storage, changes next time function called, use quickly */
201 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
202 {
203   static gchar color[8];
204   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));
205   return color;
206 }
207
208 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
209 {
210   g_assert ( vvp->background_gc );
211   gdk_color_parse ( colorname, &(vvp->background_color) );
212   gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
213 }
214
215 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
216 {
217   g_assert ( vvp->background_gc );
218   vvp->background_color = *color;
219   gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
220 }
221
222
223 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
224 {
225   GdkGC *rv;
226   GdkColor color;
227
228   rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
229   gdk_color_parse ( colorname, &color );
230   gdk_gc_set_rgb_fg_color ( rv, &color );
231   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
232   return rv;
233 }
234
235 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
236 {
237   GdkGC *rv;
238
239   rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
240   gdk_gc_set_rgb_fg_color ( rv, color );
241   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
242   return rv;
243 }
244
245 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
246 {
247   vvp->width = width;
248   vvp->height = height;
249   if ( vvp->scr_buffer )
250     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
251   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
252
253   /* TODO trigger: only if this is enabled !!! */
254   if ( vvp->snapshot_buffer )
255     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
256   vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
257 }
258
259
260 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
261 {
262   return vvp->scr_buffer;
263 }
264
265 gboolean vik_viewport_configure ( VikViewport *vvp )
266 {
267   g_return_val_if_fail ( vvp != NULL, TRUE );
268
269   vvp->width = GTK_WIDGET(vvp)->allocation.width;
270   vvp->height = GTK_WIDGET(vvp)->allocation.height;
271
272   if ( vvp->scr_buffer )
273     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
274
275   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
276
277   /* TODO trigger: only if enabled! */
278   if ( vvp->snapshot_buffer )
279     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
280
281   vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
282   /* TODO trigger */
283
284   /* this is down here so it can get a GC (necessary?) */
285   if ( ! vvp->background_gc )
286   {
287     vvp->background_gc = vik_viewport_new_gc ( vvp, "", 1 );
288     vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */
289   }
290   if ( !vvp->scale_bg_gc) {
291     vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
292   }
293
294   return FALSE; 
295 }
296
297 static void viewport_finalize ( GObject *gob )
298 {
299   VikViewport *vvp = VIK_VIEWPORT(gob);
300
301   g_return_if_fail ( vvp != NULL );
302
303   if ( vvp->scr_buffer )
304     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
305
306   if ( vvp->snapshot_buffer )
307     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
308
309   if ( vvp->alpha_pixbuf )
310     g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
311
312   if ( vvp->background_gc )
313     g_object_unref ( G_OBJECT ( vvp->background_gc ) );
314
315   if ( vvp->scale_bg_gc ) {
316     g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
317     vvp->scale_bg_gc = NULL;
318   }
319
320   G_OBJECT_CLASS(parent_class)->finalize(gob);
321 }
322
323 void vik_viewport_clear ( VikViewport *vvp )
324 {
325   g_return_if_fail ( vvp != NULL );
326   gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
327 }
328
329 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
330 {
331   vvp->draw_scale = draw_scale;
332 }
333
334 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
335 {
336   return vvp->draw_scale;
337 }
338
339 void vik_viewport_draw_scale ( VikViewport *vvp )
340 {
341   if ( vvp->draw_scale ) {
342     VikCoord left, right;
343     gdouble unit, base, diff, old_unit, old_diff, ratio;
344     gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
345     PangoFontDescription *pfd;
346     PangoLayout *pl;
347     gchar s[128];
348
349     g_return_if_fail ( vvp != NULL );
350
351     vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
352     vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
353
354     vik_units_distance_t dist_units = a_vik_get_units_distance ();
355     switch (dist_units) {
356     case VIK_UNITS_DISTANCE_KILOMETRES:
357       base = vik_coord_diff ( &left, &right ); // in meters
358       break;
359     case VIK_UNITS_DISTANCE_MILES:
360       // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
361       base = vik_coord_diff ( &left, &right ) * 0.00621371192;
362       break;
363     default:
364       base = 1; // Keep the compiler happy
365       g_critical("Houston, we've had a problem. distance=%d", dist_units);
366     }
367     ratio = (vvp->width/SCSIZE)/base;
368
369     unit = 1;
370     diff = fabs(base-unit);
371     old_unit = unit;
372     old_diff = diff;
373     odd = 1;
374     while (diff <= old_diff) {
375       old_unit = unit;
376       old_diff = diff;
377       unit = unit * (odd%2 ? 5 : 2);
378       diff = fabs(base-unit);
379       odd++;
380     }
381     unit = old_unit;
382     len = unit * ratio;
383
384     /* white background */
385     vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
386                          PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
387     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
388                          PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
389     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
390                          PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
391     /* black scale */
392     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
393                          PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
394     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
395                          PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
396     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
397                          PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
398     if (odd%2) {
399       int i;
400       for (i=1; i<5; i++) {
401         vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
402                              PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
403         vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
404                              PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
405       }
406     } else {
407       int i;
408       for (i=1; i<10; i++) {
409         vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
410                              PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
411         vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
412                              PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
413       }
414     }
415     pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); 
416     pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
417     pango_layout_set_font_description (pl, pfd);
418     pango_font_description_free (pfd);
419
420     switch (dist_units) {
421     case VIK_UNITS_DISTANCE_KILOMETRES:
422       if (unit >= 1000) {
423         sprintf(s, "%d km", (int)unit/1000);
424       } else {
425         sprintf(s, "%d m", (int)unit);
426       }
427       break;
428     case VIK_UNITS_DISTANCE_MILES:
429       // Handle units in 0.1 miles
430       if (unit < 10.0) {
431         sprintf(s, "%0.1f miles", unit/10.0);
432       }
433       else if ((int)unit == 10.0) {
434         sprintf(s, "1 mile");
435       }
436       else {
437         sprintf(s, "%d miles", (int)(unit/10.0));
438       }
439       break;
440     default:
441       g_critical("Houston, we've had a problem. distance=%d", dist_units);
442     }
443     pango_layout_set_text(pl, s, -1);
444     vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
445                            PAD + len + PAD, vvp->height - PAD - 10, pl);
446     g_object_unref(pl);
447     pl = NULL;
448   }
449 }
450
451 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
452 {
453   vvp->draw_centermark = draw_centermark;
454 }
455
456 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
457 {
458   return vvp->draw_centermark;
459 }
460
461 void vik_viewport_draw_centermark ( VikViewport *vvp )
462 {
463   if ( !vvp->draw_centermark )
464     return;
465
466   const int len = 30;
467   const int gap = 4;
468   int center_x = vvp->width/2;
469   int center_y = vvp->height/2;
470   GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
471
472   /* white back ground */
473   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
474   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
475   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
476   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
477   /* black fore ground */
478   vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
479   vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
480   vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
481   vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
482   
483 }
484
485 void vik_viewport_sync ( VikViewport *vvp )
486 {
487   g_return_if_fail ( vvp != NULL );
488   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);
489 }
490
491 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
492 {
493   gint x, y, wid, hei;
494
495   g_return_if_fail ( vvp != NULL );
496   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);
497
498   if (x_off >= 0) {
499     x = 0;
500     wid = x_off;
501   } else {
502     x = vvp->width+x_off; 
503     wid = -x_off;
504   }
505   if (y_off >= 0) {
506     y = 0;
507     hei = y_off;
508   } else {
509     y = vvp->height+y_off; 
510     hei = -y_off;
511   }
512   gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
513   gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
514 }
515
516 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
517 {
518   g_return_if_fail ( vvp != NULL );
519   if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
520     vvp->xmpp = vvp->ympp = xympp;
521
522   if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
523     viewport_utm_zone_check(vvp);
524 }
525
526 /* or could do factor */
527 void vik_viewport_zoom_in ( VikViewport *vvp )
528 {
529   g_return_if_fail ( vvp != NULL );
530   if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
531   {
532     vvp->xmpp /= 2;
533     vvp->ympp /= 2;
534
535     viewport_utm_zone_check(vvp);
536   }
537 }
538
539 void vik_viewport_zoom_out ( VikViewport *vvp )
540 {
541   g_return_if_fail ( vvp != NULL );
542   if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
543   {
544     vvp->xmpp *= 2;
545     vvp->ympp *= 2;
546
547     viewport_utm_zone_check(vvp);
548   }
549 }
550
551 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
552 {
553   if ( vvp->xmpp == vvp->ympp )
554     return vvp->xmpp;
555   return 0.0;
556 }
557
558 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
559 {
560   return vvp->xmpp;
561 }
562
563 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
564 {
565   return vvp->ympp;
566 }
567
568 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
569 {
570   if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
571     vvp->xmpp = xmpp;
572     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
573       viewport_utm_zone_check(vvp);
574   }
575 }
576
577 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
578 {
579   if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
580     vvp->ympp = ympp;
581     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
582       viewport_utm_zone_check(vvp);
583   }
584 }
585
586
587 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
588 {
589   g_return_val_if_fail ( vvp != NULL, NULL );
590   return &(vvp->center);
591 }
592
593 /* called every time we update coordinates/zoom */
594 static void viewport_utm_zone_check ( VikViewport *vvp )
595 {
596   if ( vvp->coord_mode == VIK_COORD_UTM )
597   {
598     struct UTM utm;
599     struct LatLon ll;
600     a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
601     a_coords_latlon_to_utm ( &ll, &utm );
602     if ( utm.zone != vvp->center.utm_zone )
603       *((struct UTM *)(&vvp->center)) = utm;
604
605     /* misc. stuff so we don't have to check later */
606     vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
607     vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
608   }
609 }
610
611 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
612 {
613   vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
614   if ( vvp->coord_mode == VIK_COORD_UTM )
615     viewport_utm_zone_check ( vvp );
616 }
617
618 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
619 {
620   vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
621   if ( vvp->coord_mode == VIK_COORD_UTM )
622     viewport_utm_zone_check ( vvp );
623 }
624
625 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
626 {
627   vvp->center = *coord;
628   if ( vvp->coord_mode == VIK_COORD_UTM )
629     viewport_utm_zone_check ( vvp );
630 }
631
632 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
633 {
634   g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
635
636   /* get center, then just offset */
637   vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
638   ul->mode = VIK_COORD_UTM;
639   *br = *ul;
640
641   ul->north_south += (vvp->ympp * vvp->height / 2);
642   ul->east_west -= (vvp->xmpp * vvp->width / 2);
643   br->north_south -= (vvp->ympp * vvp->height / 2);
644   br->east_west += (vvp->xmpp * vvp->width / 2);
645 }
646
647 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
648 {
649   if ( vvp->coord_mode == VIK_COORD_UTM ) {
650     *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
651     center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
652     center->zone = zone;
653   }
654 }
655
656 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
657 {
658   if ( vvp->coord_mode == VIK_COORD_UTM ) {
659     VikCoord coord;
660     g_assert ( vvp != NULL );
661     vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
662     return coord.utm_zone;
663   }
664   return '\0';
665 }
666
667 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
668 {
669   if ( vvp->coord_mode == VIK_COORD_UTM ) {
670     VikCoord coord;
671     g_assert ( vvp != NULL );
672     vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
673     return coord.utm_zone;
674   }
675   return '\0';
676 }
677
678
679 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
680 {
681   g_return_if_fail ( vvp != NULL );
682   if ( vvp->coord_mode == VIK_COORD_UTM ) {
683     /* slightly optimized */
684     vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
685     vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
686     viewport_utm_zone_check ( vvp );
687   } else {
688     VikCoord tmp;
689     vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
690     vik_viewport_set_center_coord ( vvp, &tmp );
691   }
692 }
693
694 gint vik_viewport_get_width( VikViewport *vvp )
695 {
696   g_return_val_if_fail ( vvp != NULL, 0 );
697   return vvp->width;
698 }
699
700 gint vik_viewport_get_height( VikViewport *vvp )
701 {
702   g_return_val_if_fail ( vvp != NULL, 0 );
703   return vvp->height;
704 }
705
706 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
707 {
708   if ( vvp->coord_mode == VIK_COORD_UTM ) {
709     int zone_delta;
710     struct UTM *utm = (struct UTM *) coord;
711     coord->mode = VIK_COORD_UTM;
712
713     g_return_if_fail ( vvp != NULL );
714
715     utm->zone = vvp->center.utm_zone;
716     utm->letter = vvp->center.utm_letter;
717     utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
718     zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
719     utm->zone += zone_delta;
720     utm->easting -= zone_delta * vvp->utm_zone_width;
721     utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
722   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
723     coord->mode = VIK_COORD_LATLON;
724     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
725       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);
726     else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
727       /* FIXMERCATOR */
728       coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
729       coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
730
731 #if 0
732 -->     THIS IS JUNK HERE.
733       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
734
735       (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
736       DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
737 #endif
738     }
739   }
740 }
741
742 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
743 {
744   static VikCoord tmp;
745   g_return_if_fail ( vvp != NULL );
746
747   if ( coord->mode != vvp->coord_mode )
748   {
749     g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
750     vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
751     coord = &tmp;
752   }
753
754   if ( vvp->coord_mode == VIK_COORD_UTM ) {
755     struct UTM *center = (struct UTM *) &(vvp->center);
756     struct UTM *utm = (struct UTM *) coord;
757     if ( center->zone != utm->zone && vvp->one_utm_zone )
758     {
759       *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
760       return;
761     }
762
763     *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
764           (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
765     *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
766   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
767     struct LatLon *center = (struct LatLon *) &(vvp->center);
768     struct LatLon *ll = (struct LatLon *) coord;
769     double xx,yy;
770     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
771       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 );
772       *x = xx; *y = yy;
773     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
774       /* FIXMERCATOR: Optimize */
775       *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
776       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
777     }
778   }
779 }
780
781 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
782 {
783   if ( *x1 > 20000 || *x1 < -20000 ) {
784     gdouble shrinkfactor = ABS(20000.0 / *x1);
785     *x1 = *x2 + (shrinkfactor * (*x1-*x2));
786     *y1 = *y2 + (shrinkfactor * (*y1-*y2));
787   } else if ( *y1 > 20000 || *y1 < -20000 ) {
788     gdouble shrinkfactor = ABS(20000.0 / *x1);
789     *x1 = *x2 + (shrinkfactor * (*x1-*x2));
790     *y1 = *y2 + (shrinkfactor * (*y1-*y2));
791   } else if ( *x2 > 20000 || *x2 < -20000 ) {
792     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
793     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
794     *y2 = *y1 + (shrinkfactor * (*y2-*y1));
795     g_print("%f, %d, %d\n", shrinkfactor, *x2, *y2);
796   } else if ( *y2 > 20000 || *y2 < -20000 ) {
797     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
798     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
799     *y2 = *y1 + (shrinkfactor * (*y2-*y1));
800   }
801 }
802
803 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
804 {
805   if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
806        ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
807     /*** clipping, yeah! ***/
808     a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
809     gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
810   }
811 }
812
813 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
814 {
815   if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
816     gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
817 }
818
819 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
820 {
821   if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
822     gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
823 }
824
825 /* shouldn't use this -- slow -- change the alpha channel instead. */
826 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
827                                            gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
828 {
829   gint real_dest_x = MAX(dest_x,0);
830   gint real_dest_y = MAX(dest_y,0);
831
832   if ( alpha == 0 )
833     return; /* don't waste your time */
834
835   if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
836   {
837     if ( vvp->alpha_pixbuf )
838       g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
839     vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
840     vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
841     vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
842   }
843
844   w = MIN(w,vvp->width - dest_x);
845   h = MIN(h,vvp->height - dest_y);
846
847   /* check that we are drawing within boundaries. */
848   src_x += (real_dest_x - dest_x);
849   src_y += (real_dest_y - dest_y);
850   w -= (real_dest_x - dest_x);
851   h -= (real_dest_y - dest_y);
852
853   gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
854                                  real_dest_x, real_dest_y, 0, 0, w, h );
855
856   /* do a composite */
857   gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
858
859   /* draw pixbuf_tmp */
860   vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
861 }
862
863 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
864                               gint dest_x, gint dest_y, gint w, gint h )
865 {
866   gdk_draw_pixbuf ( vvp->scr_buffer,
867 // GTK_WIDGET(vvp)->style->black_gc,
868 NULL,
869  pixbuf,
870                     src_x, src_y, dest_x, dest_y, w, h,
871                     GDK_RGB_DITHER_NONE, 0, 0 );
872 }
873
874 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
875 {
876   gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
877 }
878
879
880 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
881 {
882   gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
883 }
884
885 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
886 {
887   g_assert ( vvp );
888   return vvp->coord_mode;
889 }
890
891 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
892 {
893   g_return_if_fail ( vvp != NULL );
894   vvp->coord_mode = mode;
895   vik_coord_convert ( &(vvp->center), mode );
896 }
897
898 /* Thanks GPSDrive */
899 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 )
900 {
901   int px, py;
902   gdouble dif, lat, lon;
903   double Ra = Radius[90+(gint)zero_lat];
904
905   px = (mapSizeX2 - x) * pixelfact_x;
906   py = (-mapSizeY2 + y) * pixelfact_y;
907
908   lat = zero_lat - py / Ra;
909   lat = zero_lat - py / Ra;
910   lon =
911     zero_long -
912     px / (Ra *
913          cos (lat * DEG2RAD));
914
915   dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
916   lat = lat - dif / 1.5;
917   lon =
918     zero_long -
919     px / (Ra *
920               cos (lat * DEG2RAD));
921
922   *lt = lat;
923   *lg = lon;
924   return (TRUE);
925 }
926
927 /* Thanks GPSDrive */
928 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 )
929 {
930     double dif;
931     double Ra;
932     gint mapSizeX = 2 * mapSizeX2;
933     gint mapSizeY = 2 * mapSizeY2;
934
935     g_assert ( lt >= -90.0 && lt <= 90.0 );
936 //    lg *= rad2deg; // FIXME, optimize equations
937 //    lt *= rad2deg;
938     Ra = Radius[90+(gint)lt];
939     *x = Ra *
940          cos (lt*DEG2RAD) * (lg - zero_long);
941     *y = Ra * (lt - zero_lat);
942     dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
943     *y = *y + dif / 1.85;
944     *x = *x / pixelfact_x;
945     *y = *y / pixelfact_y;
946     *x = mapSizeX2 - *x;
947     *y += mapSizeY2;
948     if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
949         return (FALSE);
950     return (TRUE);
951 }
952
953 static void viewport_init_ra()
954 {
955   static gboolean done_before = FALSE;
956   if ( !done_before )
957   {
958     gint i;
959     for ( i = -90; i <= 90; i++)
960       Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
961     done_before = TRUE;
962   }
963 }
964
965 double calcR (double lat)
966 {
967     double a = 6378.137, r, sc, x, y, z;
968     double e2 = 0.081082 * 0.081082;
969     /*
970      * the radius of curvature of an ellipsoidal Earth in the plane of the
971      * meridian is given by
972      *
973      * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
974      *
975      *
976      * where a is the equatorial radius, b is the polar radius, and e is
977      * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
978      *
979      * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
980      * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
981      * = 0.081082 Eccentricity
982      */
983
984     lat = lat * DEG2RAD;
985     sc = sin (lat);
986     x = a * (1.0 - e2);
987     z = 1.0 - e2 * sc * sc;
988     y = pow (z, 1.5);
989     r = x / y;
990     r = r * 1000.0;
991     return r;
992 }
993
994 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
995 {
996   return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
997 }
998
999 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1000 {
1001   if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1002     gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1003 }
1004
1005 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1006 {
1007   static GdkGCValues values;
1008   gdk_gc_get_values ( gc, &values );
1009   gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1010 }
1011
1012 GdkFunction vik_gc_get_function ( GdkGC *gc )
1013 {
1014   static GdkGCValues values;
1015   gdk_gc_get_values ( gc, &values );
1016   return values.function;
1017 }
1018
1019 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1020 {
1021   vvp->drawmode = drawmode;
1022   if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1023     viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1024   else {
1025     viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1026   }
1027 }
1028
1029 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1030 {
1031   return vvp->drawmode;
1032 }
1033
1034 /******** triggering *******/
1035 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1036 {
1037   vp->trigger = trigger;
1038 }
1039
1040 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1041 {
1042   return vp->trigger;
1043 }
1044
1045 void vik_viewport_snapshot_save ( VikViewport *vp )
1046 {
1047   gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1048 }
1049
1050 void vik_viewport_snapshot_load ( VikViewport *vp )
1051 {
1052   gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1053 }
1054
1055 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1056 {
1057   vp->half_drawn = half_drawn;
1058 }
1059
1060 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1061 {
1062   return vp->half_drawn;
1063 }
1064
1065
1066 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1067  {
1068   const gchar *name = NULL;
1069   VikWindow *vw = NULL;
1070   GtkWidget *mode_button;
1071   GtkWidget *label;
1072   
1073   vw = VIK_WINDOW_FROM_WIDGET(vv);
1074   mode_button = vik_window_get_drawmode_button(vw, mode);
1075   label = gtk_bin_get_child(GTK_BIN(mode_button));
1076
1077   name = gtk_label_get_text ( GTK_LABEL(label) );
1078
1079   return name;
1080
1081 }
1082
1083 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1084 {
1085   VikCoord tleft, tright, bleft, bright;
1086
1087   vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1088   vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1089   vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1090   vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1091
1092   vik_coord_convert(&tleft, VIK_COORD_LATLON);
1093   vik_coord_convert(&tright, VIK_COORD_LATLON);
1094   vik_coord_convert(&bleft, VIK_COORD_LATLON);
1095   vik_coord_convert(&bright, VIK_COORD_LATLON);
1096
1097   *max_lat = MAX(tleft.north_south, tright.north_south);
1098   *min_lat = MIN(bleft.north_south, bright.north_south);
1099   *max_lon = MAX(tright.east_west, bright.east_west);
1100   *min_lon = MIN(tleft.east_west, bleft.east_west);
1101 }