]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
respect --osm2pgsql-cache setting
[nominatim.git] / lib / PlaceLookup.php
1 <?php
2         class PlaceLookup
3         {
4                 protected $oDB;
5
6                 protected $iPlaceID;
7
8                 protected $bIsTiger = false;
9
10                 protected $aLangPrefOrder = array();
11
12                 protected $bAddressDetails = false;
13
14                 function PlaceLookup(&$oDB)
15                 {
16                         $this->oDB =& $oDB;
17                 }
18
19                 function setLanguagePreference($aLangPrefOrder)
20                 {
21                         $this->aLangPrefOrder = $aLangPrefOrder;
22                 }
23
24                 function setIncludeAddressDetails($bAddressDetails = true)
25                 {
26                         $this->bAddressDetails = $bAddressDetails;
27                 }
28
29                 function setPlaceID($iPlaceID)
30                 {
31                         $this->iPlaceID = $iPlaceID;
32                 }
33
34                 function setOSMID($sType, $iID)
35                 {
36                         $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
37                         $this->iPlaceID = $this->oDB->getOne($sSQL);
38                 }
39
40                 function setIsTiger($b = false)
41                 {
42                         $this->bIsTiger = $b;
43                 }
44
45                 function lookup()
46                 {
47                         if (!$this->iPlaceID) return null;
48
49                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
50
51                         $sSQL = "select placex.place_id, partition, osm_type, osm_id, class, type, admin_level, housenumber, street, isin, postcode, country_code, extratags, parent_place_id, linked_place_id, rank_address, rank_search, ";
52                         $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
53                         $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
54                         $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
55                         $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
56                         $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
57                         $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
58                         $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
59
60
61                         if ($this->bIsTiger)
62                         {
63                                 $sSQL = "select place_id,partition, 'T' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, housenumber, null as street, null as isin, postcode,";
64                                 $sSQL .= " 'us' as country_code, null as extratags, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
65                                 $sSQL .= " coalesce(null,0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, 'us' as calculated_country_code, ";
66                                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
67                                 $sSQL .= " null as placename,";
68                                 $sSQL .= " null as ref,";
69                                 $sSQL .= " st_y(centroid) as lat,";
70                                 $sSQL .= " st_x(centroid) as lon";
71                                 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID;
72                         }
73
74                         $aPlace = $this->oDB->getRow($sSQL);
75
76
77                         if (PEAR::IsError($aPlace))
78                         {
79                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
80                         }
81
82                         if (!$aPlace['place_id']) return null;
83
84                         if ($this->bAddressDetails)
85                         {
86                                 $aAddress = $this->getAddressNames();
87                                 $aPlace['aAddress'] = $aAddress;
88                         }
89
90
91                         $aClassType = getClassTypes();
92                         $sAddressType = '';
93                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
94                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
95                         {
96                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
97                         }
98                         else
99                         {
100                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
101                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
102                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
103                                 else $sAddressType = $aPlace['class'];
104                         }
105
106                         $aPlace['addresstype'] = $sAddressType;
107
108                         return $aPlace;
109                 }
110
111                 function getAddressDetails($bAll = false)
112                 {
113                         if (!$this->iPlaceID) return null;
114
115                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
116
117                         $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
118                         if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
119                         $sSQL .= " order by rank_address desc,isaddress desc";
120
121                         $aAddressLines = $this->oDB->getAll($sSQL);
122                         if (PEAR::IsError($aAddressLines))
123                         {
124                                 var_dump($aAddressLines);
125                                 exit;
126                         }
127                         return $aAddressLines;
128                 }
129
130                 function getAddressNames()
131                 {
132                         $aAddressLines = $this->getAddressDetails(false);;
133
134                         $aAddress = array();
135                         $aFallback = array();
136                         $aClassType = getClassTypes();
137                         foreach($aAddressLines as $aLine)
138                         {
139                                 $bFallback = false;
140                                 $aTypeLabel = false;
141                                 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
142                                 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
143                                 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
144                                 {
145                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
146                                         $bFallback = true;
147                                 }
148                                 else
149                                 {
150                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
151                                         $bFallback = true;
152                                 }
153                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
154                                 {
155                                         $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
156                                         $sTypeLabel = str_replace(' ','_',$sTypeLabel);
157                                         if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
158                                         {
159                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
160                                         }
161                                         $aFallback[$sTypeLabel] = $bFallback;
162                                 }
163                         }
164                         return $aAddress;
165                 }
166
167         }
168 ?>