| 1 |
#! /usr/bin/env perl |
| 2 |
# |
| 3 |
# compile the map tables into perl source code |
| 4 |
# |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use Carp; |
| 8 |
use File::Spec; |
| 9 |
use Cwd; |
| 10 |
use Getopt::Long; |
| 11 |
use Pod::Usage; |
| 12 |
|
| 13 |
=pod |
| 14 |
|
| 15 |
=head1 NAME |
| 16 |
|
| 17 |
make_maps_code - [not for users] rebuild Maps.pm from the contents of the maps directory. |
| 18 |
|
| 19 |
Run this from the root of the distribution. Default behaviour is to create a new copy |
| 20 |
of the module in the current directory. |
| 21 |
|
| 22 |
=head1 SYNOPSIS |
| 23 |
|
| 24 |
build/make_maps_code --diff | --update --version v.vv |
| 25 |
|
| 26 |
Series to be built should be defined in maps/map_series.txt and |
| 27 |
data should all be in the maps/ directory. |
| 28 |
|
| 29 |
=head1 OPTIONS |
| 30 |
|
| 31 |
=over 4 |
| 32 |
|
| 33 |
=item --diff |
| 34 |
|
| 35 |
Create the module in the current directory and call diff to compare it |
| 36 |
to the version in lib |
| 37 |
|
| 38 |
=item --update |
| 39 |
|
| 40 |
Overwrite the module in lib directly. |
| 41 |
|
| 42 |
=item --version v.vv |
| 43 |
|
| 44 |
Apply the new version number v.vv to the newly created module. |
| 45 |
|
| 46 |
=back |
| 47 |
|
| 48 |
=head1 AUTHOR |
| 49 |
|
| 50 |
Toby Thurston -- 30 Oct 2017 |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
my $want_diff = 0; |
| 55 |
my $want_update = 0; |
| 56 |
my $new_version = '2.20'; |
| 57 |
my $module_name = 'Maps.pm'; |
| 58 |
my $lib_module_name = File::Spec->catfile(qw(lib Geo Coordinates OSGB), $module_name); |
| 59 |
|
| 60 |
my $options_ok = GetOptions( |
| 61 |
'diff!' => \$want_diff, |
| 62 |
'update!' => \$want_update, |
| 63 |
'version=s' => \$new_version, |
| 64 |
|
| 65 |
'usage' => sub { pod2usage(-verbose => 0, -exitstatus => 0) }, |
| 66 |
'help' => sub { pod2usage(-verbose => 1, -exitstatus => 0) }, |
| 67 |
'man' => sub { pod2usage(-verbose => 2, -exitstatus => 0) }, |
| 68 |
|
| 69 |
); |
| 70 |
die pod2usage() if @ARGV || ! $options_ok; |
| 71 |
|
| 72 |
sub polygon_area_in_km { |
| 73 |
my $p = shift; |
| 74 |
my $area = 0; |
| 75 |
for (my $i=1; $i < scalar @{$p}; $i++) { |
| 76 |
$area = $area + $p->[$i-1][0] * $p->[$i][1] |
| 77 |
- $p->[$i-1][1] * $p->[$i][0]; |
| 78 |
} |
| 79 |
return $area/2_000_000; |
| 80 |
} |
| 81 |
|
| 82 |
# return the ll and ur corners as a list of points |
| 83 |
# note that in the OSGB grid the values of the coordinates for valid grid references are not negative |
| 84 |
sub polygon_bbox { |
| 85 |
my $p = shift; |
| 86 |
my $llx = my $lly = 1e12; |
| 87 |
my $urx = my $ury = 0; |
| 88 |
for my $point ( @{$p} ) { |
| 89 |
$llx = $point->[0] if $point->[0] < $llx; |
| 90 |
$urx = $point->[0] if $point->[0] > $urx; |
| 91 |
$lly = $point->[1] if $point->[1] < $lly; |
| 92 |
$ury = $point->[1] if $point->[1] > $ury; |
| 93 |
} |
| 94 |
return [ [$llx, $lly], [$urx, $ury] ] ; |
| 95 |
} |
| 96 |
|
| 97 |
# work out labels for two boxes : NS or EW? |
| 98 |
sub pair_label_suffixes { |
| 99 |
my $b1 = shift; |
| 100 |
my $b2 = shift; |
| 101 |
my $center_E1 = $b1->[0][0] + ($b1->[1][0]-$b1->[0][0])/2; |
| 102 |
my $center_E2 = $b2->[0][0] + ($b2->[1][0]-$b2->[0][0])/2; |
| 103 |
my $center_N1 = $b1->[0][1] + ($b1->[1][1]-$b1->[0][1])/2; |
| 104 |
my $center_N2 = $b2->[0][1] + ($b2->[1][1]-$b2->[0][1])/2; |
| 105 |
my $E_diff = $center_E1 - $center_E2; |
| 106 |
my $N_diff = $center_N1 - $center_N2; |
| 107 |
my $label1; |
| 108 |
my $label2; |
| 109 |
if ( abs($E_diff) > abs($N_diff) ) { |
| 110 |
$label1 = $E_diff > 0 ? 'E' : 'W'; |
| 111 |
$label2 = $E_diff > 0 ? 'W' : 'E'; |
| 112 |
} |
| 113 |
else { |
| 114 |
$label1 = $N_diff > 0 ? 'N' : 'S'; |
| 115 |
$label2 = $N_diff > 0 ? 'S' : 'N'; |
| 116 |
} |
| 117 |
return ($label1, $label2); |
| 118 |
} |
| 119 |
|
| 120 |
die "Run this from the root directory\n" unless substr(cwd(), -25) eq 'perl-geo-coordinates-osgb'; |
| 121 |
|
| 122 |
my $map_dir = 'maps'; |
| 123 |
|
| 124 |
die "Cannot see the maps/ directory\n" unless -d $map_dir; |
| 125 |
|
| 126 |
my @series_to_build = (); |
| 127 |
open my $control, '<', 'maps/map_series.txt' or die "Cannot open control file: $!\n"; |
| 128 |
while ( <$control> ) { |
| 129 |
chomp; |
| 130 |
next unless my ($id, $file, $sides, $max_inset_area, $max_sheet_area, $short_name, $long_name) = |
| 131 |
$_ =~ /([A-Z])\s+(\S+)\s+(\d)\D+(\d+)\D+(\d+)\s+(\S+)\s+(\S.*)$/osmx; |
| 132 |
|
| 133 |
$long_name =~ s/\A\"(.*)\"\Z/$1/sx or |
| 134 |
$long_name =~ s/\A\'(.*)\'\Z/$1/sx; |
| 135 |
|
| 136 |
my $catalogue_name = File::Spec->catfile($map_dir, sprintf('catalogue-%s.txt', $file)); |
| 137 |
my $polygons_name = File::Spec->catfile($map_dir, sprintf('polygons-%s.txt', $file)); |
| 138 |
|
| 139 |
die "Cannot see $catalogue_name. $!\n" unless -f $catalogue_name; |
| 140 |
die "Cannot see $polygons_name. $!\n" unless -f $polygons_name; |
| 141 |
|
| 142 |
push @series_to_build, { |
| 143 |
id => $id, |
| 144 |
cat_file => $catalogue_name, |
| 145 |
poly_file => $polygons_name, |
| 146 |
max_inset_area => $max_inset_area, |
| 147 |
max_sheet_area => $max_sheet_area, |
| 148 |
short_name => $short_name, |
| 149 |
long_name => $long_name, |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
my @sheets; |
| 154 |
my %label_for; |
| 155 |
my %title_for; |
| 156 |
my %seen; |
| 157 |
|
| 158 |
use Business::ISBN qw( valid_isbn_checksum ); |
| 159 |
|
| 160 |
for my $s (@series_to_build) { |
| 161 |
my $series = $s->{id}; |
| 162 |
open my $cat, '<', $s->{cat_file} or die "Cannot open catalogue file for $series\n"; |
| 163 |
warn "Reading catalogue for $s->{long_name}\n"; |
| 164 |
while ( <$cat> ) { |
| 165 |
chomp; |
| 166 |
my ($nn, $isbn, $label, $title) = split ' ', $_, 4; |
| 167 |
croak "Invalid index number: $nn $series" unless $nn =~ m{\A $series (\d+) \Z}x; |
| 168 |
|
| 169 |
if ( $isbn ) { |
| 170 |
croak "Invalid ISBN for $nn : $isbn\n" unless valid_isbn_checksum($isbn); |
| 171 |
croak "Duplicate ISBN for $nn\n" if $seen{$isbn}++; |
| 172 |
} |
| 173 |
|
| 174 |
croak "Duplicate id for $nn\n" if exists $title_for{$nn}; |
| 175 |
$title =~ s/\s*\Z//iosxm; |
| 176 |
$title_for{$nn} = $title; |
| 177 |
$label_for{$nn} = $label; |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
close $cat; |
| 182 |
open my $fh, '<', $s->{poly_file} or croak "Cannot open polygon file for $series\n"; |
| 183 |
warn "Reading polygons for $s->{long_name}\n"; |
| 184 |
LINE: |
| 185 |
while (<$fh>) { |
| 186 |
|
| 187 |
my ($nn, $junk, $flag, $mpg) = split ' ', $_, 4; |
| 188 |
$mpg =~ s/\s*$//; |
| 189 |
|
| 190 |
next LINE if $mpg eq 'EMPTY'; |
| 191 |
|
| 192 |
next LINE if $flag eq '8'; |
| 193 |
|
| 194 |
croak "Missing flag" if $flag =~ m{\A\(}iosx; |
| 195 |
|
| 196 |
croak "Invalid index number: $nn $series" unless $nn =~ m{\A $series (\d+) \Z}x; |
| 197 |
|
| 198 |
croak "Missing title for $nn\n" unless my $title = $title_for{$nn}; |
| 199 |
croak "Missing label for $nn\n" unless my $label = $label_for{$nn}; |
| 200 |
|
| 201 |
# split the MULTIPOLYGON string up 1 char at a time using the reverse and chop trick |
| 202 |
my $nest = 0; |
| 203 |
my @polylist; |
| 204 |
my $p=0; |
| 205 |
my $gpm = reverse $mpg; |
| 206 |
while (length $gpm) { |
| 207 |
my $c = chop $gpm; |
| 208 |
if ($nest==1 && $c eq ',') { |
| 209 |
$p++; |
| 210 |
next; |
| 211 |
} |
| 212 |
if ($c eq '(') { $nest++ } |
| 213 |
elsif ($c eq ')') { $nest-- } |
| 214 |
else { |
| 215 |
$polylist[$p] .= $c; |
| 216 |
} |
| 217 |
} |
| 218 |
# split each POLYGON string into an array of coordinate pairs |
| 219 |
# and work out the areas, and push then into the right sides array |
| 220 |
my @sides = ( [], [] ); |
| 221 |
my $side_index = -1; |
| 222 |
|
| 223 |
for my $p (@polylist) { |
| 224 |
$p = [ map { [ split ' ' ] } split ',', $p ]; |
| 225 |
# check each pair has two and only two coordinates and that the polygon is closed |
| 226 |
for my $pt (@$p) { |
| 227 |
if (2!=@$pt) { |
| 228 |
my $err = "@$pt"; |
| 229 |
croak "Broken pair $err at line $. in $ARGV";; |
| 230 |
} |
| 231 |
} |
| 232 |
croak "Unclosed polygon" unless $p->[0][0] == $p->[-1][0] |
| 233 |
&& $p->[0][1] == $p->[-1][1]; |
| 234 |
|
| 235 |
my $a = polygon_area_in_km($p); |
| 236 |
if ($a < 0 ) { |
| 237 |
for my $pt (reverse @$p) { |
| 238 |
print join(' ', @$pt), ', '; |
| 239 |
} |
| 240 |
print "\n"; |
| 241 |
croak "Not a positive area for $label $a"; |
| 242 |
} |
| 243 |
|
| 244 |
my $b = polygon_bbox($p); |
| 245 |
|
| 246 |
# push the polys into lists here (list for side 1, list for side 2) |
| 247 |
# main side is first poly in each list |
| 248 |
|
| 249 |
if ( $a <= $s->{max_inset_area} ) { |
| 250 |
# do nothing here |
| 251 |
} |
| 252 |
elsif ( $a <= $s->{max_sheet_area} ) { |
| 253 |
croak "$series $label too many sides" if $side_index > 0; |
| 254 |
$side_index++; |
| 255 |
} |
| 256 |
else { |
| 257 |
croak "$label Area $a out of expected range"; |
| 258 |
} |
| 259 |
push @{$sides[$side_index]}, { poly => $p, area => $a, bbox => $b }; |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
# do we have two sides? if so, find labels |
| 264 |
my ($i, $key, $parent_key); |
| 265 |
if ( @{$sides[1]} > 0 ) { |
| 266 |
my @suffixes = pair_label_suffixes($sides[0][0]{bbox}, $sides[1][0]{bbox}); |
| 267 |
# patch for OL2, first side is South second is West |
| 268 |
if ($label eq 'OL2' ) { |
| 269 |
$suffixes[0] = 'S' |
| 270 |
} |
| 271 |
for my $side (0..1) { |
| 272 |
$parent_key = sprintf "%s:%s%s", $series, $label, $suffixes[$side]; |
| 273 |
$i = 0; |
| 274 |
for my $region ( @{$sides[$side]} ) { |
| 275 |
$key = sprintf "%s%s", $parent_key, $i==0 ? '' : '.'.chr(96+$i); |
| 276 |
$i++; |
| 277 |
push @sheets, { key => $key, |
| 278 |
bbox => $region->{bbox}, |
| 279 |
area => $region->{area}, |
| 280 |
poly => $region->{poly}, |
| 281 |
series => $series, |
| 282 |
number => $label, |
| 283 |
parent => $parent_key, |
| 284 |
title => $title, |
| 285 |
}; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
else { |
| 290 |
$parent_key = sprintf "%s:%s", $series, $label; |
| 291 |
$i = 0; |
| 292 |
for my $region ( @{$sides[0]} ) { |
| 293 |
$key = sprintf "%s%s", $parent_key, $i==0 ? '' : '.'.chr(96+$i); |
| 294 |
$i++; |
| 295 |
push @sheets, { key => $key, |
| 296 |
bbox => $region->{bbox}, |
| 297 |
area => $region->{area}, |
| 298 |
poly => $region->{poly}, |
| 299 |
series => $series, |
| 300 |
number => $label, |
| 301 |
parent => $parent_key, |
| 302 |
title => $title, |
| 303 |
}; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
close $fh; |
| 309 |
} |
| 310 |
|
| 311 |
my $pm_handle; |
| 312 |
my $pm_name = $want_update ? $lib_module_name : $module_name; |
| 313 |
|
| 314 |
open $pm_handle, '>', $pm_name or croak "Cannot open $pm_name for update"; |
| 315 |
warn "Writing to $pm_name\n"; |
| 316 |
print $pm_handle <<'END_PREAMBLE'; |
| 317 |
package Geo::Coordinates::OSGB::Maps; |
| 318 |
use base qw(Exporter); |
| 319 |
use strict; |
| 320 |
use warnings; |
| 321 |
our @EXPORT_OK = qw(%maps %name_for_map_series); |
| 322 |
our %maps; |
| 323 |
END_PREAMBLE |
| 324 |
print $pm_handle "our \$VERSION = '$new_version';\n"; |
| 325 |
print $pm_handle "our \%name_for_map_series = ( \n"; |
| 326 |
for my $s (@series_to_build) { |
| 327 |
printf $pm_handle " %s => '%s',\n", $s->{id}, $s->{long_name}; |
| 328 |
} |
| 329 |
print $pm_handle ");\n"; |
| 330 |
|
| 331 |
for my $m (@sheets) { |
| 332 |
print $pm_handle '$maps{"', $m->{key}, '"} = { '; |
| 333 |
printf $pm_handle 'bbox => [[%d, %d], [%d, %d]], ', $m->{bbox}->[0][0], $m->{bbox}->[0][1], $m->{bbox}->[1][0], $m->{bbox}->[1][1]; |
| 334 |
for my $f ( qw{area series number parent title} ) { |
| 335 |
die "No f\n" unless defined $f; |
| 336 |
die "No $f in $m->{key}\n" unless defined $m->{$f}; |
| 337 |
printf $pm_handle "%s => '%s', ", $f, $m->{$f}; |
| 338 |
} |
| 339 |
print $pm_handle 'polygon => [', join(',', map { sprintf '[%d,%d]', $_->[0], $_->[1] } @{$m->{poly}}), ']'; # no comma because last |
| 340 |
print $pm_handle " };\n"; |
| 341 |
} |
| 342 |
|
| 343 |
print $pm_handle "1;\n\n"; |
| 344 |
my @pod; |
| 345 |
push @pod, '=pod'; |
| 346 |
push @pod, ''; |
| 347 |
push @pod, '=head1 NAME'; |
| 348 |
push @pod, ''; |
| 349 |
push @pod, 'Geo::Coordinates::OSGB::Maps - Data for OSGB Maps'; |
| 350 |
push @pod, ''; |
| 351 |
push @pod, '=head1 VERSION'; |
| 352 |
push @pod, ''; |
| 353 |
push @pod, "V$new_version"; |
| 354 |
push @pod, ''; |
| 355 |
push @pod, '=head1 SUBROUTINES AND METHODS'; |
| 356 |
push @pod, ''; |
| 357 |
push @pod, 'This module exports no functions, but just two hashes of data.'; |
| 358 |
push @pod, ''; |
| 359 |
push @pod, '=head2 Hash C<%name_for_map_series>'; |
| 360 |
push @pod, ''; |
| 361 |
push @pod, 'The keys are the single letter codes used for each map series.'; |
| 362 |
push @pod, 'The values are the descriptive names of each series.'; |
| 363 |
push @pod, 'Currently we have:'; |
| 364 |
push @pod, ''; |
| 365 |
for my $s (@series_to_build) { |
| 366 |
push @pod, sprintf " %s : %s", $s->{id}, $s->{long_name}; |
| 367 |
} |
| 368 |
push @pod, ''; |
| 369 |
push @pod, '=head2 Hash C<%maps>'; |
| 370 |
push @pod, ''; |
| 371 |
push @pod, 'The keys are short identifiers for each sheet or inset. Where a map has more than one side, or includes'; |
| 372 |
push @pod, 'insets then, there will be a separate entry for each side and inset.'; |
| 373 |
push @pod, ''; |
| 374 |
push @pod, 'The value for each key is another hash containing the following items'; |
| 375 |
push @pod, ''; |
| 376 |
push @pod, '=over 4'; |
| 377 |
push @pod, ''; |
| 378 |
push @pod, '=item bbox'; |
| 379 |
push @pod, ''; |
| 380 |
push @pod, 'The bounding box of the sheet as a list of two pairs of coordinates (in metres'; |
| 381 |
push @pod, 'from the grid origin)'; |
| 382 |
push @pod, ''; |
| 383 |
push @pod, '=item polygon'; |
| 384 |
push @pod, ' '; |
| 385 |
push @pod, 'A list of pairs of coordinates (in metres from grid origin) that define the'; |
| 386 |
push @pod, 'corners of the sheet. The list starts at the SW corner (approximately, some do'; |
| 387 |
push @pod, 'not have an obvious place to start), and works round anticlockwise. In all'; |
| 388 |
push @pod, 'cases the last pair is the same as the first pair.'; |
| 389 |
push @pod, ''; |
| 390 |
push @pod, '=item area '; |
| 391 |
push @pod, ''; |
| 392 |
push @pod, 'The calculated area of the sheet in square kilometres.'; |
| 393 |
push @pod, ''; |
| 394 |
push @pod, '=item series'; |
| 395 |
push @pod, ''; |
| 396 |
push @pod, 'A single letter series identifier -- this will be one of the keys from the'; |
| 397 |
push @pod, '"name_for_map_series" hash.'; |
| 398 |
push @pod, ''; |
| 399 |
push @pod, '=item number'; |
| 400 |
push @pod, ''; |
| 401 |
push @pod, 'The identifier for this map within the series, not including any suffix for a'; |
| 402 |
push @pod, 'sheet or an inset. The two sides of a single map have the same number. The'; |
| 403 |
push @pod, 'number is not always an integer - Outdoor Leisure maps are designated "OL14"'; |
| 404 |
push @pod, 'etc. Those maps known by two numbers have a "number" consisting of both'; |
| 405 |
push @pod, 'numbers divided by a "/"; such as "418/OL60" in the Explorer series.'; |
| 406 |
push @pod, ''; |
| 407 |
push @pod, '=item parent'; |
| 408 |
push @pod, ''; |
| 409 |
push @pod, 'The key of the parent map for an inset. Main sheets of a map will have'; |
| 410 |
push @pod, '"parent" equal to their own key.'; |
| 411 |
push @pod, ''; |
| 412 |
push @pod, '=item title'; |
| 413 |
push @pod, ''; |
| 414 |
push @pod, 'The title of the map. Different sheets and insets from the same map will have'; |
| 415 |
push @pod, 'the same title.'; |
| 416 |
push @pod, ''; |
| 417 |
push @pod, '=back'; |
| 418 |
push @pod, ''; |
| 419 |
push @pod, '=cut'; |
| 420 |
push @pod, ''; |
| 421 |
print $pm_handle join "\n", @pod; |
| 422 |
close $pm_handle; |
| 423 |
warn sprintf "Wrote %d sheet definitions to maps module\n", scalar @sheets; |
| 424 |
|
| 425 |
if ($want_diff && -f $module_name) { |
| 426 |
system 'diff', '-b', $lib_module_name, $module_name; |
| 427 |
} |