File Coverage

blib/lib/Politics/AU/Geo.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Politics::AU::Geo;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Politics::AU::Geo - An ORLite-based ORM Database API
8              
9             =head1 SYNOPSIS
10              
11             TO BE COMPLETED
12              
13             =head1 DESCRIPTION
14              
15             TO BE COMPLETED
16              
17             =head1 METHODS
18              
19             =cut
20              
21 2     2   20401 use 5.006;
  2         6  
  2         67  
22 2     2   11 use strict;
  2         5  
  2         66  
23 2     2   19 use warnings;
  2         2  
  2         79  
24 2     2   2071 use Storable 2.20 ();
  2         7080  
  2         55  
25 2     2   2232 use Math::Polygon 1.01 ();
  0            
  0            
26             use Params::Util 0.38 ();
27             use ORLite::Mirror 1.15 ();
28              
29             our $VERSION = '0.01';
30              
31             use Politics::AU::Geo::Electorates;
32             use Politics::AU::Geo::Polygons;
33              
34             # Set up the ORLite::Mirror integration for the dataset
35             sub import {
36             my $class = shift;
37             my $params = Params::Util::_HASH(shift) || {};
38              
39             # Pass through any params from above
40             $params->{url} ||= 'http://myrepresentatives.org/db.gz';
41             $params->{maxage} ||= 30 * 24 * 60 * 60; # One week
42              
43             # Prevent double-initialisation
44             $class->can('orlite') or
45             ORLite::Mirror->import( $params, '-DEBUG' );
46              
47             return 1;
48             }
49              
50              
51              
52              
53              
54             ######################################################################
55             # Custom Methods
56              
57             =pod
58              
59             =head2 geo2electorates
60              
61             my @electorates = Politics::AU::Geo->geo2electorates( -33.895922, 151.110022 );
62              
63             The C method takes a latitude and longitude and resolves the
64             set of electorates that the point is within.
65              
66             Returns a list of L objects, or throws an
67             exception on error.
68              
69             =cut
70              
71             sub geo2electorates {
72             my $class = shift;
73             my $latitude = shift;
74             my $longitude = shift;
75              
76             # Do a first-pass query to find the electorates that we match
77             # the general bounding boxes for.
78             my @electorates = Politics::AU::Geo::Electorates->select(
79             'WHERE bblat2 <= ? and bblat1 >= ? and bblong1 <= ? and bblong2 >= ?',
80             $latitude,
81             $latitude,
82             $longitude,
83             $longitude,
84             );
85              
86             # Count the number of electorates per house
87             my %filter = ();
88             foreach ( @electorates ) {
89             $filter{$_->house_id}++;
90             }
91              
92             # Polygon filter any electorates where we have more than one for the house
93             @electorates = grep {
94             $filter{$_->house_id} == 1
95             or
96             $_->point_in_polygon( $latitude, $longitude )
97             } @electorates;
98              
99             return @electorates;
100             }
101              
102             1;
103              
104             __END__