X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fgeocoding.inc.php;h=a828366dc48aefe9feafb37d9c8e2119b70ed692;hb=03bda5524bd16c5c160a314d42baefe0b3254c62;hp=805207011055563aa76b48801d44fcbd82147f93;hpb=5a10ab142a636a7ffbd4bed1dc095657bff77890;p=platal.git diff --git a/include/geocoding.inc.php b/include/geocoding.inc.php index 8052070..a828366 100644 --- a/include/geocoding.inc.php +++ b/include/geocoding.inc.php @@ -1,6 +1,6 @@ numRows() == 0) { - $address[$area . 'Id'] = XDB::execute("INSERT INTO " . $databases[$area] . " (name, country) - VALUES ({?}, {?})", - $address[$area . 'Name'], $address['countryId']); + XDB::execute('INSERT INTO ' . $databases[$area] . ' (name, country) + VALUES ({?}, {?})', + $address[$area . 'Name'], $address['countryId']); + $address[$area . 'Id'] = XDB::insertId(); } else { $address[$area . 'Id'] = $res->fetchOneCell(); } @@ -95,7 +96,7 @@ class GMapsGeocoder extends Geocoder { public function getGeocodedAddress(array $address) { $address = $this->prepareAddress($address); - $textAddress = $address['text']; + $textAddress = $this->getTextToGeocode($address); // Try to geocode the full address. if (($geocodedData = $this->getPlacemarkForAddress($textAddress))) { @@ -128,7 +129,7 @@ class GMapsGeocoder extends Geocoder { $address['accuracy'] = 0; return $address; } - + // Updates the address with the geocoded information from Google Maps. Also // cleans up the final informations. private function getUpdatedAddress(array $address, array $geocodedData, $extraLines) { @@ -143,10 +144,6 @@ class GMapsGeocoder extends Geocoder { // We can now format the address. $this->formatAddress($address, $extraLines); - // Some entities in ISO 3166 are not countries, thus they have to be replaced - // by the country they belong to. - // TODO: fixCountry($address); - return $address; } @@ -341,7 +338,12 @@ class GMapsGeocoder extends Geocoder { $address['text'] = $address['geoloc']; $address['postalText'] = $address['geocodedPostalText']; unset($address['geoloc'], $address['geocodedPostalText']); + } else { + $address['geoloc'] = str_replace("\n", "\r\n", $address['geoloc']); + $address['geocodedPostalText'] = str_replace("\n", "\r\n", $address['geocodedPostalText']); } + $address['text'] = str_replace("\n", "\r\n", $address['text']); + $address['postalText'] = str_replace("\n", "\r\n", $address['postalText']); } // Returns the address formated for postal use. @@ -388,6 +390,36 @@ class GMapsGeocoder extends Geocoder { return $postalText; } + // Trims the name of the real country if it contains an ISO 3166-1 non-country + // item. For that purpose, we compare the last but one line of the address with + // all non-country items of ISO 3166-1. + private function getTextToGeocode($address) + { + $res = XDB::iterator('SELECT country, countryFR + FROM geoloc_countries + WHERE belongsTo IS NOT NULL'); + $countries = array(); + foreach ($res as $item) { + $countries[] = $item[0]; + $countries[] = $item[1]; + } + $textLines = explode("\n", $address['text']); + $countLines = count($textLines); + $needle = strtoupper(trim($textLines[$countLines - 2])); + $isPseudoCountry = false; + foreach ($countries as $country) { + if (strtoupper($country) == $needle) { + $isPseudoCountry = true; + break; + } + } + + if ($isPseudoCountry) { + return implode("\n", array_slice($textLines, 0, -1)); + } + return $address['text']; + } + // Search for the lign from the given address that is the closest to the geocoded thoroughfareName // and replaces the corresponding lign in the geocoded text by it. static protected function fixStreetNumber(&$address)