-
- // 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;
+
+ // Returns the address formated for postal use.
+ // The main rules are (cf AFNOR XPZ 10-011):
+ // -everything in upper case;
+ // -if there are more then than 38 characters in a lign, split it;
+ // -if there are more then than 32 characters in the description of the "street", use abbreviations.
+ private function getPostalAddress($text) {
+ static $abbreviations = array(
+ "IMPASSE" => "IMP",
+ "RUE" => "R",
+ "AVENUE" => "AV",
+ "BOULEVARD" => "BVD",
+ "ROUTE" => "R",
+ "STREET" => "ST",
+ "ROAD" => "RD",
+ );
+
+ $text = strtoupper($text);
+ $arrayText = explode("\n", $text);
+ $postalText = "";
+
+ foreach ($arrayText as $i => $lign) {
+ $postalText .= (($i == 0) ? "" : "\n");
+ if (($length = strlen($lign)) > 32) {
+ $words = explode(" ", $lign);
+ $count = 0;
+ foreach ($words as $word) {
+ if (isset($abbreviations[$word])) {
+ $word = $abbreviations[$word];
+ }
+ if ($count + ($wordLength = strlen($word)) <= 38) {
+ $postalText .= (($count == 0) ? "" : " ") . $word;
+ $count += (($count == 0) ? 0 : 1) + $wordLength;
+ } else {
+ $postalText .= "\n" . $word;
+ $count = strlen($word);
+ }