diff options
author | Matthew Somerville <matthew@balti.ukcod.org.uk> | 2010-11-22 15:49:43 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@balti.ukcod.org.uk> | 2010-11-22 15:54:23 +0000 |
commit | fadd20a3350e9032ef3dfc8b4e2faf340d5f0af2 (patch) | |
tree | 70968af5583f85dbfcac8f314ce05e93403d3b37 | |
parent | 3f62b70cd98827899f5820290ea1b52643122b5e (diff) |
Move map functions to their own package.
-rw-r--r-- | perllib/FixMyStreet/Map.pm | 296 | ||||
-rw-r--r-- | perllib/Page.pm | 277 | ||||
-rw-r--r-- | perllib/Standard.pm | 2 | ||||
-rwxr-xr-x | web/ajax.cgi | 2 | ||||
-rwxr-xr-x | web/alert.cgi | 4 | ||||
-rwxr-xr-x | web/index.cgi | 54 | ||||
-rwxr-xr-x | web/questionnaire.cgi | 8 | ||||
-rwxr-xr-x | web/rss.cgi | 4 |
8 files changed, 337 insertions, 310 deletions
diff --git a/perllib/FixMyStreet/Map.pm b/perllib/FixMyStreet/Map.pm new file mode 100644 index 000000000..c3608b390 --- /dev/null +++ b/perllib/FixMyStreet/Map.pm @@ -0,0 +1,296 @@ +#!/usr/bin/perl +# +# FixMyStreet:Map +# Adding the ability to have different maps on FixMyStreet. +# +# Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ + +package FixMyStreet::Map; + +use strict; +use LWP::Simple; + +use Problems; +use Cobrand; +use mySociety::Config; +use mySociety::Gaze; +use mySociety::GeoUtil; +use mySociety::Locale; +use mySociety::Web qw(ent NewURL); + +use constant TILE_WIDTH => mySociety::Config::get('TILES_WIDTH'); +use constant TIF_SIZE_M => mySociety::Config::get('TILES_TIFF_SIZE_METRES'); +use constant TIF_SIZE_PX => mySociety::Config::get('TILES_TIFF_SIZE_PIXELS'); +use constant SCALE_FACTOR => TIF_SIZE_M / (TIF_SIZE_PX / TILE_WIDTH); + +# display_map Q PARAMS +# PARAMS include: +# X,Y is bottom left tile of 2x2 grid +# TYPE is 1 if the map is clickable, 2 if clickable and has a form upload, +# 0 if not clickable +# PINS is HTML of pins to show +# PX,PY are coordinates of pin +# PRE/POST are HTML to show above/below map +sub display_map { + my ($q, %params) = @_; + $params{pins} ||= ''; + $params{pre} ||= ''; + $params{post} ||= ''; + my $mid_point = TILE_WIDTH; # Map is 2 TILE_WIDTHs in size, square. + if ($q->{site} eq 'barnet') { # Map is c. 380px wide + $mid_point = 189; + } + my $px = defined($params{px}) ? $mid_point - $params{px} : 0; + my $py = defined($params{py}) ? $mid_point - $params{py} : 0; + my $x = int($params{x})<=0 ? 0 : $params{x}; + my $y = int($params{y})<=0 ? 0 : $params{y}; + my $url = mySociety::Config::get('TILES_URL'); + my $tiles_url = $url . ($x-1) . '-' . $x . ',' . ($y-1) . '-' . $y . '/RABX'; + my $tiles = LWP::Simple::get($tiles_url); + return '<div id="map_box"> <div id="map"><div id="drag">' . _("Unable to fetch the map tiles from the tile server.") . '</div></div></div><div id="side">' if !$tiles; + my $tileids = RABX::unserialise($tiles); + my $tl = ($x-1) . '.' . $y; + my $tr = $x . '.' . $y; + my $bl = ($x-1) . '.' . ($y-1); + my $br = $x . '.' . ($y-1); + return '<div id="side">' if (!$tileids->[0][0] || !$tileids->[0][1] || !$tileids->[1][0] || !$tileids->[1][1]); + my $tl_src = $url . $tileids->[0][0]; + my $tr_src = $url . $tileids->[0][1]; + my $bl_src = $url . $tileids->[1][0]; + my $br_src = $url . $tileids->[1][1]; + + my $out = ''; + my $cobrand = Page::get_cobrand($q); + my $root_path_js = Cobrand::root_path_js($cobrand, $q); + my $cobrand_form_elements = Cobrand::form_elements($cobrand, 'mapForm', $q); + my $img_type; + my $form_action = Cobrand::url($cobrand, '', $q); + if ($params{type}) { + my $encoding = ''; + $encoding = ' enctype="multipart/form-data"' if ($params{type}==2); + my $pc = $q->param('pc') || ''; + my $pc_enc = ent($pc); + $out .= <<EOF; +<form action="$form_action" method="post" name="mapForm" id="mapForm"$encoding> +<input type="hidden" name="submit_map" value="1"> +<input type="hidden" name="x" id="formX" value="$x"> +<input type="hidden" name="y" id="formY" value="$y"> +<input type="hidden" name="pc" value="$pc_enc"> +$cobrand_form_elements +EOF + $img_type = '<input type="image"'; + } else { + $img_type = '<img'; + } + my $imgw = TILE_WIDTH . 'px'; + my $tile_width = TILE_WIDTH; + my $tile_type = mySociety::Config::get('TILES_TYPE'); + $out .= <<EOF; +<script type="text/javascript"> +$root_path_js +var fixmystreet = { + 'x': $x - 3, + 'y': $y - 3, + 'start_x': $px, + 'start_y': $py, + 'tile_type': '$tile_type', + 'tilewidth': $tile_width, + 'tileheight': $tile_width +}; +</script> +<div id="map_box"> +$params{pre} + <div id="map"><div id="drag"> + $img_type alt="NW map tile" id="t2.2" name="tile_$tl" src="$tl_src" style="top:0px; left:0;">$img_type alt="NE map tile" id="t2.3" name="tile_$tr" src="$tr_src" style="top:0px; left:$imgw;"><br>$img_type alt="SW map tile" id="t3.2" name="tile_$bl" src="$bl_src" style="top:$imgw; left:0;">$img_type alt="SE map tile" id="t3.3" name="tile_$br" src="$br_src" style="top:$imgw; left:$imgw;"> + <div id="pins">$params{pins}</div> + </div> +EOF + if (Cobrand::show_watermark($cobrand) && mySociety::Config::get('TILES_TYPE') ne 'streetview') { + $out .= '<div id="watermark"></div>'; + } + $out .= compass($q, $x, $y); + my $copyright; + if (mySociety::Config::get('TILES_TYPE') eq 'streetview') { + $copyright = _('Map contains Ordnance Survey data © Crown copyright and database right 2010.'); + } else { + $copyright = _('© Crown copyright. All rights reserved. Ministry of Justice 100037819 2008.'); + } + $out .= <<EOF; + </div> + <p id="copyright">$copyright</p> +$params{post} +EOF + $out .= '</div>'; + $out .= '<div id="side">'; + return $out; +} + +sub display_map_end { + my ($type) = @_; + my $out = '</div>'; + $out .= '</form>' if ($type); + return $out; +} + +sub display_pin { + my ($q, $px, $py, $col, $num) = @_; + $num = '' if !$num || $num > 9; + my $host = Page::base_url_with_lang($q, undef); + my %cols = (red=>'R', green=>'G', blue=>'B', purple=>'P'); + my $out = '<img class="pin" src="' . $host . '/i/pin' . $cols{$col} + . $num . '.gif" alt="' . _('Problem') . '" style="top:' . ($py-59) + . 'px; left:' . ($px) . 'px; position: absolute;">'; + return $out unless $_ && $_->{id} && $col ne 'blue'; + my $cobrand = Page::get_cobrand($q); + my $url = Cobrand::url($cobrand, NewURL($q, -retain => 1, + -url => '/report/' . $_->{id}, + pc => undef, + x => undef, + y => undef, + sx => undef, + sy => undef, + all_pins => undef, + no_pins => undef), $q); + $out = '<a title="' . ent($_->{title}) . '" href="' . $url . '">' . $out . '</a>'; + return $out; +} + +sub map_pins { + my ($q, $x, $y, $sx, $sy, $interval) = @_; + + my $pins = ''; + my $min_e = FixMyStreet::Map::tile_to_os($x-3); # Extra space to left/below due to rounding, I think + my $min_n = FixMyStreet::Map::tile_to_os($y-3); + my $mid_e = FixMyStreet::Map::tile_to_os($x); + my $mid_n = FixMyStreet::Map::tile_to_os($y); + my $max_e = FixMyStreet::Map::tile_to_os($x+2); + my $max_n = FixMyStreet::Map::tile_to_os($y+2); + my $cobrand = Page::get_cobrand($q); + # list of problems aoround map can be limited, but should show all pins + my $around_limit = Cobrand::on_map_list_limit($cobrand); + my $around_map; + my $around_map_list = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, $around_limit); + if ($around_limit) { + $around_map = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, undef); + } else { + $around_map = $around_map_list; + } + my @ids = (); + foreach (@$around_map_list) { + push(@ids, $_->{id}); + } + foreach (@$around_map) { + my $px = FixMyStreet::Map::os_to_px($_->{easting}, $sx); + my $py = FixMyStreet::Map::os_to_px($_->{northing}, $sy, 1); + my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; + $pins .= FixMyStreet::Map::display_pin($q, $px, $py, $col); + } + + my $dist; + mySociety::Locale::in_gb_locale { + my ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($mid_e, $mid_n, 'G'); + $dist = mySociety::Gaze::get_radius_containing_population($lat, $lon, 200000); + }; + $dist = int($dist*10+0.5)/10; + + my $limit = 20; # - @$current_map; + my $nearby = Problems::nearby($dist, join(',', @ids), $limit, $mid_e, $mid_n, $interval); + foreach (@$nearby) { + my $px = FixMyStreet::Map::os_to_px($_->{easting}, $sx); + my $py = FixMyStreet::Map::os_to_px($_->{northing}, $sy, 1); + my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; + $pins .= FixMyStreet::Map::display_pin($q, $px, $py, $col); + } + + return ($pins, $around_map_list, $nearby, $dist); +} + +sub compass ($$$) { + my ($q, $x, $y) = @_; + my @compass; + for (my $i=$x-1; $i<=$x+1; $i++) { + for (my $j=$y-1; $j<=$y+1; $j++) { + $compass[$i][$j] = NewURL($q, x=>$i, y=>$j); + } + } + my $recentre = NewURL($q); + my $host = Page::base_url_with_lang($q, undef); + return <<EOF; +<table cellpadding="0" cellspacing="0" border="0" id="compass"> +<tr valign="bottom"> +<td align="right"><a rel="nofollow" href="${compass[$x-1][$y+1]}"><img src="$host/i/arrow-northwest.gif" alt="NW" width=11 height=11></a></td> +<td align="center"><a rel="nofollow" href="${compass[$x][$y+1]}"><img src="$host/i/arrow-north.gif" vspace="3" alt="N" width=13 height=11></a></td> +<td><a rel="nofollow" href="${compass[$x+1][$y+1]}"><img src="$host/i/arrow-northeast.gif" alt="NE" width=11 height=11></a></td> +</tr> +<tr> +<td><a rel="nofollow" href="${compass[$x-1][$y]}"><img src="$host/i/arrow-west.gif" hspace="3" alt="W" width=11 height=13></a></td> +<td align="center"><a rel="nofollow" href="$recentre"><img src="$host/i/rose.gif" alt="Recentre" width=35 height=34></a></td> +<td><a rel="nofollow" href="${compass[$x+1][$y]}"><img src="$host/i/arrow-east.gif" hspace="3" alt="E" width=11 height=13></a></td> +</tr> +<tr valign="top"> +<td align="right"><a rel="nofollow" href="${compass[$x-1][$y-1]}"><img src="$host/i/arrow-southwest.gif" alt="SW" width=11 height=11></a></td> +<td align="center"><a rel="nofollow" href="${compass[$x][$y-1]}"><img src="$host/i/arrow-south.gif" vspace="3" alt="S" width=13 height=11></a></td> +<td><a rel="nofollow" href="${compass[$x+1][$y-1]}"><img src="$host/i/arrow-southeast.gif" alt="SE" width=11 height=11></a></td> +</tr> +</table> +EOF +} + +# P is easting or northing +# C is centre tile reference of displayed map +sub os_to_px { + my ($p, $c, $invert) = @_; + return tile_to_px(os_to_tile($p), $c, $invert); +} + +# Convert tile co-ordinates to pixel co-ordinates from top left of map +# C is centre tile reference of displayed map +sub tile_to_px { + my ($p, $c, $invert) = @_; + $p = TILE_WIDTH * ($p - $c + 1); + $p = 2 * TILE_WIDTH - $p if $invert; + $p = int($p + .5 * ($p <=> 0)); + return $p; +} + +# Tile co-ordinates are linear scale of OS E/N +# Will need more generalising when more zooms appear +sub os_to_tile { + return $_[0] / SCALE_FACTOR; +} +sub tile_to_os { + return int($_[0] * SCALE_FACTOR + 0.5); +} + +sub click_to_tile { + my ($pin_tile, $pin, $invert) = @_; + $pin -= TILE_WIDTH while $pin > TILE_WIDTH; + $pin += TILE_WIDTH while $pin < 0; + $pin = TILE_WIDTH - $pin if $invert; # image submits measured from top down + return $pin_tile + $pin / TILE_WIDTH; +} + +sub os_to_px_with_adjust { + my ($q, $easting, $northing, $in_x, $in_y) = @_; + + my $x = FixMyStreet::Map::os_to_tile($easting); + my $y = FixMyStreet::Map::os_to_tile($northing); + my $x_tile = $in_x || int($x); + my $y_tile = $in_y || int($y); + my $px = FixMyStreet::Map::os_to_px($easting, $x_tile); + my $py = FixMyStreet::Map::os_to_px($northing, $y_tile, 1); + if ($q->{site} eq 'barnet') { # Map is 380px + if ($py > 380) { + $y_tile--; + $py = FixMyStreet::Map::os_to_px($northing, $y_tile, 1); + } + if ($px > 380) { + $x_tile++; + $px = FixMyStreet::Map::os_to_px($easting, $x_tile); + } + } + return ($x, $y, $x_tile, $y_tile, $px, $py); +} + +1; diff --git a/perllib/Page.pm b/perllib/Page.pm index 127fad11b..06bdb27a4 100644 --- a/perllib/Page.pm +++ b/perllib/Page.pm @@ -401,275 +401,6 @@ sub error_page ($$) { print $q->header(-content_length => length($html)), $html; } -# display_map Q PARAMS -# PARAMS include: -# X,Y is bottom left tile of 2x2 grid -# TYPE is 1 if the map is clickable, 2 if clickable and has a form upload, -# 0 if not clickable -# PINS is HTML of pins to show -# PX,PY are coordinates of pin -# PRE/POST are HTML to show above/below map -sub display_map { - my ($q, %params) = @_; - $params{pins} ||= ''; - $params{pre} ||= ''; - $params{post} ||= ''; - my $mid_point = TILE_WIDTH; # Map is 2 TILE_WIDTHs in size, square. - if ($q->{site} eq 'barnet') { # Map is c. 380px wide - $mid_point = 189; - } - my $px = defined($params{px}) ? $mid_point - $params{px} : 0; - my $py = defined($params{py}) ? $mid_point - $params{py} : 0; - my $x = int($params{x})<=0 ? 0 : $params{x}; - my $y = int($params{y})<=0 ? 0 : $params{y}; - my $url = mySociety::Config::get('TILES_URL'); - my $tiles_url = $url . ($x-1) . '-' . $x . ',' . ($y-1) . '-' . $y . '/RABX'; - my $tiles = LWP::Simple::get($tiles_url); - return '<div id="map_box"> <div id="map"><div id="drag">' . _("Unable to fetch the map tiles from the tile server.") . '</div></div></div><div id="side">' if !$tiles; - my $tileids = RABX::unserialise($tiles); - my $tl = ($x-1) . '.' . $y; - my $tr = $x . '.' . $y; - my $bl = ($x-1) . '.' . ($y-1); - my $br = $x . '.' . ($y-1); - return '<div id="side">' if (!$tileids->[0][0] || !$tileids->[0][1] || !$tileids->[1][0] || !$tileids->[1][1]); - my $tl_src = $url . $tileids->[0][0]; - my $tr_src = $url . $tileids->[0][1]; - my $bl_src = $url . $tileids->[1][0]; - my $br_src = $url . $tileids->[1][1]; - - my $out = ''; - my $cobrand = Page::get_cobrand($q); - my $root_path_js = Cobrand::root_path_js($cobrand, $q); - my $cobrand_form_elements = Cobrand::form_elements($cobrand, 'mapForm', $q); - my $img_type; - my $form_action = Cobrand::url($cobrand, '', $q); - if ($params{type}) { - my $encoding = ''; - $encoding = ' enctype="multipart/form-data"' if ($params{type}==2); - my $pc = $q->param('pc') || ''; - my $pc_enc = ent($pc); - $out .= <<EOF; -<form action="$form_action" method="post" name="mapForm" id="mapForm"$encoding> -<input type="hidden" name="submit_map" value="1"> -<input type="hidden" name="x" id="formX" value="$x"> -<input type="hidden" name="y" id="formY" value="$y"> -<input type="hidden" name="pc" value="$pc_enc"> -$cobrand_form_elements -EOF - $img_type = '<input type="image"'; - } else { - $img_type = '<img'; - } - my $imgw = TILE_WIDTH . 'px'; - my $tile_width = TILE_WIDTH; - my $tile_type = mySociety::Config::get('TILES_TYPE'); - $out .= <<EOF; -<script type="text/javascript"> -$root_path_js -var fixmystreet = { - 'x': $x - 3, - 'y': $y - 3, - 'start_x': $px, - 'start_y': $py, - 'tile_type': '$tile_type', - 'tilewidth': $tile_width, - 'tileheight': $tile_width -}; -</script> -<div id="map_box"> -$params{pre} - <div id="map"><div id="drag"> - $img_type alt="NW map tile" id="t2.2" name="tile_$tl" src="$tl_src" style="top:0px; left:0;">$img_type alt="NE map tile" id="t2.3" name="tile_$tr" src="$tr_src" style="top:0px; left:$imgw;"><br>$img_type alt="SW map tile" id="t3.2" name="tile_$bl" src="$bl_src" style="top:$imgw; left:0;">$img_type alt="SE map tile" id="t3.3" name="tile_$br" src="$br_src" style="top:$imgw; left:$imgw;"> - <div id="pins">$params{pins}</div> - </div> -EOF - if (Cobrand::show_watermark($cobrand) && mySociety::Config::get('TILES_TYPE') ne 'streetview') { - $out .= '<div id="watermark"></div>'; - } - $out .= compass($q, $x, $y); - my $copyright; - if (mySociety::Config::get('TILES_TYPE') eq 'streetview') { - $copyright = _('Map contains Ordnance Survey data © Crown copyright and database right 2010.'); - } else { - $copyright = _('© Crown copyright. All rights reserved. Ministry of Justice 100037819 2008.'); - } - $out .= <<EOF; - </div> - <p id="copyright">$copyright</p> -$params{post} -EOF - $out .= '</div>'; - $out .= '<div id="side">'; - return $out; -} - -sub display_map_end { - my ($type) = @_; - my $out = '</div>'; - $out .= '</form>' if ($type); - return $out; -} - -sub display_pin { - my ($q, $px, $py, $col, $num) = @_; - $num = '' if !$num || $num > 9; - my $host = base_url_with_lang($q, undef); - my %cols = (red=>'R', green=>'G', blue=>'B', purple=>'P'); - my $out = '<img class="pin" src="' . $host . '/i/pin' . $cols{$col} - . $num . '.gif" alt="' . _('Problem') . '" style="top:' . ($py-59) - . 'px; left:' . ($px) . 'px; position: absolute;">'; - return $out unless $_ && $_->{id} && $col ne 'blue'; - my $cobrand = Page::get_cobrand($q); - my $url = Cobrand::url($cobrand, NewURL($q, -retain => 1, - -url => '/report/' . $_->{id}, - pc => undef, - x => undef, - y => undef, - sx => undef, - sy => undef, - all_pins => undef, - no_pins => undef), $q); - $out = '<a title="' . ent($_->{title}) . '" href="' . $url . '">' . $out . '</a>'; - return $out; -} - -sub map_pins { - my ($q, $x, $y, $sx, $sy, $interval) = @_; - - my $pins = ''; - my $min_e = Page::tile_to_os($x-3); # Extra space to left/below due to rounding, I think - my $min_n = Page::tile_to_os($y-3); - my $mid_e = Page::tile_to_os($x); - my $mid_n = Page::tile_to_os($y); - my $max_e = Page::tile_to_os($x+2); - my $max_n = Page::tile_to_os($y+2); - my $cobrand = Page::get_cobrand($q); - # list of problems aoround map can be limited, but should show all pins - my $around_limit = Cobrand::on_map_list_limit($cobrand); - my $around_map; - my $around_map_list = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, $around_limit); - if ($around_limit) { - $around_map = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, undef); - } else { - $around_map = $around_map_list; - } - my @ids = (); - foreach (@$around_map_list) { - push(@ids, $_->{id}); - } - foreach (@$around_map) { - my $px = Page::os_to_px($_->{easting}, $sx); - my $py = Page::os_to_px($_->{northing}, $sy, 1); - my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; - $pins .= Page::display_pin($q, $px, $py, $col); - } - - my $dist; - mySociety::Locale::in_gb_locale { - my ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($mid_e, $mid_n, 'G'); - $dist = mySociety::Gaze::get_radius_containing_population($lat, $lon, 200000); - }; - $dist = int($dist*10+0.5)/10; - - my $limit = 20; # - @$current_map; - my $nearby = Problems::nearby($dist, join(',', @ids), $limit, $mid_e, $mid_n, $interval); - foreach (@$nearby) { - my $px = Page::os_to_px($_->{easting}, $sx); - my $py = Page::os_to_px($_->{northing}, $sy, 1); - my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; - $pins .= Page::display_pin($q, $px, $py, $col); - } - - return ($pins, $around_map_list, $nearby, $dist); -} - -sub compass ($$$) { - my ($q, $x, $y) = @_; - my @compass; - for (my $i=$x-1; $i<=$x+1; $i++) { - for (my $j=$y-1; $j<=$y+1; $j++) { - $compass[$i][$j] = NewURL($q, x=>$i, y=>$j); - } - } - my $recentre = NewURL($q); - my $host = base_url_with_lang($q, undef); - return <<EOF; -<table cellpadding="0" cellspacing="0" border="0" id="compass"> -<tr valign="bottom"> -<td align="right"><a rel="nofollow" href="${compass[$x-1][$y+1]}"><img src="$host/i/arrow-northwest.gif" alt="NW" width=11 height=11></a></td> -<td align="center"><a rel="nofollow" href="${compass[$x][$y+1]}"><img src="$host/i/arrow-north.gif" vspace="3" alt="N" width=13 height=11></a></td> -<td><a rel="nofollow" href="${compass[$x+1][$y+1]}"><img src="$host/i/arrow-northeast.gif" alt="NE" width=11 height=11></a></td> -</tr> -<tr> -<td><a rel="nofollow" href="${compass[$x-1][$y]}"><img src="$host/i/arrow-west.gif" hspace="3" alt="W" width=11 height=13></a></td> -<td align="center"><a rel="nofollow" href="$recentre"><img src="$host/i/rose.gif" alt="Recentre" width=35 height=34></a></td> -<td><a rel="nofollow" href="${compass[$x+1][$y]}"><img src="$host/i/arrow-east.gif" hspace="3" alt="E" width=11 height=13></a></td> -</tr> -<tr valign="top"> -<td align="right"><a rel="nofollow" href="${compass[$x-1][$y-1]}"><img src="$host/i/arrow-southwest.gif" alt="SW" width=11 height=11></a></td> -<td align="center"><a rel="nofollow" href="${compass[$x][$y-1]}"><img src="$host/i/arrow-south.gif" vspace="3" alt="S" width=13 height=11></a></td> -<td><a rel="nofollow" href="${compass[$x+1][$y-1]}"><img src="$host/i/arrow-southeast.gif" alt="SE" width=11 height=11></a></td> -</tr> -</table> -EOF -} - -# P is easting or northing -# C is centre tile reference of displayed map -sub os_to_px { - my ($p, $c, $invert) = @_; - return tile_to_px(os_to_tile($p), $c, $invert); -} - -# Convert tile co-ordinates to pixel co-ordinates from top left of map -# C is centre tile reference of displayed map -sub tile_to_px { - my ($p, $c, $invert) = @_; - $p = TILE_WIDTH * ($p - $c + 1); - $p = 2 * TILE_WIDTH - $p if $invert; - $p = int($p + .5 * ($p <=> 0)); - return $p; -} - -# Tile co-ordinates are linear scale of OS E/N -# Will need more generalising when more zooms appear -sub os_to_tile { - return $_[0] / SCALE_FACTOR; -} -sub tile_to_os { - return int($_[0] * SCALE_FACTOR + 0.5); -} - -sub click_to_tile { - my ($pin_tile, $pin, $invert) = @_; - $pin -= TILE_WIDTH while $pin > TILE_WIDTH; - $pin += TILE_WIDTH while $pin < 0; - $pin = TILE_WIDTH - $pin if $invert; # image submits measured from top down - return $pin_tile + $pin / TILE_WIDTH; -} - -sub os_to_px_with_adjust { - my ($q, $easting, $northing, $in_x, $in_y) = @_; - - my $x = Page::os_to_tile($easting); - my $y = Page::os_to_tile($northing); - my $x_tile = $in_x || int($x); - my $y_tile = $in_y || int($y); - my $px = Page::os_to_px($easting, $x_tile); - my $py = Page::os_to_px($northing, $y_tile, 1); - if ($q->{site} eq 'barnet') { # Map is 380px - if ($py > 380) { - $y_tile--; - $py = Page::os_to_px($northing, $y_tile, 1); - } - if ($px > 380) { - $x_tile++; - $px = Page::os_to_px($easting, $x_tile); - } - } - return ($x, $y, $x_tile, $y_tile, $px, $py); -} - # send_email TO (NAME) TEMPLATE-NAME PARAMETERS # TEMPLATE-NAME is currently one of problem, update, alert, tms sub send_email { @@ -943,8 +674,8 @@ sub geocode { unless ($error = mapit_check_error($location)) { $easting = $location->{easting}; $northing = $location->{northing}; - my $xx = Page::os_to_tile($easting); - my $yy = Page::os_to_tile($northing); + my $xx = FixMyStreet::Map::os_to_tile($easting); + my $yy = FixMyStreet::Map::os_to_tile($northing); $x = int($xx); $y = int($yy); $x += 1 if ($xx - $x > 0.5); @@ -968,8 +699,8 @@ sub geocoded_string_coordinates { my $lon = $1; my $lat = $2; try { ($easting, $northing) = mySociety::GeoUtil::wgs84_to_national_grid($lat, $lon, 'G'); - my $xx = Page::os_to_tile($easting); - my $yy = Page::os_to_tile($northing); + my $xx = FixMyStreet::Map::os_to_tile($easting); + my $yy = FixMyStreet::Map::os_to_tile($northing); $x = int($xx); $y = int($yy); $x += 1 if ($xx - $x > 0.5); diff --git a/perllib/Standard.pm b/perllib/Standard.pm index eda2d89fb..de790a757 100644 --- a/perllib/Standard.pm +++ b/perllib/Standard.pm @@ -19,7 +19,7 @@ use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../commonlib/perllib"; use Page; - +use FixMyStreet::Map; package Standard; diff --git a/web/ajax.cgi b/web/ajax.cgi index fff437846..fa722f02e 100755 --- a/web/ajax.cgi +++ b/web/ajax.cgi @@ -35,7 +35,7 @@ sub main { unless ($input{all_pins}) { $interval = '6 months'; } - my ($pins, $on_map, $around_map, $dist) = Page::map_pins($q, $x, $y, $sx, $sy, $interval); + my ($pins, $on_map, $around_map, $dist) = FixMyStreet::Map::map_pins($q, $x, $y, $sx, $sy, $interval); my $cobrand = Page::get_cobrand($q); my $list = ''; my $link = ''; diff --git a/web/alert.cgi b/web/alert.cgi index 108a02683..c6a6eb647 100755 --- a/web/alert.cgi +++ b/web/alert.cgi @@ -80,8 +80,8 @@ sub alert_list { $x ||= 0; $x += 0; $y ||= 0; $y += 0; if ($x || $y) { - $e = Page::tile_to_os($input{x}); - $n = Page::tile_to_os($input{y}); + $e = FixMyStreet::Map::tile_to_os($input{x}); + $n = FixMyStreet::Map::tile_to_os($input{y}); } else { try { ($x, $y, $e, $n, $error) = Page::geocode($input{pc}, $q); diff --git a/web/index.cgi b/web/index.cgi index dcbec5a1c..00778c442 100755 --- a/web/index.cgi +++ b/web/index.cgi @@ -466,22 +466,22 @@ sub display_form { if ($input{skipped}) { # Map is being skipped if ($input{x} && $input{y}) { - $easting = Page::tile_to_os($input{x}); - $northing = Page::tile_to_os($input{y}); + $easting = FixMyStreet::Map::tile_to_os($input{x}); + $northing = FixMyStreet::Map::tile_to_os($input{y}); } else { my ($x, $y, $e, $n, $error) = Page::geocode($input{pc}, $q); $easting = $e; $northing = $n; } } elsif ($pin_x && $pin_y) { # Map was clicked on - $pin_x = Page::click_to_tile($pin_tile_x, $pin_x); - $pin_y = Page::click_to_tile($pin_tile_y, $pin_y, 1); + $pin_x = FixMyStreet::Map::click_to_tile($pin_tile_x, $pin_x); + $pin_y = FixMyStreet::Map::click_to_tile($pin_tile_y, $pin_y, 1); $input{x} ||= int($pin_x); $input{y} ||= int($pin_y); - $px = Page::tile_to_px($pin_x, $input{x}); - $py = Page::tile_to_px($pin_y, $input{y}, 1); - $easting = Page::tile_to_os($pin_x); - $northing = Page::tile_to_os($pin_y); + $px = FixMyStreet::Map::tile_to_px($pin_x, $input{x}); + $py = FixMyStreet::Map::tile_to_px($pin_y, $input{y}, 1); + $easting = FixMyStreet::Map::tile_to_os($pin_x); + $northing = FixMyStreet::Map::tile_to_os($pin_y); } elsif ($input{partial} && $input{pc} && !$input{easting} && !$input{northing}) { my ($x, $y, $error); try { @@ -491,14 +491,14 @@ sub display_form { }; return Page::geocode_choice($error, '/', $q) if ref($error) eq 'ARRAY'; return front_page($q, $error) if $error; - $input{x} = int(Page::os_to_tile($easting)); - $input{y} = int(Page::os_to_tile($northing)); - $px = Page::os_to_px($easting, $input{x}); - $py = Page::os_to_px($northing, $input{y}, 1); + $input{x} = int(FixMyStreet::Map::os_to_tile($easting)); + $input{y} = int(FixMyStreet::Map::os_to_tile($northing)); + $px = FixMyStreet::Map::os_to_px($easting, $input{x}); + $py = FixMyStreet::Map::os_to_px($northing, $input{y}, 1); } else { # Normal form submission my ($x, $y, $tile_x, $tile_y); - ($x, $y, $tile_x, $tile_y, $px, $py) = Page::os_to_px_with_adjust($q, $input{easting}, $input{northing}, undef, undef); + ($x, $y, $tile_x, $tile_y, $px, $py) = FixMyStreet::Map::os_to_px_with_adjust($q, $input{easting}, $input{northing}, undef, undef); $input{x} = $tile_x; $input{y} = $tile_y; $easting = $input_h{easting}; @@ -596,14 +596,14 @@ $cobrand_form_elements <div id="skipped-map"> EOF } else { - my $pins = Page::display_pin($q, $px, $py, 'purple'); + my $pins = FixMyStreet::Map::display_pin($q, $px, $py, 'purple'); my $type; if ($allow_photo_upload) { $type = 2; } else { $type = 1; } - $vars{form_start} = Page::display_map($q, x => $input{x}, 'y' => $input{y}, type => $type, + $vars{form_start} = FixMyStreet::Map::display_map($q, x => $input{x}, 'y' => $input{y}, type => $type, pins => $pins, px => $px, py => $py ); my $partial_id; if (my $token = $input{partial}) { @@ -770,7 +770,7 @@ EOF %vars = (%vars, category => $category, - map_end => Page::display_map_end(1), + map_end => FixMyStreet::Map::display_map_end(1), url_home => Cobrand::url($cobrand, '/', $q), submit_button => _('Submit') ); @@ -803,8 +803,8 @@ sub display_location { return front_page($q, $error) if ($error); if (!$easting || !$northing) { - $easting = Page::tile_to_os($x); - $northing = Page::tile_to_os($y); + $easting = FixMyStreet::Map::tile_to_os($x); + $northing = FixMyStreet::Map::tile_to_os($y); } # Check this location is okay to be displayed for the cobrand @@ -821,7 +821,7 @@ sub display_location { $all_text = _('Include stale reports'); $interval = '6 months'; } - my ($pins, $on_map, $around_map, $dist) = Page::map_pins($q, $x, $y, $x, $y, $interval); + my ($pins, $on_map, $around_map, $dist) = FixMyStreet::Map::map_pins($q, $x, $y, $x, $y, $interval); if ($input{no_pins}) { $hide_link = NewURL($q, -retain=>1, no_pins=>undef); $hide_text = _('Show pins'); @@ -861,8 +861,8 @@ sub display_location { my $url_skip = NewURL($q, -retain=>1, 'submit_map'=>1, skipped=>1); my $pc_h = ent($q->param('pc') || ''); my %vars = ( - 'map' => Page::display_map($q, x => $x, 'y' => $y, type => 1, pins => $pins, post => $map_links ), - map_end => Page::display_map_end(1), + 'map' => FixMyStreet::Map::display_map($q, x => $x, 'y' => $y, type => 1, pins => $pins, post => $map_links ), + map_end => FixMyStreet::Map::display_map_end(1), url_home => Cobrand::url($cobrand, '/', $q), url_rss => Cobrand::url($cobrand, NewURL($q, -retain => 1, -url=> "/rss/n/$easting,$northing", pc => undef, x => undef, 'y' => undef), $q), url_email => Cobrand::url($cobrand, NewURL($q, -retain => 1, pc => undef, -url=>'/alert', x=>$x, 'y'=>$y, feed=>"local:$easting:$northing"), $q), @@ -923,16 +923,16 @@ sub display_problem { my $problem = Problems::fetch_problem($input{id}); return display_location($q, _('Unknown problem ID')) unless $problem; return front_page($q, _('That report has been removed from FixMyStreet.'), '410 Gone') if $problem->{state} eq 'hidden'; - my ($x, $y, $x_tile, $y_tile, $px, $py) = Page::os_to_px_with_adjust($q, $problem->{easting}, $problem->{northing}, $input{x}, $input{y}); + my ($x, $y, $x_tile, $y_tile, $px, $py) = FixMyStreet::Map::os_to_px_with_adjust($q, $problem->{easting}, $problem->{northing}, $input{x}, $input{y}); # Try and have pin near centre of map if (!$input{x} && $x - $x_tile > 0.5) { $x_tile += 1; - $px = Page::os_to_px($problem->{easting}, $x_tile); + $px = FixMyStreet::Map::os_to_px($problem->{easting}, $x_tile); } if (!$input{y} && $y - $y_tile > 0.5) { $y_tile += 1; - $py = Page::os_to_px($problem->{northing}, $y_tile, 1); + $py = FixMyStreet::Map::os_to_px($problem->{northing}, $y_tile, 1); } my %vars; @@ -942,8 +942,8 @@ sub display_problem { my ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($problem->{easting}, $problem->{northing}, 'G'); my $map_links = "<p id='sub_map_links'><a href='http://maps.google.co.uk/maps?output=embed&z=16&q=" . URI::Escape::uri_escape_utf8($problem->{title} . ' - ' . $google_link) . "\@$lat,$lon'>View on Google Maps</a></p>"; - my $pins = Page::display_pin($q, $px, $py, 'blue'); - $vars{map_start} = Page::display_map($q, x => $x_tile, 'y' => $y_tile, type => 0, + my $pins = FixMyStreet::Map::display_pin($q, $px, $py, 'blue'); + $vars{map_start} = FixMyStreet::Map::display_map($q, x => $x_tile, 'y' => $y_tile, type => 0, pins => $pins, px => $px, py => $py, post => $map_links ); if ($q->{site} ne 'emptyhomes' && $problem->{state} eq 'confirmed' && $problem->{duration} > 8*7*24*60*60) { @@ -1014,7 +1014,7 @@ EOF if ($allow_photo_upload) { $vars{enctype} = ' enctype="multipart/form-data"'; } - $vars{map_end} = Page::display_map_end(0); + $vars{map_end} = FixMyStreet::Map::display_map_end(0); my %params = ( rss => [ _('Updates to this problem, FixMyStreet'), "/rss/$input_h{id}" ], title => $problem->{title} diff --git a/web/questionnaire.cgi b/web/questionnaire.cgi index 1da410b80..9184ad103 100755 --- a/web/questionnaire.cgi +++ b/web/questionnaire.cgi @@ -213,17 +213,17 @@ sub display_questionnaire { return $error; } my $reported_date_time = Page::prettify_epoch($q, $problem->{time}); - my ($x, $y, $x_tile, $y_tile, $px, $py) = Page::os_to_px_with_adjust($q, $problem->{easting}, $problem->{northing}, undef, undef); + my ($x, $y, $x_tile, $y_tile, $px, $py) = FixMyStreet::Map::os_to_px_with_adjust($q, $problem->{easting}, $problem->{northing}, undef, undef); - my $pins = Page::display_pin($q, $px, $py, $problem->{state} eq 'fixed'?'green':'red'); + my $pins = FixMyStreet::Map::display_pin($q, $px, $py, $problem->{state} eq 'fixed'?'green':'red'); my $problem_text = Page::display_problem_text($q, $problem); my $updates = Page::display_problem_updates($problem->{id}, $q); my %vars = ( input_h => \%input_h, - map_start => Page::display_map($q, x => $x_tile, y => $y_tile, pins => $pins, + map_start => FixMyStreet::Map::display_map($q, x => $x_tile, y => $y_tile, pins => $pins, px => $px, py => $py, pre => $problem_text, post => $updates ), - map_end => Page::display_map_end(0), + map_end => FixMyStreet::Map::display_map_end(0), heading => _('Questionnaire'), yes => _('Yes'), no => _('No'), diff --git a/web/rss.cgi b/web/rss.cgi index a2d9bcb4f..5386526df 100755 --- a/web/rss.cgi +++ b/web/rss.cgi @@ -87,8 +87,8 @@ sub rss_local_problems { print $q->redirect(-location => "$base/rss/n/$e,$n$d_str"); return ''; } elsif ($e && $n) { - $x = int(Page::os_to_tile($e)); - $y = int(Page::os_to_tile($n)); + $x = int(FixMyStreet::Map::os_to_tile($e)); + $y = int(FixMyStreet::Map::os_to_tile($n)); ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($e, $n, 'G'); } elsif ($pc) { my $error; |