7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
12 my $source = shift @ARGV;
13 my $zone = shift @ARGV;
14 my $servers = YAML::LoadFile("src/${source}");
18 my $countries = XML::TreeBuilder->new->parsefile("lib/countries.xml");
20 foreach my $country ($countries->look_down("_tag" => "country"))
22 my $code = $country->look_down("_tag" => "countryCode")->as_text;
23 my $name = $country->look_down("_tag" => "countryName")->as_text;
24 my $continent = $country->look_down("_tag" => "continent")->as_text;
25 my $west = $country->look_down("_tag" => "bBoxWest")->as_text;
26 my $north = $country->look_down("_tag" => "bBoxNorth")->as_text;
27 my $east = $country->look_down("_tag" => "bBoxEast")->as_text;
28 my $south = $country->look_down("_tag" => "bBoxSouth")->as_text;
29 my $lat = centre_lat( $south, $north );
30 my $lon = centre_lon( $west, $east );
33 foreach my $servername (keys %$servers)
35 my $server = $servers->{$servername};
36 my $match = match_country($server, $code, $continent);
38 if ($match eq "preferred" || $match eq "allowed")
40 my $priority = $match eq "preferred" ? 20 : 10;
41 my $distance = distance($lat, $lon, $server->{lat}, $server->{lon});
43 # print STDERR "$servername is $match for $name with distance $distance\n";
45 push @servers, { name => $servername, priority => $priority, distance => $distance };
50 code => $code, name => $name, continent => $continent,
51 lat => $lat, lon => $lon, servers => \@servers
57 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
58 my $kmlfile = IO::File->new("> kml/${zone}.kml") || die "$!";
59 my $kmlwriter = XML::Writer->new(OUTPUT => $kmlfile);
61 $kmlwriter->xmlDecl("UTF-8");
62 $kmlwriter->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
63 $kmlwriter->startTag("Document");
65 foreach my $country (values %countries)
67 my @servers = sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @{$country->{servers}};
68 my $server = $servers->{$servers[0]->{name}};
70 $zonefile->print("C\L$country->{code}\E.${zone}:$servers[0]->{name}.${zone}:600\n");
72 $kmlwriter->startTag("Placemark");
73 $kmlwriter->dataElement("name", $country->{name});
74 $kmlwriter->startTag("LineString");
75 $kmlwriter->startTag("coordinates");
76 $kmlwriter->characters("$country->{lon},$country->{lat}");
77 $kmlwriter->characters("$server->{lon},$server->{lat}");
78 $kmlwriter->endTag("coordinates");
79 $kmlwriter->endTag("LineString");
80 $kmlwriter->endTag("Placemark");
83 foreach my $server (keys %$servers)
85 $zonefile->print("Cxx.${zone}:${server}.${zone}:600\n");
88 $kmlwriter->endTag("Document");
89 $kmlwriter->endTag("kml");
102 return ( $south + $north ) / 2;
113 $lon = ( $west + $east ) / 2;
117 $lon = ( $west + $east + 360 ) / 2;
120 $lon = $lon - 360 if $lon > 180;
129 my $continent = shift;
132 if ($server->{preferred} &&
133 $server->{preferred}->{countries} &&
134 grep { $_ eq $country } @{$server->{preferred}->{countries}})
136 $match = "preferred";
138 elsif ($server->{preferred} &&
139 $server->{preferred}->{continents} &&
140 grep { $_ eq $continent } @{$server->{preferred}->{continents}})
142 $match = "preferred";
144 elsif ($server->{allowed} &&
145 $server->{allowed}->{countries} &&
146 grep { $_ eq $country } @{$server->{allowed}->{countries}})
150 elsif ($server->{allowed} &&
151 $server->{allowed}->{continents} &&
152 grep { $_ eq $continent } @{$server->{allowed}->{continents}})
156 elsif ($server->{allowed})
170 my $lat1 = deg2rad(shift);
171 my $lon1 = deg2rad(shift);
172 my $lat2 = deg2rad(shift);
173 my $lon2 = deg2rad(shift);
175 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);