source: trunk/npemap.org.uk/scripts/exporter/export.pl @ 461

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

Pull out e+n -> lat+long into a npemap perl module, and use from the geocoder

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1#!/usr/bin/perl
2#                                       Postcode Data Exporter
3#                                       ----------------------
4#
5# Exports various parts of the data, in various formats
6# Normally these are of the form:
7#   <postcode|part postcode>    <easting>       <northing>      <lat>   <long>
8#
9# Supported outputs are:
10#   -gpsonly
11#       Only export GPS sourced postcodes, averaged if several for a postcode
12#   -outer
13#       Only export the outer parts of the postcodes, averaged over all
14#   -outer1
15#       Only export the outer part + 1st inner digit, averaged over all
16#   -npeonly
17#       Only export NPE sourced postcodes, averaged if several for a postcode
18
19use strict;
20use DBI;
21use Geography::NationalGrid;
22use Geography::NationalGrid::GB;
23
24# Find our private perl libraries
25use FindBin;
26use lib "$FindBin::Bin/../../perllib";
27use NPEMap::GeoConverter;
28
29my $mode = shift;
30unless($mode) {
31        die("Use:\n\texport.pl <-gpsonly|-outer|-outer1|-npeonly>\n");
32}
33
34# What to connect with
35my $dbname = $ENV{DBNAME} || "npemap";
36my $dbhost = $ENV{DBHOST} || "";
37my $dbuser = $ENV{DBUSER} || "npemap";
38my $dbpass = $ENV{DBPASS} || "";
39
40# Connect to the database
41my $dsn = "dbi:Pg:dbname=$dbname";
42$dsn .= ";host=$dbhost" if $dbhost;
43
44my $conn = DBI->connect($dsn, $dbuser, $dbpass,
45                                        { PrintError => 0, RaiseError => 1, AutoCommit => 1 } );
46
47# Print out our header
48print "# Generated on ".gmtime()."\n";
49print "#\n";
50print "# This information is public domain, and comes from http://www.npemap.org.uk/\n";
51print "#\n";
52print "# <outward>,<inward>,<easting>,<northing>,<WGS84 lat>,<WGS84 long>\n";
53
54# Get our data fetch query
55my $query = getQuery($mode,$conn);
56
57# Process the query
58$query->execute;
59while(my ($outer,$inner,$e,$n) = $query->fetchrow_array) {
60        # Generate lat+long
61        my ($lat,$long) = eastingNorthingToLatLong($e,$n);
62
63        # Make e+n look nicer
64        $e =~ s/^(\d+)\.\d+$/$1/;
65        $n =~ s/^(\d+)\.\d+$/$1/;
66
67        # Make lat+long look nicer
68        $lat = sprintf("%0.6f", $lat);
69        $long = sprintf("%0.6f", $long);
70
71        print "$outer,$inner,$e,$n,$lat,$long\n";
72}
73
74# Close down
75$conn->disconnect;
76
77
78############################################################################
79
80# Get a query returning
81#               outer, inner, easting, northing
82# for the given mode
83sub getQuery($$) {
84        my ($mode,$conn) = @_;
85
86        if($mode eq "-gpsonly" || $mode eq "-npeonly") {
87                my $source;
88                if($mode eq "-gpsonly") {
89                        $source = getSource("FreeThePostcode.org Importer", $conn);
90                } else {
91                        $source = getSource("NPE Postcode web submission", $conn);
92                }
93
94                my $sql = 
95                        "SELECT outward, inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
96                        "FROM postcodes   ".
97                        "WHERE source = ? AND NOT deleted ".
98                        "GROUP BY outward, inward ".
99                        "ORDER BY outward, inward ";
100
101                my $sth = $conn->prepare($sql);
102                $sth->bind_param(1, $source);
103                return $sth;
104        } elsif($mode eq "-outer") {
105                my $sql = 
106                        "SELECT outward, '###' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
107                        "FROM postcodes WHERE NOT deleted ".
108                        "GROUP BY outward ".
109                        "ORDER BY outward ";
110
111                my $sth = $conn->prepare($sql);
112                return $sth;
113        } elsif($mode eq "-outer1") {
114                my $sql = 
115                        "SELECT outward, substr(inward,1,1) || '##' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
116                        "FROM postcodes WHERE NOT deleted ".
117                        "GROUP BY outward, substr(inward,1,1) ".
118                        "ORDER BY outward, inward";
119
120                my $sth = $conn->prepare($sql);
121                return $sth;
122        } else {
123                die("Invalid mode '$mode'");
124        }
125}
126
127sub getSource($$) {
128        my ($source,$conn) = @_;
129
130        my $sth = $conn->prepare("SELECT id FROM sources WHERE name = ?");
131        $sth->bind_param(1, $source);
132        $sth->execute;
133
134        my $s = undef;
135        my @row = $sth->fetchrow_array;
136        if($sth->fetchrow_array) {
137                die("Should only find one source with name '$source', got more than one!");
138        }
139
140        unless(@row) {
141                die("Can't find id for source with name '$source'");
142        }
143
144        return $row[0]; 
145}
Note: See TracBrowser for help on using the repository browser.