| 1 | package WWW::EFA::CoordinatesFactory; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use WWW::EFA::Coordinates; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | A Factory for creating L<WWW::EFA::Coordinates> objects. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | my $factory = WWW::EFA::CoordinatesFactory->new(); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 METHODS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head2 coordinates_from_XY | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Method to extract the coordinates from an element with x and y attributes | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $coordinates = $factory->coordinates_from_XY( $doc->findnodes( 'itdDeparture' ) ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Expects an XML::LibXML::Element of XML like this: | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | <itdDeparture stopID="8" x="11535078.00000" y="48143035.00000" mapName="WGS84" area="5" | ||||||||||||||||||||||||||||
| 28 | platform="" platformName="" stopName="Donnersbergerbrücke" nameWO="Donnersbergerbrücke" countdown="1"> | ||||||||||||||||||||||||||||
| 29 | ... | ||||||||||||||||||||||||||||
| 30 | </itdDeparture> | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Returns a L<WWW::EFA::Coordinates> object | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub coordinates_from_XY { | ||||||||||||||||||||||||||
|
Subroutine
coordinates_from_XY: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | my $elem = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if( not $elem->hasAttribute( 'y' ) or not $elem->hasAttribute( 'x' ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$elem->hasAttribute("y") and $elem->hasAttribute("x")
MC/DC0%0/2
$elem->hasAttribute("y") and $elem->hasAttribute("x")
$elem->hasAttribute("y")
$elem->hasAttribute("x")
Truth table0%0/3
A and B
A= $elem->hasAttribute("y")B= $elem->hasAttribute("x")
| |||||||||||||||||||||||||||||
| 40 | 0 | return undef; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my $coordinates = WWW::EFA::Coordinates->new( | |||||||||||||||||||||||||||
| 44 | longitude => &_clean_coordinates( $elem->getAttribute( 'x' ) ), | ||||||||||||||||||||||||||||
| 45 | latitude => &_clean_coordinates( $elem->getAttribute( 'y' ) ), | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | 0 | return $coordinates; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 coordinates_from_itdPathCoordinates | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Method to extract the coordinates from an itdPathCoordinates element | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | my $coordinates = $factory->coordinates_from_itdPathCoordinates( $doc->findnodes( 'itdPathCoordinates' ) ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Expects an XML::LibXML::Element of XML like this: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | <itdPathCoordinates> | ||||||||||||||||||||||||||||
| 61 | <coordEllipsoid>WGS84</coordEllipsoid> | ||||||||||||||||||||||||||||
| 62 | <coordType>GEO_DECIMAL</coordType> | ||||||||||||||||||||||||||||
| 63 | <itdCoordinateString decimal="." cs="," ts=" ">11529230.00000,48140331.00000</itdCoordinateString> | ||||||||||||||||||||||||||||
| 64 | </itdPathCoordinates> | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub coordinates_from_itdPathCoordinates { | ||||||||||||||||||||||||||
|
Subroutine
coordinates_from_itdPathCoordinates: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 0 | my $elem = shift; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my( $map_elem ) = $elem->findnodes( 'coordEllipsoid' ); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if( not $map_elem ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | croak( "Could not find element: coordEllipsoid" ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my( $type_elem ) = $elem->findnodes( 'coordType' ); | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if( not $type_elem ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | croak( "Could not find element: coordType" ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my( $coord_elem ) = $elem->findnodes( 'itdCoordinateString' ); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if( not $coord_elem ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | croak( "Could not find element: itdCoordinateString" ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Make sure expected type and map_name | ||||||||||||||||||||||||||||
| 87 | 0 | my $type = $type_elem->textContent; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if( not $type eq 'GEO_DECIMAL' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | croak( "Invalid coordinate type: " . $type ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | my $map_name = $map_elem->textContent; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if( not $map_name eq 'WGS84' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | croak( "Invalid map name: " . $map_name ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my $separator = $coord_elem->getAttribute( 'cs' ); | |||||||||||||||||||||||||||
| 97 | 0 | my( $longitude, $latitude ) = &_clean_coordinates( split( $separator, $coord_elem->textContent ) ); | |||||||||||||||||||||||||||
| 98 | 0 | my $coordinates = WWW::EFA::Coordinates->new( | |||||||||||||||||||||||||||
| 99 | longitude => $longitude, | ||||||||||||||||||||||||||||
| 100 | latitude => $latitude, | ||||||||||||||||||||||||||||
| 101 | map_name => $map_name, | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | 0 | return $coordinates; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Sometimes coordinates are given in the format '11529230.00000' when they really mean '11.529230' | ||||||||||||||||||||||||||||
| 108 | # This will clean them up... | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub _clean_coordinates { | ||||||||||||||||||||||||||
|
Subroutine
_clean_coordinates: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 110 | 0 | my @in = @_; | |||||||||||||||||||||||||||
| 111 | 0 | foreach( 0 .. $#in ){ | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if( $in[$_] and $in[$_] =~ m/^(\d+)(\d{6})\D?/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$in[$_] and $in[$_] =~ /^(\d+)(\d{6})\D?/
MC/DC0%0/2
$in[$_] and $in[$_] =~ /^(\d+)(\d{6})\D?/
$in[$_]
$in[$_] =~ /^(\d+)(\d{6})\D?/
Truth table0%0/3
A and B
A= $in[$_]B= $in[$_] =~ /^(\d+)(\d{6})\D?/
| |||||||||||||||||||||||||||||
| 113 | 0 | $in[$_] = ( $1 + ( $2 * 1e-6 ) ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | return @in; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Copyright 2011, Robin Clarke, Munich, Germany | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Robin Clarke <perl@robinclarke.net> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||