diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/Map/BingOL.pm | 63 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map/OSM.pm | 51 |
2 files changed, 81 insertions, 33 deletions
diff --git a/perllib/FixMyStreet/Map/BingOL.pm b/perllib/FixMyStreet/Map/BingOL.pm index 70f9dbda1..94df6fff9 100644 --- a/perllib/FixMyStreet/Map/BingOL.pm +++ b/perllib/FixMyStreet/Map/BingOL.pm @@ -7,9 +7,12 @@ # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ package FixMyStreet::Map::BingOL; +use base 'FixMyStreet::Map::OSM'; use strict; -use mySociety::Gaze; + +use constant ZOOM_LEVELS => 5; +use constant MIN_ZOOM_LEVEL => 13; sub header_js { return ' @@ -20,25 +23,45 @@ sub header_js { '; } -# display_map C PARAMS -# PARAMS include: -# latitude, longitude for the centre point of the map -# CLICKABLE is set if the map is clickable -# PINS is array of pins to show, location and colour -sub display_map { - my ($self, $c, %params) = @_; - - my $dist = mySociety::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude}, 200_000 ); - my $zoom = 2; - $zoom = 3 if $dist < 10; - - $c->stash->{map} = { - %params, - type => 'osm', - zoom => $zoom, - map_type => '""', # Is set by the JavaScript - copyright => _('Map contains Ordnance Survey data © Crown copyright and database right 2010. Microsoft'), - }; +# Is set by the JavaScript +sub map_type { + return '""'; +} + +sub copyright { + return _('Map contains Ordnance Survey data © Crown copyright and database right 2010. Microsoft'); +} + +sub get_quadkey { + my ($x, $y, $z) = @_; + my $key = ''; + for (my $i = $z; $i > 0; $i--) { + my $digit = 0; + my $mask = 1 << ($i - 1); + $digit++ if ($x & $mask) != 0; + $digit += 2 if ($y & $mask) != 0; + $key .= $digit; + } + return $key; +} + +sub map_tiles { + my ($self, $x, $y, $z) = @_; + if ($z >= 16) { + return [ + "http://a.os.openstreetmap.org/sv/$z/" . ($x-1) . "/" . ($y-1) . ".png", + "http://b.os.openstreetmap.org/sv/$z/$x/" . ($y-1) . ".png", + "http://c.os.openstreetmap.org/sv/$z/" . ($x-1) . "/$y.png", + "http://os.openstreetmap.org/sv/$z/$x/$y.png", + ]; + } else { + return [ + "http://ecn.t0.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y-1, $z) . ".png?g=587&productSet=mmOS", + "http://ecn.t1.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y-1, $z) . ".png?g=587&productSet=mmOS", + "http://ecn.t2.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y, $z) . ".png?g=587&productSet=mmOS", + "http://ecn.t3.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y, $z) . ".png?g=587&productSet=mmOS", + ]; + } } 1; diff --git a/perllib/FixMyStreet/Map/OSM.pm b/perllib/FixMyStreet/Map/OSM.pm index 194f403c9..363f4b01e 100644 --- a/perllib/FixMyStreet/Map/OSM.pm +++ b/perllib/FixMyStreet/Map/OSM.pm @@ -10,8 +10,12 @@ package FixMyStreet::Map::OSM; use strict; use Math::Trig; +use mySociety::Gaze; use Utils; +use constant ZOOM_LEVELS => 5; +use constant MIN_ZOOM_LEVEL => 13; + sub header_js { return ' <script type="text/javascript" src="/jslib/OpenLayers-2.10/OpenLayers.js"></script> @@ -24,6 +28,17 @@ sub map_type { return 'OpenLayers.Layer.OSM.Mapnik'; } +sub map_tiles { + my ($self, $x, $y, $z) = @_; + my $tile_url = $self->base_tile_url(); + return [ + "http://a.$tile_url/$z/" . ($x - 1) . "/" . ($y - 1) . ".png", + "http://b.$tile_url/$z/$x/" . ($y - 1) . ".png", + "http://c.$tile_url/$z/" . ($x - 1) . "/$y.png", + "http://$tile_url/$z/$x/$y.png", + ]; +} + sub base_tile_url { return 'tile.openstreetmap.org'; } @@ -40,40 +55,50 @@ sub copyright { sub display_map { my ($self, $c, %params) = @_; + # Adjust zoom level dependent upon population density + my $dist = mySociety::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude}, 200_000 ); + my $default_zoom = ZOOM_LEVELS - 3; + $default_zoom = ZOOM_LEVELS - 2 if $dist < 10; + # Map centre may be overridden in the query string $params{latitude} = Utils::truncate_coordinate($c->req->params->{lat} + 0) if defined $c->req->params->{lat}; $params{longitude} = Utils::truncate_coordinate($c->req->params->{lon} + 0) if defined $c->req->params->{lon}; - my $zoom = defined $c->req->params->{zoom} ? $c->req->params->{zoom} + 0 : 2; - $zoom = 3 if $zoom > 3; + my $zoom = defined $c->req->params->{zoom} ? $c->req->params->{zoom} + 0 : $default_zoom; + $zoom = ZOOM_LEVELS - 1 if $zoom >= ZOOM_LEVELS; $zoom = 0 if $zoom < 0; - my $zoom_act = 14 + $zoom; + my $zoom_act = MIN_ZOOM_LEVEL + $zoom; my ($x_tile, $y_tile) = latlon_to_tile_with_adjust($params{latitude}, $params{longitude}, $zoom_act); foreach my $pin (@{$params{pins}}) { ($pin->{px}, $pin->{py}) = latlon_to_px($pin->{latitude}, $pin->{longitude}, $x_tile, $y_tile, $zoom_act); } - my $compass = { - north => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x_tile, $y_tile-1, $zoom_act ) ], - south => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x_tile, $y_tile+1, $zoom_act ) ], - west => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x_tile-1, $y_tile, $zoom_act ) ], - east => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x_tile+1, $y_tile, $zoom_act ) ], - here => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x_tile, $y_tile, $zoom_act ) ], - }; $c->stash->{map} = { %params, type => 'osm', map_type => $self->map_type(), - tile_url => $self->base_tile_url(), + tiles => $self->map_tiles( $x_tile, $y_tile, $zoom_act ), copyright => $self->copyright(), x_tile => $x_tile, y_tile => $y_tile, zoom => $zoom, zoom_act => $zoom_act, - compass => $compass, + zoom_levels => ZOOM_LEVELS, + compass => compass( $x_tile, $y_tile, $zoom_act ), + }; +} + +sub compass { + my ( $x, $y, $z ) = @_; + return { + north => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x, $y-1, $z ) ], + south => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x, $y+1, $z ) ], + west => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x-1, $y, $z ) ], + east => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x+1, $y, $z ) ], + here => [ map { Utils::truncate_coordinate($_) } tile_to_latlon( $x, $y, $z ) ], }; } @@ -141,7 +166,7 @@ sub click_to_wgs84 { my ($self, $c, $pin_tile_x, $pin_x, $pin_tile_y, $pin_y) = @_; my $tile_x = click_to_tile($pin_tile_x, $pin_x); my $tile_y = click_to_tile($pin_tile_y, $pin_y); - my $zoom = 14 + (defined $c->req->params->{zoom} ? $c->req->params->{zoom} : 2); + my $zoom = MIN_ZOOM_LEVEL + (defined $c->req->params->{zoom} ? $c->req->params->{zoom} : 2); my ($lat, $lon) = tile_to_latlon($tile_x, $tile_y, $zoom); return ( $lat, $lon ); } |