Generated files should not appears on CVS
[viking.git] / src / usgs.c
blobaa9649adb5c06f12e9807ded57aa4e667192689c
1 /*
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
22 #include <gtk/gtk.h>
23 #include <math.h>
24 #include "coords.h"
25 #include "vikcoord.h"
26 #include "mapcoord.h"
27 #include "http.h"
29 #define MARGIN_OF_ERROR 0.0001
30 #define REALLYCLOSE(x,y) (ABS((x)-(y)) < MARGIN_OF_ERROR)
32 #define USGS_SCALE_TO_MPP .1016
33 #define TZ(x) ((x)*USGS_SCALE_TO_MPP)
35 /* defined as "2" = "25k" */
36 static gint mpp_to_scale ( gdouble mpp )
38 if ( REALLYCLOSE(mpp,TZ(10)) || REALLYCLOSE(mpp,TZ(24)) || REALLYCLOSE(mpp,TZ(25)) || REALLYCLOSE(mpp,TZ(50)) || REALLYCLOSE(mpp,TZ(100)) || REALLYCLOSE(mpp,TZ(200)) || REALLYCLOSE(mpp,TZ(250)) || REALLYCLOSE(mpp,TZ(500)) )
39 return (gint) (mpp / .1016);
40 return 0;
43 gboolean usgs_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
45 g_assert ( src->mode == VIK_COORD_UTM );
47 if ( xmpp != ympp )
48 return FALSE;
50 dest->scale = mpp_to_scale ( xmpp );
51 if ( ! dest->scale )
52 return FALSE;
54 dest->x = (gint)(((gint)(src->east_west))/(800*xmpp));
55 dest->y = (gint)(((gint)(src->north_south))/(600*xmpp));
56 dest->z = src->utm_zone;
57 return TRUE;
60 void usgs_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
62 gdouble mpp = TZ ( src->scale );
63 dest->mode = VIK_COORD_UTM;
64 dest->utm_zone = src->z;
65 dest->east_west = ((src->x * 800) + 400) * mpp;
66 dest->north_south = ((src->y * 600) + 300) * mpp;
69 gint usgs_scale_to_drg ( gint scale )
71 switch ( scale ) {
72 case 10:
73 case 25:
74 case 50:
75 return 25;
76 case 100:
77 case 200:
78 return 100;
79 default:
80 return 250;
84 static const char *usgs_scale_factor() {
85 static char str[11];
86 snprintf(str,sizeof(str),"%d%d%d", 044, 393, 0xA573);
87 return str;
91 void usgs_download ( MapCoord *src, const gchar *dest_fn )
93 /* find center as above */
94 gdouble mpp = TZ ( src->scale );
95 gint easting = ((src->x * 800) + 400) * mpp;
96 gint northing = ((src->y * 600) + 300) * mpp;
97 gchar *uri = g_strdup_printf ( "/map.asp?z=%d&e=%d&n=%d&datum=NAD83&u=4&size=l&s=%d&chkDRG=DRG%d",
98 src->z, easting, northing, src->scale, usgs_scale_to_drg(src->scale) );
99 usgs_hack ( usgs_scale_factor(), uri, dest_fn );
100 g_free ( uri );