diff options
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bexley.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode.pm | 25 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bexley.pm | 69 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bing.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Google.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/OSM.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Zurich.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map/Bexley.pm | 39 | ||||
-rw-r--r-- | t/cobrand/bexley.t | 55 | ||||
-rw-r--r-- | t/geocode/google.t | 4 | ||||
-rw-r--r-- | t/map/tests.t | 1 | ||||
-rw-r--r-- | web/js/map-bexley.js | 24 |
13 files changed, 222 insertions, 23 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bexley.pm b/perllib/FixMyStreet/Cobrand/Bexley.pm index a395543bd..5539e8bfc 100644 --- a/perllib/FixMyStreet/Cobrand/Bexley.pm +++ b/perllib/FixMyStreet/Cobrand/Bexley.pm @@ -8,8 +8,8 @@ sub council_area_id { 2494 } sub council_area { 'Bexley' } sub council_name { 'London Borough of Bexley' } sub council_url { 'bexley' } -sub get_geocoder { 'OSM' } -sub map_type { 'OSM' } +sub get_geocoder { 'Bexley' } +sub map_type { 'Bexley' } sub disambiguate_location { my $self = shift; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index a6c6f34c4..829c85f5d 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -1192,8 +1192,7 @@ Return the default geocoder from config. =cut sub get_geocoder { - my ($self, $c) = @_; - return $c->config->{GEOCODER}; + FixMyStreet->config('GEOCODER'); } diff --git a/perllib/FixMyStreet/Geocode.pm b/perllib/FixMyStreet/Geocode.pm index d552afaa5..61c968269 100644 --- a/perllib/FixMyStreet/Geocode.pm +++ b/perllib/FixMyStreet/Geocode.pm @@ -13,12 +13,14 @@ use JSON::MaybeXS; use LWP::Simple qw($ua); use Path::Tiny; use URI::Escape; -use FixMyStreet::Geocode::Bing; -use FixMyStreet::Geocode::Google; -use FixMyStreet::Geocode::OSM; -use FixMyStreet::Geocode::Zurich; use Utils; +use Module::Pluggable + sub_name => 'geocoders', + search_path => __PACKAGE__, + require => 1, + except => qr/Address/; + # lookup STRING CONTEXT # Given a user-inputted string, try and convert it into co-ordinates using either # MaPit if it's a postcode, or some web API otherwise. Returns an array of @@ -44,14 +46,17 @@ sub lookup { sub string { my ($s, $c) = @_; - my $service = $c->cobrand->get_geocoder($c); + my $service = $c->cobrand->get_geocoder(); $service = $service->{type} if ref $service; - $service = 'OSM' unless $service =~ /^(Bing|Google|OSM|Zurich)$/; - $service = 'OSM' if $service eq 'Bing' && !FixMyStreet->config('BING_MAPS_API_KEY'); - $service = "FixMyStreet::Geocode::${service}::string"; - no strict 'refs'; - return &$service($s, $c); + $service = __PACKAGE__ . '::' . $service; + my %avail = map { $_ => 1 } __PACKAGE__->geocoders; + + if (!$avail{$service} || ($service->can('setup') && !$service->setup)) { + $service = __PACKAGE__ . '::OSM'; + } + + return $service->string($s, $c); } # escape STRING CONTEXT diff --git a/perllib/FixMyStreet/Geocode/Bexley.pm b/perllib/FixMyStreet/Geocode/Bexley.pm new file mode 100644 index 000000000..a70a42cd1 --- /dev/null +++ b/perllib/FixMyStreet/Geocode/Bexley.pm @@ -0,0 +1,69 @@ +package FixMyStreet::Geocode::Bexley; +use parent 'FixMyStreet::Geocode::OSM'; + +use warnings; +use strict; + +use URI::Escape; + +my $base = 'http://tilma.mysociety.org/mapserver/bexley?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=Streets&outputFormat=geojson&Filter=%3CFilter%3E%3CPropertyIsLike%20wildcard=%27*%27%20singleChar=%27.%27%20escape=%27!%27%3E%3CPropertyName%3EADDRESS%3C/PropertyName%3E%3CLiteral%3E{{str}}%3C/Literal%3E%3C/PropertyIsLike%3E%3C/Filter%3E'; + +# Data is ALL CAPS +sub recase { + my $word = shift; + return $word if $word =~ /FP/; + return lc $word if $word =~ /^(AND|TO)$/; + return ucfirst lc $word; +} + +sub string { + my ($cls, $s, $c) = @_; + + my $osm = $cls->SUPER::string($s, $c); + my $js = query_layer($s); + return $osm unless $js && @{$js->{features}}; + + my ( $error, @valid_locations, $latitude, $longitude, $address ); + foreach (sort { $a->{properties}{ADDRESS} cmp $b->{properties}{ADDRESS} } @{$js->{features}}) { + my @lines = @{$_->{geometry}{coordinates}}; + @lines = ([ @lines ]) if $_->{geometry}{type} eq 'LineString'; + my @points = map { @$_ } @lines; + my $mid = int @points/2; + my $e = $points[$mid][0]; + my $n = $points[$mid][1]; + ( $latitude, $longitude ) = Utils::convert_en_to_latlon_truncated( $e, $n ); + $address = sprintf("%s, %s", $_->{properties}{ADDRESS}, $_->{properties}{TOWN}); + $address =~ s/([\w']+)/recase($1)/ge; + push @$error, { + address => $address, + latitude => $latitude, + longitude => $longitude + }; + push (@valid_locations, $_); + } + + if ($osm->{latitude}) { # one result from OSM + push @$error, { + address => $osm->{address}, + latitude => $osm->{latitude}, + longitude => $osm->{longitude}, + }; + return { error => $error }; + } + + if (ref $osm->{error} eq 'ARRAY') { + push @$error, @{$osm->{error}}; + return { error => $error }; + } + + return { latitude => $latitude, longitude => $longitude, address => $address } + if scalar @valid_locations == 1; + return { error => $error }; +} + +sub query_layer { + my $s = uc shift; + $s = URI::Escape::uri_escape_utf8("*$s*"); + (my $url = $base) =~ s/\{\{str\}\}/$s/; + return FixMyStreet::Geocode::cache('bexley', $url); +} diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index 9e425441a..ee5e15f8c 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -11,13 +11,19 @@ use strict; use FixMyStreet::Geocode; use Utils; +sub setup { + my $cls = shift; + return 1 if FixMyStreet->config('BING_MAPS_API_KEY'); + return 0; +} + # string STRING CONTEXT # Looks up on Bing Maps API, and caches, a user-inputted location. # Returns array of (LAT, LON, ERROR), where ERROR is either undef, a string, or # an array of matches if there are more than one. The information in the query # may be used to disambiguate the location in cobranded versions of the site. sub string { - my ( $s, $c ) = @_; + my ( $cls, $s, $c ) = @_; my $params = $c->cobrand->disambiguate_location($s); # Allow cobrand to fixup the user input diff --git a/perllib/FixMyStreet/Geocode/Google.pm b/perllib/FixMyStreet/Geocode/Google.pm index ad1881541..455d9cec0 100644 --- a/perllib/FixMyStreet/Geocode/Google.pm +++ b/perllib/FixMyStreet/Geocode/Google.pm @@ -16,7 +16,7 @@ use URI::Escape; # an array of matches if there are more than one. The information in the query # may be used to disambiguate the location in cobranded versions of the site. sub string { - my ( $s, $c ) = @_; + my ( $cls, $s, $c ) = @_; my $params = $c->cobrand->disambiguate_location($s); # Allow cobrand to fixup the user input diff --git a/perllib/FixMyStreet/Geocode/OSM.pm b/perllib/FixMyStreet/Geocode/OSM.pm index 0d296f299..a36ae3192 100644 --- a/perllib/FixMyStreet/Geocode/OSM.pm +++ b/perllib/FixMyStreet/Geocode/OSM.pm @@ -23,7 +23,7 @@ my $nominatimbase = "http://nominatim.openstreetmap.org/"; # an array of matches if there are more than one. The information in the query # may be used to disambiguate the location in cobranded versions of the site. sub string { - my ( $s, $c ) = @_; + my ( $cls, $s, $c ) = @_; my $params = $c->cobrand->disambiguate_location($s); # Allow cobrand to fixup the user input @@ -52,14 +52,15 @@ sub string { return { error => _('Sorry, we could not find that location.') }; } - my ( $error, @valid_locations, $latitude, $longitude ); + my ( $error, @valid_locations, $latitude, $longitude, $address ); foreach (@$js) { $c->cobrand->call_hook(geocoder_munge_results => $_); ( $latitude, $longitude ) = map { Utils::truncate_coordinate($_) } ( $_->{lat}, $_->{lon} ); + $address = $_->{display_name}; push (@$error, { - address => $_->{display_name}, + address => $address, icon => $_->{icon}, latitude => $latitude, longitude => $longitude @@ -67,7 +68,7 @@ sub string { push (@valid_locations, $_); } - return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1; + return { latitude => $latitude, longitude => $longitude, address => $address } if scalar @valid_locations == 1; return { error => $error }; } diff --git a/perllib/FixMyStreet/Geocode/Zurich.pm b/perllib/FixMyStreet/Geocode/Zurich.pm index c7bd9e9d9..38df431c9 100644 --- a/perllib/FixMyStreet/Geocode/Zurich.pm +++ b/perllib/FixMyStreet/Geocode/Zurich.pm @@ -60,7 +60,7 @@ sub setup_soap { # versions of the site. sub string { - my ( $s, $c ) = @_; + my ( $cls, $s, $c ) = @_; setup_soap(); diff --git a/perllib/FixMyStreet/Map/Bexley.pm b/perllib/FixMyStreet/Map/Bexley.pm new file mode 100644 index 000000000..d3c7a013a --- /dev/null +++ b/perllib/FixMyStreet/Map/Bexley.pm @@ -0,0 +1,39 @@ +# FixMyStreet:Map::Bexley +# +# A combination of FMS OS maps and our own tiles + +package FixMyStreet::Map::Bexley; +use base 'FixMyStreet::Map::FMS'; + +use strict; + +use constant ZOOM_LEVELS => 7; + +sub map_template { 'fms' } + +sub map_javascript { [ + '/vendor/OpenLayers/OpenLayers.wfs.js', + '/js/map-OpenLayers.js', + '/js/map-bing-ol.js', + '/js/map-fms.js', + '/js/map-bexley.js', +] } + +sub map_tiles { + my ( $self, %params ) = @_; + my ( $x, $y, $z ) = ( $params{x_tile}, $params{y_tile}, $params{zoom_act} ); + if ($z >= 17) { + my $base = "//%stilma.mysociety.org/bexley/%d/%d/%d.png"; + return [ + sprintf($base, 'a.', $z, $x-1, $y-1), + sprintf($base, 'b.', $z, $x, $y-1), + sprintf($base, 'c.', $z, $x-1, $y), + sprintf($base, '', $z, $x, $y), + ]; + } else { + return $self->SUPER::map_tiles(%params); + } +} + +1; + diff --git a/t/cobrand/bexley.t b/t/cobrand/bexley.t index f227e7450..3937129d3 100644 --- a/t/cobrand/bexley.t +++ b/t/cobrand/bexley.t @@ -2,8 +2,11 @@ use CGI::Simple; use Test::MockModule; use FixMyStreet::TestMech; use FixMyStreet::Script::Reports; +use Catalyst::Test 'FixMyStreet::App'; use_ok 'FixMyStreet::Cobrand::Bexley'; +use_ok 'FixMyStreet::Geocode::Bexley'; +use_ok 'FixMyStreet::Map::Bexley'; my $ukc = Test::MockModule->new('FixMyStreet::Cobrand::UKCouncils'); $ukc->mock('lookup_site_code', sub { @@ -40,6 +43,7 @@ $category->update; FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'bexley' ], MAPIT_URL => 'http://mapit.uk/', + MAP_TYPE => 'Bexley', STAGING_FLAGS => { send_reports => 1, skip_checks => 0 }, COBRAND_FEATURES => { open311_email => { bexley => { p1 => 'p1@bexley', lighting => 'thirdparty@notbexley.example.com' } } }, }, sub { @@ -147,4 +151,55 @@ subtest 'nearest road returns correct road' => sub { is $cobrand->_nearest_feature($cfg, 545451, 174380, $features), '20101226'; }; +subtest 'correct map tiles used' => sub { + my %test = ( + 16 => [ '-', 'oml' ], + 20 => [ '.', 'bexley' ] + ); + foreach my $zoom (qw(16 20)) { + my $tiles = FixMyStreet::Map::Bexley->map_tiles(x_tile => 123, y_tile => 456, zoom_act => $zoom); + my ($sep, $lyr) = @{$test{$zoom}}; + is_deeply $tiles, [ + "//a${sep}tilma.mysociety.org/$lyr/$zoom/122/455.png", + "//b${sep}tilma.mysociety.org/$lyr/$zoom/123/455.png", + "//c${sep}tilma.mysociety.org/$lyr/$zoom/122/456.png", + "//tilma.mysociety.org/$lyr/$zoom/123/456.png", + ]; + } +}; + +my $geo = Test::MockModule->new('FixMyStreet::Geocode'); +$geo->mock('cache', sub { + my $typ = shift; + return [] if $typ eq 'osm'; + return { + features => [ + { + properties => { ADDRESS => 'BRAMPTON ROAD', TOWN => 'BEXLEY' }, + geometry => { type => 'LineString', coordinates => [ [ 1, 2 ], [ 3, 4] ] }, + }, + { + properties => { ADDRESS => 'FOOTPATH TO BRAMPTON ROAD', TOWN => 'BEXLEY' }, + geometry => { type => 'MultiLineString', coordinates => [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] }, + }, + ], + } if $typ eq 'bexley'; +}); + +subtest 'geocoder' => sub { + my $c = ctx_request('/'); + my $results = FixMyStreet::Geocode::Bexley->string("Brampton Road", $c); + is_deeply $results, { error => [ + { + 'latitude' => '49.766844', + 'longitude' => '-7.557122', + 'address' => 'Brampton Road, Bexley' + }, { + 'address' => 'Footpath to Brampton Road, Bexley', + 'longitude' => '-7.557097', + 'latitude' => '49.766863' + } + ] }; +}; + done_testing(); diff --git a/t/geocode/google.t b/t/geocode/google.t index ee3c15ea8..40e8caf9a 100644 --- a/t/geocode/google.t +++ b/t/geocode/google.t @@ -11,12 +11,12 @@ use Catalyst::Test 'FixMyStreet::App'; use t::Mock::GoogleGeocoder; my $c = ctx_request('/'); -my $r = FixMyStreet::Geocode::Google::string("one result", $c); +my $r = FixMyStreet::Geocode::Google->string("one result", $c); ok $r->{latitude}; ok $r->{longitude}; $c->stash->{cobrand} = FixMyStreet::Cobrand::Tester->new; -$r = FixMyStreet::Geocode::Google::string("two results", $c); +$r = FixMyStreet::Geocode::Google->string("two results", $c); is scalar @{$r->{error}}, 2; done_testing; diff --git a/t/map/tests.t b/t/map/tests.t index a06759a33..1b152620e 100644 --- a/t/map/tests.t +++ b/t/map/tests.t @@ -2,6 +2,7 @@ use FixMyStreet::Map; use Test::More; my $requires = { + 'Bexley' => 'map-bexley.js', 'Bing' => 'map-bing-ol.js', 'Bristol' => 'map-wmts-bristol.js', 'Bromley' => 'map-fms.js', diff --git a/web/js/map-bexley.js b/web/js/map-bexley.js new file mode 100644 index 000000000..38911f950 --- /dev/null +++ b/web/js/map-bexley.js @@ -0,0 +1,24 @@ +fixmystreet.maps.config = (function(original) { + return function(){ + original(); + fixmystreet.map_type = OpenLayers.Layer.Bexley; + }; +})(fixmystreet.maps.config); + +OpenLayers.Layer.Bexley = OpenLayers.Class(OpenLayers.Layer.BingUK, { + get_urls: function(bounds, z) { + if (z < 17) { + return OpenLayers.Layer.BingUK.prototype.get_urls.apply(this, arguments); + } + + var urls = []; + var servers = [ '', 'a.', 'b.', 'c.' ]; + var base = "//{S}tilma.mysociety.org/bexley/${z}/${x}/${y}.png"; + for (var i=0; i < servers.length; i++) { + urls.push( base.replace('{S}', servers[i]) ); + } + return urls; + }, + + CLASS_NAME: "OpenLayers.Layer.Bexley" +}); |