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
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);
43 gboolean
usgs_coord_to_mapcoord ( const VikCoord
*src
, gdouble xmpp
, gdouble ympp
, MapCoord
*dest
)
45 g_assert ( src
->mode
== VIK_COORD_UTM
);
50 dest
->scale
= mpp_to_scale ( xmpp
);
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
;
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
)
84 static const char *usgs_scale_factor() {
86 snprintf(str
,sizeof(str
),"%d%d%d", 044, 393, 0xA573);
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
);