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

Last change on this file since 512 was 512, checked in by Nick Burch, 13 years ago

Support getting gb and ie postcodes, with the optional parameter ie

  • 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
6use strict;
7use warnings;
8
9use CGI::Fast qw/:standard -debug/;
10
11# Find our private perl libraries
12use FindBin;
13use lib "$FindBin::Bin/../perllib";
14use NPEMap;
15
16sub print_err;
17
18# Set up database handler to try and make sure it's ready for the first
19# request
20# No point in handling errors here since they'll get handled by the request
21# handler
22my $dbh = setup_dbh();
23
24
25my @fields = qw(mineasting minnorthing maxeasting maxnorthing);
26my $max_distance = 50000; # in metres
27
28my %grid_max = (
29        easting  => { 'gb' => 700000, 'ie' => 400000 },
30        northing => { 'gb' => 1300000, 'ie' => 500000 },
31);
32
33my $cgi;
34# Process incoming requests
35REQUEST: while ($cgi = new CGI::Fast) {
36
37    # In case the database went away, make sure we have a connection
38    unless ($dbh = setup_dbh()) {
39        print_internal_err('Error setting up database connection');
40        next REQUEST;
41    }
42
43        # Did they request the irish grid?
44        my $grid = 'gb';
45        my $ie = 0;
46        if(defined $cgi->param('ie')) {
47                $grid = 'ie';
48                $ie = 1;
49        }
50
51    # Input validation
52    foreach my $field (@fields) {
53        unless (defined $cgi->param($field)) {
54            print_err ("Parameter '$field' missing");
55            next REQUEST;
56        }
57    }
58
59    # Are the Eastings in a valid range?
60    foreach (qw(mineasting maxeasting)) {
61        if (($cgi->param($_) > $grid_max{'easting'}->{$grid}) or
62            ($cgi->param($_) < 0)) {
63            print_err ("Parameter '$_' must be an integer between 0 and ".($grid_max{'easting'}->{$grid}/1000).",000");
64            next REQUEST;
65        }
66    }
67
68    # Are the Northings in a valid range?
69    foreach (qw(minnorthing maxnorthing)) {
70        if (($cgi->param($_) > $grid_max{'northing'}->{$grid}) or
71            ($cgi->param($_) < 0)) {
72            print_err("Parameter '$_' must be an integer between 0 and ".($grid_max{'northing'}->{$grid}/1000).",000");
73            next REQUEST;
74        }
75    }
76   
77    # Is the box too big?
78    if (($cgi->param('maxeasting') - $cgi->param('mineasting') > $max_distance) or ($cgi->param('maxnorthing') - $cgi->param('minnorthing') > $max_distance)) {
79        print_err("The requested box is too large");
80        next REQUEST;
81    }
82
83        my ($e,$n) = ("easting","northing");
84        if($ie) { ($e,$n) = ("ie_easting","ie_northing"); }
85    my $sth = $dbh->prepare("SELECT outward || ' ' || inward AS postcode, $e, $n,id,source FROM postcodes WHERE $e BETWEEN ? AND ? AND $n BETWEEN ? AND ? AND NOT deleted");
86   
87    if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) {
88        print "Content-type: text/javascript\n\n";
89
90        while(my @row = $sth->fetchrow_array) {
91                        my ($postcode,$easting,$northing,$id,$source) = @row;
92            print "addMarker('$postcode', $easting, $northing, [$id, $source]);\n";
93        }
94        print "completeMarkers();\n";
95    } else {
96        print_internal_err("Database error retrieving data");
97    }
98
99}
100
101# No more requests to serve, so tidy up
102$dbh->disconnect;
103
104# Helper routines
105sub print_err {
106    my $err = shift;
107    print header("text/plain", "400 $err");
108    print "Error: $err\n";
109}
110
111sub print_internal_err {
112    my $err = shift;
113    print header("text/plain", "500 $err");
114    print "Error: $err\n";
115}
Note: See TracBrowser for help on using the repository browser.