User:GranD/simplify.pl

From OpenStreetMap Wiki
Jump to navigation Jump to search
use strict;

my $head;
my $in_obj;
my $id, my $type;
my %tags;

binmode (STDIN, ':utf8');

while(<>) {
	if ($_ =~ /^\s*<\?xml/ || $_ =~ /^\s*<\/?osm/) {
		print $_;
	}
	elsif (/<(node|way|relation)\s+id\="(\d+)".+>/) {
		$in_obj = 1;
		$head = $_;
	        $type = $1;
		$id = $2;
	}
        elsif (/<\/(node|way|relation)>/) {
		if (exists $tags{'name'} && ($tags{'place'} eq 'city' || $tags{'place'} eq 'town')) {
			if (exists $tags{'name:ru'}) {
				$tags{'name'} = $tags{'name:ru'}.' ('.$tags{'name'}.')';
				$tags{'place'} = $tags{'place'}.':ru';
			#} elsif ($tags{'place'} eq 'city' || $tags{'place'} eq 'town') {
			} elsif ($tags{'place'} eq 'city') {
				print STDERR "$type\t$id\t$tags{'name'}\n";
			}
			print $head;
			for my $k(sort keys %tags) {
				print "    <tag k=\"$k\" v=\"$tags{$k}\"/>\n";
			}
			print $_;
		}
		$in_obj = 0;
		%tags = ();
		$head = undef;
	}
        elsif (/<tag k="([^"]+)" v="([^"]+)"\/>/) {
		if (!$in_obj) {
			die "Error 1";
		} else {
			$tags{$1} = $2;
		}
	}
}