]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
docs: give recomendations about flatnode file size
[nominatim.git] / lib / ReverseGeocode.php
1 <?php
2         class ReverseGeocode
3         {
4                 protected $oDB;
5                 protected $iMaxRank = 28;
6
7                 function ReverseGeocode(&$oDB)
8                 {
9                         $this->oDB =& $oDB;
10                 }
11
12                 function setZoom($iZoom)
13                 {
14                         // Zoom to rank, this could probably be calculated but a lookup gives fine control
15                         $aZoomRank = array(
16                                 0 => 2, // Continent / Sea
17                                 1 => 2,
18                                 2 => 2,
19                                 3 => 4, // Country
20                                 4 => 4,
21                                 5 => 8, // State
22                                 6 => 10, // Region
23                                 7 => 10,
24                                 8 => 12, // County
25                                 9 => 12,
26                                 10 => 17, // City
27                                 11 => 17,
28                                 12 => 18, // Town / Village
29                                 13 => 18,
30                                 14 => 22, // Suburb
31                                 15 => 22,
32                                 16 => 26, // Street, TODO: major street?
33                                 17 => 26,
34                                 18 => 30, // or >, Building
35                                 19 => 30, // or >, Building
36                                 );
37                         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
38                 }
39
40                 // returns { place_id =>, type => '(osm|tiger)' }
41                 // fails if no place was found
42                 function lookup($fLat, $fLon, $bDoInterpolation = true)
43                 {
44                         $sPointSQL = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
45                         $iMaxRank = $this->iMaxRank;
46
47                         // Find the nearest point
48                         $fSearchDiam = 0.0004;
49                         $iPlaceID = null;
50                         $aArea = false;
51                         $fMaxAreaDistance = 1;
52                         $bIsInUnitedStates = false;
53                         $bPlaceIsTiger = false;
54                         $bPlaceIsLine = false;
55                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
56                         {
57                                 $fSearchDiam = $fSearchDiam * 2;
58
59                                 // If we have to expand the search area by a large amount then we need a larger feature
60                                 // then there is a limit to how small the feature should be
61                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
62                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
63                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
64                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
65                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
66                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
67                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
68                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
69
70                                 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code';
71                                 $sSQL .= ' FROM placex';
72                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
73                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
74                                 $sSQL .= ' and (name is not null or housenumber is not null)';
75                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
76                                 $sSQL .= ' and indexed_status = 0 ';
77                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
78                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
79                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
80                                 if (CONST_Debug) var_dump($sSQL);
81                                 $aPlace = chksql($this->oDB->getRow($sSQL),
82                                                  "Could not determine closest place.");
83                                 $iPlaceID = $aPlace['place_id'];
84                                 $iParentPlaceID = $aPlace['parent_place_id'];
85                                 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
86                         }
87                         // if a street or house was found, look in interpolation lines table
88                         if ($bDoInterpolation && $this->iMaxRank >= 28 && $aPlace && $aPlace['rank_search'] >= 26)
89                         {
90                                 // if a house was found, search the interpolation line that is at least as close as the house
91                                 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
92                                 $sSQL .= ' FROM location_property_osmline';
93                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
94                                 $sSQL .= ' and indexed_status = 0 ';
95                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
96                                 
97                                 if (CONST_Debug)
98                                 {
99                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
100                                         var_dump($sSQL);
101
102                                         $aAllHouses = chksql($this->oDB->getAll($sSQL));
103                                         foreach($aAllHouses as $i)
104                                         {
105                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
106                                         }
107                                 }
108                                 $aPlaceLine = chksql($this->oDB->getRow($sSQL),
109                                                      "Could not determine closest housenumber on an osm interpolation line.");
110                                 if ($aPlaceLine)
111                                 {
112                                         if (CONST_Debug) var_dump('found housenumber in interpolation lines table', $aPlaceLine);
113                                         if ($aPlace['rank_search'] == 30)
114                                         {
115                                                 // if a house was already found in placex, we have to find out, 
116                                                 // if the placex house or the interpolated house are closer to the searched point
117                                                 // distance between point and placex house
118                                                 $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry) as distance FROM placex as house WHERE house.place_id='.$iPlaceID;
119                                                 $aDistancePlacex = chksql($this->oDB->getRow($sSQL),
120                                                                           "Could not determine distance between searched point and placex house.");
121                                                 $fDistancePlacex = $aDistancePlacex['distance'];
122                                                 // distance between point and interpolated house (fraction on interpolation line)
123                                                 $sSQL = 'SELECT ST_distance('.$sPointSQL.', ST_LineInterpolatePoint(linegeo, '.$aPlaceLine['fraction'].')) as distance';
124                                                 $sSQL .= ' FROM location_property_osmline WHERE place_id = '.$aPlaceLine['place_id'];
125                                                 $aDistanceInterpolation = chksql($this->oDB->getRow($sSQL),
126                                                                                  "Could not determine distance between searched point and interpolated house.");
127                                                 $fDistanceInterpolation = $aDistanceInterpolation['distance'];
128                                                 if ($fDistanceInterpolation < $fDistancePlacex)
129                                                 {
130                                                         // interpolation is closer to point than placex house
131                                                         $bPlaceIsLine = true;
132                                                         $aPlace = $aPlaceLine;
133                                                         $iPlaceID = $aPlaceLine['place_id'];
134                                                         $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
135                                                         $fFraction = $aPlaceLine['fraction'];
136                                                         $iMaxRank = 30;
137                                                 }
138                                                 // else: nothing to do, take placex house from above
139                                         }
140                                         else
141                                         {
142                                                 $bPlaceIsLine = true;
143                                                 $aPlace = $aPlaceLine;
144                                                 $iPlaceID = $aPlaceLine['place_id'];
145                                                 $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
146                                                 $fFraction = $aPlaceLine['fraction'];
147                                                 $iMaxRank = 30;
148                                         }
149                                 }
150                         }
151                         
152                         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
153                         if (CONST_Use_US_Tiger_Data && $bDoInterpolation && $bIsInUnitedStates && $this->iMaxRank >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 ))
154                         {
155                                 $fSearchDiam = 0.001;
156                                 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
157                                 //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
158                                 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
159                                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';  //no centroid anymore in Tiger data, now we have lines
160                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
161
162                                 if (CONST_Debug)
163                                 {
164                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
165                                         var_dump($sSQL);
166
167                                         $aAllHouses = chksql($this->oDB->getAll($sSQL));
168                                         foreach($aAllHouses as $i)
169                                         {
170                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
171                                         }
172                                 }
173
174                                 $aPlaceTiger = chksql($this->oDB->getRow($sSQL),
175                                                       "Could not determine closest Tiger place.");
176                                 if ($aPlaceTiger)
177                                 {
178                                         if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
179                                         $bPlaceIsTiger = true;
180                                         $aPlace = $aPlaceTiger;
181                                         $iPlaceID = $aPlaceTiger['place_id'];
182                                         $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
183                                         $fFraction = $aPlaceTiger['fraction'];
184                                         $iMaxRank = 30;
185                                 }
186                         }
187
188                         // The point we found might be too small - use the address to find what it is a child of
189                         if ($iPlaceID && $iMaxRank < 28)
190                         {
191                                 if (($aPlace['rank_search'] > 28 || $bPlaceIsTiger || $bPlaceIsLine) && $iParentPlaceID)
192                                 {
193                                         $iPlaceID = $iParentPlaceID;
194                                 }
195                                 $sSQL  = 'select address_place_id';
196                                 $sSQL .= ' FROM place_addressline';
197                                 $sSQL .= " WHERE place_id = $iPlaceID";
198                                 $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
199                                 $sSQL .= ' LIMIT 1';
200                                 $iPlaceID = chksql($this->oDB->getOne($sSQL),
201                                                    "Could not get parent for place.");
202                                 if (!$iPlaceID)
203                                 {
204                                         $iPlaceID = $aPlace['place_id'];
205                                 }
206                         }
207                         return array('place_id' => $iPlaceID,
208                                                 'type' => $bPlaceIsTiger ? 'tiger' : ($bPlaceIsLine ? 'interpolation' : 'osm'),
209                                                 'fraction' => ($bPlaceIsTiger || $bPlaceIsLine) ? $fFraction : -1);
210                 }
211                 
212         }
213 ?>