]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/AddressDetails.php
fix merge issues
[nominatim.git] / lib-php / AddressDetails.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 require_once(CONST_LibDir.'/ClassTypes.php');
14
15 /**
16  * Detailed list of address parts for a single result
17  */
18 class AddressDetails
19 {
20     private $iPlaceID;
21     private $aAddressLines;
22
23     public function __construct(&$oDB, $iPlaceID, $sHousenumber, $mLangPref)
24     {
25         $this->iPlaceID = $iPlaceID;
26
27         if (is_array($mLangPref)) {
28             $mLangPref = $oDB->getArraySQL($oDB->getDBQuotedList($mLangPref));
29         }
30
31         if (!isset($sHousenumber)) {
32             $sHousenumber = -1;
33         }
34
35         $sSQL = 'SELECT *,';
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';
39
40         $this->aAddressLines = $oDB->getAll($sSQL);
41     }
42
43     private static function isAddress($aLine)
44     {
45         return $aLine['isaddress'] || $aLine['type'] == 'country_code';
46     }
47
48     public function getAddressDetails($bAll = false)
49     {
50         if ($bAll) {
51             return $this->aAddressLines;
52         }
53
54         return array_filter($this->aAddressLines, array(__CLASS__, 'isAddress'));
55     }
56
57     public function getLocaleAddress()
58     {
59         $aParts = array();
60         $sPrevResult = '';
61
62         foreach ($this->aAddressLines as $aLine) {
63             if ($aLine['isaddress'] && $sPrevResult != $aLine['localname']) {
64                 $sPrevResult = $aLine['localname'];
65                 $aParts[] = $sPrevResult;
66             }
67         }
68
69         return join(', ', $aParts);
70     }
71
72     public function getAddressNames()
73     {
74         $aAddress = array();
75
76         foreach ($this->aAddressLines as $aLine) {
77             if (!self::isAddress($aLine)) {
78                 continue;
79             }
80
81             $sTypeLabel = ClassTypes\getLabelTag($aLine);
82
83             $sName = null;
84             if (isset($aLine['localname']) && $aLine['localname']!=='') {
85                 $sName = $aLine['localname'];
86             } elseif (isset($aLine['housenumber']) && $aLine['housenumber']!=='') {
87                 $sName = $aLine['housenumber'];
88             }
89
90             if (isset($sName)
91                 && (!isset($aAddress[$sTypeLabel])
92                     || $aLine['class'] == 'place')
93             ) {
94                 $aAddress[$sTypeLabel] = $sName;
95
96                 if (!empty($aLine['name'])) {
97                     $this->addSubdivisionCode($aAddress, $aLine['admin_level'], $aLine['name']);
98                 }
99             }
100         }
101
102         return $aAddress;
103     }
104
105     /**
106      * Annotates the given json with geocodejson address information fields.
107      *
108      * @param array  $aJson  Json hash to add the fields to.
109      *
110      * Geocodejson has the following fields:
111      *  street, locality, postcode, city, district,
112      *  county, state, country
113      *
114      * Postcode and housenumber are added by type, district is not used.
115      * All other fields are set according to address rank.
116      */
117     public function addGeocodeJsonAddressParts(&$aJson)
118     {
119         foreach (array_reverse($this->aAddressLines) as $aLine) {
120             if (!$aLine['isaddress']) {
121                 continue;
122             }
123
124             if (!isset($aLine['localname']) || $aLine['localname'] == '') {
125                 continue;
126             }
127
128             if ($aLine['type'] == 'postcode' || $aLine['type'] == 'postal_code') {
129                 $aJson['postcode'] = $aLine['localname'];
130                 continue;
131             }
132
133             if ($aLine['type'] == 'house_number') {
134                 $aJson['housenumber'] = $aLine['localname'];
135                 continue;
136             }
137
138             if ($this->iPlaceID == $aLine['place_id']) {
139                 continue;
140             }
141
142             $iRank = (int)$aLine['rank_address'];
143
144             if ($iRank > 25 && $iRank < 28) {
145                 $aJson['street'] = $aLine['localname'];
146             } elseif ($iRank >= 22 && $iRank <= 25) {
147                 $aJson['locality'] = $aLine['localname'];
148             } elseif ($iRank >= 17 && $iRank <= 21) {
149                 $aJson['district'] = $aLine['localname'];
150             } elseif ($iRank >= 13 && $iRank <= 16) {
151                 $aJson['city'] = $aLine['localname'];
152             } elseif ($iRank >= 10 && $iRank <= 12) {
153                 $aJson['county'] = $aLine['localname'];
154             } elseif ($iRank >= 5 && $iRank <= 9) {
155                 $aJson['state'] = $aLine['localname'];
156             } elseif ($iRank == 4) {
157                 $aJson['country'] = $aLine['localname'];
158             }
159         }
160     }
161
162     public function getAdminLevels()
163     {
164         $aAddress = array();
165         foreach (array_reverse($this->aAddressLines) as $aLine) {
166             if (self::isAddress($aLine)
167                 && isset($aLine['admin_level'])
168                 && $aLine['admin_level'] < 15
169                 && !isset($aAddress['level'.$aLine['admin_level']])
170             ) {
171                 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
172             }
173         }
174         return $aAddress;
175     }
176
177     public function debugInfo()
178     {
179         return $this->aAddressLines;
180     }
181
182     private function addSubdivisionCode(&$aAddress, $iAdminLevel, $nameDetails)
183     {
184         if (is_string($nameDetails)) {
185             $nameDetails = json_decode('{' . str_replace('"=>"', '":"', $nameDetails) . '}', true);
186         }
187         if (!empty($nameDetails['ISO3166-2'])) {
188             $aAddress["ISO3166-2-lvl$iAdminLevel"] = $nameDetails['ISO3166-2'];
189         }
190     }
191 }