forked from liosha/osm2mp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp-postprocess.pl
executable file
·208 lines (150 loc) · 5.36 KB
/
mp-postprocess.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use utf8;
use Encode;
use Encode::Locale;
use match::smart;
use Getopt::Long;
Encode::Locale::decode_argv();
GetOptions(
'e|encoding=s' => \my $encoding,
);
while ( my $file = encode locale_fs => shift @ARGV ) {
if ( $file =~ /\.dbf$/ix ) {
process_dbf( $file, encoding => $encoding);
}
else {
process_mp( $file, encoding => $encoding || 'cp1251' );
}
}
exit;
sub process_dbf {
my ($file, %opt) = @_;
my $encoding = $opt{encoding} || 'utf8';
require XBase;
my $dbf = XBase->new($file) or die XBase->errstr();
for my $n ( 0 .. $dbf->last_record() ) {
my $rec = $dbf->get_record_as_hash($n);
my %update;
for my $field ( qw/ NAME STATE L_STATE R_STATE / ) {
next if !$rec->{$field};
my $val = decode $encoding => ($update{$field} || $rec->{$field});
$update{$field} = encode $encoding => _clear_region($val);
}
for my $field ( qw/ NAME / ) {
next if !$rec->{$field};
my $val = decode $encoding => ($update{$field} || $rec->{$field});
$update{$field} = encode $encoding => _clear_street($val);
}
next if !%update;
$dbf->update_record_hash($n, %update);
}
$dbf->close();
return;
}
sub process_mp {
my ($file, %opt) = @_;
my $encoding = $opt{encoding} || 'utf8';
rename $file, "$file.old";
open my $in, "<:encoding($encoding)", "$file.old";
open my $out, ">:encoding($encoding)", $file;
my $object;
LINE:
while ( my $line = readline $in ) {
if ( $line =~ /^\[(.*)\]/ ) {
$object = $1;
}
my ($tag, $val) = $line =~ / ^ \s* (\w+) \s* = \s* (.*\S) \s* $ /xms;
if ( $tag |M| [ qw/ Label StreetDesc CityName RegionName / ] ) {
$val = _clear_bad_symbols($val);
}
# region name
if ( $tag |M| [ qw/DefaultRegionCountry RegionName/ ] ) {
$val = _clear_region($val);
}
# street names
if ( ($object eq 'POLYLINE' && $tag |M| 'Label') || $tag |M| 'StreetDesc' ) {
$val = _clear_street($val);
}
$line = "$tag=$val\n" if $tag;
print {$out} $line;
}
close $in;
close $out;
unlink "$file.old";
return;
}
BEGIN {
my @short_names = (
# russian
[ 'ул(?:|ица)' => 'ул.' ],
[ 'пер(?:|еул|еулок)' => 'пер.' ],
[ 'пр(?:\-к?т|осп|оспект)' => 'пр-т' ],
[ 'пр(?:\-з?д|оезд)' => 'пр-д' ],
[ 'п' => 'п.' ],
[ 'пл(?:|ощадь)' => 'пл.' ],
[ 'ш(?:|оссе)' => 'ш.' ],
[ 'туп(?:|ик)' => 'туп.' ],
[ 'б(?:ул|ульв|-р|ульвар)' => 'б-р' ],
[ 'наб(?:|ережная)' => 'наб.' ],
[ 'ал(?:|лея)' => 'ал.' ],
[ 'мост' => 'мост' ],
[ 'тракт' => 'тракт' ],
[ 'просек' => 'просек' ],
[ 'линия' => 'линия' ],
[ 'кв(?:|арт|артал)' => 'кв.' ],
[ 'м(?:к?рн?|икрорайон)' => 'мкр' ],
# ukrainian
[ 'вул(?:|иця)' => 'вул.' ],
[ 'пр(?:|овулок)' => 'пр.' ],
[ 'шосе' => 'ш.' ],
[ 'проїзд' => 'пр-д' ],
[ 'площа' => 'пл.' ],
[ 'мікрорайон' => 'мкр' ],
[ 'набережна' => 'наб.' ],
[ 'алея' => 'ал.' ],
);
sub _clear_street {
my ($in) = @_;
my $line = join q{ }, map { ucfirst } grep { $_ } split / /, $in;
SUFF:
for my $short ( @short_names ) {
my ( $s, $r ) = @$short;
next SUFF unless
my ( $prefix, $postfix )
= $line =~ / ^ (?: (.*\S)? \s+ )? $s (?: (?: \s+ | \s*\.\s* ) (.*) )? $ /ix;
next SUFF unless $prefix || $postfix;
$line = join q{ }, grep {$_} ($prefix, $postfix);
$line =~ s/(\d+-?.?[йяе])(\s+(.*))/$2 $1/;
$line =~ s/(\d+)-?.?([йяе])(\s.*)?$/$1-$2/;
$line =~ s/\s\s+/ /;
$line =~ s/^ //;
$line =~ s/ $//;
return "$line $r";
}
return $in;
}
}
sub _clear_region {
my ($line) = @_;
$line =~ s/область/обл./;
$line =~ s/район/р-н/;
$line =~ s/Автономный округ - Югра автономный округ/АО (Югра)/;
$line =~ s/Кабардино-Балкарская республика/Кабардино-Балкария/;
$line =~ s/Карачаево-Черкесская республика/Карачаево-Черкесия/;
$line =~ s/автономный округ/АО/i;
$line =~ s/автономная обл./АО/i;
$line =~ s/городской округ/ГО/ig;
$line =~ s/ город//i;
$line =~ s/([ие]я|[^я]) республика/$1/i;
$line =~ s/ - Алания//;
return $line;
}
sub _clear_bad_symbols {
my ($line) = @_;
$line =~ s/[«»"]//g;
$line =~ s/
//g;
return $line;
}