| 1 |
#!/usr/bin/env perl |
| 2 |
#------------------------------------------------------------------------------- |
| 3 |
# File: build_geolocation |
| 4 |
# |
| 5 |
# Description: Parse geonames files to create ExifTool geolocation database |
| 6 |
# |
| 7 |
# Syntax: build_geolocation [OPTIONS] [DBFILE] ... |
| 8 |
# |
| 9 |
# Options: (see -h output) |
| 10 |
# |
| 11 |
# Created: 2024-03-03 - P. Harvey |
| 12 |
# 2024-04-15 - PH Clean up and add options for public release |
| 13 |
# 2024-04-22 - PH Increased number of possible feature codes from |
| 14 |
# 32 to 64. Convert backslashes in directory names |
| 15 |
# 2024-04-24 - PH Fixed problem with population exponent when run |
| 16 |
# under ActivePerl |
| 17 |
# 2024-04-29 - PH Added feature types and default to db v1.03 |
| 18 |
# 2024-06-13 - PH Tweaked case of imported feature types |
| 19 |
# 2025-09-11 - PH Changed priority of alternate-language names to |
| 20 |
# use the preferred name if available and fixed |
| 21 |
# flag counts in verbose output |
| 22 |
# |
| 23 |
# Notes: Requires these files from https://download.geonames.org/export/ |
| 24 |
# |
| 25 |
# allCountries.txt (or other input database if specified) |
| 26 |
# countryInfo.txt |
| 27 |
# admin1CodesASCII.txt |
| 28 |
# admin2Codes.txt |
| 29 |
# featureCodes_XX.txt (optional) |
| 30 |
# alternateNamesV2.txt (optional) |
| 31 |
# |
| 32 |
# Output datbase format (Geolocation.dat): |
| 33 |
# |
| 34 |
# Header: |
| 35 |
# "GeolocationV.VV\tNNNN\n" (V.VV=version, NNNN=num city entries) |
| 36 |
# "# <comment>\n" |
| 37 |
# NNNN City entries: |
| 38 |
# Offset Format Description |
| 39 |
# 0 int16u - latitude high 16 bits (converted to 0-0x100000 range) |
| 40 |
# 2 int8u - latitude low 4 bits, longitude low 4 bits |
| 41 |
# 3 int16u - longitude high 16 bits |
| 42 |
# 5 int8u - index of country in country list |
| 43 |
# 6 int8u - 0xf0 = population E exponent (in format "N.Fe+0E"), 0x0f = population N digit |
| 44 |
# 7 int16u - 0xf000 = population F digit, 0x0fff = index in region list (admin1) |
| 45 |
# 9 int16u - v1.02: 0x7fff = index in subregion (admin2), 0x8000 = high bit of time zone |
| 46 |
# 9 int16u - v1.03: index in subregion list (admin2) |
| 47 |
# 11 int8u - low byte of time zone index |
| 48 |
# 12 int8u - 0x3f = feature code index (see below), v1.03: 0x80 = high bit of time zone |
| 49 |
# 13 string - UTF8 City name, terminated by newline |
| 50 |
# "\0\0\0\0\x01" |
| 51 |
# Country entries: |
| 52 |
# 1. 2-character country code |
| 53 |
# 2. Country name, terminated by newline |
| 54 |
# "\0\0\0\0\x02" |
| 55 |
# Region entries: |
| 56 |
# 1. Region name, terminated by newline |
| 57 |
# "\0\0\0\0\x03" |
| 58 |
# Subregion entries: |
| 59 |
# 1. Subregion name, terminated by newline |
| 60 |
# "\0\0\0\0\x04" |
| 61 |
# Time zone entries: |
| 62 |
# 1. Time zone name, terminated by newline |
| 63 |
# "\0\0\0\0\x05" (feature codes added in v1.03) |
| 64 |
# Feature codes: |
| 65 |
# 1. Feature code, optional space-followed-by-feature-name, then newline |
| 66 |
# "\0\0\0\0\0" |
| 67 |
# |
| 68 |
# Feature Codes v1.02: (see http://www.geonames.org/export/codes.html#P for descriptions) |
| 69 |
# |
| 70 |
# 0. Other 3. PPLA2 6. PPLA5 9. PPLF 12. PPLR 15. PPLX |
| 71 |
# 1. PPL 4. PPLA3 7. PPLC 10. PPLG 13. PPLS |
| 72 |
# 2. PPLA 5. PPLA4 8. PPLCH 11. PPLL 14. STLMT |
| 73 |
# |
| 74 |
# Feature Codes v1.03 and later are listed at the end of the database |
| 75 |
#------------------------------------------------------------------------------- |
| 76 |
|
| 77 |
use strict; |
| 78 |
|
| 79 |
my $dbVer = '1.03'; # default output database version |
| 80 |
my $dbFile = 'allCountries.txt'; # default database file |
| 81 |
my $countryFile = 'countryInfo.txt'; # mandatory country names file |
| 82 |
my $regionFile = 'admin1CodesASCII.txt'; # mandatory region names file |
| 83 |
my $admin2File = 'admin2Codes.txt'; # mandatory subregion names file |
| 84 |
my $featureFile = 'featureCodes_en.txt'; # optional feature names file |
| 85 |
my $altNamesFile = 'alternateNamesV2.txt'; # optional alternate names file |
| 86 |
my $outFile = 'Geolocation.dat'; # output ExifTool database file |
| 87 |
my $outAltNames = 'AltNames.dat'; # output alternate names file |
| 88 |
my $outDirName = 'Geolocation_out'; # output directory for database files |
| 89 |
my $geoLang = 'GeoLang'; # output directory for language files |
| 90 |
|
| 91 |
my %defaults = ( |
| 92 |
file => $dbFile, |
| 93 |
minpop => 2000, |
| 94 |
def_codes => 'PPLA,PPLA2', |
| 95 |
def_codesp => 'PPL,PPLA,PPLA2,PPLA3,PPLA4,PPLA5,PPLC,PPLCH,PPLF,PPLG,PPLH,PPLL,PPLQ,PPLR,PPLS,PPLW,PPLX,STLMT', |
| 96 |
); |
| 97 |
|
| 98 |
# languages to read from geonames database |
| 99 |
my @languages = qw(cs de en en-ca en-gb es fi fr it ja ko nl pl ru sk sv tr zh zh-cn zh-tw); |
| 100 |
|
| 101 |
# indices of feature codes (v1.02 is hard-coded in ExifTool) |
| 102 |
my @fc102 = qw( |
| 103 |
Other PPL PPLA PPLA2 PPLA3 PPLA4 PPLA5 PPLC PPLCH |
| 104 |
PPLF PPLG PPLL PPLR PPLS STLMT PPLX |
| 105 |
); |
| 106 |
# base features for v1.03+ |
| 107 |
my @fc103 = qw( |
| 108 |
Other PPL PPLA PPLA2 PPLA3 PPLA4 PPLA5 PPLC PPLCH |
| 109 |
PPLF PPLG PPLH PPLL PPLQ PPLR PPLS PPLW PPLX STLMT |
| 110 |
); |
| 111 |
my $i = 0; |
| 112 |
my @featureCodes = $dbVer eq '1.02' ? @fc102 : @fc103; |
| 113 |
my %featureCodes = map { $_ => $i++ } @featureCodes; |
| 114 |
my %featureNames; |
| 115 |
|
| 116 |
my ($dbfile, @dbfiles, $outDir, $verbose, $noLang, %needRgn); |
| 117 |
my %optArgs = ( p => 1, c => 1, cp => 1, l => 1, o => 1, ver => 1 ); |
| 118 |
|
| 119 |
# process command-line arguments |
| 120 |
my $opts = { }; |
| 121 |
while (@ARGV) { |
| 122 |
my $opt = shift; |
| 123 |
if (not $opt =~ s/^-//) { |
| 124 |
$opt = '.' unless length $opt; |
| 125 |
$opt =~ tr(\\)(/); # use forward slashes |
| 126 |
$opt =~ s(/$)(); # remove trailing slash |
| 127 |
$opt = "$opt/$defaults{file}" if -d $opt; |
| 128 |
-e $opt or die "Error opening database $opt\n"; |
| 129 |
push @dbfiles, { %defaults, %$opts, file => $opt }; |
| 130 |
$opts = { }; |
| 131 |
next; |
| 132 |
} |
| 133 |
my $arg; |
| 134 |
if ($optArgs{$opt}) { |
| 135 |
$arg = shift; |
| 136 |
defined $arg or die "Expecting argument for -$opt option\n"; |
| 137 |
} |
| 138 |
if ($opt eq 'p') { |
| 139 |
$arg = uc $arg; |
| 140 |
if ($arg =~ /=/) { |
| 141 |
my ($cc, $mp) = split /=/, $arg; |
| 142 |
$mp =~ /^\d+$/ or die "Expecting number on rhs of '=' for -p option\n"; |
| 143 |
my @cc = split /,/, $cc; |
| 144 |
foreach $cc (@cc) { |
| 145 |
$cc =~ /^([A-Z]{2})(\..+)?$/ or die "Invalid country/region '$cc' for -p option\n"; |
| 146 |
$needRgn{$1} = $needRgn{$cc} = 1 if length $cc > 2; |
| 147 |
$$opts{cc_minpop}{$cc} = $mp; |
| 148 |
} |
| 149 |
} else { |
| 150 |
$arg =~ /^\d+$/ or die "Expecting number for -p option\n"; |
| 151 |
$$opts{minpop} = $arg; |
| 152 |
} |
| 153 |
} elsif ($opt =~ /^c(p?)$/) { |
| 154 |
my $p = $1; |
| 155 |
$arg = uc $arg; |
| 156 |
my ($cc, $co); |
| 157 |
if ($arg =~ /=/) { |
| 158 |
($cc, $co) = split /=/, $arg; |
| 159 |
} else { |
| 160 |
($cc, $co) = ('??', $arg); |
| 161 |
} |
| 162 |
my $sign = $co =~ s/^([-+])// ? $1 : ''; |
| 163 |
my @co = split /,/, $co; |
| 164 |
my @cc = split /,/, $cc; |
| 165 |
# store lookup for features to keep for each country ('??' = any country) |
| 166 |
foreach $cc (@cc) { |
| 167 |
$cc =~ /^([A-Z]{2}|\?\?)(\..+)?$/ or die "Invalid country/region '$cc' for -$opt option\n"; |
| 168 |
$needRgn{$1} = $needRgn{$cc} = 1 if length $cc > 2; |
| 169 |
if (not $sign) { |
| 170 |
$$opts{"keep$p"}{$cc} = { }; |
| 171 |
} elsif (not $$opts{"keep$p"}{$cc}) { |
| 172 |
# start from defaults |
| 173 |
my %codes = map { $_ => 1 } split /,/, $defaults{"def_codes$p"}; |
| 174 |
$$opts{"keep$p"}{$cc} = \%codes; |
| 175 |
} |
| 176 |
foreach $co (@co) { |
| 177 |
if ($sign eq '-') { |
| 178 |
delete $$opts{"keep$p"}{$cc}{$co}; |
| 179 |
} else { |
| 180 |
$$opts{"keep$p"}{$cc}{$co} = 1; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} elsif ($opt eq 'l') { |
| 185 |
$arg = lc $arg; |
| 186 |
my @langs = split ',', $arg; |
| 187 |
if (not @langs) { |
| 188 |
undef @languages; |
| 189 |
$noLang = 1; |
| 190 |
} elsif ($langs[0] =~ s/^-//) { |
| 191 |
@languages = grep !/^$langs[0]$/, @languages foreach @langs; |
| 192 |
} else { |
| 193 |
@languages = @langs; |
| 194 |
} |
| 195 |
} elsif ($opt eq 'o') { |
| 196 |
$outDir = $arg; |
| 197 |
} elsif ($opt eq 'ver') { |
| 198 |
$dbVer = $arg; |
| 199 |
$dbVer =~ /^1\.0[23]$/ or die "Unsupported version number $dbVer\n"; |
| 200 |
} elsif ($opt eq 'v') { |
| 201 |
$verbose = 1; |
| 202 |
} elsif ($opt eq 'h') { |
| 203 |
my $defcp = $defaults{def_codesp}; |
| 204 |
$defcp =~ s/(PPLG,)/\n $1/; |
| 205 |
my $defLang = join ',', @languages; |
| 206 |
$defLang =~ s/(ja,)/\n $1/; |
| 207 |
print <<"END"; |
| 208 |
Description: Build ExifTool Geolocation database. |
| 209 |
|
| 210 |
Syntax: build_geolocation [OPTIONS] [DBFILE] ... |
| 211 |
|
| 212 |
Options: |
| 213 |
DBFILE - Input database file name or directory. Multiple input database |
| 214 |
files may be specified. The -p, -c and -cp options apply to |
| 215 |
the database that comes after them on the command line. The |
| 216 |
other input files are assumed to be in the same directory as |
| 217 |
the first database file. Default is "$dbFile". |
| 218 |
-p POP - Minimum population for cities to include. POP may be a number |
| 219 |
or be of the form "CC[,C2...]=###" to set different limits for |
| 220 |
specific countries/regions, where CC and C2 are country codes |
| 221 |
with optional region name or code appended after a period (eg. |
| 222 |
"CA.Ontario,US=500" sets the minimum population to 500 for |
| 223 |
cities on Ontario Canada or the U.S.). If a region is |
| 224 |
specified, either the full name or the geonames admin1 code may |
| 225 |
be used, and case and spaces are not significant. May be |
| 226 |
multiple -p options for each input DBFILE. Default is "$defaults{minpop}". |
| 227 |
-c CODE - Feature codes to always include, regardless of population. CODE |
| 228 |
is a comma-separated list of feature codes, with an optional |
| 229 |
leading comma-separated list of country/region codes followed |
| 230 |
by an equals sign to apply only to specific countries. The |
| 231 |
feature-code list may begin with a dash to remove entries from |
| 232 |
the default list, or a plus sign to add entries. May be |
| 233 |
multiple -c options for each intput DBFILE. Country/region and |
| 234 |
feature names are case insensitive. Default is "$defaults{def_codes}". |
| 235 |
-cp CODE - Additional features to include if above minimum population. |
| 236 |
Default is "$defcp". |
| 237 |
-l LANG - Alternate languages to read from $altNamesFile if |
| 238 |
available. These are used to generate $outAltNames an the |
| 239 |
$geoLang files. LANG is a comma-separated list of language |
| 240 |
codes, starting with a dash to remove items from the default |
| 241 |
list. May be set to an empty string to disable generation |
| 242 |
of alternate language files even if $altNamesFile |
| 243 |
exists. The same set of languages applies to all input |
| 244 |
database files. Default is "$defLang". |
| 245 |
-o OUTDIR - Output directory name. Default is the same directory as the |
| 246 |
first input database file. A directory named $outDirName |
| 247 |
containing the output files will be created in this directory. |
| 248 |
-ver VER - Version for output geolocation database (default is $dbVer). |
| 249 |
-v - Verbose messages. |
| 250 |
-h - Show this help. |
| 251 |
|
| 252 |
Input files (download from https://download.geonames.org/export/dump/): |
| 253 |
$dbFile - default database file (smaller files with names |
| 254 |
like "cities###.txt" may be specified instead) |
| 255 |
$countryFile - mandatory country names file |
| 256 |
$regionFile - mandatory region names file |
| 257 |
$admin2File - mandatory subregion names file |
| 258 |
$featureFile - optional feature codes file(s) |
| 259 |
$altNamesFile - optional alternate names file (must exist to |
| 260 |
to generate $outAltNames and $geoLang files) |
| 261 |
|
| 262 |
Output files: |
| 263 |
$outDirName - default output directory name |
| 264 |
$outFile - ExifTool database file |
| 265 |
$outAltNames - alternate names file |
| 266 |
$geoLang - directory for alternate language files |
| 267 |
|
| 268 |
Author: |
| 269 |
Copyright 2024, Phil Harvey |
| 270 |
|
| 271 |
This is free software; you can redistribute it and/or modify it under the |
| 272 |
same terms as Perl itself. |
| 273 |
END |
| 274 |
exit 0; |
| 275 |
} else { |
| 276 |
die "Unknown option '-$opt'\n"; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if (@dbfiles) { |
| 281 |
# apply any remaining options to last database file |
| 282 |
$dbfiles[-1]{$_} = $$opts{$_} foreach keys %$opts; |
| 283 |
} else { |
| 284 |
# use default database file if none specified |
| 285 |
push @dbfiles, { %defaults, %$opts }; |
| 286 |
unless (-e $dbfiles[0]{file}) { |
| 287 |
# also look in script directory |
| 288 |
if ($0 =~ m{(.*)/} and -e "$1/$dbfiles[0]{file}") { |
| 289 |
$dbfiles[0]{file} = "$1/$dbfiles[0]{file}"; |
| 290 |
} else { |
| 291 |
die qq(Database "$dbfiles[0]{file}" not found. Use -h option for help.\n); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
# determine our working directory |
| 297 |
my $dbdir = $dbfiles[0]{file}; |
| 298 |
$dbdir = '.' unless $dbdir =~ s(/[^/]*$)(); |
| 299 |
|
| 300 |
# add default feature code lookups if necessary |
| 301 |
foreach $dbfile (@dbfiles) { |
| 302 |
my $p; |
| 303 |
foreach $p ('', 'p') { |
| 304 |
next if $$dbfile{"keep$p"}{'??'}; |
| 305 |
my %codes = map { $_ => 1 } split /,/, $defaults{"def_codes$p"}; |
| 306 |
$$dbfile{"keep$p"}{'??'} = \%codes; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
# pre-read region file if necessary |
| 311 |
if (%needRgn) { |
| 312 |
open REGION, '<', "$dbdir/$regionFile" or die "Error opening $dbdir/$regionFile\n"; |
| 313 |
while (<REGION>) { |
| 314 |
my @items = split /\t/; |
| 315 |
my $rgn = $items[0]; |
| 316 |
my ($cc) = split /\./, $rgn; |
| 317 |
next unless $needRgn{$cc}; |
| 318 |
unless ($needRgn{$rgn}) { # allow region code to be used |
| 319 |
$rgn = $cc . '.' . uc$items[1]; # also support full region name |
| 320 |
unless ($needRgn{$rgn}) { |
| 321 |
$rgn =~ tr/ //d; |
| 322 |
next unless $needRgn{$rgn}; # also allow no spaces |
| 323 |
} |
| 324 |
} |
| 325 |
$needRgn{$rgn} = [$items[0], "$cc.$items[1]"]; |
| 326 |
} |
| 327 |
close REGION; |
| 328 |
foreach (sort keys %needRgn) { |
| 329 |
next if length == 2; |
| 330 |
die "No matching region for $_\n" unless ref $needRgn{$_}; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if ($verbose) { |
| 335 |
my $langs = join ',', @languages; |
| 336 |
$langs or $langs = '<none>'; |
| 337 |
print "Languages to read from input database(s):\n $langs\n"; |
| 338 |
foreach $dbfile (@dbfiles) { |
| 339 |
print "Parameters for reading $$dbfile{file}:\n"; |
| 340 |
print " Minimum populations (??=any country):\n"; |
| 341 |
print " ??=$$dbfile{minpop}\n"; |
| 342 |
foreach (reverse sort keys %{$$dbfile{cc_minpop}}) { |
| 343 |
my $cc = ref $needRgn{$_} ? $needRgn{$_}[1] : $_; |
| 344 |
print " $cc=$$dbfile{cc_minpop}{$_}\n"; |
| 345 |
} |
| 346 |
print " Features to keep regardless of population:\n"; |
| 347 |
foreach (reverse sort keys %{$$dbfile{keep}}) { |
| 348 |
my $cc = ref $needRgn{$_} ? $needRgn{$_}[1] : $_; |
| 349 |
print " $cc=",join(',', sort keys %{$$dbfile{keep}{$_}}), "\n"; |
| 350 |
} |
| 351 |
print " Features to keep for population >= minimum:\n"; |
| 352 |
foreach (reverse sort keys %{$$dbfile{keepp}}) { |
| 353 |
my $cc = ref $needRgn{$_} ? $needRgn{$_}[1] : $_; |
| 354 |
print " $_=",join(',', sort keys %{$$dbfile{keepp}{$_}}), "\n"; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
# translate option region arguments to region codes |
| 360 |
foreach $dbfile (@dbfiles) { |
| 361 |
my ($type, $cc); |
| 362 |
foreach $type (qw(cc_minpop keep keepp)) { |
| 363 |
my @cc = keys %{$$dbfile{$type}}; |
| 364 |
foreach $cc (@cc) { |
| 365 |
next unless ref $needRgn{$cc}; |
| 366 |
my $tmp = $$dbfile{$type}{$cc}; |
| 367 |
delete $$dbfile{$type}{$cc}; |
| 368 |
$$dbfile{$type}{$needRgn{$cc}[0]} = $tmp; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
$outDir = "$dbdir/$outDirName" unless defined $outDir; |
| 374 |
-d $outDir or mkdir $outDir, 0777 or die "Error creating output directory '$outDir'\n"; |
| 375 |
-e "$dbdir/$_" or die "Missing input file $dbdir/$_\n" foreach $countryFile, $regionFile, $admin2File; |
| 376 |
|
| 377 |
# order of country codes, region names and subregions in database |
| 378 |
my (%orderCC, %orderRgn, %orderSub); |
| 379 |
|
| 380 |
# languages to read from geonames database (converted to lower case) |
| 381 |
my %languages = map { $_ => 1 } @languages; |
| 382 |
|
| 383 |
# language codes supported by ExifTool |
| 384 |
my @supportedLangs = qw(cs de en-ca en-gb es fi fr it ja ko nl pl ru sk sv tr zh-cn zh-tw); |
| 385 |
|
| 386 |
# supported country-specific languages |
| 387 |
my %ccLang = ( TW => 'zh', CN => 'zh', CA => 'en', GB => 'en' ); |
| 388 |
my (%lang, %featureLang, %haveCountry, %cityFlags, %rgnFlags, %subFlags, %ccFlags, %flags); |
| 389 |
my (%haveRegion, %haveSubRgn, $filesize, $percent); |
| 390 |
|
| 391 |
sub GetFileSize($) |
| 392 |
{ |
| 393 |
my $file = shift; |
| 394 |
seek $file, 0, 2 or die "Seek error\n"; |
| 395 |
my $size = tell $file; |
| 396 |
seek $file, 0, 0 or die "Seek error\n"; |
| 397 |
return $size; |
| 398 |
} |
| 399 |
|
| 400 |
# pre-scan database to determine which countries, regions subregions and |
| 401 |
# feature codes we will be using |
| 402 |
foreach $dbfile (@dbfiles) { |
| 403 |
my $database = $$dbfile{file}; |
| 404 |
my $upgraded; |
| 405 |
|
| 406 |
print "Reading $database... 0%"; |
| 407 |
flush STDOUT; |
| 408 |
|
| 409 |
# pre-read the files to initialize necessary variables |
| 410 |
open INFILE, '<', $database or die "Error opening $database\n"; |
| 411 |
$filesize = GetFileSize(\*INFILE); |
| 412 |
|
| 413 |
open OUTFILE, '>', "$outDir/$outFile" or die "Error creating $outFile in $outDir\n"; |
| 414 |
binmode(OUTFILE); |
| 415 |
|
| 416 |
$$dbfile{kept} = [ ]; |
| 417 |
$percent = -1; |
| 418 |
while (<INFILE>) { |
| 419 |
my $p = int(100 * tell(INFILE) / $filesize + 0.5); |
| 420 |
if ($percent != $p) { |
| 421 |
printf("\b\b\b\b%3d%%", $percent = $p); |
| 422 |
flush STDOUT; |
| 423 |
} |
| 424 |
my @items = split /\t/; |
| 425 |
my ($dbnum, $code, $cc, $rgn, $sub, $pop) = @items[0,7,8,10,11,14]; |
| 426 |
next unless @items > 17 and $cc =~ /^[A-Z]{2}$/; |
| 427 |
my ($minpop, $keep); |
| 428 |
if ($needRgn{$cc} and defined $$dbfile{cc_minpop}{"$cc$rgn"}) { |
| 429 |
$minpop = $$dbfile{cc_minpop}{"$cc$rgn"}; |
| 430 |
} elsif (defined $$dbfile{cc_minpop}{$cc}) { |
| 431 |
$minpop = $$dbfile{cc_minpop}{$cc}; |
| 432 |
} else { |
| 433 |
$minpop = $$dbfile{minpop}; |
| 434 |
} |
| 435 |
# keep regardless of population |
| 436 |
if ($needRgn{$cc} and $$dbfile{keep}{"$cc$rgn"}) { |
| 437 |
$keep = $$dbfile{keep}{"$cc$rgn"}{$code}; |
| 438 |
} elsif ($$dbfile{keep}{$cc}) { |
| 439 |
$keep = $$dbfile{keep}{$cc}{$code}; |
| 440 |
} else { |
| 441 |
$keep = $$dbfile{keep}{'??'}{$code}; |
| 442 |
} |
| 443 |
if ($pop < $minpop) { |
| 444 |
next unless $keep; |
| 445 |
} elsif ($needRgn{$cc} and $$dbfile{keepp}{"$cc$rgn"}) { |
| 446 |
next unless $$dbfile{keepp}{"$cc$rgn"}{$code}; |
| 447 |
} elsif ($$dbfile{keepp}{$cc}) { |
| 448 |
next unless $$dbfile{keepp}{$cc}{$code}; |
| 449 |
} else { |
| 450 |
next unless $$dbfile{keepp}{'??'}{$code}; |
| 451 |
} |
| 452 |
push @{$$dbfile{kept}}, $_; |
| 453 |
$lang{$dbnum} = { alt => [ ] }; |
| 454 |
$haveCountry{$cc} = 1; |
| 455 |
$haveRegion{"$cc$rgn"} = 1; |
| 456 |
$haveSubRgn{"$cc$rgn.$sub"} = 1; |
| 457 |
# add new feature codes (up to maximum index of 0x3f) |
| 458 |
unless ($featureCodes{$code} or @featureCodes > 0x3f) { |
| 459 |
if ($dbVer eq '1.02') { |
| 460 |
next if $code =~ /^(PPLH|PPLQ|PPLW)$/; # (stored as "Other" in v1.02) |
| 461 |
$dbVer = '1.03'; |
| 462 |
$upgraded = 1; # print upgrade warning |
| 463 |
@featureCodes = @fc103; |
| 464 |
my $i = 0; |
| 465 |
%featureCodes = map { $_ => $i++ } @featureCodes; |
| 466 |
next if $featureCodes{$code}; |
| 467 |
} |
| 468 |
push @featureCodes, $code; |
| 469 |
$featureCodes{$code} = $#featureCodes; |
| 470 |
} |
| 471 |
} |
| 472 |
close INFILE; |
| 473 |
print "\b\b\b\bDone.\n"; |
| 474 |
warn "Some feature codes not supported by version 1.02, writing as 1.03 instead.\n" if $upgraded; |
| 475 |
} |
| 476 |
|
| 477 |
# read feature names |
| 478 |
if (open INFILE, '<', "$dbdir/$featureFile") { |
| 479 |
print "Reading $dbdir/$featureFile\n"; |
| 480 |
while (<INFILE>) { |
| 481 |
my @items = split /\t/; |
| 482 |
$items[0] =~ s/^.\.//; # remove feature group and "." separator |
| 483 |
next unless $featureCodes{$items[0]}; |
| 484 |
my $name = ucfirst $items[1]; |
| 485 |
$name =~ s/ ([a-z])/ \U$1/g; |
| 486 |
$name =~ s/(.)\b(Of|Or|A|An|On|In|The|By|For|And)\b/$1\L$2/g; |
| 487 |
$featureNames{$items[0]} = $name; |
| 488 |
} |
| 489 |
close INFILE; |
| 490 |
} else { |
| 491 |
print "Not found: $dbdir/$featureFile\n"; |
| 492 |
print "--> Not storing feature type names\n"; |
| 493 |
} |
| 494 |
|
| 495 |
# read country names |
| 496 |
$i = 0; |
| 497 |
open INFILE, '<', "$dbdir/$countryFile" or die "Error opening $dbdir/$countryFile\n"; |
| 498 |
print "Reading $dbdir/$countryFile\n"; |
| 499 |
while (<INFILE>) { |
| 500 |
next if /^#/; |
| 501 |
my @items = split /\t/; |
| 502 |
next unless $haveCountry{$items[0]}; |
| 503 |
$lang{$items[16]} = { alt => [ ] }; # reference lookup by db number |
| 504 |
$orderCC{$items[0]} = $i++; # (entry 0 is the first country) |
| 505 |
} |
| 506 |
close INFILE; |
| 507 |
printf " %.6d countries (0x%.4x)\n",$i,$i if $verbose; |
| 508 |
die "Too many countries!\n" if $i > 0x100; # (no default 0 entry) |
| 509 |
|
| 510 |
# read region (admin1) names |
| 511 |
$i = 0; |
| 512 |
open REGION, '<', "$dbdir/$regionFile" or die "Error opening $dbdir/$regionFile\n"; |
| 513 |
print "Reading $dbdir/$regionFile\n"; |
| 514 |
while (<REGION>) { |
| 515 |
chomp; |
| 516 |
my @items = split /\t/; |
| 517 |
$items[0] =~ tr/.//d; # (remove "." separator) |
| 518 |
next unless $haveRegion{$items[0]}; |
| 519 |
$lang{$items[3]} = { alt => [ ] }; # reference lookup by db number |
| 520 |
$orderRgn{$items[0]} = ++$i; # (entry 0 is default "" region) |
| 521 |
} |
| 522 |
close REGION; |
| 523 |
printf " %.6d regions (0x%.4x)\n",$i,$i if $verbose; |
| 524 |
die "Too many regions!\n" if $i > 0x0fff; # (account for default 0 entry) |
| 525 |
|
| 526 |
# read subregion (admin2) names |
| 527 |
$i = 0; |
| 528 |
open ADMIN2, '<', "$dbdir/$admin2File" or die "Error opening $dbdir/$admin2File\n"; |
| 529 |
print "Reading $dbdir/$admin2File\n"; |
| 530 |
while (<ADMIN2>) { |
| 531 |
chomp; |
| 532 |
my @items = split /\t/; |
| 533 |
$items[0] =~ s/\.//; # (remove first "." separator) |
| 534 |
next unless $haveSubRgn{$items[0]}; |
| 535 |
$lang{$items[3]} = { alt => [ ] }; # reference lookup by db number |
| 536 |
$orderSub{$items[0]} = ++$i; # (entry 0 is default "" subregion) |
| 537 |
} |
| 538 |
close ADMIN2; |
| 539 |
printf " %.6d subregions (0x%.4x)\n",$i,$i if $verbose; |
| 540 |
if ($i > ($dbVer eq '1.02' ? 0x7fff : 0xffff)) { |
| 541 |
die "Too many subregions!\n" if $i > 0xffff; |
| 542 |
$dbVer = '1.03'; |
| 543 |
warn "Too many subregions for version 1.02, writing as 1.03 instead.\n"; |
| 544 |
} |
| 545 |
|
| 546 |
# read alternate names file if available |
| 547 |
if (not $noLang and open INFILE, '<', "$dbdir/$altNamesFile") { |
| 548 |
$filesize = GetFileSize(\*INFILE); |
| 549 |
print "Reading $dbdir/$altNamesFile... 0%"; |
| 550 |
my %bestPri; |
| 551 |
while (<INFILE>) { |
| 552 |
my $p = int(100 * tell(INFILE) / $filesize + 0.5); |
| 553 |
if ($percent != $p) { |
| 554 |
printf("\b\b\b\b%3d%%", $percent = $p); |
| 555 |
flush STDOUT; |
| 556 |
} |
| 557 |
# items: 0=altID,1=geoID,2=lang,3=alt name,4=preferred,5=short,6=colloquial,7=historic |
| 558 |
my @items = split /\t/; |
| 559 |
my $lkup = $lang{$items[1]} or next; |
| 560 |
my $altList = $lang{$items[1]}{alt}; |
| 561 |
my $lng = lc $items[2]; |
| 562 |
next if $lng and not $languages{$lng}; |
| 563 |
push @$altList, $items[3] unless grep /^\Q$items[3]\E$/i, @$altList; |
| 564 |
next unless $lng; |
| 565 |
my $flags = 0; |
| 566 |
# keep only the best translation for this name for each language |
| 567 |
$items[$_] and $flags |= (1<<($_-4)) foreach 4,5,6,7; |
| 568 |
$flags{$items[1]} = ( $flags{$items[1]} || 0 ) | $flags; |
| 569 |
next if $items[6] or $items[7]; # ignore colloquial and historic names |
| 570 |
# give more weight to preferred name and less weight to short name |
| 571 |
my $pri = ($items[4] ? 2 : 0) + ($items[5] ? 0 : 1); |
| 572 |
my $langPri = $bestPri{$lng}; |
| 573 |
$langPri or $langPri = $bestPri{$lng} = { }; |
| 574 |
next if $$langPri{$items[1]} and $$langPri{$items[1]} > $pri; |
| 575 |
$$langPri{$items[1]} = $pri; |
| 576 |
# save language-specific name for this feature, removing commas |
| 577 |
($$lkup{$lng} = $items[3]) =~ tr/,//d; |
| 578 |
} |
| 579 |
print "\b\b\b\bDone.\n"; |
| 580 |
close INFILE; |
| 581 |
# read alternate feature names |
| 582 |
if (%featureNames) { |
| 583 |
my $lng; |
| 584 |
foreach $lng (@languages) { |
| 585 |
next if $lng eq 'en' or not $languages{$lng}; |
| 586 |
my $file = "$dbdir/$featureFile"; |
| 587 |
$file =~ s/_en\./_$lng./ or next; |
| 588 |
next unless open INFILE, '<', $file; |
| 589 |
print "Reading $file\n"; |
| 590 |
while (<INFILE>) { |
| 591 |
my @items = split /\t/; |
| 592 |
$items[0] =~ s/^.\.//; # remove feature group and "." separator |
| 593 |
next unless $featureNames{$items[0]}; |
| 594 |
utf8::decode($items[1]); |
| 595 |
my $name = ucfirst $items[1]; |
| 596 |
$name =~ s/ (.)/ \U$1/g; |
| 597 |
# change $name back to byte string |
| 598 |
if ($] >= 5.006 and (eval { require Encode; Encode::is_utf8($name) } or $@)) { |
| 599 |
$name = $@ ? pack('C*',unpack($] < 5.010000 ? 'U0C*' : 'C0C*',$name)) : Encode::encode('utf8',$name); |
| 600 |
} |
| 601 |
next if $name eq $featureNames{$items[0]}; |
| 602 |
$featureLang{$lng}{$items[0]} = $name; |
| 603 |
} |
| 604 |
close INFILE; |
| 605 |
} |
| 606 |
} |
| 607 |
} else { |
| 608 |
print "Not found: $dbdir/$altNamesFile\n--> " unless $noLang; |
| 609 |
print "Not writing alternate languages\n"; |
| 610 |
$noLang = 1; |
| 611 |
} |
| 612 |
|
| 613 |
my (%coords, %langLookups); |
| 614 |
|
| 615 |
foreach $dbfile (@dbfiles) { |
| 616 |
my $database = $$dbfile{file}; |
| 617 |
|
| 618 |
print "Processing database entries... 0%"; |
| 619 |
my $i = 0; |
| 620 |
my $num = scalar @{$$dbfile{kept}}; |
| 621 |
|
| 622 |
foreach (@{$$dbfile{kept}}) { |
| 623 |
my @items = split /\t/; |
| 624 |
my ($lat, $lon) = @items[4,5]; |
| 625 |
$lat = int(($lat + 90) / 180 * 0x100000 + 0.5) & 0xfffff; |
| 626 |
$lon = int(($lon + 180) / 360 * 0x100000 + 0.5) & 0xfffff; |
| 627 |
my $coord = pack('nCn',$lat>>4,(($lat&0x0f)<<4)|($lon&0x0f),$lon>>4);; |
| 628 |
# take the city with the highest population if there are |
| 629 |
# multiple cities with the same reduced coordinates |
| 630 |
if ($coords{$coord} and $coords{$coord}[6] >= $items[14]) { |
| 631 |
next; |
| 632 |
} |
| 633 |
# coords=(0.lat,1.lon,2.city,3.cc,4.rgn,5.admin2,6.population,7.timezone,8.feature code,9.alt names) |
| 634 |
my ($altList, $alt); |
| 635 |
die "Internal error\n" unless $lang{$items[0]} and $altList = $lang{$items[0]}{alt}; |
| 636 |
if (@$altList) { |
| 637 |
tr/,//d foreach @$altList; |
| 638 |
$alt = join ',', sort @$altList; |
| 639 |
} else { |
| 640 |
$alt = ''; |
| 641 |
} |
| 642 |
$coords{$coord} = [ @items[4,5,1,8,10,11,14,17,7] ]; |
| 643 |
$coords{$coord}[9] = $alt; |
| 644 |
my $lkup = $lang{$items[0]}; # 0=geoID |
| 645 |
my $key = $items[1]; # 1=city |
| 646 |
$lkup or die "Missing language for geoID $items[0]\n"; |
| 647 |
$cityFlags{$flags{$items[0]}} = ($cityFlags{$flags{$items[0]}} || 0) + 1 if defined $flags{$items[0]}; |
| 648 |
my $ccLang = $ccLang{$items[8]}; # get country-specific language |
| 649 |
if ($ccLang and $$lkup{$ccLang}) { |
| 650 |
my $lc = $ccLang . '-' . lc($items[8]); # eg. zh-cn |
| 651 |
# add country suffix for this language in this country |
| 652 |
$$lkup{$lc} = $$lkup{$ccLang} unless $$lkup{$lc}; |
| 653 |
} |
| 654 |
foreach (@supportedLangs) { |
| 655 |
next unless $$lkup{$_} and $$lkup{$_} ne $key; # (ignore if same) |
| 656 |
$langLookups{$_}{$key} or $langLookups{$_}{$key} = [ ]; |
| 657 |
push @{$langLookups{$_}{$key}}, "$items[8]$items[10].$items[11],$$lkup{$_}"; |
| 658 |
} |
| 659 |
my $p = int(100 * ++$i / $num + 0.5); |
| 660 |
next if $percent == $p; |
| 661 |
printf("\b\b\b\b%3d%%", $percent = $p); |
| 662 |
flush STDOUT; |
| 663 |
} |
| 664 |
print "\b\b\b\bDone.\n"; |
| 665 |
} |
| 666 |
|
| 667 |
# write city database |
| 668 |
my $str = $noLang ? '' : " and $outAltNames"; |
| 669 |
my @t = localtime; |
| 670 |
my $date = sprintf('%.4d-%.2d-%.2d', $t[5]+1900, $t[4]+1, $t[3]); |
| 671 |
print "Writing $outDir/$outFile (version $dbVer)$str...\n"; |
| 672 |
print OUTFILE "Geolocation$dbVer\t",scalar(keys %coords),"\n"; |
| 673 |
print OUTFILE "# $date Cities with population $dbfiles[0]{minpop} or greater from geonames.org with a Creative Commons license\n"; |
| 674 |
|
| 675 |
if ($noLang) { |
| 676 |
unlink "$outDir/$outAltNames"; |
| 677 |
} else { |
| 678 |
open ALTOUT, ">$outDir/$outAltNames"; |
| 679 |
binmode ALTOUT; |
| 680 |
} |
| 681 |
my (%tz, @tz, %fcodes); |
| 682 |
my $tzNum = 0; |
| 683 |
foreach (sort { $a cmp $b } keys %coords) { |
| 684 |
my $items = $coords{$_}; |
| 685 |
# @$items=(0.lat,1.lon,2.city,3.cc,4.rgn,5.admin2,6.population,7.timezone,8.feature code,9.alt names) |
| 686 |
my $iCC = $orderCC{$$items[3]}; |
| 687 |
defined $iCC or warn("Unknown country code $$items[3]\n"), next; |
| 688 |
my $iRgn = $orderRgn{"$$items[3]$$items[4]"} || 0; |
| 689 |
my $iSub = $orderSub{"$$items[3]$$items[4].$$items[5]"} || 0; |
| 690 |
my $tn = $tz{$$items[7]}; |
| 691 |
unless ($tn) { |
| 692 |
push @tz, $$items[7]; |
| 693 |
$tn = $tz{$$items[7]} = $tzNum++; |
| 694 |
} |
| 695 |
# convert population to our binary format |
| 696 |
# Note: format in ActivePerl is "3.1e+004", but "3.1+04" in other Perls, |
| 697 |
# but other Perls will round 34500 to "3.4e+04", so add 1 to get "3.5e+04" |
| 698 |
$$items[6] += 1 if $$items[6] > 100 and not $$items[6] % 10; |
| 699 |
my $pop = sprintf('%.1e',$$items[6]); |
| 700 |
# pack CC, population and region index into a 32-bit integer |
| 701 |
my $code = ($iCC << 24) | (substr($pop,-1,1)<<20) | (substr($pop,0,1)<<16) | (substr($pop,2,1)<<12) | $iRgn; |
| 702 |
$fcodes{$$items[8]} = ($fcodes{$$items[8]} || 0) + 1; |
| 703 |
my $fc = $featureCodes{$$items[8]} || 0; |
| 704 |
# store high bit of timezone index |
| 705 |
if ($tn > 255) { |
| 706 |
if ($dbVer eq '1.02') { |
| 707 |
$iSub |= 0x8000; |
| 708 |
$tn -= 256; |
| 709 |
} else { |
| 710 |
$fc |= 0x80; |
| 711 |
$tn -= 256; |
| 712 |
} |
| 713 |
} |
| 714 |
my $pt = pack('NnCC', $code, $iSub, $tn, $fc); |
| 715 |
$$items[2] =~ tr/,//d; # remove any commas |
| 716 |
print OUTFILE "$_$pt$$items[2]\n"; |
| 717 |
next if $noLang; |
| 718 |
$$items[9] =~ tr/,/\n/; |
| 719 |
print ALTOUT $$items[9],"\0"; |
| 720 |
} |
| 721 |
my $altSize = 0; |
| 722 |
unless ($noLang) { |
| 723 |
$altSize = tell ALTOUT; |
| 724 |
close ALTOUT; |
| 725 |
} |
| 726 |
print OUTFILE "\0\0\0\0\x01\n"; # section terminator |
| 727 |
|
| 728 |
die "Too many time zones!\n" if $tzNum > 0x01ff; |
| 729 |
|
| 730 |
if ($verbose) { |
| 731 |
$i = 0; |
| 732 |
print "Features kept:\n"; |
| 733 |
foreach (sort keys %fcodes) { |
| 734 |
my $fc = $featureCodes{$_} || 0; |
| 735 |
printf "%6d (%2d) %s\n", $fcodes{$_}, $fc, $_; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
# write country codes |
| 740 |
open COUNTRY, '<', "$dbdir/$countryFile" or die "Error opening $dbdir/$countryFile\n"; |
| 741 |
my %cc; |
| 742 |
while (<COUNTRY>) { |
| 743 |
next if /^#/; |
| 744 |
my @items = split /\t/; |
| 745 |
next unless $haveCountry{$items[0]}; |
| 746 |
$cc{$items[4]} = $items[0]; |
| 747 |
die "country code error\n" if length $items[0] != 2; |
| 748 |
$items[4] =~ tr/,//d; # remove any commas |
| 749 |
print OUTFILE "$items[0]$items[4]\n"; |
| 750 |
if ($lang{$items[16]}) { # (16=geoID) |
| 751 |
my $lkup = $lang{$items[16]}; |
| 752 |
my $key = $items[4]; # country name |
| 753 |
$ccFlags{$flags{$items[16]}} = ($ccFlags{$flags{$items[16]}} || 0) + 1 if defined $flags{$items[16]}; |
| 754 |
foreach (@supportedLangs) { |
| 755 |
next unless $$lkup{$_} and $$lkup{$_} ne $key; # (ignore if same) |
| 756 |
$langLookups{$_}{$key} or $langLookups{$_}{$key} = [ ]; |
| 757 |
push @{$langLookups{$_}{$key}}, ",$$lkup{$_}"; |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
close COUNTRY; |
| 762 |
|
| 763 |
print OUTFILE "\0\0\0\0\x02\n"; # section terminator |
| 764 |
|
| 765 |
# write regions |
| 766 |
print OUTFILE "\n"; # (null region) |
| 767 |
open REGION, '<', "$dbdir/$regionFile" or die "Error opening $dbdir/$regionFile\n"; |
| 768 |
my %region; |
| 769 |
while (<REGION>) { |
| 770 |
chomp; |
| 771 |
my @items = split /\t/; |
| 772 |
#items: 0=region code, 1=name, 2=ascii, 3=geoID |
| 773 |
$items[0] =~ tr/.//d; # (remove "." separator) |
| 774 |
next unless $haveRegion{$items[0]}; |
| 775 |
$region{$items[0]} = $items[1]; |
| 776 |
$items[1] =~ tr/,//d; # remove any commas |
| 777 |
print OUTFILE "$items[1]\n"; |
| 778 |
if ($lang{$items[3]}) { # (3=geoID) |
| 779 |
my $lkup = $lang{$items[3]}; |
| 780 |
my $key = $items[1]; # region name |
| 781 |
my $cc = substr($items[0], 0, 2); |
| 782 |
$rgnFlags{$flags{$items[3]}} = ($rgnFlags{$flags{$items[3]}} || 0) + 1 if defined $flags{$items[3]}; |
| 783 |
foreach (@supportedLangs) { |
| 784 |
next unless $$lkup{$_} and $$lkup{$_} ne $key; # (ignore if same) |
| 785 |
$langLookups{$_}{$key} or $langLookups{$_}{$key} = [ ]; |
| 786 |
push @{$langLookups{$_}{$key}}, "$cc,$$lkup{$_}"; |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
close REGION; |
| 791 |
|
| 792 |
print OUTFILE "\0\0\0\0\x03\n"; # section terminator |
| 793 |
|
| 794 |
# write subregions |
| 795 |
print OUTFILE "\n"; # (null admin2) |
| 796 |
open ADMIN2, '<', "$dbdir/$admin2File" or die "Error opening $dbdir/$admin2File\n"; |
| 797 |
my %subregion; |
| 798 |
while (<ADMIN2>) { |
| 799 |
chomp; |
| 800 |
my @items = split /\t/; |
| 801 |
#items: 0=admin2 code, 1=name, 2=ascii, 3=geoID |
| 802 |
$items[0] =~ s/\.//; # (remove first "." separator) |
| 803 |
next unless $haveSubRgn{$items[0]}; |
| 804 |
$subregion{$items[0]} = $items[1]; |
| 805 |
$items[1] =~ tr/,//d; # remove any commas |
| 806 |
print OUTFILE "$items[1]\n"; |
| 807 |
if ($lang{$items[3]}) { # (3=geoID) |
| 808 |
my $lkup = $lang{$items[3]}; |
| 809 |
my $key = $items[1]; # region name |
| 810 |
$subFlags{$flags{$items[3]}} = ($subFlags{$flags{$items[3]}} || 0) + 1 if defined $flags{$items[3]}; |
| 811 |
my $rc = $items[0]; |
| 812 |
$rc =~ s/\..*//; # (remove subregion code) |
| 813 |
foreach (@supportedLangs) { |
| 814 |
next unless $$lkup{$_} and $$lkup{$_} ne $key; # (ignore if same) |
| 815 |
$langLookups{$_}{$key} or $langLookups{$_}{$key} = [ ]; |
| 816 |
push @{$langLookups{$_}{$key}}, "$rc,$$lkup{$_}"; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
close ADMIN2; |
| 821 |
|
| 822 |
print OUTFILE "\0\0\0\0\x04\n"; # section terminator |
| 823 |
|
| 824 |
# write timezones |
| 825 |
print OUTFILE $_,"\n" foreach @tz; |
| 826 |
|
| 827 |
print OUTFILE "\0\0\0\0\x05\n"; # section terminator |
| 828 |
|
| 829 |
# write feature codes and optional names |
| 830 |
foreach (@featureCodes) { |
| 831 |
print OUTFILE $_; |
| 832 |
print OUTFILE ' ', $featureNames{$_} if $featureNames{$_}; |
| 833 |
print OUTFILE "\n"; |
| 834 |
} |
| 835 |
|
| 836 |
# write terminator and close Geolocation.dat |
| 837 |
print OUTFILE "\0\0\0\0\0\n"; # file terminator |
| 838 |
my $outSize = tell OUTFILE; |
| 839 |
close OUTFILE; |
| 840 |
|
| 841 |
# write language lookups |
| 842 |
my $langSize = 0; |
| 843 |
my $langDir = "$outDir/$geoLang"; |
| 844 |
# delete existing languages |
| 845 |
unlink <"$langDir/*.pm">; |
| 846 |
if ($noLang) { |
| 847 |
rmdir $langDir; |
| 848 |
} else { |
| 849 |
my $n = scalar(keys %langLookups); |
| 850 |
print "Writing $n language files to $outDir/$geoLang...\n"; |
| 851 |
mkdir $langDir, 0777; |
| 852 |
my ($lng, $key, $str, $nm, $alt); |
| 853 |
foreach $lng (sort keys %langLookups) { # ($lng = language code) |
| 854 |
my $myLng = $lng; |
| 855 |
$myLng =~ tr/-/_/; |
| 856 |
my $lkup = $langLookups{$lng}; |
| 857 |
my $file = "$myLng.pm"; |
| 858 |
open OUT, ">$langDir/$file" or die "Error creating $file\n"; |
| 859 |
binmode OUT; |
| 860 |
print OUT "# Geolocation language translations for $myLng\n"; |
| 861 |
print OUT "#\n# Based on Creative Commons database from geonames.org\n\n"; |
| 862 |
print OUT "%Image::ExifTool::GeoLang::${myLng}::Translate = (\n"; |
| 863 |
foreach $key (sort keys %$lkup) { |
| 864 |
($nm = $key) =~ s/'/\\'/g; |
| 865 |
# count entries and use the most common one, then add others with country+region ID's |
| 866 |
# (entries in @$li are of the form: City:"CCRc,Sc,Alt", Sub:"CCRc,Alt", Rgn:"CC,Alt", Country:",Alt") |
| 867 |
# (Rc = region code, Sc = subregion code) |
| 868 |
my $li = $$lkup{$key}; |
| 869 |
my %count; |
| 870 |
# sort by popularity of alternate name |
| 871 |
foreach (@$li) { |
| 872 |
my $val = $_; |
| 873 |
$val =~ s/.*?,//; |
| 874 |
$count{$val} = ($count{$val} || 0) + 1; |
| 875 |
} |
| 876 |
my @order = sort { $count{$b} <=> $count{$a} or length($a) <=> length($b) or $a cmp $b } keys %count; |
| 877 |
my $first = 1; |
| 878 |
foreach $alt (@order) { |
| 879 |
foreach (sort @$li) { |
| 880 |
my ($code,$val) = split ',', $_, 2; |
| 881 |
# ($code will be empty for a country name, and 2 characters for a region name, |
| 882 |
# and contain a "." for a city name) |
| 883 |
next unless $val eq $alt; # don't add if alternate name is the same |
| 884 |
die "Backslash in translated name" if $val =~ /\\/; |
| 885 |
$val =~ s/'/\\'/g; # escape single quotes |
| 886 |
if ($first and $val !~ /\(/) { # (don't add general translation if name is qualified with brackets) |
| 887 |
print OUT "\t'$nm' => '$val',\n"; |
| 888 |
undef $first; |
| 889 |
last; |
| 890 |
} |
| 891 |
# format for keys in language table |
| 892 |
# City: "CCRgn,Subregion,City", "CCRgn,,City", "CC,City", ",City" |
| 893 |
# Subregion: "CCRgn,Subregion,", "CCRgn,," |
| 894 |
# Region: "CCRgn," |
| 895 |
# Country: "CC," |
| 896 |
# Any: "Name" |
| 897 |
if (not $code) { |
| 898 |
# this is a country |
| 899 |
$code = $cc{$key}; |
| 900 |
printf OUT "\t'$code,' => '$val',\n"; |
| 901 |
} elsif ($code !~ /\./) { |
| 902 |
# this is a region or subregion |
| 903 |
print OUT "\t'$code$nm,' => '$val',\n"; |
| 904 |
} else { |
| 905 |
# this is a city |
| 906 |
# use region/subregions name instead of code |
| 907 |
my $sub = $subregion{$code} || ''; |
| 908 |
$sub =~ s/'/\\'/g; |
| 909 |
$code =~ s/\..*//; |
| 910 |
$code = substr($code,0,2) . $region{$code} if $region{$code}; |
| 911 |
$code =~ s/'/\\'/g; |
| 912 |
print OUT "\t'$code,$sub,$nm' => '$val',\n"; |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
if ($featureLang{$lng}) { |
| 918 |
print OUT "\n\t# feature types\n"; |
| 919 |
foreach (sort keys %{$featureLang{$lng}}) { |
| 920 |
my $ftype = $featureLang{$lng}{$_}; |
| 921 |
$ftype =~ s/'/\\'/g; |
| 922 |
print OUT "\t$_ => '$ftype',\n" |
| 923 |
} |
| 924 |
} |
| 925 |
print OUT ");\n\n1; #end\n"; |
| 926 |
$langSize += tell OUT; |
| 927 |
close OUT; |
| 928 |
} |
| 929 |
if ($verbose) { |
| 930 |
my @type = ( City => \%cityFlags, Region => \%rgnFlags, Subregion => \%subFlags, Country => \%ccFlags ); |
| 931 |
for (;;) { |
| 932 |
my $type = shift @type or last; |
| 933 |
my $flags = shift @type; |
| 934 |
print "$type flags:\n"; |
| 935 |
printf(" 0x%.2x - %d\n", 0, $$flags{0} || 0); |
| 936 |
my @label = qw(preferred short colloquial historic); |
| 937 |
foreach my $bit (0..3) { |
| 938 |
my $n = 0; |
| 939 |
$_ & (1<<$bit) and $n += $$flags{$_} foreach keys %$flags; |
| 940 |
printf(" 0x%.2x - %d (%s)\n", (1<<$bit), $n, shift(@label)) if $n; |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
print "Output file size(s):\n"; |
| 947 |
printf "%8.2f MB %s (%d entries)\n", $outSize / 1e6, $outFile, scalar(keys %coords); |
| 948 |
printf "%8.2f MB %s\n", $altSize / 1e6, $outAltNames if $altSize; |
| 949 |
printf "%8.2f MB %s/*.pm\n", $langSize / 1e6, $geoLang if $langSize; |
| 950 |
printf "%8.2f MB Total\n", ($outSize + $altSize + $langSize) / 1e6 if $altSize or $langSize; |
| 951 |
|
| 952 |
# end |