3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
13 require_once(CONST_LibDir.'/ClassTypes.php');
16 * Detailed list of address parts for a single result
21 private $aAddressLines;
23 public function __construct(&$oDB, $iPlaceID, $sHousenumber, $mLangPref)
25 $this->iPlaceID = $iPlaceID;
27 if (is_array($mLangPref)) {
28 $mLangPref = $oDB->getArraySQL($oDB->getDBQuotedList($mLangPref));
31 if (!isset($sHousenumber)) {
36 $sSQL .= ' get_name_by_language(name,'.$mLangPref.') as localname';
37 $sSQL .= ' FROM get_addressdata('.$iPlaceID.','.$sHousenumber.')';
38 $sSQL .= ' ORDER BY rank_address DESC, isaddress DESC';
40 $this->aAddressLines = $oDB->getAll($sSQL);
43 private static function isAddress($aLine)
45 return $aLine['isaddress'] || $aLine['type'] == 'country_code';
48 public function getAddressDetails($bAll = false)
51 return $this->aAddressLines;
54 return array_filter($this->aAddressLines, array(__CLASS__, 'isAddress'));
57 public function getLocaleAddress()
62 foreach ($this->aAddressLines as $aLine) {
63 if ($aLine['isaddress'] && $sPrevResult != $aLine['localname']) {
64 $sPrevResult = $aLine['localname'];
65 $aParts[] = $sPrevResult;
69 return join(', ', $aParts);
72 public function getAddressNames()
76 foreach ($this->aAddressLines as $aLine) {
77 if (!self::isAddress($aLine)) {
81 $sTypeLabel = ClassTypes\getLabelTag($aLine);
84 if (isset($aLine['localname']) && $aLine['localname']!=='') {
85 $sName = $aLine['localname'];
86 } elseif (isset($aLine['housenumber']) && $aLine['housenumber']!=='') {
87 $sName = $aLine['housenumber'];
91 && (!isset($aAddress[$sTypeLabel])
92 || $aLine['class'] == 'place')
94 $aAddress[$sTypeLabel] = $sName;
102 * Annotates the given json with geocodejson address information fields.
104 * @param array $aJson Json hash to add the fields to.
106 * Geocodejson has the following fields:
107 * street, locality, postcode, city, district,
108 * county, state, country
110 * Postcode and housenumber are added by type, district is not used.
111 * All other fields are set according to address rank.
113 public function addGeocodeJsonAddressParts(&$aJson)
115 foreach (array_reverse($this->aAddressLines) as $aLine) {
116 if (!$aLine['isaddress']) {
120 if (!isset($aLine['localname']) || $aLine['localname'] == '') {
124 if ($aLine['type'] == 'postcode' || $aLine['type'] == 'postal_code') {
125 $aJson['postcode'] = $aLine['localname'];
129 if ($aLine['type'] == 'house_number') {
130 $aJson['housenumber'] = $aLine['localname'];
134 if ($this->iPlaceID == $aLine['place_id']) {
138 $iRank = (int)$aLine['rank_address'];
140 if ($iRank > 25 && $iRank < 28) {
141 $aJson['street'] = $aLine['localname'];
142 } elseif ($iRank >= 22 && $iRank <= 25) {
143 $aJson['locality'] = $aLine['localname'];
144 } elseif ($iRank >= 17 && $iRank <= 21) {
145 $aJson['district'] = $aLine['localname'];
146 } elseif ($iRank >= 13 && $iRank <= 16) {
147 $aJson['city'] = $aLine['localname'];
148 } elseif ($iRank >= 10 && $iRank <= 12) {
149 $aJson['county'] = $aLine['localname'];
150 } elseif ($iRank >= 5 && $iRank <= 9) {
151 $aJson['state'] = $aLine['localname'];
152 } elseif ($iRank == 4) {
153 $aJson['country'] = $aLine['localname'];
158 public function getAdminLevels()
161 foreach (array_reverse($this->aAddressLines) as $aLine) {
162 if (self::isAddress($aLine)
163 && isset($aLine['admin_level'])
164 && $aLine['admin_level'] < 15
165 && !isset($aAddress['level'.$aLine['admin_level']])
167 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
173 public function debugInfo()
175 return $this->aAddressLines;