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