Politics-AU-Geo

 view release on metacpan or  search on metacpan

lib/Politics/AU/Geo.pm  view on Meta::CPAN

package Politics::AU::Geo;

=pod

=head1 NAME

Politics::AU::Geo - An ORLite-based ORM Database API

=head1 SYNOPSIS

  TO BE COMPLETED

=head1 DESCRIPTION

TO BE COMPLETED

=head1 METHODS

=cut

use 5.006;
use strict;
use warnings;
use Storable       2.20 ();
use Math::Polygon  1.01 ();
use Params::Util   0.38 ();
use ORLite::Mirror 1.15 ();

our $VERSION = '0.01';

use Politics::AU::Geo::Electorates;
use Politics::AU::Geo::Polygons;

# Set up the ORLite::Mirror integration for the dataset
sub import {
	my $class  = shift;
	my $params = Params::Util::_HASH(shift) || {};

	# Pass through any params from above
	$params->{url}    ||= 'http://myrepresentatives.org/db.gz';
	$params->{maxage} ||= 30 * 24 * 60 * 60; # One week

	# Prevent double-initialisation
	$class->can('orlite') or
	ORLite::Mirror->import( $params, '-DEBUG' );

	return 1;
}





######################################################################
# Custom Methods

=pod

=head2 geo2electorates

  my @electorates = Politics::AU::Geo->geo2electorates( -33.895922, 151.110022 );

The C<geo2electorates> method takes a latitude and longitude and resolves the
set of electorates that the point is within.

Returns a list of L<Politics::AU::Geo::Electorates> objects, or throws an
exception on error.

=cut

sub geo2electorates {
	my $class     = shift;
	my $latitude  = shift;
	my $longitude = shift;

	# Do a first-pass query to find the electorates that we match
	# the general bounding boxes for.
	my @electorates = Politics::AU::Geo::Electorates->select(
		'WHERE bblat2 <= ? and bblat1 >= ? and bblong1 <= ? and bblong2 >= ?',
		$latitude,
		$latitude,
		$longitude,
		$longitude,
	);



( run in 2.031 seconds using v1.01-cache-2.11-cpan-98e64b0badf )