| 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 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 2 | ▶ | use Storable 2.20 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 0 | ▶ | use Math::Polygon 1.01 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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<geo2electorates> 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<Politics::AU::Geo::Electorates> 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__ | ||