2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
24 #include "vikcoordlayer_pixmap.h"
26 static VikCoordLayer *coord_layer_copy ( VikCoordLayer *vcl, gpointer vp );
27 static gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp );
28 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id );
29 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color );
30 static void coord_layer_post_read ( VikCoordLayer *vcl, VikViewport *vp );
32 VikLayerParamScale param_scales[] = {
33 { 0.05, 60.0, 0.25, 10 },
37 VikLayerParam coord_layer_params[] = {
38 { "color", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, "Color:", VIK_LAYER_WIDGET_ENTRY },
39 { "min_inc", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, "Minutes Width:", VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
40 { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, "Line Thickness:", VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 },
44 enum { PARAM_COLOR = 0, PARAM_MIN_INC, PARAM_LINE_THICKNESS, NUM_PARAMS };
46 VikLayerInterface vik_coord_layer_interface = {
58 (VikLayerFuncCreate) vik_coord_layer_create,
59 (VikLayerFuncRealize) NULL,
60 (VikLayerFuncPostRead) coord_layer_post_read,
61 (VikLayerFuncFree) vik_coord_layer_free,
63 (VikLayerFuncProperties) NULL,
64 (VikLayerFuncDraw) vik_coord_layer_draw,
65 (VikLayerFuncChangeCoordMode) NULL,
67 (VikLayerFuncAddMenuItems) NULL,
68 (VikLayerFuncSublayerAddMenuItems) NULL,
70 (VikLayerFuncSublayerRenameRequest) NULL,
71 (VikLayerFuncSublayerToggleVisible) NULL,
73 (VikLayerFuncCopy) coord_layer_copy,
75 (VikLayerFuncSetParam) coord_layer_set_param,
76 (VikLayerFuncGetParam) coord_layer_get_param,
78 (VikLayerFuncReadFileData) NULL,
79 (VikLayerFuncWriteFileData) NULL,
81 (VikLayerFuncCopyItem) NULL,
82 (VikLayerFuncPasteItem) NULL,
83 (VikLayerFuncFreeCopiedItem) NULL,
84 (VikLayerFuncDragDropRequest) NULL,
87 struct _VikCoordLayer {
91 guint8 line_thickness;
95 GType vik_coord_layer_get_type ()
97 static GType vcl_type = 0;
101 static const GTypeInfo vcl_info =
103 sizeof (VikCoordLayerClass),
104 NULL, /* base_init */
105 NULL, /* base_finalize */
106 NULL, /* class init */
107 NULL, /* class_finalize */
108 NULL, /* class_data */
109 sizeof (VikCoordLayer),
111 NULL /* instance init */
113 vcl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikCoordLayer", &vcl_info, 0 );
119 static VikCoordLayer *coord_layer_copy ( VikCoordLayer *vcl, gpointer vp )
121 VikCoordLayer *rv = vik_coord_layer_new ( );
123 rv->color = g_strdup ( vcl->color );
124 rv->deg_inc = vcl->deg_inc;
125 rv->line_thickness = vcl->line_thickness;
127 g_object_ref ( rv->gc );
131 gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp )
135 case PARAM_COLOR: if ( vcl->color ) g_free ( vcl->color ); vcl->color = g_strdup ( data.s ); break;
136 case PARAM_MIN_INC: vcl->deg_inc = data.d / 60.0; break;
137 case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vcl->line_thickness = data.u; break;
142 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id )
144 VikLayerParamData rv;
147 case PARAM_COLOR: rv.s = vcl->color ? vcl->color : ""; break;
148 case PARAM_MIN_INC: rv.d = vcl->deg_inc * 60.0; break;
149 case PARAM_LINE_THICKNESS: rv.i = vcl->line_thickness; break;
154 static void coord_layer_post_read ( VikCoordLayer *vcl, VikViewport *vp )
157 g_object_unref ( G_OBJECT(vcl->gc) );
159 vcl->gc = vik_viewport_new_gc ( vp, vcl->color, vcl->line_thickness );
162 VikCoordLayer *vik_coord_layer_new ( )
164 VikCoordLayer *vcl = VIK_COORD_LAYER ( g_object_new ( VIK_COORD_LAYER_TYPE, NULL ) );
165 vik_layer_init ( VIK_LAYER(vcl), VIK_LAYER_COORD );
168 vcl->deg_inc = 1.0/60.0;
169 vcl->line_thickness = 3;
174 void vik_coord_layer_draw ( VikCoordLayer *vcl, gpointer data )
176 VikViewport *vp = (VikViewport *) data;
182 if ( vik_viewport_get_coord_mode(vp) != VIK_COORD_UTM )
184 VikCoord left, right, left2, right2;
186 gint x1, y1, x2, y2, smod = 1, mmod = 1;
187 gboolean mins = FALSE, secs = FALSE;
188 GdkGC *dgc = vik_viewport_new_gc(vp, vcl->color, vcl->line_thickness);
189 GdkGC *mgc = vik_viewport_new_gc(vp, vcl->color, MAX(vcl->line_thickness/2, 1));
190 GdkGC *sgc = vik_viewport_new_gc(vp, vcl->color, MAX(vcl->line_thickness/5, 1));
192 vik_viewport_screen_to_coord ( vp, 0, 0, &left );
193 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &right );
194 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &left2 );
195 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &right2 );
197 #define CLINE(gc, c1, c2) { \
198 vik_viewport_coord_to_screen(vp, (c1), &x1, &y1); \
199 vik_viewport_coord_to_screen(vp, (c2), &x2, &y2); \
200 vik_viewport_draw_line (vp, (gc), x1, y1, x2, y2); \
205 if (60*fabs(l-r) < 4) {
207 smod = MIN(6, (int)ceil(3600*fabs(l-r)/30.0));
211 mmod = MIN(6, (int)ceil(60*fabs(l-r)/30.0));
213 for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
215 for (j=i*60+1; j<(i+1)*60; j+=1.0) {
216 left.east_west = j/3600.0;
217 left2.east_west = j/3600.0;
218 if ((int)j % smod == 0) CLINE(sgc, &left, &left2);
222 left.east_west = i/60.0;
223 left2.east_west = i/60.0;
224 if ((int)i % mmod == 0) CLINE(mgc, &left, &left2);
226 if ((int)i % 60 == 0) {
227 left.east_west = i/60.0;
228 left2.east_west = i/60.0;
229 CLINE(dgc, &left, &left2);
233 vik_viewport_screen_to_coord ( vp, 0, 0, &left );
234 l = left2.north_south;
235 r = left.north_south;
236 for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
238 for (j=i*60+1; j<(i+1)*60; j+=1.0) {
239 left.north_south = j/3600.0;
240 right.north_south = j/3600.0;
241 if ((int)j % smod == 0) CLINE(sgc, &left, &right);
245 left.north_south = i/60.0;
246 right.north_south = i/60.0;
247 if ((int)i % mmod == 0) CLINE(mgc, &left, &right);
249 if ((int)i % 60 == 0) {
250 left.north_south = i/60.0;
251 right.north_south = i/60.0;
252 CLINE(dgc, &left, &right);
262 if ( vik_viewport_get_coord_mode(vp) == VIK_COORD_UTM )
264 const struct UTM *center = (const struct UTM *)vik_viewport_get_center ( vp );
265 gdouble xmpp = vik_viewport_get_xmpp ( vp ), ympp = vik_viewport_get_ympp ( vp );
266 guint16 width = vik_viewport_get_width ( vp ), height = vik_viewport_get_height ( vp );
267 struct LatLon ll, ll2, min, max;
273 utm.northing = center->northing - ( ympp * height / 2 );
275 a_coords_utm_to_latlon ( &utm, &ll );
277 utm.northing = center->northing + ( ympp * height / 2 );
279 a_coords_utm_to_latlon ( &utm, &ll2 );
282 /* find corner coords in lat/lon.
283 start at whichever is less: top or bottom left lon. goto whichever more: top or bottom right lon
285 struct LatLon topleft, topright, bottomleft, bottomright;
288 temp_utm.easting -= (width/2)*xmpp;
289 temp_utm.northing += (height/2)*ympp;
290 a_coords_utm_to_latlon ( &temp_utm, &topleft );
291 temp_utm.easting += (width*xmpp);
292 a_coords_utm_to_latlon ( &temp_utm, &topright );
293 temp_utm.northing -= (height*ympp);
294 a_coords_utm_to_latlon ( &temp_utm, &bottomright );
295 temp_utm.easting -= (width*xmpp);
296 a_coords_utm_to_latlon ( &temp_utm, &bottomleft );
297 min.lon = (topleft.lon < bottomleft.lon) ? topleft.lon : bottomleft.lon;
298 max.lon = (topright.lon > bottomright.lon) ? topright.lon : bottomright.lon;
299 min.lat = (bottomleft.lat < bottomright.lat) ? bottomleft.lat : bottomright.lat;
300 max.lat = (topleft.lat > topright.lat) ? topleft.lat : topright.lat;
303 lon = ((double) ((long) ((min.lon)/ vcl->deg_inc))) * vcl->deg_inc;
304 ll.lon = ll2.lon = lon;
306 for (; ll.lon <= max.lon; ll.lon+=vcl->deg_inc, ll2.lon+=vcl->deg_inc )
308 a_coords_latlon_to_utm ( &ll, &utm );
309 x1 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
310 a_coords_latlon_to_utm ( &ll2, &utm );
311 x2 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
312 vik_viewport_draw_line (vp, vcl->gc, x1, height, x2, 0);
316 utm.easting = center->easting - ( xmpp * width / 2 );
318 a_coords_utm_to_latlon ( &utm, &ll );
320 utm.easting = center->easting + ( xmpp * width / 2 );
322 a_coords_utm_to_latlon ( &utm, &ll2 );
324 /* really lat, just reusing a variable */
325 lon = ((double) ((long) ((min.lat)/ vcl->deg_inc))) * vcl->deg_inc;
326 ll.lat = ll2.lat = lon;
328 for (; ll.lat <= max.lat ; ll.lat+=vcl->deg_inc, ll2.lat+=vcl->deg_inc )
330 a_coords_latlon_to_utm ( &ll, &utm );
331 x1 = (height / 2) - ( (utm.northing - center->northing) / ympp );
332 a_coords_latlon_to_utm ( &ll2, &utm );
333 x2 = (height / 2) - ( (utm.northing - center->northing) / ympp );
334 vik_viewport_draw_line (vp, vcl->gc, width, x2, 0, x1);
339 void vik_coord_layer_free ( VikCoordLayer *vcl )
341 if ( vcl->gc != NULL )
342 g_object_unref ( G_OBJECT(vcl->gc) );
344 if ( vcl->color != NULL )
345 g_free ( vcl->color );
348 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color )
351 g_free ( vcl->color );
353 vcl->color = g_strdup ( color );
356 g_object_unref ( G_OBJECT(vcl->gc) );
358 vcl->gc = vik_viewport_new_gc ( vp, vcl->color, vcl->line_thickness );
361 VikCoordLayer *vik_coord_layer_create ( VikViewport *vp )
363 VikCoordLayer *vcl = vik_coord_layer_new ();
364 coord_layer_update_gc ( vcl, vp, "red" );