]> git.street.me.uk Git - andy/viking.git/blob - src/usgs.c
Use configure.ac version
[andy/viking.git] / src / usgs.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
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"
28
29 #define MARGIN_OF_ERROR 0.0001
30 #define REALLYCLOSE(x,y) (ABS((x)-(y)) < MARGIN_OF_ERROR)
31
32 #define USGS_SCALE_TO_MPP .1016
33 #define TZ(x) ((x)*USGS_SCALE_TO_MPP)
34
35 /* defined as "2" = "25k" */
36 static gint mpp_to_scale ( gdouble mpp )
37 {
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;
41 }
42
43 gboolean usgs_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
44 {
45   g_assert ( src->mode == VIK_COORD_UTM );
46
47   if ( xmpp != ympp )
48     return FALSE;
49
50   dest->scale = mpp_to_scale ( xmpp );
51   if ( ! dest->scale )
52     return FALSE;
53
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;
58 }
59
60 void usgs_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
61 {
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;
67 }
68
69 gint usgs_scale_to_drg ( gint scale )
70 {
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;
81   }
82 }
83
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;
88   }
89
90
91 void usgs_download ( MapCoord *src, const gchar *dest_fn )
92 {
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 );
101 }