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

Last change on this file since 149 was 149, checked in by Nick Burch, 14 years ago

Remaining modes

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