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

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

Include 2+6 NGRs in the dump

  • 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>,<2+6 NGR>\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        # Generate 2+6 NGR
63        my $ngr = eastingNorthingToNGR($e,$n);
64
65        # Make e+n look nicer
66        $e =~ s/^(\d+)\.\d+$/$1/;
67        $n =~ s/^(\d+)\.\d+$/$1/;
68
69        # Make lat+long look nicer
70        $lat = sprintf("%0.6f", $lat);
71        $long = sprintf("%0.6f", $long);
72
73        print "$outer,$inner,$e,$n,$lat,$long,$ngr\n";
74}
75
76# Close down
77$conn->disconnect;
78
79
80############################################################################
81
82# Get a query returning
83#               outer, inner, easting, northing
84# for the given mode
85sub getQuery($$) {
86        my ($mode,$conn) = @_;
87
88        if($mode eq "-gpsonly" || $mode eq "-npeonly") {
89                my $source;
90                if($mode eq "-gpsonly") {
91                        $source = getSource("FreeThePostcode.org Importer", $conn);
92                } else {
93                        $source = getSource("NPE Postcode web submission", $conn);
94                }
95
96                my $sql = 
97                        "SELECT outward, inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
98                        "FROM postcodes   ".
99                        "WHERE source = ? AND NOT deleted ".
100                        "GROUP BY outward, inward ".
101                        "ORDER BY outward, inward ";
102
103                my $sth = $conn->prepare($sql);
104                $sth->bind_param(1, $source);
105                return $sth;
106        } elsif($mode eq "-outer") {
107                my $sql = 
108                        "SELECT outward, '###' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
109                        "FROM postcodes WHERE NOT deleted ".
110                        "GROUP BY outward ".
111                        "ORDER BY outward ";
112
113                my $sth = $conn->prepare($sql);
114                return $sth;
115        } elsif($mode eq "-outer1") {
116                my $sql = 
117                        "SELECT outward, substr(inward,1,1) || '##' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing ".
118                        "FROM postcodes WHERE NOT deleted ".
119                        "GROUP BY outward, substr(inward,1,1) ".
120                        "ORDER BY outward, inward";
121
122                my $sth = $conn->prepare($sql);
123                return $sth;
124        } else {
125                die("Invalid mode '$mode'");
126        }
127}
128
129sub getSource($$) {
130        my ($source,$conn) = @_;
131
132        my $sth = $conn->prepare("SELECT id FROM sources WHERE name = ?");
133        $sth->bind_param(1, $source);
134        $sth->execute;
135
136        my $s = undef;
137        my @row = $sth->fetchrow_array;
138        if($sth->fetchrow_array) {
139                die("Should only find one source with name '$source', got more than one!");
140        }
141
142        unless(@row) {
143                die("Can't find id for source with name '$source'");
144        }
145
146        return $row[0]; 
147}
Note: See TracBrowser for help on using the repository browser.