-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtogeojson-elev-time
executable file
·107 lines (87 loc) · 1.98 KB
/
togeojson-elev-time
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
#!/usr/bin/perl
use POSIX;
use XML::Parser;
print "{\n\"type\": \"FeatureCollection\",\n\"features\": [\n";
$within = 0;
%coordinates = ();
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, $s) = @_;
$s =~ s/\n//g;
if ($state eq "ele") {
$ele .= $s;
}
if ($state eq "time") {
$time .= $s;
}
}
sub handle_end {
my ($expat, $element) = @_;
if ($element eq "trkseg" || $element eq "trk") {
if ($#coordinates >= 0) {
if ($within) {
print ",\n";
}
$within = 1;
print "{ \"type\": \"Feature\", \"properties\": { }, \"geometry\": { \"type\": \"LineString\", \"coordinates\": [ ";
for ($i = 0; $i <= $#coordinates; $i++) {
if ($i != 0) {
print ", ";
}
my ($lon, $lat, $ele, $time) = split(/,/, $coordinates[$i]);
print "[ $lon, $lat, $ele ]";
}
print "], \"attributes\": [ ";
for ($i = 0; $i <= $#coordinates; $i++) {
if ($i != 0) {
print ", ";
}
my ($lon, $lat, $ele, $time) = split(/,/, $coordinates[$i]);
print "{ \"time\": \"$time\" }";
}
print "] } }\n";
}
@coordinates = ();
} elsif ($element eq "trkpt") {
if ($lat ne "" && $lon ne "") {
$ele =~ s/[ ,]*$//;
$time =~ s/[ ,]*$//;
$time =~ s/^[ ,]*//;
push @coordinates, "$lon,$lat,$ele,$time";
}
$ele = "";
$time = "";
}
}
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;
print STDERR "$file\n";
$parser = new XML::Parser(Handlers => { Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char,
});
$parser->parsefile($file);
}
}
print "]\n";
print "}\n";