Index: trunk/tools/osm-tools/osm2pgsql-style/create-wikimedia-style.pl |
— | — | @@ -0,0 +1,160 @@ |
| 2 | +#!/usr/bin/env perl |
| 3 | + |
| 4 | +=head1 NAME |
| 5 | + |
| 6 | +create-wikimedia-style - Munge L<osm2pgsql(1)>'s F<default.style> and create a custom F<wikimedia.style> |
| 7 | + |
| 8 | +=head1 SYNOPSIS |
| 9 | + |
| 10 | + # Read a list of language codes created with |
| 11 | + # wikipedia-language-codes and emit a wikimedia.style |
| 12 | + wikipedia-language-codes.pl > wikipedia-languages.yml |
| 13 | + create-wikimedia-style.pl --languages wikipedia-languages.yml --style /usr/local/src/osm/applications/utils/export/osm2pgsql/default.style > wikimedia.style |
| 14 | + |
| 15 | +=head1 OPTIONS |
| 16 | + |
| 17 | +=over |
| 18 | + |
| 19 | +=item -h, --help |
| 20 | + |
| 21 | +Print a usage message listing all available options |
| 22 | + |
| 23 | +=item --languages |
| 24 | + |
| 25 | +A YAML file to read languages from, e.g. F<wikipedia-languages.yml> |
| 26 | + |
| 27 | +=item --style |
| 28 | + |
| 29 | +The style file to read for munging, |
| 30 | +e.g. F</usr/local/src/osm/applications/utils/export/osm2pgsql/default.style> |
| 31 | + |
| 32 | +=head1 AUTHOR |
| 33 | + |
| 34 | +E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com> |
| 35 | + |
| 36 | +=cut |
| 37 | + |
| 38 | +use strict; |
| 39 | +use warnings; |
| 40 | + |
| 41 | +use YAML::Syck qw(LoadFile); |
| 42 | + |
| 43 | +use Getopt::Long; |
| 44 | +use Pod::Usage (); |
| 45 | + |
| 46 | +# |
| 47 | +# Get command line options |
| 48 | +# |
| 49 | + |
| 50 | +Getopt::Long::Parser->new( |
| 51 | + config => [ qw< bundling no_ignore_case no_require_order > ], |
| 52 | +)->getoptions( |
| 53 | + 'h|help' => \my $help, |
| 54 | + 'languages=s' => \my $languages, |
| 55 | + 'style=s' => \my $style, |
| 56 | +) or help(); |
| 57 | + |
| 58 | +help() if $help; |
| 59 | + |
| 60 | +unless (-r $languages) |
| 61 | +{ |
| 62 | + warn "Can't read the file `$languages'"; |
| 63 | + help(); |
| 64 | +} |
| 65 | + |
| 66 | +unless (-r $style) |
| 67 | +{ |
| 68 | + warn "Can't read the file `$style'"; |
| 69 | + help(); |
| 70 | +} |
| 71 | + |
| 72 | +# |
| 73 | +# main |
| 74 | +# |
| 75 | + |
| 76 | +my %languages = %{ LoadFile($languages) }; |
| 77 | +my @languages = sorted_languages(%languages); |
| 78 | + |
| 79 | +my $default_style = cat($style); |
| 80 | +my $wikimedia_style = inject_languages($default_style, @languages); |
| 81 | + |
| 82 | +print $wikimedia_style; |
| 83 | + |
| 84 | +exit 0; |
| 85 | + |
| 86 | +sub inject_languages |
| 87 | +{ |
| 88 | + my ($style, @lang) = @_; |
| 89 | + my @style = split /^/, $style; |
| 90 | + my @munged; |
| 91 | + |
| 92 | + for my $line (@style) |
| 93 | + { |
| 94 | + chomp $line; |
| 95 | + |
| 96 | + unless ($line =~ /^node,way name/) { |
| 97 | + push @munged => $line; |
| 98 | + } else { |
| 99 | + my $cn = scalar(@lang); |
| 100 | + push @munged => |
| 101 | + "", |
| 102 | + "##################################################################################", |
| 103 | + "# This is a list of $cn languages for which we want to gather the name:lang tags #", |
| 104 | + "##################################################################################", |
| 105 | + ""; |
| 106 | + push @munged => "# name= is the generic fallback"; |
| 107 | + push @munged => $line; |
| 108 | + push @munged => ""; |
| 109 | + |
| 110 | + for my $lang (@lang) { |
| 111 | + my $code = $lang->[0]; |
| 112 | + my $name = $lang->[1]; |
| 113 | + |
| 114 | + my $munge = $line; |
| 115 | + $munge =~ s/(?<=name)/:$code/; |
| 116 | + |
| 117 | + push @munged => |
| 118 | + "# $lang->[0] ($lang->[1])", |
| 119 | + $munge; |
| 120 | + } |
| 121 | + |
| 122 | + push @munged => ""; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return join("\n", @munged) . "\n"; |
| 127 | +} |
| 128 | + |
| 129 | +sub cat |
| 130 | +{ |
| 131 | + my $file = shift; |
| 132 | + open my $fh, "<", $file or die "Can't open $file for reading: $!"; |
| 133 | + my $content = join '', <$fh>; |
| 134 | + close $fh; |
| 135 | + return $content; |
| 136 | +} |
| 137 | + |
| 138 | +sub sorted_languages |
| 139 | +{ |
| 140 | + my %lang = @_; |
| 141 | + my @ret; |
| 142 | + |
| 143 | + for my $key (sort keys %lang) |
| 144 | + { |
| 145 | + push @ret => [ $key, $lang{$key} ]; |
| 146 | + } |
| 147 | + |
| 148 | + @ret; |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +sub help |
| 153 | +{ |
| 154 | + require Pod::Usage; |
| 155 | + my %arg = @_; |
| 156 | + |
| 157 | + Pod::Usage::pod2usage( |
| 158 | + -verbose => $arg{ verbose }, |
| 159 | + -exitval => $arg{ exitval } || 0, |
| 160 | + ); |
| 161 | +} |