File talk:Military expenditure percent of GDP.svg

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

To update this map:

First, grab the newest blank map file to make sure you have up-to-date borders.

Now, open the military expenditure SVG file in a text editor (or even better, an XML editor) and copy the legend_group g node into the main svg node of the blank map.

Finally, you can use something like the following perl script to automatically generate the stylesheet for coloring in countries (which you should then copy into the appropriate place in the stylesheet of the blank map):

#!/usr/bin/perl -w
# I hereby place this script in the public domain. Copyright nobody, 2008.
#
# Usage: military-expenditure-color.pl 2034rank.txt iso1366_en_code_lists.txt > style.css
#
# Get the CIA military expenditure rank file from
# https://www.cia.gov/library/publications/the-world-factbook/rankorder/2034rank.txt
#
# Get the ISO 3166 codes file from
# http://www.iso.org/iso/iso3166_en_code_lists.txt
#

use Text::Unaccent;

my $usage = "Usage: $0 2034rank.txt iso1366_en_code_lists.txt > style.css\n";
die $usage unless @ARGV == 2;

my $cia_rank_filename = $ARGV[0];
my $iso_3166_filename = $ARGV[1];

# prepopulate due to different country names used by CIA and ISO
my %iso = (
'bahamas the' => 'bs',
'brunei' => 'bn',
'burma' => 'mm',
'congo democratic republic of the' => 'cd',
'congo republic of the' => 'cg',
'gambia the' => 'gm',
'iran' => 'ir',
'korea north' => 'kp',
'korea south' => 'kr',
'laos' => 'la',
'libya' => 'ly',
'macedonia' => 'mk',
'moldova' => 'md',
'russia' => 'ru',
'syria' => 'sy',
'taiwan' => 'tw',
'tanzania' => 'tz',
'vietnam' => 'vn',
);

open my $ISO, $iso_3166_filename or die;

# skip first 2 lines
<$ISO>; <$ISO>;

for(<$ISO>) {
	chomp;
	my ($country, $code) = split ';' ;
	$country = unac_string("ISO-8859-1", $country);
	$country =~ tr/A-Z/a-z/;
	$country =~ s/[^a-z -]//g;
	$code =~ tr/A-Z/a-z/;
	$code =~ s/[^a-z -]//g; #needed to get rid of various crap
	#print "$country = $code\n";
	$iso{$country} = $code;
}

close $ISO;

open my $CIA, $cia_rank_filename or die;

my @countries = ([],[],[],[],[],[],[]);
my @colors = ('#1e90ff', '#40e0d0', '#11ee11', '#9acd32', '#fff70f', '#ffd700', '#ff8c00');

for(<$CIA>) {
	next unless /^\d+\s+/;
	my @ary = split " *\t *";
	my $country = $ary[1];
	my $percent = $ary[2];
	$country =~ tr/A-Z/a-z/;
	$country =~ s/[^a-z -]//g;
	if (defined $iso{$country}) {
		if ($percent < 1.0) { push @{$countries[0]}, ".$iso{$country}"; }
		elsif ($percent < 2.0) { push @{$countries[1]}, ".$iso{$country}"; }
		elsif ($percent < 3.0) { push @{$countries[2]}, ".$iso{$country}"; }
		elsif ($percent < 4.0) { push @{$countries[3]}, ".$iso{$country}"; }
		elsif ($percent < 5.0) { push @{$countries[4]}, ".$iso{$country}"; }
		elsif ($percent < 10.0) { push @{$countries[5]}, ".$iso{$country}"; }
		elsif ($percent > 0.0) { push @{$countries[6]}, ".$iso{$country}"; }
		else { print STDERR "undefined spending: '$country' '$percent'\n"; }
	}
	else {
		print STDERR "undefined country name '$country'!\n";
	}	
}

for (my $i = 0; $i < @colors; $i++) {
	print join ', ' , @{$countries[$i]};
	print "\n";
	print "{\n    opacity: 1;\n    fill: " . $colors[$i] . ";\n}\n\n";
}