]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
strip query log to minimum info not available in apache logs
[nominatim.git] / lib / ReverseGeocode.php
1 <?php
2         class ReverseGeocode
3         {
4                 protected $oDB;
5
6                 protected $fLat;
7                 protected $fLon;
8                 protected $iMaxRank = 28;
9
10                 protected $aLangPrefOrder = array();
11
12                 protected $bShowAddressDetails = true;
13
14                 function ReverseGeocode(&$oDB)
15                 {
16                         $this->oDB =& $oDB;
17                 }
18
19                 function setLanguagePreference($aLangPref)
20                 {
21                         $this->aLangPrefOrder = $aLangPref;
22                 }
23
24                 function setIncludeAddressDetails($bAddressDetails = true)
25                 {
26                         $this->bAddressDetails = $bAddressDetails;
27                 }
28
29                 function setLatLon($fLat, $fLon)
30                 {
31                         $this->fLat = (float)$fLat;
32                         $this->fLon = (float)$fLon;
33                 }
34
35                 function setRank($iRank)
36                 {
37                         $this->iMaxRank = $iRank;
38                 }
39
40                 function setZoom($iZoom)
41                 {
42                         // Zoom to rank, this could probably be calculated but a lookup gives fine control
43                         $aZoomRank = array(
44                                 0 => 2, // Continent / Sea
45                                 1 => 2,
46                                 2 => 2,
47                                 3 => 4, // Country
48                                 4 => 4,
49                                 5 => 8, // State
50                                 6 => 10, // Region
51                                 7 => 10,
52                                 8 => 12, // County
53                                 9 => 12,
54                                 10 => 17, // City
55                                 11 => 17,
56                                 12 => 18, // Town / Village
57                                 13 => 18,
58                                 14 => 22, // Suburb
59                                 15 => 22,
60                                 16 => 26, // Street, TODO: major street?
61                                 17 => 26,
62                                 18 => 30, // or >, Building
63                                 19 => 30, // or >, Building
64                                 );
65                         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
66                 }
67
68                 function lookup()
69                 {
70                         $sPointSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
71                         $iMaxRank = $this->iMaxRank;
72
73                         // Find the nearest point
74                         $fSearchDiam = 0.0004;
75                         $iPlaceID = null;
76                         $aArea = false;
77                         $fMaxAreaDistance = 1;
78                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
79                         {
80                                 $fSearchDiam = $fSearchDiam * 2;
81
82                                 // If we have to expand the search area by a large amount then we need a larger feature
83                                 // then there is a limit to how small the feature should be
84                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
85                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
86                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
87                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
88                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
89                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
90                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
91                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
92
93                                 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
94                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
95                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
96                                 $sSQL .= ' and (name is not null or housenumber is not null)';
97                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
98                                 $sSQL .= ' and indexed_status = 0 ';
99                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
100                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
101                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
102                                 if (CONST_Debug) var_dump($sSQL);
103                                 $aPlace = $this->oDB->getRow($sSQL);
104                                 if (PEAR::IsError($aPlace))
105                                 {
106                                         failInternalError("Could not determine closest place.", $sSQL, $aPlace);
107                                 }
108                                 $iPlaceID = $aPlace['place_id'];
109                                 $iParentPlaceID = $aPlace['parent_place_id'];
110                         }
111
112                         // The point we found might be too small - use the address to find what it is a child of
113                         if ($iPlaceID && $iMaxRank < 28)
114                         {
115                                 if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
116                                 {
117                                         $iPlaceID = $iParentPlaceID;
118                                 }
119                                 $sSQL = "select address_place_id from place_addressline where place_id = $iPlaceID order by abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc limit 1";
120                                 $iPlaceID = $this->oDB->getOne($sSQL);
121                                 if (PEAR::IsError($iPlaceID))
122                                 {
123                                         failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
124                                 }
125                                 if (!$iPlaceID)
126                                 {
127                                         $iPlaceID = $aPlace['place_id'];
128                                 }
129                         }
130
131                         $oPlaceLookup = new PlaceLookup($this->oDB);
132                         $oPlaceLookup->setLanguagePreference($this->aLangPrefOrder);
133                         $oPlaceLookup->setIncludeAddressDetails($this->bAddressDetails);
134                         $oPlaceLookup->setPlaceId($iPlaceID);
135
136                         return $oPlaceLookup->lookup();
137                 }
138         }
139 ?>