Index: trunk/tools/osm-tools/osm2pgsql-style/create-mapnik-stylesheets.pl |
— | — | @@ -0,0 +1,211 @@ |
| 2 | +#!/usr/bin/env perl |
| 3 | + |
| 4 | +=head1 NAME |
| 5 | + |
| 6 | +create-mapnik-stylesheets.pl - Create the the mapnik stylesheets needed to render l18n-enabled maps |
| 7 | + |
| 8 | +=head1 SYNOPSIS |
| 9 | + |
| 10 | + perl wikipedia-language-codes.pl > wikipedia-languages.yml |
| 11 | + perl create-mapnik-stylesheets.pl \ |
| 12 | + --out-dir /tmp/osm.xml \ |
| 13 | + --languages wikipedia-languages.yml \ |
| 14 | + --osm-template ~/src/osm/applications/rendering/mapnik/osm-template.xml \ |
| 15 | + --symbols-dir /usr/local/src/osm/applications/rendering/mapnik/symbols \ |
| 16 | + --world-boundaries-dir /sql/world_boundaries/unpacked \ |
| 17 | + --psql-host localhost \ |
| 18 | + --psql-port 5432 \ |
| 19 | + --psql-name gis \ |
| 20 | + --psql-user gis \ |
| 21 | + --psql-pass gis |
| 22 | + |
| 23 | +=head1 OPTIONS |
| 24 | + |
| 25 | +=over |
| 26 | + |
| 27 | +=item -h, --help |
| 28 | + |
| 29 | +Print a usage message listing all available options |
| 30 | + |
| 31 | +=item --languages |
| 32 | + |
| 33 | +A YAML file to read languages from, e.g. F<wikipedia-languages.yml> |
| 34 | + |
| 35 | +=head1 AUTHOR |
| 36 | + |
| 37 | +E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com> |
| 38 | + |
| 39 | +=cut |
| 40 | + |
| 41 | +use feature ':5.10'; |
| 42 | +use strict; |
| 43 | +use warnings; |
| 44 | + |
| 45 | +use YAML::Syck qw(LoadFile); |
| 46 | + |
| 47 | +use Getopt::Long; |
| 48 | +use Pod::Usage (); |
| 49 | +use File::Spec::Functions qw(catfile); |
| 50 | + |
| 51 | +use IO::Handle; |
| 52 | + |
| 53 | +# |
| 54 | +# Get command line options |
| 55 | +# |
| 56 | + |
| 57 | +Getopt::Long::Parser->new( |
| 58 | + config => [ qw< bundling no_ignore_case no_require_order > ], |
| 59 | +)->getoptions( |
| 60 | + 'h|help' => \my $help, |
| 61 | + |
| 62 | + 'languages=s' => \my $languages, |
| 63 | + |
| 64 | + 'osm-template=s' => \my $osm_template, |
| 65 | + 'symbols-dir=s' => \my $symbols_dir, |
| 66 | + 'world-boundaries-dir=s' => \my $world_boundaries_dir, |
| 67 | + |
| 68 | + 'out-dir=s' => \my $out_dir, |
| 69 | + |
| 70 | + 'psql-host=s' => \(my $psql_host = 'localhost'), |
| 71 | + 'psql-port=s' => \(my $psql_port = 5432), |
| 72 | + 'psql-name=s' => \my $psql_name, |
| 73 | + 'psql-user=s' => \my $psql_user, |
| 74 | + 'psql-pass=s' => \my $psql_pass, |
| 75 | + |
| 76 | +) or help(); |
| 77 | + |
| 78 | +help() if $help; |
| 79 | + |
| 80 | +if (not defined $languages) |
| 81 | +{ |
| 82 | + warn "--languages must be supplied"; |
| 83 | + help(); |
| 84 | +} |
| 85 | + |
| 86 | +unless (-r $languages) |
| 87 | +{ |
| 88 | + warn "Can't read the file `$languages'"; |
| 89 | + help(); |
| 90 | +} |
| 91 | + |
| 92 | +if (not defined $osm_template) |
| 93 | +{ |
| 94 | + warn "--osm-template must be supplied"; |
| 95 | + help(); |
| 96 | +} |
| 97 | + |
| 98 | +unless (-r $osm_template) |
| 99 | +{ |
| 100 | + warn "Can't read the file `$osm_template'"; |
| 101 | + help(); |
| 102 | +} |
| 103 | + |
| 104 | +if (not defined $psql_user) |
| 105 | +{ |
| 106 | + warn "--psql-user must be supplied"; |
| 107 | + help(); |
| 108 | +} |
| 109 | + |
| 110 | +# |
| 111 | +# Config |
| 112 | +# |
| 113 | + |
| 114 | +# Our languages |
| 115 | +my %languages = %{ LoadFile($languages) }; |
| 116 | +my @languages = sorted_languages(%languages); |
| 117 | + |
| 118 | +# |
| 119 | +# main |
| 120 | +# |
| 121 | + |
| 122 | +my $osm_xml_template = cat($osm_template); |
| 123 | +$osm_xml_template = generic_replacement($osm_xml_template); |
| 124 | + |
| 125 | +for my $language (@languages) |
| 126 | +{ |
| 127 | + my $code = $language->[0]; |
| 128 | + my $name = $language->[1]; |
| 129 | + |
| 130 | + my $basename = "osm-$code.xml"; |
| 131 | + my $out_file = catfile($out_dir, $basename); |
| 132 | + |
| 133 | + my $language_template = language_replacement($osm_xml_template, $code); |
| 134 | + |
| 135 | + say STDERR "Writing osm.xml for language $code to $out_file"; |
| 136 | + open my $fh, ">", $out_file or die "Can't open `$out_file' for writing"; |
| 137 | + print $fh $language_template; |
| 138 | + $fh->sync; |
| 139 | + close $fh or die "Can't close `$out_file'"; |
| 140 | +} |
| 141 | + |
| 142 | +#print $osm_xml_template; |
| 143 | + |
| 144 | +exit 0; |
| 145 | + |
| 146 | +sub generic_replacement |
| 147 | +{ |
| 148 | + my $xml = shift; |
| 149 | + |
| 150 | + my %replace = ( |
| 151 | + SYMBOLS_DIR => $symbols_dir, |
| 152 | + WORLD_BOUNDARIES_DIR => $world_boundaries_dir, |
| 153 | + |
| 154 | + DBHOST => $psql_host, |
| 155 | + DBPORT => $psql_port, |
| 156 | + DBNAME => $psql_name, |
| 157 | + DBUSER => $psql_user, |
| 158 | + DBPASS => $psql_pass, |
| 159 | + ); |
| 160 | + |
| 161 | + while (my ($k, $v) = each %replace) |
| 162 | + { |
| 163 | + $xml =~ s/%$k%/$v/g; |
| 164 | + } |
| 165 | + |
| 166 | + return $xml; |
| 167 | +} |
| 168 | + |
| 169 | +sub language_replacement |
| 170 | +{ |
| 171 | + my ($xml, $code) = @_; |
| 172 | + |
| 173 | + # %PREFIX%_point |
| 174 | + # view_planet_osm_point_lang_aa |
| 175 | + $xml =~ s/%PREFIX%_(\S+)/"view_planet_osm_${1}_lang_${code}"/g; |
| 176 | + |
| 177 | + return $xml; |
| 178 | +} |
| 179 | + |
| 180 | +sub cat |
| 181 | +{ |
| 182 | + my $file = shift; |
| 183 | + open my $fh, "<", $file or die "Can't open $file for reading: $!"; |
| 184 | + my $content = join '', <$fh>; |
| 185 | + close $fh; |
| 186 | + return $content; |
| 187 | +} |
| 188 | + |
| 189 | +sub sorted_languages |
| 190 | +{ |
| 191 | + my %lang = @_; |
| 192 | + my @ret; |
| 193 | + |
| 194 | + for my $key (sort keys %lang) |
| 195 | + { |
| 196 | + push @ret => [ $key, $lang{$key} ]; |
| 197 | + } |
| 198 | + |
| 199 | + @ret; |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +sub help |
| 204 | +{ |
| 205 | + require Pod::Usage; |
| 206 | + my %arg = @_; |
| 207 | + |
| 208 | + Pod::Usage::pod2usage( |
| 209 | + -verbose => $arg{ verbose }, |
| 210 | + -exitval => $arg{ exitval } || 0, |
| 211 | + ); |
| 212 | +} |