]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
ignore man_made objects in reverse search
[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                         $iMaxRank_orig = $this->iMaxRank;
73
74                         // Find the nearest point
75                         $fSearchDiam = 0.0004;
76                         $iPlaceID = null;
77                         $aArea = false;
78                         $fMaxAreaDistance = 1;
79                         $bIsInUnitedStates = false;
80                         $bPlaceIsTiger = false;
81                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
82                         {
83                                 $fSearchDiam = $fSearchDiam * 2;
84
85                                 // If we have to expand the search area by a large amount then we need a larger feature
86                                 // then there is a limit to how small the feature should be
87                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
88                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
89                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
90                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
91                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
92                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
93                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
94                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
95
96                                 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code from placex';
97                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
98                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
99                                 $sSQL .= ' and (name is not null or housenumber is not null)';
100                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
101                                 $sSQL .= ' and indexed_status = 0 ';
102                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
103                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
104                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
105                                 if (CONST_Debug) var_dump($sSQL);
106                                 $aPlace = $this->oDB->getRow($sSQL);
107                                 if (PEAR::IsError($aPlace))
108                                 {
109                                         failInternalError("Could not determine closest place.", $sSQL, $aPlace);
110                                 }
111                                 $iPlaceID = $aPlace['place_id'];
112                                 $iParentPlaceID = $aPlace['parent_place_id'];
113                                 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
114                         }
115
116                         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
117                         if ($bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) 
118                         {
119                                 $fSearchDiam = 0.001;
120                                 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search ';
121                                 if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
122                                 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
123                                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.')';
124                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', centroid) ASC limit 1';
125
126
127                                 // print all house numbers in the parent (street)
128                                 if (CONST_Debug)
129                                 {
130                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
131                                         var_dump($sSQL);
132
133                                         $aAllHouses = $this->oDB->getAll($sSQL);
134                                         foreach($aAllHouses as $i)
135                                         {
136                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
137                                         }
138                                 }
139
140                                 $aPlaceTiger = $this->oDB->getRow($sSQL);
141                                 if (PEAR::IsError($aPlace))
142                                 {
143                                         failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
144                                 }
145                                 if ($aPlaceTiger)
146                                 {
147                                         if (CONST_Debug) var_dump('found Tiger place', $aPlaceTiger);
148                                         $bPlaceIsTiger = true;
149                                         $aPlace = $aPlaceTiger;
150                                         $iPlaceID = $aPlaceTiger['place_id'];
151                                         $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
152                                 }
153                         }
154
155                         // The point we found might be too small - use the address to find what it is a child of
156                         if ($iPlaceID && $iMaxRank < 28)
157                         {
158                                 if ($aPlace['rank_search'] > 28 && $iParentPlaceID && !$bPlaceIsTiger)
159                                 {
160                                         $iPlaceID = $iParentPlaceID;
161                                 }
162                                 $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";
163                                 $iPlaceID = $this->oDB->getOne($sSQL);
164                                 if (PEAR::IsError($iPlaceID))
165                                 {
166                                         failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
167                                 }
168                                 if (!$iPlaceID)
169                                 {
170                                         $iPlaceID = $aPlace['place_id'];
171                                 }
172                         }
173
174                         $oPlaceLookup = new PlaceLookup($this->oDB);
175                         $oPlaceLookup->setLanguagePreference($this->aLangPrefOrder);
176                         $oPlaceLookup->setIncludeAddressDetails($this->bAddressDetails);
177                         $oPlaceLookup->setPlaceId($iPlaceID);
178                         $oPlaceLookup->setIsTiger($bPlaceIsTiger);
179
180                         return $oPlaceLookup->lookup();
181                 }
182         }
183 ?>