3 #=======================================================================
7 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License, see end of file for legal stuff.
9 #=======================================================================
20 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 push(@main::version_array
, $rcs_id);
24 $VERSION = ($rcs_id =~ / (\d+) /, $1);
29 &list_nearest_waypoints &ddd_to_dms &distance
34 our $wpt_elems = "ele|time|magvar|geoidheight|name|cmt|desc|src|link|sym|" .
35 "type|fix|sat|hdop|vdop|pdop|ageofdgpsdata|dgpsid|" .
39 my $waypoint_file = "$ENV{HOME}/bin/src/gpstools/tests/waypoints.gpx";
40 my @orig_waypoints = load_waypoints
($waypoint_file);
42 sub list_nearest_waypoints
{
44 my ($Lat, $Lon, $Count) = @_;
46 my @Waypoints = @orig_waypoints;
48 D
("list_nearest_waypoints('$Lat', '$Lon', '$Count')");
50 for my $i (0 .. $#Waypoints) {
51 $Waypoints[$i]{'distance'} = distance
(
53 $Waypoints[$i]{'lat'},
57 my $dstr = "$i is { ";
58 for my $role (keys %{ $Waypoints[$i] }) {
59 $dstr .= "$role=\"$Waypoints[$i]{$role}\" ";
65 # my @Sorted = sort { $Waypoints[$b]{distance} } <=> $Waypoints[$a]{distance} } keys @Waypoints;
66 my @Sorted = sort_waypoints
("distance", "+", \
@Waypoints);
69 for my $i (0 .. $#Waypoints) {
70 my $dstr = "sorted $i is { ";
71 for my $role (keys %{ $Sorted[$i] }) {
72 $dstr .= "$role=\"$Sorted[$i]{$role}\" ";
78 for my $i2 (0 .. $Count - 1) {
80 $Retval .= "<near pos=\"$pos\"> <name>$Sorted[$i2]{'name'}</name> <distance>$Sorted[$i2]{'distance'}</distance> </near>";
87 # Return distance between two positions. {{{
88 my ($Lat1, $Lon1, $Lat2, $Lon2, $Unit) = @_;
89 defined($Unit) || ($Unit = "metre");
92 my $geo = new Geo
::Distance
;
93 $Retval = $geo->distance($Unit, $Lat1, $Lon1 => $Lat2, $Lon2);
103 D
("Opening $File for read");
104 if (open(WaypFP
, "<", $File)) {
105 my $Data = join("", <WaypFP
>);
106 # D("Data = '$Data'");
108 @Retval = parse_waypoints
($Data);
110 $main::Opt
{'verbose'} && warn("$File: Cannot open file for read\n");
113 D
("load_waypoints('$File') returns '" . join("|", @Retval) . "'");
118 sub parse_waypoints
{
122 # D("parse_waypoints('$Data')");
125 <wpt
\b(.*?
)\blat
="(.+?)".+?
\blon
="(.+?)".*?
>(.*?
)</wpt
>
127 my ($Lat, $Lon, $el_wpt) =
128 (1.0 * $2, 1.0 * $3, $4);
130 $wpt->{'lat'} = $Lat;
131 $wpt->{'lon'} = $Lon;
132 D
("parse: Lat = '$Lat', Lon = '$Lon'");
135 <($wpt_elems)\b.*?
>(.*?
)</($wpt_elems)>
142 D
("push Retval '$wpt'");
145 D
("parse_waypoints() returns '" . join("|", @Retval) . "'");
151 # FIXME: Unfinished. {{{
152 my ($Key, $Dir) = (shift, shift);
159 # Convert floating-point degrees into D°M'S.S" (ISO-8859-1).
160 # Necessary for import into GPSman. Based on toDMS() from
161 # gpstrans-0.39 to ensure compatibility.
165 my ($Hour, $Min, $Sec) =
169 ($ddd =~ /^\-?(\d*)(\.\d+)?$/) || return(undef);
170 length($ddd) || ($ddd = 0);
177 $ddd = ($ddd - $Hour) * 60.0;
179 $Sec = ($ddd - $Min) * 60.0;
189 $Retval = sprintf("%s%.0f\xB0%02.0f'%04.1f\"",