ctucx.git: gpx-map

render gpx files to tiles and display them on a map

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 
#!/usr/bin/env perl
use POSIX;
use XML::Parser;

$pi = 4 * atan2(1, 1);

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") {
		$oldlat = $oldlon = "";
	} elsif ($element eq "trkpt") {
		if ($lat ne "" && $lon ne "" && $oldlat ne "" && $oldlon ne "") {
			if ($lat ne $oldlat || $lon ne $oldlon) {
				print "$oldlat,$oldlon $lat,$lon ";

				$rat = cos(($lat + $oldlat) / 2 * $pi / 180);
				$ang = atan2($lat - $oldlat, ($lon - $oldlon) * $rat);

				printf("8:%d\n", ($ang + $pi) * 256 / (2 * $pi));
			}
		}

		$oldlat = $lat;
		$oldlon = $lon;
		%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);
	}
}