Geo::Coordinates::Parser - A coordinate parser class.


Geo-Coordinates-Parser documentation Contained in the Geo-Coordinates-Parser distribution.

Index


Code Index:

NAME

Top

Geo::Coordinates::Parser - A coordinate parser class.

SYNOPSIS

Top

	use Geo::Coordinates::Parser;
	my $coordinateparser = Geo::Coordinates::Parser->new('.');
	my $decimaldegree = $coordinateparser->parse(q{E25°42'60"});
	$decimaldegree = $coordinateparser->parse(q{E25.12346"});




DESCRIPTION

Top

This module provides a method for parsing a coordinate string.

METHODS

Top

This module provides the following methods:

new($decimal_delimiter)

Returns a new Geo::Coordinates::Parser object. The decimal delimiter can be given as an argument. If no argument is given then period "." character is used as decimal delimiter.

Usage:

	my $coordinateparser = Geo::Coordinates::Parser->new(); # or
	my $coordinateparser = Geo::Coordinates::Parser->new('.'); # same as above, or
	my $coordinateparser = Geo::Coordinates::Parser->new(','); # , is the decimal delimiter

parse($coordinatestring)

Parses the coordinate string and returns it's decimal value. It uses Geo::Coordinates::DecimalDegrees to turn degrees, minutes and seconds into the equivalent decimal degree. The argument can be either a longitude or a latitude. It doesn't test the sanity of the data. The method simply removes all unnecessary characters and then converts the degrees, minutes and seconds to a decimal degree.

Usage:

	my $decimal = $coordinateparser->parse('E25'42'60');

decimal_delimiter($decimal_delimiter)

Returns the decimal delimiter. If an argument is given then it's sets the delimiter to the given value.

Usage:

	$coordinateparser->decimal_delimiter; # Returns the delimiter
	$coordinateparser->decimal_delimiter(','); # Sets and returns , as the delimiter
	$coordinateparser->decimal_delimiter; # Returns now , as the delimiter

REQUIRES

Top

Geo::Coordinates::DecimalDegrees

SEE ALSO

Top

Geo::Coordinates::DecimalDegrees

AUTHOR

Top

Carl Räihä, <carl.raiha at gmail.com>

COPYRIGHT AND LICENSE

Top


Geo-Coordinates-Parser documentation Contained in the Geo-Coordinates-Parser distribution.
package Geo::Coordinates::Parser;

our $VERSION = '0.01';

use strict;
use warnings;
use Geo::Coordinates::DecimalDegrees qw(dms2decimal dm2decimal);

sub new {
	my $class = shift;
	my $decimal_delimiter = shift || '.';
	my $self = {'decimal_delimiter' => $decimal_delimiter};
	bless($self, $class);

	return $self;
}

sub parse {
	my $self = shift;
	my $coordinatestring = shift;
	unless ($coordinatestring) {
		return undef;
	}
	
	# Remove all extra
	my $decimal_delimiter = $self->decimal_delimiter;
	$coordinatestring =~ s/[^0-9$decimal_delimiter]/ /g;
	$coordinatestring =~ s/^\s+//;
	$coordinatestring =~ s/\s+$//;
	
	# Split into degrees, minutes and seconds
	my ($degrees, $minutes, $seconds) = split(/\s+/, $coordinatestring);

	# Return in decimal format with the right delimiter
	if ($minutes and $seconds) {
		return dms2decimal($degrees, $minutes, $seconds);
	} elsif ($minutes and not $seconds) {
		return dm2decimal($degrees, $minutes);
	} else {
		return $degrees;
	}
}

sub decimal_delimiter {
	my $self = shift;
	my $decimal_delimiter = shift;
	if ($decimal_delimiter) {
		$self->{'decimal_delimiter'} = $decimal_delimiter;
	}
	return $self->{'decimal_delimiter'}; 
}

1;