source: trunk/npemap.org.uk/cgi/get-distant-postcodes.fcgi @ 198

Last change on this file since 198 was 198, checked in by Nick Burch, 14 years ago

Include a header

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006 Dominic Hargreaves
4# See accompanying file "LICENCE" for licence details
5#
6# Find postcodes that seem to be a long way from their friends
7
8use strict;
9use warnings;
10
11use DBI;
12use CGI::Fast qw/:standard -debug/;
13
14use vars qw($dbname $dbhost $dbuser $dbpass);
15
16sub print_err;
17sub setup_dbh;
18
19# Read in database config
20my $config = 'npemap.conf';
21do $config or die "Can't read $config!\n";
22
23
24# Set up database handler to try and make sure it's ready for the first
25# request
26# No point in handling errors here since they'll get handled by the request
27# handler
28my $dbh;
29setup_dbh();
30
31
32my $cgi;
33# Process incoming requests
34REQUEST: while ($cgi = new CGI::Fast) {
35
36    # In case the database went away, make sure we have a connection
37    unless (setup_dbh()) {
38        print_internal_err('Error setting up database connection');
39        next REQUEST;
40    }
41
42    # Input validation
43        my $doing_outer1 = 0;
44    if (defined $cgi->param("outer1") || defined $cgi->param("outerone")) {
45        $doing_outer1 = 1;
46    }
47
48        # How big a distance before we flag it?
49        # 10km / 10km
50        my $flag_distance = 100 * 1000;
51        if($doing_outer1) {
52                $flag_distance = 10 * 1000;
53        }
54
55        my $pcpart = "outward";
56        my $pcgroup = "outward";
57        if($doing_outer1) {
58                $pcpart = "outward || substr(inward,1,1)";
59                $pcgroup = "outward, substr(inward,1,1)";
60        }
61
62        # Tidy up from another run, if required
63        my $checksql = "SELECT tablename FROM pg_tables WHERE tablename = 'averages'";
64        my @has_table = $dbh->selectrow_array($checksql);
65        if(@has_table) {
66                $dbh->do("DROP TABLE averages;");
67        }
68
69        # Get our sql
70        # First calculate the average
71        my $prepsql .= "SELECT $pcpart AS pcpart, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing INTO TEMP TABLE averages FROM postcodes GROUP BY $pcgroup ";
72
73        # Then join to find problem ones
74        my $sql .= "SELECT outward || ' ' || inward AS postcode, $pcpart AS pcpart, easting, northing, avg_easting, avg_northing, id FROM postcodes ";
75        $sql .= "INNER JOIN averages ON ($pcpart = pcpart) ";
76        $sql .= "WHERE abs(avg_easting-easting) > $flag_distance ";
77        $sql .= "OR abs(avg_northing-northing) > $flag_distance ";
78        $sql .= "ORDER BY outward, inward ";
79
80        # Build the temp table
81    $dbh->do($prepsql);
82
83        # Do the query
84    my $sth = $dbh->prepare($sql);
85    if ($sth->execute()) {
86        print "Content-type: text/javascript\n\n";
87
88                print "// postcode, match part, easting, northing, avg easting, avg northing\n";
89        while(my @row = $sth->fetchrow_array) {
90                        # Tidy up avg
91                        my $avg_e = int($row[4]);
92                        my $avg_n = int($row[5]);
93
94                        # Print
95                        print "addProblem('$row[0]','$row[1]', $row[2], $row[3], $avg_e, $avg_n);\n";
96        }
97    } else {
98        print_internal_err("Database error retrieving data");
99    }
100
101        # Tidy up
102        @has_table = $dbh->selectrow_array($checksql);
103        if(@has_table) {
104                $dbh->do("DROP TABLE averages");
105        }
106}
107
108# No more requests to serve, so tidy up
109$dbh->disconnect;
110
111# Helper routines
112sub print_err {
113    my $err = shift;
114    print header("text/plain", "400 $err");
115    print "Error: $err\n";
116}
117
118sub print_internal_err {
119    my $err = shift;
120    print header("text/plain", "500 $err");
121    print "Error: $err\n";
122}
123
124sub setup_dbh {
125    # $dbh is global
126    my $data_source = "dbi:Pg:dbname=$dbname";
127    $data_source .= ";host=$dbhost" if $dbhost;
128    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
129}
Note: See TracBrowser for help on using the repository browser.