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

Last change on this file since 666 was 666, checked in by Nick Burch, 12 years ago

Update to include source too

  • Property svn:executable set to *
File size: 6.3 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006-2007 Nick Burch
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21#
22#                                       Postcode Data Exporter
23#                                       ----------------------
24#
25# Exports various parts of the data, in various formats
26# Normally these are of the form:
27#   <postcode|part postcode>    <easting>       <northing>      <lat>   <long>
28#
29# Supported outputs are:
30#   -gpsonly
31#       Only export GPS sourced postcodes, averaged if several for a postcode
32#   -outer
33#       Only export the outer parts of the postcodes, averaged over all
34#   -outer1
35#       Only export the outer part + 1st inner digit, averaged over all
36#       (Excludes entries where we only have an outward, but no inward)
37#   -npeonly
38#       Only export NPE sourced postcodes, averaged if several for a postcode
39#   -all
40#       Outputs all postcodes, averaged if several for a postcode, along
41#       with their source
42
43use strict;
44use DBI;
45use Geography::NationalGrid;
46use Geography::NationalGrid::GB;
47use Geography::NationalGrid::IE;
48
49# Find our private perl libraries
50use FindBin;
51use lib "$FindBin::Bin/../../perllib";
52use NPEMap::GeoConverter;
53
54my $mode = shift;
55unless($mode) {
56        die("Use:\n\texport.pl <-gpsonly|-outer|-outer1|-npeonly|-all>\n");
57}
58
59# What to connect with
60my $dbname = $ENV{DBNAME} || "npemap";
61my $dbhost = $ENV{DBHOST} || "";
62my $dbuser = $ENV{DBUSER} || "npemap";
63my $dbpass = $ENV{DBPASS} || "";
64
65# Connect to the database
66my $dsn = "dbi:Pg:dbname=$dbname";
67$dsn .= ";host=$dbhost" if $dbhost;
68
69my $conn = DBI->connect($dsn, $dbuser, $dbpass,
70                                        { PrintError => 0, RaiseError => 1, AutoCommit => 1 } );
71
72# Print out our header
73print "# Generated on ".gmtime()."\n";
74print "#\n";
75print "# This information is public domain, and comes from http://www.npemap.org.uk/\n";
76print "#\n";
77print "# <outward>,<inward>,<easting>,<northing>,<WGS84 lat>,<WGS84 long>,<2+6 NGR>,<grid>,<sources>\n";
78
79# Get our data fetch query
80my $query = getQuery($mode,$conn);
81
82# Process the query
83$query->execute;
84while(my ($outer,$inner,$e,$n,$ie_e,$ie_n,$source) = $query->fetchrow_array) {
85        # What grid are we on?
86        my $grid = "osgb";
87        if(!$e && $ie_e) {
88                $grid = "osie";
89                $e = $ie_e;
90                $n = $ie_n;
91        }
92       
93        # Generate lat+long
94        my ($lat,$long) = eastingNorthingToLatLong($e,$n,$grid);
95        # Generate 2+6 NGR
96        my $ngr = eastingNorthingToNGR($e,$n,$grid);
97
98        # Make e+n look nicer
99        $e =~ s/^(\d+)\.\d+$/$1/;
100        $n =~ s/^(\-?\d+)\.\d+$/$1/;
101
102        # Make lat+long look nicer
103        $lat = sprintf("%0.6f", $lat);
104        $long = sprintf("%0.6f", $long);
105
106        # Make Source Nicer
107        $source =~ s/^\{//;
108        $source =~ s/\}$//;
109        $source =~ s/\"//g;
110        $source =~ s/,/;/g;
111
112        print "$outer,$inner,$e,$n,$lat,$long,$ngr,$grid,$source\n";
113}
114
115# Close down
116$conn->disconnect;
117
118
119############################################################################
120
121# Get a query returning
122#               outer, inner, easting, northing
123# for the given mode
124sub getQuery($$) {
125        my ($mode,$conn) = @_;
126
127        if($mode eq "-gpsonly" || $mode eq "-npeonly" || $mode eq "-postboxonly" || $mode eq "-all") {
128                my $source;
129                if($mode eq "-gpsonly") {
130                        $source = getSource("FreeThePostcode.org Importer", $conn);
131                } elsif($mode eq "-postboxonly") {
132                        $source = getSource("Dracos.co.uk Postbox Importer", $conn);
133                } else {
134                        $source = getSource("NPE Postcode web submission", $conn);
135                }
136
137                my $source_sql = "";
138                if($source) {
139                        $source_sql = " AND source = ? ";
140                }
141
142                my $sql = 
143                        "SELECT outward, inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing, AVG(ie_easting) AS avg_easting_ie, AVG(ie_northing) AS avg_northing_ie, to_array(DISTINCT sources.name) AS sources ".
144                        "FROM postcodes   ".
145                        "INNER JOIN sources ON (postcodes.source = sources.id) ".
146                        "WHERE NOT deleted ".$source_sql.
147                        "GROUP BY outward, inward ".
148                        "ORDER BY outward, inward ";
149
150                my $sth = $conn->prepare($sql);
151                if($source) {
152                        $sth->bind_param(1, $source);
153                }
154                return $sth;
155        } elsif($mode eq "-outer") {
156                my $sql = 
157                        "SELECT outward, '###' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing, AVG(ie_easting) AS avg_easting_ie, AVG(ie_northing) AS avg_northing_ie, to_array(DISTINCT sources.name) AS sources ".
158                        "FROM postcodes ".
159                        "INNER JOIN sources ON (postcodes.source = sources.id) ".
160                        "WHERE NOT deleted ".
161                        "GROUP BY outward ".
162                        "ORDER BY outward ";
163
164                my $sth = $conn->prepare($sql);
165                return $sth;
166        } elsif($mode eq "-outer1") {
167                # Not - will exclude data where we only have an outward, and no inward
168                my $sql = 
169                        "SELECT outward, substr(inward,1,1) || '##' AS inward, AVG(easting) AS avg_easting, AVG(northing) AS avg_northing, AVG(ie_easting) AS avg_easting_ie, AVG(ie_northing) AS avg_northing_ie, to_array(DISTINCT sources.name) AS sources ".
170                        "FROM postcodes ".
171                        "INNER JOIN sources ON (postcodes.source = sources.id) ".
172                        "WHERE NOT deleted AND length(inward) > 0 ".
173                        "GROUP BY outward, substr(inward,1,1) ".
174                        "ORDER BY outward, inward";
175
176                my $sth = $conn->prepare($sql);
177                return $sth;
178        } else {
179                die("Invalid mode '$mode'");
180        }
181}
182
183sub getSource($$) {
184        my ($source,$conn) = @_;
185
186        my $sth = $conn->prepare("SELECT id FROM sources WHERE name = ?");
187        $sth->bind_param(1, $source);
188        $sth->execute;
189
190        my $s = undef;
191        my @row = $sth->fetchrow_array;
192        if($sth->fetchrow_array) {
193                die("Should only find one source with name '$source', got more than one!");
194        }
195
196        unless(@row) {
197                die("Can't find id for source with name '$source'");
198        }
199
200        return $row[0]; 
201}
Note: See TracBrowser for help on using the repository browser.