forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIGNOpenLSProvider.php
More file actions
140 lines (115 loc) · 4.83 KB
/
Copy pathIGNOpenLSProvider.php
File metadata and controls
140 lines (115 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider;
use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Exception\UnsupportedException;
use Geocoder\Exception\InvalidCredentialsException;
use Geocoder\Exception\NoResultException;
/**
* @author Antoine Corcy <contact@sbin.dk>
*/
class IGNOpenLSProvider extends AbstractProvider implements ProviderInterface
{
/**
* @var string
*/
const ENDPOINT_URL = 'http://gpp3-wxs.ign.fr/%s/geoportail/ols?output=xml&xls=';
/**
* @var string
*/
const ENDPOINT_QUERY = '<xls:XLS xmlns:xls="http://www.opengis.net/xls" version="1.2"><xls:RequestHeader/><xls:Request methodName="LocationUtilityService" version="1.2" maximumResponses="%d"><xls:GeocodeRequest returnFreeForm="false"><xls:Address countryCode="StreetAddress"><xls:freeFormAddress>%s</xls:freeFormAddress></xls:Address></xls:GeocodeRequest></xls:Request></xls:XLS>';
/**
* @var string
*/
private $apiKey = null;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $apiKey An API key.
*/
public function __construct(HttpAdapterInterface $adapter, $apiKey)
{
parent::__construct($adapter, null);
$this->apiKey = $apiKey;
}
/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
{
if (null === $this->apiKey) {
throw new InvalidCredentialsException('No API Key provided');
}
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedException('The IGNOpenLSProvider does not support IP addresses.');
}
$query = sprintf('%s%s',
sprintf(self::ENDPOINT_URL, $this->apiKey),
urlencode(sprintf(self::ENDPOINT_QUERY, $this->getMaxResults(), $address))
);
$content = $this->getAdapter()->getContent($query);
$doc = new \DOMDocument;
if (!@$doc->loadXML($content)) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$xml = new \SimpleXMLElement($content);
if (isset($xml->ErrorList->Error) || null === $xml->Response->GeocodeResponse) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$numberOfGeocodedAddresses = (int) $xml->Response
->GeocodeResponse
->GeocodeResponseList['numberOfGeocodedAddresses'];
if (isset($numberOfGeocodedAddresses) && 0 === $numberOfGeocodedAddresses) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$results = array();
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
for ($i = 0; $i < $numberOfGeocodedAddresses; $i++) {
$positions = $xml->xpath('//gml:pos');
$positions = explode(' ', $positions[$i]);
$zipcode = $xml->xpath('//xls:PostalCode');
$city = $xml->xpath('//xls:Place[@type="Municipality"]');
$bbox = $xml->xpath('//xls:Place[@type="Bbox"]');
$streetNumber = $xml->xpath('//xls:Building');
$cityDistrict = $xml->xpath('//xls:Street');
$bounds = null;
if (isset($bbox[$i])) {
list($bounds['west'], $bounds['south'], $bounds['east'], $bounds['north']) = explode(';', $bbox[$i]);
}
$results[] = array_merge($this->getDefaults(), array(
'latitude' => isset($positions[0]) ? (float) $positions[0] : null,
'longitude' => isset($positions[1]) ? (float) $positions[1] : null,
'bounds' => $bounds,
'streetNumber' => isset($streetNumber[$i]) ? (string) $streetNumber[$i]->attributes() : null,
'streetName' => isset($cityDistrict[$i]) ? (string) $cityDistrict[$i] : null,
'city' => isset($city[$i]) ? (string) $city[$i] : null,
'zipcode' => isset($zipcode[$i]) ? (string) $zipcode[$i] : null,
'cityDistrict' => isset($cityDistrict[$i]) ? (string) $cityDistrict[$i] : null,
'country' => 'France',
'countryCode' => 'FR',
'timezone' => 'Europe/Paris'
));
}
return $results;
}
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
{
throw new UnsupportedException('The IGNOpenLSProvider is not able to do reverse geocoding.');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'ign_openls';
}
}