]> git.openstreetmap.org Git - nominatim.git/blob - lib/AddressDetails.php
first draft
[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($sCountry = null)
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             $sTypeLabel = ClassTypes\getSimpleLabel($aLine);
76
77             if ($sTypeLabel === false) {
78                 $aTypeLabel = ClassTypes\getFallbackLabel($aLine['rank_address'],
79                                                           $sCountry);
80                 $bFallback = true;
81             }
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                 $sTypeLabel = strtolower(str_replace(' ', '_', $sTypeLabel));
92                 if (!isset($aAddress[$sTypeLabel])
93                     || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel])
94                     || $aLine['class'] == 'place'
95                 ) {
96                     $aAddress[$sTypeLabel] = $sName;
97                     $aFallback[$sTypeLabel] = $bFallback;
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             } elseif ($aLine['type'] == 'house_number') {
131                 $aJson['housenumber'] = $aLine['localname'];
132             }
133
134             if ($this->iPlaceID == $aLine['place_id']) {
135                 continue;
136             }
137
138             $iRank = (int)$aLine['rank_address'];
139
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'];
154             }
155         }
156     }
157
158     public function getAdminLevels()
159     {
160         $aAddress = array();
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']])
166             ) {
167                 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
168             }
169         }
170         return $aAddress;
171     }
172
173     public function debugInfo()
174     {
175         return $this->aAddressLines;
176     }
177 }