]> git.openstreetmap.org Git - dns.git/blob - bin/mkcountries
Add stub entry for London tile cache
[dns.git] / bin / mkcountries
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use XML::TreeBuilder;
7 use YAML;
8
9 # Get arguments
10 my $bandwidthfile = shift @ARGV;
11 my $originsfile = shift @ARGV;
12
13 # Initialise origins
14 my $origins = {};
15
16 # Create a parser for the country database
17 my $countries = XML::TreeBuilder->new;
18
19 # Parse the country database
20 $countries->parsefile("lib/countries.xml");
21
22 # Load the per-country bandwidth details
23 my $bandwidth = YAML::LoadFile($bandwidthfile);
24
25 # Fill in country table and work out which clusters each can use
26 foreach my $country ($countries->look_down("_tag" => "country"))
27 {
28     my $code = $country->look_down("_tag" => "countryCode")->as_text;
29     my $name = $country->look_down("_tag" => "countryName")->as_text;
30     my $population = $country->look_down("_tag" => "population")->as_text;
31     my $bandwidth = $bandwidth->{$code} || 0;
32     my $continent = $country->look_down("_tag" => "continent")->as_text;
33     my $west = $country->look_down("_tag" => "west")->as_text;
34     my $north = $country->look_down("_tag" => "north")->as_text;
35     my $east = $country->look_down("_tag" => "east")->as_text;
36     my $south = $country->look_down("_tag" => "south")->as_text;
37     my $lat = centre_lat($south, $north);
38     my $lon = centre_lon($west, $east);
39
40     $origins->{$code} = {
41         code => $code, name => $name,
42         country => $code, continent => $continent,
43         bandwidth => $bandwidth, lat => $lat, lon => $lon
44     };
45 }
46
47 # Save the origins
48 YAML::DumpFile($originsfile, $origins);
49
50 exit 0;
51
52 #
53 # Find the centre value between two latitudes
54 #
55 sub centre_lat
56 {
57     my $south = shift;
58     my $north = shift;
59
60     return ( $south + $north ) / 2;
61 }
62
63 #
64 # Find the centre value between two longitudes
65 #
66 sub centre_lon
67 {
68     my $west = shift;
69     my $east = shift;
70     my $lon;
71
72     if ($west < $east)
73     {
74         $lon = ( $west + $east ) / 2;
75     }
76     else
77     {
78         $lon = ( $west + $east + 360 ) / 2;
79     }
80
81     $lon = $lon - 360 if $lon > 180;
82
83     return $lon
84 }