[OSM-dev] Extracting house-number from string - solution (?)
Andreas Kalsch
andreaskalsch at gmx.de
Sat May 2 17:40:33 BST 2009
A function inside a PHP class I coded some time ago to populate a
database with Google Maps API data. The code is currently not maintained
any more so I am not sure if it's still working correctly. The input
string is not a complete geocode request but the thoroughfare of an
address, made of street and house number. Just give it a try - it worked
fine for me, as far as I remember:
/**
* thoroughfare-Angabe in Straßenname und Hausnummer aufsplitten
*
* @param $thoroughfare string
* @param $country string Country-Code, von dem abhängt, ob
Hausnummer links oder rechts steht
* @return array(thoroughfareName, houseNumber)
*/
//// berücksichtige HK - kein leerzeichen vor Hausnr
// OK
static function splitThoroughfare($thoroughfare, $country) {
$parts = preg_split("/[ \,]+/", $thoroughfare);
$partsC = count($parts);
if ($partsC == 1) return array($parts[0], NULL);
// I wrote this class for the Google Geocoding API, and sometimes Google
returned the house number on the left side - no matter of the country
// I think this is fixed no and so self::$googleForcesHouseNumberLeft is
false
$houseNumberIsLeft = self::$googleForcesHouseNumberLeft
? true
// I18n::$houseNumberLeftCountries is an array with all country codes
(upper case) of countries where house number is left
: in_array($country, I18n::$houseNumberLeftCountries);
$summand = $houseNumberIsLeft ? 1 : -1;
for (
$i = $houseNumberIsLeft ? 0 : $partsC - 1;
$houseNumberIsLeft ? ($i < $partsC) : ($i >= 0);
$i += $summand) {
// part of street name found
if (
preg_match('/\p{L}{2}/u', $parts[$i])
||
preg_match('/\p{L}/u', $parts[$i])
&& preg_match('/^\D+$/', $parts[$i])
) {
if ($houseNumberIsLeft) {
$thoroughfare = array_slice($parts, $i);
$houseNumber = array_slice($parts, 0, $i);
}
else {
$thoroughfare = array_slice($parts, 0, $i + 1);
$houseNumber = array_slice($parts, $i + 1);
}
return array(
implode(' ', $thoroughfare),
implode(' ', $houseNumber)
);
}
}
}
More information about the dev
mailing list