Changeset 365


Ignore:
Timestamp:
Nov 12, 2006, 5:09:10 PM (13 years ago)
Author:
Nick Burch
Message:

Finish off geocoder

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/npemap.org.uk/cgi/geocoder.fcgi

    r364 r365  
    6565        my $inward;
    6666        if(defined $cgi->param("postcode")) {
    67                 $postcode = $cgi->param("postcode");
    68                 ($outward,$inward) = ($postcode =~ /^(\w+\d+\w?)\s*(\d\w\w)$/);
     67                $postcode = uc($cgi->param("postcode"));
     68                ($outward,$inward) = ($postcode =~ /^([A-Z]+\d+[A-Z]?)\s*(\d[A-Z][A-Z])$/);
    6969                unless($outward && $inward) {
    7070                        print_err ("Invalid postcode '$postcode' supplied");
     
    9090        }
    9191        unless($matched) {
    92                 my ($mpart) = ($outward =~ /^(\w+)\d/);
     92                my ($mpart) = ($outward =~ /^([A-Z]+)\d/);
    9393                my $mlen = length($mpart);
    9494
    9595                ($easting,$northing,$matched,$pc) =
    96                                 execute($area_matcher,$mlen,$mlen,$mpart,$mlen);
     96                                execute($area_matcher,$mpart,$mlen,$mlen,$mpart,$mlen);
     97
     98                unless($matched) {
     99                        print_err "Postcode area '$mpart' not found, postcode probably invalid";
     100                        next REQUEST;
     101                }
    97102        }
    98103
    99         print "$easting $northing $matched $pc\n";
    100 
     104        # Render
     105        if($output eq "xml") {
     106                print header("text/xml");
     107                print "<?xml version='1.0'?>\n";
     108                print "<geocoder>\n";
     109                print "  <request>\n";
     110                print "     <postcode>$postcode</postcode>\n";
     111                print "     <outward>$outward</outward>\n";
     112                print "     <inward>$inward</inward>\n";
     113                print "  </request>\n\n";
     114                print "  <easting>".int($easting)."</easting>\n";
     115                print "  <northing>".int($northing)."</northing>\n";
     116                print "  <postcode>$pc</postcode>\n";
     117                print "</geocoder>\n";
     118        } else {
     119                print header("text/plain");
     120                print "# Easting,Northing,Matched Postcode\n";
     121                print int($easting).",".int($northing).",'$pc'\n";
     122        }
    101123}
    102124
     
    137159        my $outer_sql = $base_sql_a . " outward || ' ###'". $base_sql_b .
    138160                   "AND outward = ? GROUP BY outward";
    139         my $area_sql = $base_sql_a . " substr(outward,1,?) " . $base_sql_b .
     161        my $area_sql = $base_sql_a . " substr(?,1,?) " . $base_sql_b .
    140162                       "AND substr(outward,1,?) = ? ".
    141163                       "GROUP BY substr(outward,1,?)";
    142 print $area_sql."\n";
    143164
    144165        my $full_matcher = $dbh->prepare($full_sql);
Note: See TracChangeset for help on using the changeset viewer.