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