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