aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Map/Utils.pm
blob: 6952067855132237bc9fed1b35cb2e781404dcba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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 Problems;
use Cobrand;
use mySociety::Gaze;
use mySociety::GeoUtil;
use mySociety::Locale;
use mySociety::Web qw(NewURL);

sub header {
    my ($q, $type) = @_;
    return '' unless $type;

    my $cobrand = Page::get_cobrand($q);
    my $cobrand_form_elements = Cobrand::form_elements($cobrand, 'mapForm', $q);
    my $form_action = Cobrand::url($cobrand, '', $q);
    my $encoding = '';
    $encoding = ' enctype="multipart/form-data"' if $type==2;
    my $pc = $q->param('pc') || '';
    my $pc_enc = ent($pc);
    return <<EOF;
<form action="$form_action" method="post" name="mapForm" id="mapForm"$encoding>
<input type="hidden" name="submit_map" value="1">
<input type="hidden" name="pc" value="$pc_enc">
$cobrand_form_elements
EOF
}

sub map_features {
    my ($q, $easting, $northing, $interval) = @_;

    my $min_e = $easting - 500;
    my $min_n = $northing - 500;
    my $mid_e = $easting;
    my $mid_n = $northing;
    my $max_e = $easting + 500;
    my $max_n = $northing + 500;

    # list of problems aoround map can be limited, but should show all pins
    my ($around_map, $around_map_list);
    if (my $around_limit = Cobrand::on_map_list_limit(Page::get_cobrand($q))) {
        $around_map_list = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, $around_limit);
        $around_map = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, undef);
    } else {
        $around_map = $around_map_list = Problems::around_map($min_e, $max_e, $min_n, $max_n, $interval, undef);
    }

    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;
    my @ids = map { $_->{id} } @$around_map_list;
    my $nearby = Problems::nearby($dist, join(',', @ids), $limit, $mid_e, $mid_n, $interval);

    return ($around_map, $around_map_list, $nearby, $dist);
}

sub map_pins {
    my ($q, $x, $y, $sx, $sy, $interval) = @_;

    my $e = FixMyStreet::Map::tile_to_os($x);
    my $n = FixMyStreet::Map::tile_to_os($y);
    my ($around_map, $around_map_list, $nearby, $dist) = FixMyStreet::Map::map_features($q, $e, $n, $interval);

    my $pins = '';
    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);
    }

    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
}

1;