diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UK.pm | 3 | ||||
-rw-r--r-- | perllib/HighwaysEngland.pm | 39 |
2 files changed, 42 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm index a9f8c2f01..512dfa973 100644 --- a/perllib/FixMyStreet/Cobrand/UK.pm +++ b/perllib/FixMyStreet/Cobrand/UK.pm @@ -6,6 +6,7 @@ use JSON::MaybeXS; use mySociety::MaPit; use mySociety::VotingArea; use Utils; +use HighwaysEngland; sub country { return 'GB'; } sub area_types { [ 'DIS', 'LBO', 'MTD', 'UTA', 'CTY', 'COI', 'LGD' ] } @@ -89,6 +90,8 @@ sub geocode_postcode { latitude => $location->{wgs84_lat}, longitude => $location->{wgs84_lon}, }; + } elsif (my $junction_location = HighwaysEngland::junction_lookup($s)) { + return $junction_location; } return {}; } diff --git a/perllib/HighwaysEngland.pm b/perllib/HighwaysEngland.pm new file mode 100644 index 000000000..d4ef65369 --- /dev/null +++ b/perllib/HighwaysEngland.pm @@ -0,0 +1,39 @@ +package HighwaysEngland; + +use strict; +use warnings; + +use FixMyStreet; +use DBI; +use Utils; + +sub database_file { FixMyStreet->path_to('../data/roads.sqlite') } + +sub junction_lookup { + my $s = shift; + if ($s =~ /^\s*(?<road>[AM][0-9MT]*)[\s,.]*(junction|junc|j)\s*(?<junction>.*?)\s*$/i + || $s =~ /^\s*(junction|junc|j)\s*(?<junction>.*?)[,.\s]*(?<road>[AM][0-9MT]*)\s*$/i + ) { + return _lookup_db($+{road}, 'junction', $+{junction}, 'name') || undef; + } elsif ($s =~ /^\s*(?<road>[AM][^ ]*)\s*(?<dist>[0-9.]+)\s*$/i + || $s =~ /^\s*(?<dist>[0-9.]+)\s*(?<road>[AM][^ ]*)\s*$/i + ) { + return _lookup_db($+{road}, 'sign', $+{dist}, 'distance') || undef; + } +} + +sub _lookup_db { + my ($road, $table, $thing, $thing_name) = @_; + my $db = DBI->connect("dbi:SQLite:dbname=".database_file(), undef, undef) or return; + $thing = "J$thing" if $table eq 'junction' && $thing =~ /^[1-9]/; + my $results = $db->selectall_arrayref( + "SELECT * FROM $table where road=? and $thing_name=?", + { Slice => {} }, uc $road, uc $thing); + return unless $results; + if (@$results) { + my ($lat, $lon) = Utils::convert_en_to_latlon($results->[0]{easting}, $results->[0]{northing}); + return { latitude => $lat, longitude => $lon }; + } +} + +1; |