Completed
Push — master ( 9595c4...4e2149 )
by Antoine
06:18
created

Haversine::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
cc 1
eloc 15
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Geotools\CLI\Command\Distance;
13
14
use League\Geotools\Coordinate\Coordinate;
15
use League\Geotools\Coordinate\Ellipsoid;
16
use League\Geotools\Geotools;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Command-line distance:haversine class
24
 *
25
 * @author Antoine Corcy <[email protected]>
26
 */
27
class Haversine extends \Symfony\Component\Console\Command\Command
28
{
29 9
    protected function configure()
30
    {
31 9
        $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames();
32
33 9
        $this
34 9
            ->setName('distance:haversine')
35 9
            ->setDescription('Compute the distance between 2 coordinates using the haversine algorithm, in meters by default')
36 9
            ->addArgument('origin', InputArgument::REQUIRED, 'The origin "Lat,Long" coordinate')
37 9
            ->addArgument('destination', InputArgument::REQUIRED, 'The destination "Lat,Long" coordinate')
38 9
            ->addOption('km', null, InputOption::VALUE_NONE, 'If set, the distance will be shown in kilometers')
39 9
            ->addOption('mi', null, InputOption::VALUE_NONE, 'If set, the distance will be shown in miles')
40 9
            ->addOption('ft', null, InputOption::VALUE_NONE, 'If set, the distance will be shown in feet')
41 9
            ->addOption('ellipsoid', null, InputOption::VALUE_REQUIRED,
42 9
                'If set, the name of the ellipsoid to use', Ellipsoid::WGS84)
43 9
            ->setHelp(<<<EOT
44
<info>Available ellipsoids</info>: $availableEllipsoids
45
46
<info>Example with WGS66 ellipsoid and output in feet</info>:
47
48 9
    %command.full_name% "40° 26.7717, -79° 56.93172" "30°16′57″N 029°48′32″W" <comment>--ellipsoid=WGS66 --ft</comment>
49
EOT
50 9
            );
51 9
    }
52
53 8
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55 8
        $ellipsoid = Ellipsoid::createFromName($input->getOption('ellipsoid'));
56 6
        $from      = new Coordinate($input->getArgument('origin'), $ellipsoid);
57 5
        $to        = new Coordinate($input->getArgument('destination'), $ellipsoid);
58
59 5
        $geotools = new Geotools;
60 5
        $distance = $geotools->distance()->setFrom($from)->setTo($to);
61
62 5
        if ($input->getOption('km')) {
63 1
            $distance->in('km');
64 1
        }
65
66 5
        if ($input->getOption('mi')) {
67 2
            $distance->in('mi');
68 2
        }
69
70 5
        if ($input->getOption('ft')) {
71 1
            $distance->in('ft');
72 1
        }
73
74 5
        $output->writeln(sprintf('<value>%s</value>', $distance->haversine()));
75 5
    }
76
}
77