]> git.street.me.uk Git - andy/viking.git/blob - src/vikcoordlayer.c
fix plotting of DEM data on elevation graph
[andy/viking.git] / src / vikcoordlayer.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  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include <math.h>
22
23 #include "viking.h"
24 #include "vikcoordlayer_pixmap.h"
25
26 static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len );
27 static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
28 static gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp );
29 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id );
30 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color );
31 static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file );
32
33 static VikLayerParamScale param_scales[] = {
34   { 0.05, 60.0, 0.25, 10 },
35   { 1, 10, 1, 0 },
36 };
37
38 static VikLayerParam coord_layer_params[] = {
39   { "color", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, "Color:", VIK_LAYER_WIDGET_ENTRY },
40   { "min_inc", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, "Minutes Width:", VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
41   { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, "Line Thickness:", VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 },
42 };
43
44
45 enum { PARAM_COLOR = 0, PARAM_MIN_INC, PARAM_LINE_THICKNESS, NUM_PARAMS };
46
47 VikLayerInterface vik_coord_layer_interface = {
48   "Coord",
49   &coordlayer_pixbuf,
50
51   NULL,
52   0,
53
54   coord_layer_params,
55   NUM_PARAMS,
56   NULL,
57   0,
58
59   VIK_MENU_ITEM_ALL,
60
61   (VikLayerFuncCreate)                  vik_coord_layer_create,
62   (VikLayerFuncRealize)                 NULL,
63                                         coord_layer_post_read,
64   (VikLayerFuncFree)                    vik_coord_layer_free,
65
66   (VikLayerFuncProperties)              NULL,
67   (VikLayerFuncDraw)                    vik_coord_layer_draw,
68   (VikLayerFuncChangeCoordMode)         NULL,
69
70   (VikLayerFuncSetMenuItemsSelection)   NULL,
71   (VikLayerFuncGetMenuItemsSelection)   NULL,
72
73   (VikLayerFuncAddMenuItems)            NULL,
74   (VikLayerFuncSublayerAddMenuItems)    NULL,
75
76   (VikLayerFuncSublayerRenameRequest)   NULL,
77   (VikLayerFuncSublayerToggleVisible)   NULL,
78
79   (VikLayerFuncMarshall)                coord_layer_marshall,
80   (VikLayerFuncUnmarshall)              coord_layer_unmarshall,
81
82   (VikLayerFuncSetParam)                coord_layer_set_param,
83   (VikLayerFuncGetParam)                coord_layer_get_param,
84
85   (VikLayerFuncReadFileData)            NULL,
86   (VikLayerFuncWriteFileData)           NULL,
87
88   (VikLayerFuncDeleteItem)              NULL,
89   (VikLayerFuncCopyItem)                NULL,
90   (VikLayerFuncPasteItem)               NULL,
91   (VikLayerFuncFreeCopiedItem)          NULL,
92   (VikLayerFuncDragDropRequest)         NULL,
93 };
94
95 struct _VikCoordLayer {
96   VikLayer vl;
97   GdkGC *gc;
98   gdouble deg_inc;
99   guint8 line_thickness;
100   gchar *color;
101 };
102
103 GType vik_coord_layer_get_type ()
104 {
105   static GType vcl_type = 0;
106
107   if (!vcl_type)
108   {
109     static const GTypeInfo vcl_info =
110     {
111       sizeof (VikCoordLayerClass),
112       NULL, /* base_init */
113       NULL, /* base_finalize */
114       NULL, /* class init */
115       NULL, /* class_finalize */
116       NULL, /* class_data */
117       sizeof (VikCoordLayer),
118       0,
119       NULL /* instance init */
120     };
121     vcl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikCoordLayer", &vcl_info, 0 );
122   }
123
124   return vcl_type;
125 }
126
127 static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len )
128 {
129   vik_layer_marshall_params ( VIK_LAYER(vcl), data, len );
130 }
131
132 static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
133 {
134   VikCoordLayer *rv = vik_coord_layer_new ( vvp );
135   vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp );
136   return rv;
137 }
138
139 gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp )
140 {
141   switch ( id )
142   {
143     case PARAM_COLOR: if ( vcl->color ) g_free ( vcl->color ); vcl->color = g_strdup ( data.s ); break;
144     case PARAM_MIN_INC: vcl->deg_inc = data.d / 60.0; break;
145     case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vcl->line_thickness = data.u; break;
146   }
147   return TRUE;
148 }
149
150 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id )
151 {
152   VikLayerParamData rv;
153   switch ( id )
154   {
155     case PARAM_COLOR: rv.s = vcl->color ? vcl->color : ""; break;
156     case PARAM_MIN_INC: rv.d = vcl->deg_inc * 60.0; break;
157     case PARAM_LINE_THICKNESS: rv.i = vcl->line_thickness; break;
158   }
159   return rv;
160 }
161
162 static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file )
163 {
164   VikCoordLayer *vcl = VIK_COORD_LAYER(vl);
165   if ( vcl->gc )
166     g_object_unref ( G_OBJECT(vcl->gc) );
167
168   vcl->gc = vik_viewport_new_gc ( vp, vcl->color, vcl->line_thickness );
169 }
170
171 VikCoordLayer *vik_coord_layer_new ( )
172 {
173   VikCoordLayer *vcl = VIK_COORD_LAYER ( g_object_new ( VIK_COORD_LAYER_TYPE, NULL ) );
174   vik_layer_init ( VIK_LAYER(vcl), VIK_LAYER_COORD );
175
176   vcl->gc = NULL;
177   vcl->deg_inc = 1.0/60.0;
178   vcl->line_thickness = 3;
179   vcl->color = NULL;
180   return vcl;
181 }
182
183 void vik_coord_layer_draw ( VikCoordLayer *vcl, gpointer data )
184 {
185   VikViewport *vp = (VikViewport *) data;
186
187   if ( !vcl->gc ) {
188     return;
189   }
190
191   if ( vik_viewport_get_coord_mode(vp) != VIK_COORD_UTM ) 
192   {
193     VikCoord left, right, left2, right2;
194     gdouble l, r, i, j;
195     gint x1, y1, x2, y2, smod = 1, mmod = 1;
196     gboolean mins = FALSE, secs = FALSE;
197     GdkGC *dgc = vik_viewport_new_gc(vp, vcl->color, vcl->line_thickness);
198     GdkGC *mgc = vik_viewport_new_gc(vp, vcl->color, MAX(vcl->line_thickness/2, 1));
199     GdkGC *sgc = vik_viewport_new_gc(vp, vcl->color, MAX(vcl->line_thickness/5, 1));
200
201     vik_viewport_screen_to_coord ( vp, 0, 0, &left );
202     vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &right );
203     vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &left2 );
204     vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &right2 );
205
206 #define CLINE(gc, c1, c2) { \
207           vik_viewport_coord_to_screen(vp, (c1), &x1, &y1);  \
208           vik_viewport_coord_to_screen(vp, (c2), &x2, &y2);  \
209           vik_viewport_draw_line (vp, (gc), x1, y1, x2, y2); \
210         }
211
212     l = left.east_west;
213     r = right.east_west;
214     if (60*fabs(l-r) < 4) {
215       secs = TRUE;
216       smod = MIN(6, (int)ceil(3600*fabs(l-r)/30.0));
217     }
218     if (fabs(l-r) < 4) {
219       mins = TRUE;
220       mmod = MIN(6, (int)ceil(60*fabs(l-r)/30.0));
221     }
222     for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
223       if (secs) {
224         for (j=i*60+1; j<(i+1)*60; j+=1.0) {
225           left.east_west = j/3600.0;
226           left2.east_west = j/3600.0;
227           if ((int)j % smod == 0) CLINE(sgc, &left, &left2);
228         }
229       }
230       if (mins) {
231         left.east_west = i/60.0;
232         left2.east_west = i/60.0;
233         if ((int)i % mmod == 0) CLINE(mgc, &left, &left2);
234       }
235       if ((int)i % 60 == 0) {
236         left.east_west = i/60.0;
237         left2.east_west = i/60.0;
238         CLINE(dgc, &left, &left2);
239       }
240     }
241
242     vik_viewport_screen_to_coord ( vp, 0, 0, &left );
243     l = left2.north_south;
244     r = left.north_south;
245     for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
246       if (secs) {
247         for (j=i*60+1; j<(i+1)*60; j+=1.0) {
248           left.north_south = j/3600.0;
249           right.north_south = j/3600.0;
250           if ((int)j % smod == 0) CLINE(sgc, &left, &right);
251         }
252       }
253       if (mins) {
254         left.north_south = i/60.0;
255         right.north_south = i/60.0;
256         if ((int)i % mmod == 0) CLINE(mgc, &left, &right);
257       }
258       if ((int)i % 60 == 0) {
259         left.north_south = i/60.0;
260         right.north_south = i/60.0;
261         CLINE(dgc, &left, &right);
262       }
263     }
264 #undef CLINE
265     g_object_unref(dgc);
266     g_object_unref(sgc);
267     g_object_unref(mgc);
268     return;
269   }
270
271   if ( vik_viewport_get_coord_mode(vp) == VIK_COORD_UTM ) 
272   {
273     const struct UTM *center = (const struct UTM *)vik_viewport_get_center ( vp );
274     gdouble xmpp = vik_viewport_get_xmpp ( vp ), ympp = vik_viewport_get_ympp ( vp );
275     guint16 width = vik_viewport_get_width ( vp ), height = vik_viewport_get_height ( vp );
276     struct LatLon ll, ll2, min, max;
277     double lon;
278     int x1, x2;
279     struct UTM utm;
280
281     utm = *center;
282     utm.northing = center->northing - ( ympp * height / 2 );
283
284     a_coords_utm_to_latlon ( &utm, &ll );
285
286     utm.northing = center->northing + ( ympp * height / 2 );
287
288     a_coords_utm_to_latlon ( &utm, &ll2 );
289
290     {
291       /* find corner coords in lat/lon.
292         start at whichever is less: top or bottom left lon. goto whichever more: top or bottom right lon
293       */
294       struct LatLon topleft, topright, bottomleft, bottomright;
295       struct UTM temp_utm;
296       temp_utm = *center;
297       temp_utm.easting -= (width/2)*xmpp;
298       temp_utm.northing += (height/2)*ympp;
299       a_coords_utm_to_latlon ( &temp_utm, &topleft );
300       temp_utm.easting += (width*xmpp);
301       a_coords_utm_to_latlon ( &temp_utm, &topright );
302       temp_utm.northing -= (height*ympp);
303       a_coords_utm_to_latlon ( &temp_utm, &bottomright );
304       temp_utm.easting -= (width*xmpp);
305       a_coords_utm_to_latlon ( &temp_utm, &bottomleft );
306       min.lon = (topleft.lon < bottomleft.lon) ? topleft.lon : bottomleft.lon;
307       max.lon = (topright.lon > bottomright.lon) ? topright.lon : bottomright.lon;
308       min.lat = (bottomleft.lat < bottomright.lat) ? bottomleft.lat : bottomright.lat;
309       max.lat = (topleft.lat > topright.lat) ? topleft.lat : topright.lat;
310     }
311
312     lon = ((double) ((long) ((min.lon)/ vcl->deg_inc))) * vcl->deg_inc;
313     ll.lon = ll2.lon = lon;
314
315     for (; ll.lon <= max.lon; ll.lon+=vcl->deg_inc, ll2.lon+=vcl->deg_inc )
316     {
317       a_coords_latlon_to_utm ( &ll, &utm );
318       x1 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
319       a_coords_latlon_to_utm ( &ll2, &utm );
320       x2 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
321       vik_viewport_draw_line (vp, vcl->gc, x1, height, x2, 0);
322     }
323
324     utm = *center;
325     utm.easting = center->easting - ( xmpp * width / 2 );
326
327     a_coords_utm_to_latlon ( &utm, &ll );
328
329     utm.easting = center->easting + ( xmpp * width / 2 );
330
331     a_coords_utm_to_latlon ( &utm, &ll2 );
332
333     /* really lat, just reusing a variable */
334     lon = ((double) ((long) ((min.lat)/ vcl->deg_inc))) * vcl->deg_inc;
335     ll.lat = ll2.lat = lon;
336
337     for (; ll.lat <= max.lat ; ll.lat+=vcl->deg_inc, ll2.lat+=vcl->deg_inc )
338     {
339       a_coords_latlon_to_utm ( &ll, &utm );
340       x1 = (height / 2) - ( (utm.northing - center->northing) / ympp );
341       a_coords_latlon_to_utm ( &ll2, &utm );
342       x2 = (height / 2) - ( (utm.northing - center->northing) / ympp );
343       vik_viewport_draw_line (vp, vcl->gc, width, x2, 0, x1);
344     }
345   }
346 }
347
348 void vik_coord_layer_free ( VikCoordLayer *vcl )
349 {
350   if ( vcl->gc != NULL )
351     g_object_unref ( G_OBJECT(vcl->gc) );
352
353   if ( vcl->color != NULL )
354     g_free ( vcl->color );
355 }
356
357 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color )
358 {
359   if ( vcl->color )
360     g_free ( vcl->color );
361
362   vcl->color = g_strdup ( color );
363
364   if ( vcl->gc )
365     g_object_unref ( G_OBJECT(vcl->gc) );
366
367   vcl->gc = vik_viewport_new_gc ( vp, vcl->color, vcl->line_thickness );
368 }
369
370 VikCoordLayer *vik_coord_layer_create ( VikViewport *vp )
371 {
372   VikCoordLayer *vcl = vik_coord_layer_new ();
373   coord_layer_update_gc ( vcl, vp, "red" );
374   return vcl;
375 }
376