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

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

Support ie or grid=osie

  • Property svn:executable set to *
File size: 3.4 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        if(defined $cgi->param('grid') && ($cgi->param('grid') eq 'osie')) {
51                $grid = 'ie';
52                $ie = 1;
53        }
54
55    # Input validation
56    foreach my $field (@fields) {
57        unless (defined $cgi->param($field)) {
58            print_err ("Parameter '$field' missing");
59            next REQUEST;
60        }
61    }
62
63    # Are the Eastings in a valid range?
64    foreach (qw(mineasting maxeasting)) {
65        if (($cgi->param($_) > $grid_max{'easting'}->{$grid}) or
66            ($cgi->param($_) < 0)) {
67            print_err ("Parameter '$_' must be an integer between 0 and ".($grid_max{'easting'}->{$grid}/1000).",000");
68            next REQUEST;
69        }
70    }
71
72    # Are the Northings in a valid range?
73    foreach (qw(minnorthing maxnorthing)) {
74        if (($cgi->param($_) > $grid_max{'northing'}->{$grid}) or
75            ($cgi->param($_) < 0)) {
76            print_err("Parameter '$_' must be an integer between 0 and ".($grid_max{'northing'}->{$grid}/1000).",000");
77            next REQUEST;
78        }
79    }
80   
81    # Is the box too big?
82    if (($cgi->param('maxeasting') - $cgi->param('mineasting') > $max_distance) or ($cgi->param('maxnorthing') - $cgi->param('minnorthing') > $max_distance)) {
83        print_err("The requested box is too large");
84        next REQUEST;
85    }
86
87        my ($e,$n) = ("easting","northing");
88        if($ie) { ($e,$n) = ("ie_easting","ie_northing"); }
89    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");
90   
91    if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) {
92        print "Content-type: text/javascript\n\n";
93
94        while(my @row = $sth->fetchrow_array) {
95                        my ($postcode,$easting,$northing,$id,$source) = @row;
96            print "addMarker('$postcode', $easting, $northing, [$id, $source]);\n";
97        }
98        print "completeMarkers();\n";
99    } else {
100        print_internal_err("Database error retrieving data");
101    }
102
103}
104
105# No more requests to serve, so tidy up
106$dbh->disconnect;
107
108# Helper routines
109sub print_err {
110    my $err = shift;
111    print header("text/plain", "400 $err");
112    print "Error: $err\n";
113}
114
115sub print_internal_err {
116    my $err = shift;
117    print header("text/plain", "500 $err");
118    print "Error: $err\n";
119}
Note: See TracBrowser for help on using the repository browser.