X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fgeocoding.inc.php;h=da809d78a5014f9541451bcdda8f80e119358727;hb=c67b874fd8d442bfded35295a819e7ce11a2d6dc;hp=2e99434b5b5c3a84a61ff47be2cba25e3e20988b;hpb=fb813fb52d5ab65ca9a5b92b5cb9089523380d79;p=platal.git diff --git a/include/geocoding.inc.php b/include/geocoding.inc.php index 2e99434..da809d7 100644 --- a/include/geocoding.inc.php +++ b/include/geocoding.inc.php @@ -1,6 +1,6 @@ 'geoloc_localities', ); - if (isset($address[$area . 'Name']) && isset($databases[$area])) { + if (isset($address[$area . 'Name']) && isset($databases[$area]) && !empty($address[$area . 'Name'])) { $res = XDB::query("SELECT id FROM " . $databases[$area] . " WHERE name = {?}", $address[$area . 'Name']); if ($res->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(); } + } else { + $address[$area . 'Id'] = null; } } @@ -92,6 +95,10 @@ class GMapsGeocoder extends Geocoder { // Maximum number of Geocoding calls to the Google Maps API. const MAX_GMAPS_RPC_CALLS = 5; + // Maximum levenshtein distance authorized between input and geocoded text in a single line. + const MAX_LINE_DISTANCE = 5; + // Maximum levenshtein distance authorized between input and geocoded text in the whole text. + const MAX_TOTAL_DISTANCE = 6; public function getGeocodedAddress(array $address) { $address = $this->prepareAddress($address); @@ -133,16 +140,7 @@ class GMapsGeocoder extends Geocoder { // cleans up the final informations. private function getUpdatedAddress(array $address, array $geocodedData, $extraLines) { $this->fillAddressWithGeocoding(&$address, $geocodedData); - - // If the accuracy is 6, it means only the street has been gecoded - // but not the number, thus we need to fix it. - if ($address['accuracy'] == 6) { - $this->fixStreetNumber($address); - } - - // We can now format the address. $this->formatAddress($address, $extraLines); - return $address; } @@ -322,20 +320,23 @@ class GMapsGeocoder extends Geocoder { $countGeoloc = count($arrayGeoloc); $countText = count($arrayText); + $totalDistance = 0; if (($countText > $countGeoloc) || ($countText < $countGeoloc - 1) || (($countText == $countGeoloc - 1) && ($arrayText[$countText - 1] == strtoupper($address['country'])))) { $same = false; } else { for ($i = 0; $i < $countGeoloc && $i < $countText; ++$i) { - if (levenshtein($arrayText[$i], trim($arrayGeoloc[$i])) > 3) { + $lineDistance = levenshtein($arrayText[$i], trim($arrayGeoloc[$i])); + $totalDistance += $lineDistance; + if ($lineDistance > self::MAX_LINE_DISTANCE || $totalDistance > self::MAX_TOTAL_DISTANCE) { $same = false; + break; } } } + if ($same) { - $address['text'] = $address['geoloc']; - $address['postalText'] = $address['geocodedPostalText']; unset($address['geoloc'], $address['geocodedPostalText']); } else { $address['geoloc'] = str_replace("\n", "\r\n", $address['geoloc']); @@ -344,7 +345,7 @@ class GMapsGeocoder extends Geocoder { $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. // The main rules are (cf AFNOR XPZ 10-011): // -everything in upper case; @@ -414,41 +415,9 @@ class GMapsGeocoder extends Geocoder { } if ($isPseudoCountry) { - return $address['text']; - } - return implode("\n", array_slice($textLines, 0, -1)); - } - - // 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) - { - if (isset($address['thoroughfareName'])) { - $thoroughfareName = $address['thoroughfareName']; - $thoroughfareToken = strtoupper(trim(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), - array("", "\n"), $thoroughfareName))); - $geolocLines = explode("\n", $address['geoloc']); - $textLines = explode("\n", $address['text']); - $mindist = strlen($thoroughfareToken); - $minpos = 0; - $pos = 0; - foreach ($textLines as $i => $token) { - if (($l = levenshtein(strtoupper(trim(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), - array("", "\n"), $token))), - $thoroughfareToken)) < $mindist) { - $mindist = $l; - $minpos = $i; - } - } - foreach ($geolocLines as $i => $line) { - if (strtoupper(trim($thoroughfareName)) == strtoupper(trim($line))) { - $pos = $i; - break; - } - } - $geolocLines[$pos] = $textLines[$minpos]; - $address['geoloc'] = implode("\n", $geolocLines); + return implode("\n", array_slice($textLines, 0, -1)); } + return $address['text']; } }