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\Geocoder; |
13
|
|
|
|
14
|
|
|
use Geocoder\Formatter\StringFormatter; |
15
|
|
|
use Geocoder\ProviderAggregator; |
16
|
|
|
use Http\Discovery\HttpClientDiscovery; |
17
|
|
|
use League\Geotools\Batch\Batch; |
18
|
|
|
use League\Geotools\Coordinate\Coordinate; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Command-line geocoder:reverse class |
26
|
|
|
* |
27
|
|
|
* @author Antoine Corcy <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Reverse extends Command |
30
|
2 |
|
{ |
31
|
|
|
protected function configure() |
32
|
|
|
{ |
33
|
2 |
|
$this |
34
|
2 |
|
->setName('geocoder:reverse') |
35
|
2 |
|
->setDescription('Reverse geocode street address, IPv4 or IPv6 against a provider with an adapter') |
36
|
2 |
|
->addArgument('coordinate', InputArgument::REQUIRED, 'The coordinate to reverse') |
37
|
2 |
|
->addOption('provider', null, InputOption::VALUE_REQUIRED, |
38
|
2 |
|
'If set, the name of the provider to use, Google Maps by default', 'google_maps') |
39
|
2 |
|
->addOption('cache', null, InputOption::VALUE_REQUIRED, |
40
|
2 |
|
'If set, the name of the cache to use, Redis by default') |
41
|
2 |
|
->addOption('raw', null, InputOption::VALUE_NONE, |
42
|
2 |
|
'If set, the raw format of the reverse geocoding result') |
43
|
2 |
|
->addOption('json', null, InputOption::VALUE_NONE, |
44
|
2 |
|
'If set, the json format of the reverse geocoding result') |
45
|
2 |
|
->addOption('args', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
46
|
2 |
|
'If set, the provider constructor arguments like api key, locale, region, ssl, toponym and service') |
47
|
2 |
|
->addOption('format', null, InputOption::VALUE_REQUIRED, |
48
|
2 |
|
'If set, the format of the reverse geocoding result', '%S %n, %z %L') |
49
|
2 |
|
->setHelp(<<<EOT |
50
|
2 |
|
<info>Available providers</info>: {$this->getProviders()} <comment>(some providers need arguments)</comment> |
51
|
2 |
|
<info>Available dumpers</info>: {$this->getDumpers()} |
52
|
2 |
|
|
53
|
2 |
|
<info>Use the default provider with the socket adapter and format the output</info>: |
54
|
|
|
|
55
|
|
|
%command.full_name% "48.8631507, 2.388911" <comment>--format="%L, %R, %C" --adapter=socket</comment> |
56
|
|
|
|
57
|
|
|
<info>Use the OpenStreetMaps provider with the default adapter</info>: |
58
|
|
|
|
59
|
|
|
%command.full_name% "48.8631507, 2.388911" <comment>--provider=openstreetmaps</comment> |
60
|
|
|
EOT |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
1 |
|
$coordinate = new Coordinate($input->getArgument('coordinate')); |
67
|
|
|
|
68
|
1 |
|
$geocoder = new ProviderAggregator; |
69
|
|
|
$httpClient = HttpClientDiscovery::find(); |
70
|
|
|
$provider = $this->getProvider($input->getOption('provider')); |
71
|
|
|
|
72
|
|
|
if ($input->getOption('args')) { |
73
|
|
|
$args = is_array($input->getOption('args')) |
74
|
|
|
? implode(',', $input->getOption('args')) |
75
|
|
|
: $input->getOption('args'); |
76
|
|
|
$geocoder->registerProvider(new $provider($httpClient, $args)); |
77
|
|
|
} else { |
78
|
|
|
$geocoder->registerProvider(new $provider($httpClient)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$batch = new Batch($geocoder); |
82
|
|
|
if ($input->getOption('cache')) { |
83
|
|
|
$batch->setCache( $this->getCache($input->getOption('cache'))); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$reversed = $batch->reverse($coordinate)->parallel(); |
87
|
|
|
$address = $reversed[0]->first(); |
88
|
|
|
|
89
|
|
|
if ($input->getOption('raw')) { |
90
|
|
|
$result = array(); |
91
|
|
|
$result[] = sprintf('<label>HttpClient</label>: <value>%s</value>', get_class($httpClient)); |
92
|
|
|
$result[] = sprintf('<label>Provider</label>: <value>%s</value>', $provider); |
93
|
|
|
$result[] = sprintf('<label>Cache</label>: <value>%s</value>', isset($cache) ? $cache : 'None'); |
|
|
|
|
94
|
|
|
if ($input->getOption('args')) { |
95
|
|
|
$result[] = sprintf('<label>Arguments</label>: <value>%s</value>', $args); |
96
|
|
|
} |
97
|
|
|
$result[] = '---'; |
98
|
|
|
$coordinates = $address->getCoordinates(); |
99
|
|
|
$result[] = sprintf('<label>Latitude</label>: <value>%s</value>', null !== $coordinates ? $coordinates->getLatitude() : ''); |
100
|
|
|
$result[] = sprintf('<label>Longitude</label>: <value>%s</value>', null !== $coordinates ? $coordinates->getLongitude() : ''); |
101
|
|
|
if ($address->getBounds()) { |
102
|
|
|
$bounds = $address->getBounds()->toArray(); |
103
|
|
|
$result[] = '<label>Bounds</label>'; |
104
|
|
|
$result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']); |
105
|
|
|
$result[] = sprintf(' - <label>West</label>: <value>%s</value>', $bounds['west']); |
106
|
|
|
$result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']); |
107
|
|
|
$result[] = sprintf(' - <label>East</label>: <value>%s</value>', $bounds['east']); |
108
|
|
|
} |
109
|
|
|
$result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $address->getStreetNumber()); |
110
|
|
|
$result[] = sprintf('<label>Street Name</label>: <value>%s</value>', $address->getStreetName()); |
111
|
|
|
$result[] = sprintf('<label>Zipcode</label>: <value>%s</value>', $address->getPostalCode()); |
112
|
|
|
$result[] = sprintf('<label>City</label>: <value>%s</value>', $address->getLocality()); |
113
|
|
|
$result[] = sprintf('<label>City District</label>: <value>%s</value>', $address->getSubLocality()); |
114
|
|
|
if ( NULL !== $adminLevels = $address->getAdminLevels() ) { |
115
|
|
|
$result[] = '<label>Admin Levels</label>'; |
116
|
|
|
foreach ($adminLevels as $adminLevel) { |
117
|
|
|
$result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$country = $address->getCountry(); |
121
|
|
|
$result[] = sprintf('<label>Country</label>: <value>%s</value>', null !== $country ? $country->getName() : ''); |
122
|
|
|
$result[] = sprintf('<label>Country Code</label>: <value>%s</value>', null !== $country ? $country->getCode() : ''); |
123
|
|
|
$result[] = sprintf('<label>Timezone</label>: <value>%s</value>', $address->getTimezone()); |
124
|
|
|
} elseif ($input->getOption('json')) { |
125
|
|
|
$result = sprintf('<value>%s</value>', json_encode($address->toArray())); |
126
|
|
|
} else { |
127
|
|
|
$result = (new StringFormatter)->format($address, $input->getOption('format')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$output->writeln($result); |
|
|
|
|
131
|
|
|
return 0; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.