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