diff options
Diffstat (limited to 'perllib/FixMyStreet/Map')
-rw-r--r-- | perllib/FixMyStreet/Map/CheshireEast.pm | 70 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map/OSM.pm | 40 |
2 files changed, 90 insertions, 20 deletions
diff --git a/perllib/FixMyStreet/Map/CheshireEast.pm b/perllib/FixMyStreet/Map/CheshireEast.pm new file mode 100644 index 000000000..4e59f2593 --- /dev/null +++ b/perllib/FixMyStreet/Map/CheshireEast.pm @@ -0,0 +1,70 @@ +package FixMyStreet::Map::CheshireEast; +use base 'FixMyStreet::Map::OSM'; + +use strict; +use Utils; + +use constant MIN_ZOOM_LEVEL => 7; + +sub map_javascript { [ + '/vendor/OpenLayers/OpenLayers.wfs.js', + '/js/map-OpenLayers.js', + '/js/map-cheshireeast.js', +] } + +sub tile_parameters { { + origin_x => -3276800, + origin_y => 3276800, +} } + +sub resolutions { ( + 1792.003584007169, + 896.0017920035843, + 448.0008960017922, + 224.0004480008961, + 112.000224000448, + 56.000112000224014, + 28.000056000111993, + 14.000028000056004, + 7.000014000028002, + 2.8000056000112004, + 1.4000028000056002, + 0.7000014000028001, + 0.35000070000140004, + 0.14000028000056003, +) } + +my $url = 'https://maps-cache.cheshiresharedservices.gov.uk/maps/?wmts/CE_OS_AllBasemaps_COLOUR/oscce_grid/%d/%d/%d.jpeg&KEY=3a3f5c60eca1404ea114e6941c9d3895'; + +sub map_tiles { + my ( $self, %params ) = @_; + my ( $x, $y, $z ) = ( $params{x_tile}, $params{y_tile}, $params{zoom_act} ); + return [ + sprintf($url, $z, $x-1, $y-1), + sprintf($url, $z, $x, $y-1), + sprintf($url, $z, $x-1, $y), + sprintf($url, $z, $x, $y), + ]; +} + +sub latlon_to_tile($$$$) { + my ($self, $lat, $lon, $zoom) = @_; + my ($x, $y) = eval { Utils::convert_latlon_to_en($lat, $lon) }; + my $tile_params = $self->tile_parameters; + my $res = ($self->resolutions)[$zoom]; + my $fx = ( $x - $tile_params->{origin_x} ) / ($res * 256); + my $fy = ( $tile_params->{origin_y} - $y ) / ($res * 256); + return ( $fx, $fy ); +} + +sub tile_to_latlon { + my ($self, $fx, $fy, $zoom) = @_; + my $tile_params = $self->tile_parameters; + my $res = ($self->resolutions)[$zoom]; + my $x = $fx * $res * 256 + $tile_params->{origin_x}; + my $y = $tile_params->{origin_y} - $fy * $res * 256; + my ($lat, $lon) = Utils::convert_en_to_latlon($x, $y); + return ( $lat, $lon ); +} + +1; diff --git a/perllib/FixMyStreet/Map/OSM.pm b/perllib/FixMyStreet/Map/OSM.pm index a6cb6acea..ef465d7dc 100644 --- a/perllib/FixMyStreet/Map/OSM.pm +++ b/perllib/FixMyStreet/Map/OSM.pm @@ -87,10 +87,10 @@ sub generate_map_data { $zoom = $numZoomLevels - 1 if $zoom >= $numZoomLevels; $zoom = 0 if $zoom < 0; $params{zoom_act} = $zoomOffset + $zoom; - ($params{x_tile}, $params{y_tile}) = latlon_to_tile_with_adjust($params{latitude}, $params{longitude}, $params{zoom_act}); + ($params{x_tile}, $params{y_tile}) = $self->latlon_to_tile_with_adjust($params{latitude}, $params{longitude}, $params{zoom_act}); foreach my $pin (@{$params{pins}}) { - ($pin->{px}, $pin->{py}) = latlon_to_px($pin->{latitude}, $pin->{longitude}, $params{x_tile}, $params{y_tile}, $params{zoom_act}); + ($pin->{px}, $pin->{py}) = $self->latlon_to_px($pin->{latitude}, $pin->{longitude}, $params{x_tile}, $params{y_tile}, $params{zoom_act}); } return { @@ -102,24 +102,24 @@ sub generate_map_data { zoom => $zoom, zoomOffset => $zoomOffset, numZoomLevels => $numZoomLevels, - compass => compass( $params{x_tile}, $params{y_tile}, $params{zoom_act} ), + compass => $self->compass( $params{x_tile}, $params{y_tile}, $params{zoom_act} ), }; } sub compass { - my ( $x, $y, $z ) = @_; + my ( $self, $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 ) ], + north => [ map { Utils::truncate_coordinate($_) } $self->tile_to_latlon( $x, $y-1, $z ) ], + south => [ map { Utils::truncate_coordinate($_) } $self->tile_to_latlon( $x, $y+1, $z ) ], + west => [ map { Utils::truncate_coordinate($_) } $self->tile_to_latlon( $x-1, $y, $z ) ], + east => [ map { Utils::truncate_coordinate($_) } $self->tile_to_latlon( $x+1, $y, $z ) ], + here => [ map { Utils::truncate_coordinate($_) } $self->tile_to_latlon( $x, $y, $z ) ], }; } # Given a lat/lon, convert it to OSM tile co-ordinates (precise). -sub latlon_to_tile($$$) { - my ($lat, $lon, $zoom) = @_; +sub latlon_to_tile($$$$) { + my ($self, $lat, $lon, $zoom) = @_; my $x_tile = ($lon + 180) / 360 * 2**$zoom; my $y_tile = (1 - log(tan(deg2rad($lat)) + sec(deg2rad($lat))) / pi) / 2 * 2**$zoom; return ( $x_tile, $y_tile ); @@ -127,9 +127,9 @@ sub latlon_to_tile($$$) { # Given a lat/lon, convert it to OSM tile co-ordinates (nearest actual tile, # adjusted so the point will be near the centre of a 2x2 tiled map). -sub latlon_to_tile_with_adjust($$$) { - my ($lat, $lon, $zoom) = @_; - my ($x_tile, $y_tile) = latlon_to_tile($lat, $lon, $zoom); +sub latlon_to_tile_with_adjust($$$$) { + my ($self, $lat, $lon, $zoom) = @_; + my ($x_tile, $y_tile) = $self->latlon_to_tile($lat, $lon, $zoom); # Try and have point near centre of map if ($x_tile - int($x_tile) > 0.5) { @@ -143,7 +143,7 @@ sub latlon_to_tile_with_adjust($$$) { } sub tile_to_latlon { - my ($x, $y, $zoom) = @_; + my ($self, $x, $y, $zoom) = @_; my $n = 2 ** $zoom; my $lon = $x / $n * 360 - 180; my $lat = rad2deg(atan(sinh(pi * (1 - 2 * $y / $n)))); @@ -151,9 +151,9 @@ sub tile_to_latlon { } # Given a lat/lon, convert it to pixel co-ordinates from the top left of the map -sub latlon_to_px($$$$$) { - my ($lat, $lon, $x_tile, $y_tile, $zoom) = @_; - my ($pin_x_tile, $pin_y_tile) = latlon_to_tile($lat, $lon, $zoom); +sub latlon_to_px($$$$$$) { + my ($self, $lat, $lon, $x_tile, $y_tile, $zoom) = @_; + my ($pin_x_tile, $pin_y_tile) = $self->latlon_to_tile($lat, $lon, $zoom); my $pin_x = tile_to_px($pin_x_tile, $x_tile); my $pin_y = tile_to_px($pin_y_tile, $y_tile); return ($pin_x, $pin_y); @@ -182,8 +182,8 @@ 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 = MIN_ZOOM_LEVEL + (defined $c->get_param('zoom') ? $c->get_param('zoom') : 3); - my ($lat, $lon) = tile_to_latlon($tile_x, $tile_y, $zoom); + my $zoom = $self->MIN_ZOOM_LEVEL + (defined $c->get_param('zoom') ? $c->get_param('zoom') : 3); + my ($lat, $lon) = $self->tile_to_latlon($tile_x, $tile_y, $zoom); return ( $lat, $lon ); } |