Merge remote branch 'origin/platal-1.0.0'
[platal.git] / include / geocoding.inc.php
CommitLineData
4c906759
SJ
1<?php
2/***************************************************************************
d4c08d89 3 * Copyright (C) 2003-2010 Polytechnique.org *
4c906759
SJ
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
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.
25abstract 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);
29
73f6c165
SJ
30 // Cleans the address from its geocoded data
31 abstract public function stripGeocodingFromAddress(array $address);
32
4c906759
SJ
33 // Updates geoloc_administrativeareas, geoloc_subadministrativeareas and
34 // geoloc_localities databases with new geocoded data and returns the
35 // corresponding id.
36 static public function getAreaId(array &$address, $area)
37 {
38 static $databases = array(
39 'administrativeArea' => 'geoloc_administrativeareas',
40 'subAdministrativeArea' => 'geoloc_subadministrativeareas',
41 'locality' => 'geoloc_localities',
42 );
43
44 if (isset($address[$area . 'Name']) && isset($databases[$area])) {
45 $res = XDB::query("SELECT id
46 FROM " . $databases[$area] . "
47 WHERE name = {?}",
48 $address[$area . 'Name']);
49 if ($res->numRows() == 0) {
505fb9bb
SJ
50 XDB::execute('INSERT INTO ' . $databases[$area] . ' (name, country)
51 VALUES ({?}, {?})',
52 $address[$area . 'Name'], $address['countryId']);
53 $address[$area . 'Id'] = XDB::insertId();
4c906759
SJ
54 } else {
55 $address[$area . 'Id'] = $res->fetchOneCell();
56 }
57 }
58 }
8f60459a
SJ
59
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)
63 {
64 $textArray = explode("\n", $text);
65 for ($i = 0; $i < count($textArray); ++$i) {
66 if ($i > $limit || strpos($textLine, $postalCode) !== false) {
67 $limit = $i; break;
68 }
69 }
70 return implode("\n", array_slice($textArray, 0, $limit));
71 }
2aa2c77a
SJ
72
73 // Returns the number of non geocoded addresses for a user.
74 static public function countNonGeocoded($pid)
75 {
76 $res = XDB::query("SELECT COUNT(*)
77 FROM profile_addresses
78 WHERE pid = {?} AND FIND_IN_SET('home', type) AND accuracy = 0",
79 $pid);
80 return $res->fetchOneCell();
81 }
4c906759
SJ
82}
83
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
89//
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).
92class GMapsGeocoder extends Geocoder {
93
94 // Maximum number of Geocoding calls to the Google Maps API.
95 const MAX_GMAPS_RPC_CALLS = 5;
96
97 public function getGeocodedAddress(array $address) {
98 $address = $this->prepareAddress($address);
00e5200b 99 $textAddress = $this->getTextToGeocode($address);
4c906759
SJ
100
101 // Try to geocode the full address.
102 if (($geocodedData = $this->getPlacemarkForAddress($textAddress))) {
103 return $this->getUpdatedAddress($address, $geocodedData, null);
104 }
105
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);
117 }
118 }
119
120 // No geocoding could be done, the initial address is returned as-is.
121 return $address;
122 }
123
73f6c165 124 public function stripGeocodingFromAddress(array $address) {
5a10ab14
SJ
125 unset($address['geoloc'], $address['geoloc_choice'], $address['geocodedPostalText'],
126 $address['countryId'], $address['country'], $address['administrativeAreaName'],
73f6c165
SJ
127 $address['subAdministrativeAreaName'], $address['localityName'],
128 $address['thoroughfareName'], $address['postalCode']);
129 $address['accuracy'] = 0;
130 return $address;
131 }
00e5200b 132
4c906759
SJ
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);
137
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);
142 }
143
144 // We can now format the address.
145 $this->formatAddress($address, $extraLines);
146
4c906759
SJ
147 return $address;
148 }
149
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);
156
157 return ($geoData ? $this->getPlacemarkFromJson($geoData) : null);
158 }
159
160 // Prepares address to be geocoded
161 private function prepareAddress($address) {
162 $address['text'] = preg_replace('/\s*\n\s*/m', "\n", trim($address['text']));
5a10ab14 163 $address['postalText'] = $this->getPostalAddress($address['text']);
4c906759
SJ
164 $address['updateTime'] = time();
165 unset($address['changed']);
166 return $address;
167 }
168
169 // Builds the Google Maps geocoder url to fetch information about @p address.
170 // Returns the built url.
171 private function getGeocodingUrl($address) {
172 global $globals;
173
174 $parameters = array(
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.
182 );
183
184 return $globals->geocoder->gmaps_url . '?' . http_build_query($parameters);
185 }
186
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) {
190 global $globals;
191
192 // Prepare a backtrace object to log errors.
193 $bt = null;
194 if ($globals->debug & DEBUG_BT) {
195 if (!isset(PlBacktrace::$bt['Geoloc'])) {
196 new PlBacktrace('Geoloc');
197 }
198 $bt = &PlBacktrace::$bt['Geoloc'];
199 $bt->start($url);
200 }
201
202 // Fetch the geocoding data.
203 $rawData = file_get_contents($url);
204 if (!$rawData) {
205 if ($bt) {
206 $bt->stop(0, "Could not retrieve geocoded address from GoogleMaps.");
207 }
208 return null;
209 }
210
211 // Decode the JSON-encoded data, and check for their validity.
212 $data = json_decode($rawData, true);
213 if ($bt) {
214 $bt->stop(count($data), null, $data);
215 }
216
217 return $data;
218 }
219
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.
229 return null;
230 }
231
232 // Check that at least one placemark was found.
233 if (count($data['Placemark']) == 0) {
234 return null;
235 }
236
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']) {
242 $result = $place;
243 }
244 }
245
246 return $result;
247 }
248
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'];
260 }
261
262 $currentPosition = $geocodedData['AddressDetails'];
263 if (isset($currentPosition['Country'])) {
264 $currentPosition = $currentPosition['Country'];
265 $address['countryId'] = $currentPosition['CountryNameCode'];
266 $address['country'] = $currentPosition['CountryName'];
267 }
268 if (isset($currentPosition['AdministrativeArea'])) {
269 $currentPosition = $currentPosition['AdministrativeArea'];
270 $address['administrativeAreaName'] = $currentPosition['AdministrativeAreaName'];
271 }
272 if (isset($currentPosition['SubAdministrativeArea'])) {
273 $currentPosition = $currentPosition['SubAdministrativeArea'];
274 $address['subAdministrativeAreaName'] = $currentPosition['SubAdministrativeAreaName'];
275 }
276 if (isset($currentPosition['Locality'])) {
277 $currentPosition = $currentPosition['Locality'];
278 $address['localityName'] = $currentPosition['LocalityName'];
279 }
280 if (isset($currentPosition['Thoroughfare'])) {
281 $address['thoroughfareName'] = $currentPosition['Thoroughfare']['ThoroughfareName'];
282 }
283 if (isset($currentPosition['PostalCode'])) {
284 $address['postalCode'] = $currentPosition['PostalCode']['PostalCodeNumber'];
285 }
286
287 // Gets coordinates.
288 if (isset($geocodedData['Point']['coordinates'][0])) {
289 $address['latitude'] = $geocodedData['Point']['coordinates'][0];
290 }
291 if (isset($geocodedData['Point']['coordinates'][1])) {
292 $address['longitude'] = $geocodedData['Point']['coordinates'][1];
293 }
294 if (isset($geocodedData['ExtendedData']['LatLonBox']['north'])) {
295 $address['north'] = $geocodedData['ExtendedData']['LatLonBox']['north'];
296 }
297 if (isset($geocodedData['ExtendedData']['LatLonBox']['south'])) {
298 $address['south'] = $geocodedData['ExtendedData']['LatLonBox']['south'];
299 }
300 if (isset($geocodedData['ExtendedData']['LatLonBox']['east'])) {
301 $address['east'] = $geocodedData['ExtendedData']['LatLonBox']['east'];
302 }
303 if (isset($geocodedData['ExtendedData']['LatLonBox']['west'])) {
304 $address['west'] = $geocodedData['ExtendedData']['LatLonBox']['west'];
305 }
306 }
307
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) {
312 $same = true;
313 if ($extraLines) {
314 $address['geoloc'] = $extraLines . "\n" . $address['geoloc'];
315 }
5a10ab14 316 $address['geocodedPostalText'] = $this->getPostalAddress($address['geoloc']);
4c906759
SJ
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);
325
326 if (($countText > $countGeoloc) || ($countText < $countGeoloc - 1)
327 || (($countText == $countGeoloc - 1)
328 && ($arrayText[$countText - 1] == strtoupper($address['country'])))) {
329 $same = false;
330 } else {
331 for ($i = 0; $i < $countGeoloc && $i < $countText; ++$i) {
332 if (levenshtein($arrayText[$i], trim($arrayGeoloc[$i])) > 3) {
333 $same = false;
334 }
335 }
336 }
337 if ($same) {
338 $address['text'] = $address['geoloc'];
5a10ab14
SJ
339 $address['postalText'] = $address['geocodedPostalText'];
340 unset($address['geoloc'], $address['geocodedPostalText']);
eecbf7f5
SJ
341 } else {
342 $address['geoloc'] = str_replace("\n", "\r\n", $address['geoloc']);
343 $address['geocodedPostalText'] = str_replace("\n", "\r\n", $address['geocodedPostalText']);
4c906759 344 }
eecbf7f5
SJ
345 $address['text'] = str_replace("\n", "\r\n", $address['text']);
346 $address['postalText'] = str_replace("\n", "\r\n", $address['postalText']);
4c906759
SJ
347 }
348
5a10ab14
SJ
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(
356 "IMPASSE" => "IMP",
357 "RUE" => "R",
358 "AVENUE" => "AV",
359 "BOULEVARD" => "BVD",
360 "ROUTE" => "R",
361 "STREET" => "ST",
362 "ROAD" => "RD",
363 );
364
365 $text = strtoupper($text);
366 $arrayText = explode("\n", $text);
367 $postalText = "";
368
369 foreach ($arrayText as $i => $lign) {
370 $postalText .= (($i == 0) ? "" : "\n");
371 if (($length = strlen($lign)) > 32) {
372 $words = explode(" ", $lign);
373 $count = 0;
374 foreach ($words as $word) {
375 if (isset($abbreviations[$word])) {
376 $word = $abbreviations[$word];
377 }
378 if ($count + ($wordLength = strlen($word)) <= 38) {
379 $postalText .= (($count == 0) ? "" : " ") . $word;
380 $count += (($count == 0) ? 0 : 1) + $wordLength;
381 } else {
382 $postalText .= "\n" . $word;
383 $count = strlen($word);
384 }
385 }
386 } else {
387 $postalText .= $lign;
388 }
389 }
390 return $postalText;
391 }
392
00e5200b
SJ
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)
397 {
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];
405 }
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;
413 break;
414 }
415 }
416
417 if ($isPseudoCountry) {
02c4b93a 418 return implode("\n", array_slice($textLines, 0, -1));
00e5200b 419 }
02c4b93a 420 return $address['text'];
00e5200b
SJ
421 }
422
4c906759
SJ
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)
426 {
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);
434 $minpos = 0;
435 $pos = 0;
436 foreach ($textLines as $i => $token) {
437 if (($l = levenshtein(strtoupper(trim(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"),
438 array("", "\n"), $token))),
439 $thoroughfareToken)) < $mindist) {
440 $mindist = $l;
441 $minpos = $i;
442 }
443 }
444 foreach ($geolocLines as $i => $line) {
445 if (strtoupper(trim($thoroughfareName)) == strtoupper(trim($line))) {
446 $pos = $i;
447 break;
448 }
449 }
450 $geolocLines[$pos] = $textLines[$minpos];
451 $address['geoloc'] = implode("\n", $geolocLines);
452 }
453 }
454}
455
456// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
457?>