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

Last change on this file since 150 was 150, checked in by Nick Burch, 15 years ago

Include a key in the header

  • Property svn:executable set to *
File size: 4.1 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 Geo::HelmertTransform;
22use Geography::NationalGrid;
23use Geography::NationalGrid::GB;
24
25my $mode = shift;
26unless($mode) {
27        die("Use:\n\texport.pl <-gpsonly|-outer|-outer1|-npeonly>\n");
28}
29
30# What to connect with
31my $dbname = $ENV{DBNAME} || "npemap";
32my $dbhost = $ENV{DBHOST} || "";
33my $dbuser = $ENV{DBUSER} || "npemaps";
34my $dbpass = $ENV{DBPASS} || "npemaps";
35
36# Connect to the database
37my $dsn = "dbi:Pg:dbname=$dbname";
38$dsn .= ";host=$dbhost" if $dbhost;
39
40my $conn = DBI->connect($dsn, $dbuser, $dbpass,
41                                        { PrintError => 0, RaiseError => 1, AutoCommit => 1 } );
42
43# Print out our header
44print "# Generated on ".gmtime()."\n";
45print "#\n";
46print "# <outward> <inward> <easting> <northing> <WGS84 lat> <WGS84 long>\n";
47
48# Get our data fetch query
49my $query = getQuery($mode,$conn);
50
51# Process the query
52$query->execute;
53while(my ($outer,$inner,$e,$n) = $query->fetchrow_array) {
54        # Generate lat+long
55        my ($lat,$long) = makeLatLong($e,$n);
56
57        # Make e+n look nicer
58        $e =~ s/^(\d+)\.\d+$/$1/;
59        $n =~ s/^(\d+)\.\d+$/$1/;
60
61        # Make lat+long look nicer
62        $lat = sprintf("%0.6f", $lat);
63        $long = sprintf("%0.6f", $long);
64
65        print "$outer $inner $e $n $lat $long\n";
66}
67
68# Close down
69$conn->disconnect;
70
71
72############################################################################
73
74sub makeLatLong {
75        my ($e,$n) = @_;
76
77        # Turn e+n into OSGB lat+long
78        my $point = Geography::NationalGrid::GB->new( Easting=>$e, Northing=>$n );
79        my $oslat = $point->latitude;
80        my $oslong = $point->longitude;
81
82        # Turn OSGB lat+long into WGS84 lat+long
83        my $osgb_helper = Geo::HelmertTransform::datum("Airy1830");
84        my $wgs84_helper = Geo::HelmertTransform::datum('WGS84');
85
86        my ($lat,$long,$h) = 
87                Geo::HelmertTransform::convert_datum($osgb_helper, $wgs84_helper, $oslat, $oslong, 0);
88
89        return ($lat,$long);
90}
91
92############################################################################
93
94# Get a query returning
95#               outer, inner, easting, northing
96# for the given mode
97sub getQuery($$) {
98        my ($mode,$conn) = @_;
99
100        if($mode eq "-gpsonly" || $mode eq "-npeonly") {
101                my $source;
102                if($mode eq "-gpsonly") {
103                        $source = getSource("FreeThePostcode.org Importer", $conn);
104                } else {
105                        $source = getSource("NPE Postcode web submission", $conn);
106                }
107
108                my $sql = 
109                        "SELECT outward, inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
110                        "FROM postcodes   ".
111                        "WHERE source = ? ".
112                        "GROUP BY outward, inward ".
113                        "ORDER BY outward, inward ";
114
115                my $sth = $conn->prepare($sql);
116                $sth->bind_param(1, $source);
117                return $sth;
118        } elsif($mode eq "-outer") {
119                my $sql = 
120                        "SELECT outward, '###' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
121                        "FROM postcodes   ".
122                        "GROUP BY outward ".
123                        "ORDER BY outward ";
124
125                my $sth = $conn->prepare($sql);
126                return $sth;
127        } elsif($mode eq "-outer1") {
128                my $sql = 
129                        "SELECT outward, substr(inward,1,1) || '##' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
130                        "FROM postcodes   ".
131                        "GROUP BY outward, substr(inward,1,1) ".
132                        "ORDER BY outward, inward";
133
134                my $sth = $conn->prepare($sql);
135                return $sth;
136        } else {
137                die("Invalid mode '$mode'");
138        }
139}
140
141sub getSource($$) {
142        my ($source,$conn) = @_;
143
144        my $sth = $conn->prepare("SELECT id FROM sources WHERE name = ?");
145        $sth->bind_param(1, $source);
146        $sth->execute;
147
148        my $s = undef;
149        my @row = $sth->fetchrow_array;
150        if($sth->fetchrow_array) {
151                die("Should only find one source with name '$source', got more than one!");
152        }
153
154        unless(@row) {
155                die("Can't find id for source with name '$source'");
156        }
157
158        return $row[0]; 
159}
Note: See TracBrowser for help on using the repository browser.