2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // Interface for an address geocoder. It provides support for transforming a free
23 // form address into a fully structured one.
24 // TODO: define and use an Address object instead of a key-value map.
25 abstract class Geocoder
{
26 // Geocodes @p the address, and returns the corresponding updated address.
27 // Unknown key-value pairs available in the input map are retained as-is.
28 abstract public function getGeocodedAddress(array $address);
30 // Cleans the address from its geocoded data
31 abstract public function stripGeocodingFromAddress(array $address);
33 // Updates geoloc_administrativeareas, geoloc_subadministrativeareas and
34 // geoloc_localities databases with new geocoded data and returns the
36 static public function getAreaId(array &$address, $area)
38 static $databases = array(
39 'administrativeArea' => 'geoloc_administrativeareas',
40 'subAdministrativeArea' => 'geoloc_subadministrativeareas',
41 'locality' => 'geoloc_localities',
44 if (isset($address[$area . 'Name']) && isset($databases[$area])) {
45 $res = XDB
::query("SELECT id
46 FROM " . $databases[$area] . "
48 $address[$area . 'Name']);
49 if ($res->numRows() == 0) {
50 XDB
::execute('INSERT INTO ' . $databases[$area] . ' (name, country)
52 $address[$area . 'Name'], $address['countryId']);
53 $address[$area . 'Id'] = XDB
::insertId();
55 $address[$area . 'Id'] = $res->fetchOneCell();
60 // Returns the part of the text preceeding the line with the postal code
61 // and the city name, within the limit of $limit number of lines.
62 static public function getFirstLines($text, $postalCode, $limit)
64 $textArray = explode("\n", $text);
65 for ($i = 0; $i < count($textArray); ++
$i) {
66 if ($i > $limit ||
strpos($textLine, $postalCode) !== false
) {
70 return implode("\n", array_slice($textArray, 0, $limit));
73 // Returns the number of non geocoded addresses for a user.
74 static public function countNonGeocoded($pid)
76 $res = XDB
::query("SELECT COUNT(*)
77 FROM profile_addresses
78 WHERE pid = {?} AND FIND_IN_SET('home', type) AND accuracy = 0",
80 return $res->fetchOneCell();
84 // Implementation of a Geocoder using the Google Maps API. Please refer to
85 // the following links for details:
86 // http://code.google.com/apis/maps/documentation/services.html#Geocoding
87 // http://code.google.com/intl/en/apis/maps/documentation/geocoding/
88 // http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy
90 // It requires the properties gmaps_key and gmaps_url to be defined in section
91 // Geocoder in plat/al's configuration (platal.ini & platal.conf).
92 class GMapsGeocoder
extends Geocoder
{
94 // Maximum number of Geocoding calls to the Google Maps API.
95 const MAX_GMAPS_RPC_CALLS
= 5;
97 public function getGeocodedAddress(array $address) {
98 $address = $this->prepareAddress($address);
99 $textAddress = $this->getTextToGeocode($address);
101 // Try to geocode the full address.
102 if (($geocodedData = $this->getPlacemarkForAddress($textAddress))) {
103 return $this->getUpdatedAddress($address, $geocodedData, null
);
106 // If the full geocoding failed, try to geocode only the final part of the address.
107 // We start by geocoding everything but the first line, and continue until we get
108 // a result. To respect the limit of GMaps calls, we ignore the first few lines
109 // if there are too many address lines.
110 $addressLines = explode("\n", $textAddress);
111 $linesCount = count($addressLines);
112 for ($i = max(1, $linesCount - self
::MAX_GMAPS_RPC_CALLS +
1); $i < $linesCount; ++
$i) {
113 $extraLines = implode("\n", array_slice($addressLines, 0, $i));
114 $toGeocode = implode("\n", array_slice($addressLines, $i));
115 if (($geocodedData = $this->getPlacemarkForAddress($toGeocode))) {
116 return $this->getUpdatedAddress($address, $geocodedData, $extraLines);
120 // No geocoding could be done, the initial address is returned as-is.
124 public function stripGeocodingFromAddress(array $address) {
125 unset($address['geoloc'], $address['geoloc_choice'], $address['geocodedPostalText'],
126 $address['countryId'], $address['country'], $address['administrativeAreaName'],
127 $address['subAdministrativeAreaName'], $address['localityName'],
128 $address['thoroughfareName'], $address['postalCode']);
129 $address['accuracy'] = 0;
133 // Updates the address with the geocoded information from Google Maps. Also
134 // cleans up the final informations.
135 private function getUpdatedAddress(array $address, array $geocodedData, $extraLines) {
136 $this->fillAddressWithGeocoding(&$address, $geocodedData);
138 // If the accuracy is 6, it means only the street has been gecoded
139 // but not the number, thus we need to fix it.
140 if ($address['accuracy'] == 6) {
141 $this->fixStreetNumber($address);
144 // We can now format the address.
145 $this->formatAddress($address, $extraLines);
150 // Retrieves the Placemark object (see #getPlacemarkFromJson()) for the @p
151 // address, by querying the Google Maps API. Returns the array on success,
152 // and null otherwise.
153 private function getPlacemarkForAddress($address) {
154 $url = $this->getGeocodingUrl($address);
155 $geoData = $this->getGeoJsonFromUrl($url);
157 return ($geoData ?
$this->getPlacemarkFromJson($geoData) : null
);
160 // Prepares address to be geocoded
161 private function prepareAddress($address) {
162 $address['text'] = preg_replace('/\s*\n\s*/m', "\n", trim($address['text']));
163 $address['postalText'] = $this->getPostalAddress($address['text']);
164 $address['updateTime'] = time();
165 unset($address['changed']);
169 // Builds the Google Maps geocoder url to fetch information about @p address.
170 // Returns the built url.
171 private function getGeocodingUrl($address) {
175 'key' => $globals->geocoder
->gmaps_key
,
176 'sensor' => 'false', // The queried address wasn't obtained from a GPS sensor.
177 'hl' => 'fr', // Output langage.
178 'oe' => 'utf8', // Output encoding.
179 'output' => 'json', // Output format.
180 'gl' => 'fr', // Location preferences (addresses are in France by default).
181 'q' => $address, // The queries address.
184 return $globals->geocoder
->gmaps_url
. '?' . http_build_query($parameters);
187 // Fetches JSON-encoded data from a Google Maps API url, and decode them.
188 // Returns the json array on success, and null otherwise.
189 private function getGeoJsonFromUrl($url) {
192 // Prepare a backtrace object to log errors.
194 if ($globals->debug
& DEBUG_BT
) {
195 if (!isset(PlBacktrace
::$bt['Geoloc'])) {
196 new PlBacktrace('Geoloc');
198 $bt = &PlBacktrace
::$bt['Geoloc'];
202 // Fetch the geocoding data.
203 $rawData = file_get_contents($url);
206 $bt->stop(0, "Could not retrieve geocoded address from GoogleMaps.");
211 // Decode the JSON-encoded data, and check for their validity.
212 $data = json_decode($rawData, true
);
214 $bt->stop(count($data), null
, $data);
220 // Extracts the most appropriate placemark from the JSON data fetched from
221 // Google Maps. Returns a Placemark array on success, and null otherwise. See
222 // http://code.google.com/apis/maps/documentation/services.html#Geocoding_Structured
223 // for details on the Placemark structure.
224 private function getPlacemarkFromJson(array $data) {
225 // Check for geocoding failures.
226 if (!isset($data['Status']['code']) ||
$data['Status']['code'] != 200) {
227 // TODO: handle non-200 codes in a better way, since the code might
228 // indicate a temporary error on Google's side.
232 // Check that at least one placemark was found.
233 if (count($data['Placemark']) == 0) {
237 // Extract the placemark with the best accuracy. This is not always the
238 // best result (since the same address may yield two different placemarks).
239 $result = $data['Placemark'][0];
240 foreach ($data['Placemark'] as $place) {
241 if ($place['AddressDetails']['Accuracy'] > $result['AddressDetails']['Accuracy']) {
249 // Fills the address with the geocoded data
250 private function fillAddressWithGeocoding(&$address, $geocodedData) {
251 // The geocoded address three is
252 // Country -> AdministrativeArea -> SubAdministrativeArea -> Locality -> Thoroughfare
253 // with all the possible shortcuts
254 // The address is formatted as xAL, or eXtensible Address Language, an international
255 // standard for address formatting.
256 // xAL documentation: http://www.oasis-open.org/committees/ciq/ciq.html#6
257 $address['geoloc'] = str_replace(", ", "\n", $geocodedData['address']);
258 if (isset($geocodedData['AddressDetails']['Accuracy'])) {
259 $address['accuracy'] = $geocodedData['AddressDetails']['Accuracy'];
262 $currentPosition = $geocodedData['AddressDetails'];
263 if (isset($currentPosition['Country'])) {
264 $currentPosition = $currentPosition['Country'];
265 $address['countryId'] = $currentPosition['CountryNameCode'];
266 $address['country'] = $currentPosition['CountryName'];
268 if (isset($currentPosition['AdministrativeArea'])) {
269 $currentPosition = $currentPosition['AdministrativeArea'];
270 $address['administrativeAreaName'] = $currentPosition['AdministrativeAreaName'];
272 if (isset($currentPosition['SubAdministrativeArea'])) {
273 $currentPosition = $currentPosition['SubAdministrativeArea'];
274 $address['subAdministrativeAreaName'] = $currentPosition['SubAdministrativeAreaName'];
276 if (isset($currentPosition['Locality'])) {
277 $currentPosition = $currentPosition['Locality'];
278 $address['localityName'] = $currentPosition['LocalityName'];
280 if (isset($currentPosition['Thoroughfare'])) {
281 $address['thoroughfareName'] = $currentPosition['Thoroughfare']['ThoroughfareName'];
283 if (isset($currentPosition['PostalCode'])) {
284 $address['postalCode'] = $currentPosition['PostalCode']['PostalCodeNumber'];
288 if (isset($geocodedData['Point']['coordinates'][0])) {
289 $address['latitude'] = $geocodedData['Point']['coordinates'][0];
291 if (isset($geocodedData['Point']['coordinates'][1])) {
292 $address['longitude'] = $geocodedData['Point']['coordinates'][1];
294 if (isset($geocodedData['ExtendedData']['LatLonBox']['north'])) {
295 $address['north'] = $geocodedData['ExtendedData']['LatLonBox']['north'];
297 if (isset($geocodedData['ExtendedData']['LatLonBox']['south'])) {
298 $address['south'] = $geocodedData['ExtendedData']['LatLonBox']['south'];
300 if (isset($geocodedData['ExtendedData']['LatLonBox']['east'])) {
301 $address['east'] = $geocodedData['ExtendedData']['LatLonBox']['east'];
303 if (isset($geocodedData['ExtendedData']['LatLonBox']['west'])) {
304 $address['west'] = $geocodedData['ExtendedData']['LatLonBox']['west'];
308 // Formats the text of the geocoded address using the unused data and
309 // compares it to the given address. If they are too different, the user
310 // will be asked to choose between them.
311 private function formatAddress(&$address, $extraLines) {
314 $address['geoloc'] = $extraLines . "\n" . $address['geoloc'];
316 $address['geocodedPostalText'] = $this->getPostalAddress($address['geoloc']);
317 $geoloc = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
318 array("", "\n"), $address['geoloc']));
319 $text = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
320 array("", "\n"), $address['text']));
321 $arrayGeoloc = explode("\n", $geoloc);
322 $arrayText = explode("\n", $text);
323 $countGeoloc = count($arrayGeoloc);
324 $countText = count($arrayText);
326 if (($countText > $countGeoloc) ||
($countText < $countGeoloc - 1)
327 ||
(($countText == $countGeoloc - 1)
328 && ($arrayText[$countText - 1] == strtoupper($address['country'])))) {
331 for ($i = 0; $i < $countGeoloc && $i < $countText; ++
$i) {
332 if (levenshtein($arrayText[$i], trim($arrayGeoloc[$i])) > 3) {
338 $address['text'] = $address['geoloc'];
339 $address['postalText'] = $address['geocodedPostalText'];
340 unset($address['geoloc'], $address['geocodedPostalText']);
342 $address['geoloc'] = str_replace("\n", "\r\n", $address['geoloc']);
343 $address['geocodedPostalText'] = str_replace("\n", "\r\n", $address['geocodedPostalText']);
345 $address['text'] = str_replace("\n", "\r\n", $address['text']);
346 $address['postalText'] = str_replace("\n", "\r\n", $address['postalText']);
349 // Returns the address formated for postal use.
350 // The main rules are (cf AFNOR XPZ 10-011):
351 // -everything in upper case;
352 // -if there are more then than 38 characters in a lign, split it;
353 // -if there are more then than 32 characters in the description of the "street", use abbreviations.
354 private function getPostalAddress($text) {
355 static $abbreviations = array(
359 "BOULEVARD" => "BVD",
365 $text = strtoupper($text);
366 $arrayText = explode("\n", $text);
369 foreach ($arrayText as $i => $lign) {
370 $postalText .= (($i == 0) ?
"" : "\n");
371 if (($length = strlen($lign)) > 32) {
372 $words = explode(" ", $lign);
374 foreach ($words as $word) {
375 if (isset($abbreviations[$word])) {
376 $word = $abbreviations[$word];
378 if ($count +
($wordLength = strlen($word)) <= 38) {
379 $postalText .= (($count == 0) ?
"" : " ") . $word;
380 $count +
= (($count == 0) ?
0 : 1) +
$wordLength;
382 $postalText .= "\n" . $word;
383 $count = strlen($word);
387 $postalText .= $lign;
393 // Trims the name of the real country if it contains an ISO 3166-1 non-country
394 // item. For that purpose, we compare the last but one line of the address with
395 // all non-country items of ISO 3166-1.
396 private function getTextToGeocode($address)
398 $res = XDB
::iterator('SELECT country, countryFR
399 FROM geoloc_countries
400 WHERE belongsTo IS NOT NULL');
401 $countries = array();
402 foreach ($res as $item) {
403 $countries[] = $item[0];
404 $countries[] = $item[1];
406 $textLines = explode("\n", $address['text']);
407 $countLines = count($textLines);
408 $needle = strtoupper(trim($textLines[$countLines - 2]));
409 $isPseudoCountry = false
;
410 foreach ($countries as $country) {
411 if (strtoupper($country) == $needle) {
412 $isPseudoCountry = true
;
417 if ($isPseudoCountry) {
418 return implode("\n", array_slice($textLines, 0, -1));
420 return $address['text'];
423 // Search for the lign from the given address that is the closest to the geocoded thoroughfareName
424 // and replaces the corresponding lign in the geocoded text by it.
425 static protected function fixStreetNumber(&$address)
427 if (isset($address['thoroughfareName'])) {
428 $thoroughfareName = $address['thoroughfareName'];
429 $thoroughfareToken = strtoupper(trim(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"),
430 array("", "\n"), $thoroughfareName)));
431 $geolocLines = explode("\n", $address['geoloc']);
432 $textLines = explode("\n", $address['text']);
433 $mindist = strlen($thoroughfareToken);
436 foreach ($textLines as $i => $token) {
437 if (($l = levenshtein(strtoupper(trim(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"),
438 array("", "\n"), $token))),
439 $thoroughfareToken)) < $mindist) {
444 foreach ($geolocLines as $i => $line) {
445 if (strtoupper(trim($thoroughfareName)) == strtoupper(trim($line))) {
450 $geolocLines[$pos] = $textLines[$minpos];
451 $address['geoloc'] = implode("\n", $geolocLines);
456 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: