CGI-Wiki-Plugin-Locator-Grid

 view release on metacpan or  search on metacpan

lib/CGI/Wiki/Plugin/Locator/Grid.pm  view on Meta::CPAN

  my $x_field = $locator->x_field;

An accessor, returns the name of the metadata field used to store the
x-coordinate.

=cut

sub x_field {
    my $self = shift;
    return $self->{x};
}

=item B<y_field>

  my $y_field = $locator->y_field;

An accessor, returns the name of the metadata field used to store the
y-coordinate.

=cut

sub y_field {
    my $self = shift;
    return $self->{y};
}

=item B<coordinates>

  my ($x, $y) = $locator->coordinates( node => "Jerusalem Tavern" );

Returns the x and y co-ordinates stored as metadata last time the node
was written.

=cut

sub coordinates {
    my ($self, %args) = @_;
    my $store = $self->datastore;
    # This is the slightly inefficient but neat and tidy way to do it -
    # calling on as much existing stuff as possible.
    my %node_data = $store->retrieve_node( $args{node} );
    my %metadata  = %{$node_data{metadata}};
    return ($metadata{$self->{x}}[0], $metadata{$self->{y}}[0]);
}

=item B<distance>

  # Find the straight-line distance between two nodes, in metres.
  my $distance = $locator->distance( from_node => "Jerusalem Tavern",
                                     to_node   => "Calthorpe Arms" );

  # Or in kilometres, and between a node and a point.
  my $distance = $locator->distance( from_x  => 531467,
                                     from_y  => 183246,
				     to_node => "Duke of Cambridge",
				     unit    => "kilometres" );

Defaults to metres if C<unit> is not supplied or is not recognised.
Recognised units at the moment: C<metres>, C<kilometres>.

Returns C<undef> if one of the endpoints does not exist, or does not
have both co-ordinates defined. The C<node> specification of an
endpoint overrides the x/y co-ords if both specified (but don't do
that).

B<Note:> Works to the nearest metre. Well, actually, calls C<int> and
rounds down, but if anyone cares about that they can send a patch.

=cut

sub distance {
    my ($self, %args) = @_;

    $args{unit} ||= "metres";
    my (@from, @to);

    if ( $args{from_node} ) {
        @from = $self->coordinates( node => $args{from_node} );
    } elsif ( $args{from_x} and $args{from_y} ) {
        @from = @args{ qw( from_x from_y ) };
    }

    if ( $args{to_node} ) {
        @to = $self->coordinates( node => $args{to_node} );
    } elsif ( $args{to_x} and $args{to_y} ) {
        @to = @args{ qw( to_x to_y ) };
    }

    return undef unless ( $from[0] and $from[1] and $to[0] and $to[1] );

    my $metres = int( sqrt(   ($from[0] - $to[0])**2
                            + ($from[1] - $to[1])**2 ) + 0.5 );

    if ( $args{unit} eq "metres" ) {
        return $metres;
    } else {
        return $metres/1000;
    }
}

=item B<find_within_distance>

  # Find all the things within 200 metres of a given place.
  my @others = $locator->find_within_distance( node   => "Albion",
                                               metres => 200 );

  # Or within 200 metres of a given location.
  my @things = $locator->find_within_distance( x      => 530774,
                                               y      => 182260,
                                               metres => 200 );

Units currently understood: C<metres>, C<kilometres>. If both C<node>
and C<x>/C<y> are supplied then C<node> takes precedence. Croaks if
insufficient start point data supplied.

=cut

sub find_within_distance {
    my ($self, %args) = @_;
    my $store = $self->datastore;
    my $dbh = eval { $store->dbh; }
      or croak "find_within_distance is only implemented for database stores";
    my $metres = $args{metres}



( run in 1.159 second using v1.01-cache-2.11-cpan-13bb782fe5a )