From 82a11cae2ddb7cfe3b2a180346bb374efe2e6a9d Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Fri, 15 May 2020 17:36:37 +0200 Subject: [PATCH] first draft --- lib/AddressDetails.php | 18 +++---- lib/ClassTypes.php | 119 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 11 deletions(-) diff --git a/lib/AddressDetails.php b/lib/AddressDetails.php index 3322c6b2..a721f4c5 100644 --- a/lib/AddressDetails.php +++ b/lib/AddressDetails.php @@ -61,7 +61,7 @@ class AddressDetails return join(', ', $aParts); } - public function getAddressNames() + public function getAddressNames($sCountry = null) { $aAddress = array(); $aFallback = array(); @@ -72,10 +72,11 @@ class AddressDetails } $bFallback = false; - $aTypeLabel = ClassTypes\getInfo($aLine); + $sTypeLabel = ClassTypes\getSimpleLabel($aLine); - if ($aTypeLabel === false) { - $aTypeLabel = ClassTypes\getFallbackInfo($aLine); + if ($sTypeLabel === false) { + $aTypeLabel = ClassTypes\getFallbackLabel($aLine['rank_address'], + $sCountry); $bFallback = true; } @@ -87,16 +88,13 @@ class AddressDetails } if (isset($sName)) { - $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel']) ? $aTypeLabel['simplelabel'] : $aTypeLabel['label']); - $sTypeLabel = str_replace(' ', '_', $sTypeLabel); + $sTypeLabel = strtolower(str_replace(' ', '_', $sTypeLabel)); if (!isset($aAddress[$sTypeLabel]) - || isset($aFallback[$sTypeLabel]) + || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place' ) { $aAddress[$sTypeLabel] = $sName; - if ($bFallback) { - $aFallback[$sTypeLabel] = $bFallback; - } + $aFallback[$sTypeLabel] = $bFallback; } } } diff --git a/lib/ClassTypes.php b/lib/ClassTypes.php index a7c2cd4f..5afbf258 100644 --- a/lib/ClassTypes.php +++ b/lib/ClassTypes.php @@ -2,6 +2,119 @@ namespace Nominatim\ClassTypes; +/** + * Create a simplfied label for the given place. + * + * @param array[] $aPlace Information about the place to label. + * + * A simplified label groups various object types together under a common + * label. + */ +function getSimpleLabel($aPlace) +{ + static $aRoadLabels = array ( + 'motorway_junction' => 'Junction', + 'motorway' => 'Road', + 'trunk' => 'Road', + 'primary' => 'Road', + 'secondary' => 'Road', + 'tertiary' => 'Road', + 'residential' => 'Road', + 'unclassified' => 'Road', + 'living_street' => 'Road', + 'service' => 'Road', + 'track' => 'Road', + 'byway' => 'Road', + 'steps' => 'Footway', + 'motorway_link' => 'Road', + 'trunk_link' => 'Road', + 'primary_link' => 'Road', + 'secondary_link' => 'Road', + 'tertiary_link' => 'Road', + 'construction' => 'Road' + ); + + if ($aPlace['class'] == 'highway' and isset($aRoadLabels[$aPlace['type']])) { + return $aRoadLabels[$aPlace['type']]; + } + + return getLabel($aPlace); +} + +/** + * Create a label for the given place. + * + * @param array[] $aPlace Information about the place to label. + */ +function getLabel($aPlace, $sCountry = null) +{ + if ($aPlace['class'] == 'boundary' + && $aPlace['type'] == 'administrative') + && !isset($aPlace['place_type']) + ) { + return getBoundaryLabel((int)($aPlace['admin_level'] ?? 15, + $aPlace['country_code'] ?? null) + } + + return ucwords(str_replace('_', ' ', $aPlace['place_type'] ?? $aPlace['type'])); +} + +/** + * Return a generic simple label to be used for the given address rank + * in the given country. + * + * @param int $iRankAddress Address rank of the object to be labeled. + * @param string $sCountry Country code of the country where the object is + * in. May be null, in which case a world-wide + * fallback is used. + * + * @return string + */ +function getFallbackLabel($iRankAddress, $sCountry = null) +{ + return getBoundaryLabel((int)($iRankAddress / 2), $sCountry, + 'address'.$iRankAddress); +} + +/** + * Return a simple label for an administrative boundary for the given country. + * + * @param int $iAdminLevel Content of admin_level tag. + * @param string $sCountry Country code of the country where the object is + * in. May be null, in which case a world-wide + * fallback is used. + * @param string $sFallback String to return if no explicit string is listed. + * + * @return string + */ +function getBoundaryLabel($iAdminLevel, $sCountry, $sFallback = 'Administrative') +{ + static $aBoundaryList = array ( + 'default' => array ( + 1 => 'Continent', + 2 => 'Country', + 3 => 'Region', + 4 => 'State', + 5 => 'State District', + 6 => 'County', + 7 => 'Municipality', + 8 => 'City', + 9 => 'City District' + 10 => 'Suburb', + 11 => 'Neighbourhood' + ) + ); + + if (isset($aBoundaryList[$sCountry]) + && isset($aBoundaryList[$sCountry][$iAdminLevel]) + ) { + return $aBoundaryList[$sCountry][$iAdminLevel]; + } + + return $aBoundaryList['default'][$iAdminLevel] ?? $sFallback; +} + + function getInfo($aPlace) { $aClassType = getList(); @@ -73,9 +186,11 @@ function getListWithImportance() return $aOrders; } + + function getList() { - return array( + static $aPropertyCache = array( 'boundary:administrative:1' => array('label' => 'Continent', 'frequency' => 0, 'icon' => 'poi_boundary_administrative', 'defdiameter' => 0.32), 'boundary:administrative:2' => array('label' => 'Country', 'frequency' => 0, 'icon' => 'poi_boundary_administrative', 'defdiameter' => 0.32), 'place:country' => array('label' => 'Country', 'frequency' => 0, 'icon' => 'poi_boundary_administrative', 'defzoom' => 6, 'defdiameter' => 15), @@ -379,4 +494,6 @@ function getList() 'railway:abandoned' => array('label' => 'Abandoned', 'frequency' => 641), 'railway:disused' => array('label' => 'Disused', 'frequency' => 72), ); + + return $aPropertyCache; } -- 2.39.5