-
Notifications
You must be signed in to change notification settings - Fork 26
/
togeo
executable file
·144 lines (114 loc) · 3.04 KB
/
togeo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl
use POSIX;
use XML::Parser;
$ENV{'TZ'} = "GMT";
sub acos {
if ($_[0] * $_[0] > 1) {
return 0;
}
return atan2(sqrt(1 - $_[0] * $_[0]), $_[0]);
}
$pi = 4 * atan2(1, 1);
$r = 6367 / 1.609344;
$foot = 180 / $r / 5280 / $pi;
# http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
# nautical mile definition requires 6367 km radius for the earth
# exactly 1.609344 miles per kilometer, .621371192 kilometers per mile
# so r is radius of earth in miles, $foot is one foot in degrees
sub dist {
my $lat1 = $_[0] * $pi/180;
my $lon1 = $_[1] * $pi/180;
my $lat2 = $_[2] * $pi/180;
my $lon2 = $_[3] * $pi/180;
my $latd = $lat2 - $lat1;
my $lond = ($lon2 - $lon1) * cos(($lat1 + $lat2) / 2);
my $d = sqrt($latd * $latd + $lond * $lond) * 180 / $pi / $foot;
if ($d >= 1000) {
$d = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($lon2 - $lon1)) * $r * 5280;
}
return $d;
}
sub handle_start {
my ($expat, $element, @tags) = @_;
my %tags = @tags;
if ($element eq "trkpt") {
$lat = $tags{'lat'};
$lon = $tags{'lon'};
} elsif ($element eq "ele") {
$state = "ele";
} elsif ($element eq "time") {
$state = "time";
} else {
$state = "";
}
}
sub handle_char {
my ($expat, $string) = @_;
$text{$state} .= $string;
}
sub handle_end {
my ($expat, $element) = @_;
if ($element eq "trkseg" || $element eq "trk") {
if ($oldwhen != 0) {
print "$oldwhen --\n";
$oldwhen = 0;
}
} elsif ($element eq "trkpt") {
if ($text{'time'} =~ /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
($year, $month, $day, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6);
} else {
($year, $month, $day, $hour, $minute, $second) = (1970, 1, 1, 0, 0, 0);
}
$date = sprintf("%04d-%02d-%02d", $year, $month, $day);
$time = sprintf("%02d:%02d:%02d", $hour, $minute, $second);
if ($text{'ele'} ne "") {
$ele = $text{'ele'};
} else {
$ele = 0;
}
$year -= 1900;
$month -= 1;
$when = POSIX::mktime($second, $minute, $hour, $day, $month, $year, 0, 0, -1);
if ($oldwhen != 0) {
if ($oldwhen < $when) {
$sec = $when - $oldwhen;
$dist = dist($oldlat, $oldlon, $lat, $lon);
$speed = ($dist / 5280) / ($sec / 60 / 60);
print "$oldwhen $oldlat,$oldlon to $lat,$lon $olddate $oldtime ";
printf("%d ", $sec);
printf("%.4f %.4f ", $dist, $speed);
printf("%.2f %.2f ", $oldele, $ele);
if ($dist != 0) {
printf("%.4f%% G\n", 100 * ($ele - $oldele) * 3.2808399 / $dist);
} else {
printf("?%% G\n");
}
} else {
print "$oldwhen --\n";
}
}
$oldwhen = $when;
$oldlat = $lat;
$oldlon = $lon;
$olddate = $date;
$oldtime = $time;
$oldele = $ele;
%text = ();
}
}
if ($#ARGV < 0) {
$parser = new XML::Parser(Handlers => { Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char,
});
$parser->parse(*STDIN);
} else {
for $file (@ARGV) {
$oldwhen = 0;
$parser = new XML::Parser(Handlers => { Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char,
});
$parser->parsefile($file);
}
}