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