#!/usr/bin/perl
use Archive::Zip;
use strict;
use vars qw(%COUNTRY $countryfile $outfile $infile $zip);
select STDERR; $| = 1; select STDOUT;
$countryfile = shift || 'kuni.txt';
$outfile = shift || 'out.kmz';
$infile = shift || 'KunitoriAreaMap.kmz';
print STDERR "Reading KMZ file...";
$zip = Archive::Zip->new($infile);
print STDERR " Done.\n";
print STDERR "Reading data file...";
read_country($countryfile);
print STDERR " Done.\n";
{
my ($mem) = $zip->members();
my $content = $zip->contents($mem);
my $newcontent = '';
print STDERR "Reading content...";
for (split("\n",$content)) {
if (m!国境!..m!!) {
if (//) {
my ($country) = m!(.*?)!;
$newcontent .= "$_\n";
unless ($COUNTRY{$country}) {
$newcontent .= "\t\t\t\t0\n";
}
} elsif (//) {
; # 出力しない
} else {
$newcontent .= "$_\n";
}
} else {
$newcontent .= "$_\n";
}
}
print STDERR " Done.\n";
print STDERR "Rewriting content...";
$zip->contents($mem, $newcontent);
print STDERR " Done.\n";
print STDERR "Writing to new file...";
$zip->writeToFileNamed($outfile);
print STDERR " Done.\n";
}
print STDERR "Done.\n";
#sleep 5;
sub read_country {
my ($file) = @_;
open IN, $file or die "Can't open file $file.";
while () {
chomp;
next if /^\s*#/; # コメントは読み飛ばす
my ($country, $visibility) = split("\t",$_);
$COUNTRY{$country} = $visibility;
}
close IN;
}