8 protected $iMaxRank = 28;
10 protected $aLangPrefOrder = array();
12 protected $bIncludePolygonAsPoints = false;
13 protected $bIncludePolygonAsText = false;
14 protected $bIncludePolygonAsGeoJSON = false;
15 protected $bIncludePolygonAsKML = false;
16 protected $bIncludePolygonAsSVG = false;
17 protected $fPolygonSimplificationThreshold = 0.0;
20 function ReverseGeocode(&$oDB)
25 function setLanguagePreference($aLangPref)
27 $this->aLangPrefOrder = $aLangPref;
30 function setLatLon($fLat, $fLon)
32 $this->fLat = (float)$fLat;
33 $this->fLon = (float)$fLon;
36 function setRank($iRank)
38 $this->iMaxRank = $iRank;
41 function setZoom($iZoom)
43 // Zoom to rank, this could probably be calculated but a lookup gives fine control
45 0 => 2, // Continent / Sea
57 12 => 18, // Town / Village
61 16 => 26, // Street, TODO: major street?
63 18 => 30, // or >, Building
64 19 => 30, // or >, Building
66 $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
69 function setIncludePolygonAsPoints($b = true)
71 $this->bIncludePolygonAsPoints = $b;
74 function getIncludePolygonAsPoints()
76 return $this->bIncludePolygonAsPoints;
79 function setIncludePolygonAsText($b = true)
81 $this->bIncludePolygonAsText = $b;
84 function getIncludePolygonAsText()
86 return $this->bIncludePolygonAsText;
89 function setIncludePolygonAsGeoJSON($b = true)
91 $this->bIncludePolygonAsGeoJSON = $b;
94 function setIncludePolygonAsKML($b = true)
96 $this->bIncludePolygonAsKML = $b;
99 function setIncludePolygonAsSVG($b = true)
101 $this->bIncludePolygonAsSVG = $b;
104 function setPolygonSimplificationThreshold($f)
106 $this->fPolygonSimplificationThreshold = $f;
109 // returns { place_id =>, type => '(osm|tiger)' }
110 // fails if no place was found
113 $sPointSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
114 $iMaxRank = $this->iMaxRank;
115 $iMaxRank_orig = $this->iMaxRank;
117 // Find the nearest point
118 $fSearchDiam = 0.0004;
121 $fMaxAreaDistance = 1;
122 $bIsInUnitedStates = false;
123 $bPlaceIsTiger = false;
124 $bPlaceIsLine = false;
125 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
127 $fSearchDiam = $fSearchDiam * 2;
129 // If we have to expand the search area by a large amount then we need a larger feature
130 // then there is a limit to how small the feature should be
131 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
132 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
133 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
134 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
135 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
136 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
137 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
138 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
140 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code';
141 $sSQL .= ' FROM placex';
142 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
143 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
144 $sSQL .= ' and (name is not null or housenumber is not null)';
145 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
146 $sSQL .= ' and indexed_status = 0 ';
147 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
148 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
149 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
150 if (CONST_Debug) var_dump($sSQL);
151 $aPlace = $this->oDB->getRow($sSQL);
152 if (PEAR::IsError($aPlace))
154 failInternalError("Could not determine closest place.", $sSQL, $aPlace);
156 $iPlaceID = $aPlace['place_id'];
157 $iParentPlaceID = $aPlace['parent_place_id'];
158 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
160 // if a street or house was found, look in interpolation lines table
161 if ($iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 || $aPlace['rank_search'] == 30))
163 $fSearchDiam = 0.001;
164 if ($aPlace['rank_search'] == 30)
166 // if a house was found, the closest road needs to be searched, to use its place id as parent_place_id for the interpolation line search
167 // because a road can be closer to the point than the house from above
169 while(!$iRoadID && $fSearchDiam < $fMaxAreaDistance)
171 $fSearchDiam = $fSearchDiam * 2;
172 $sSQL = 'select place_id ';
173 $sSQL .= ' FROM placex';
174 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
175 $sSQL .= ' and (rank_search = 26 or rank_search = 27)';
176 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
177 $sSQL .= ' and indexed_status = 0 ';
178 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
179 $aPlaceRoad = $this->oDB->getRow($sSQL);
180 if (PEAR::IsError($aPlace))
182 failInternalError("Could not determine closest place.", $sSQL, $aPlace);
184 $iRoadID = $aPlaceRoad['place_id'];
185 $iTempPlaceID = $iRoadID;
190 // if a street was found, we can take its place_id as parent_place_id
191 $iTempPlaceID = $iPlaceID;
193 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
194 //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
195 $sSQL .= ' FROM location_property_osmline WHERE parent_place_id = '.$iTempPlaceID;
196 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.') AND indexed_status = 0';
197 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
200 $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
203 $aAllHouses = $this->oDB->getAll($sSQL);
204 foreach($aAllHouses as $i)
206 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
209 $aPlaceLine = $this->oDB->getRow($sSQL);
210 if (PEAR::IsError($aPlaceLine))
212 failInternalError("Could not determine closest housenumber on an osm interpolation line.", $sSQL, $aPlaceLine);
214 $iInterpolationLinePlaceID = $aPlaceLine['place_id'];
217 if (CONST_Debug) var_dump('found housenumber in interpolation lines table', $aPlaceLine);
218 if ($aPlace['rank_search'] == 30)
220 // if a house was already found in placex, we have to find out,
221 // if the placex house or the interpolated house are closer to the searched point
222 // distance between point and placex house
223 $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry) as distance FROM placex as house WHERE house.place_id='.$iPlaceID;
224 $aDistancePlacex = $this->oDB->getRow($sSQL);
225 if (PEAR::IsError($aDistancePlacex))
227 failInternalError("Could not determine distance between searched point and placex house.", $sSQL, $aDistancePlacex);
229 $fDistancePlacex = $aDistancePlacex['distance'];
230 // distance between point and interpolated house (fraction on interpolation line)
231 $sSQL = 'SELECT ST_distance('.$sPointSQL.', ST_LineInterpolatePoint(linegeo, '.$aPlaceLine['fraction'].')) as distance';
232 $sSQL .= ' FROM location_property_osmline WHERE place_id = '.$iInterpolationLinePlaceID;
233 $aDistanceInterpolation = $this->oDB->getRow($sSQL);
234 if (PEAR::IsError($aDistanceInterpolation))
236 failInternalError("Could not determine distance between searched point and interpolated house.", $sSQL, $aDistanceInterpolation);
238 $fDistanceInterpolation = $aDistanceInterpolation['distance'];
239 if ($fDistanceInterpolation < $fDistancePlacex)
241 // interpolation is closer to point than placex house
242 $bPlaceIsLine = true;
243 $aPlace = $aPlaceLine;
244 $iPlaceID = $iInterpolationLinePlaceID;
245 $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
246 $fFraction = $aPlaceLine['fraction'];
248 // else: nothing to do, take placex house from above
252 $bPlaceIsLine = true;
253 $aPlace = $aPlaceLine;
254 $iPlaceID = $aPlaceLine['place_id'];
255 $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
256 $fFraction = $aPlaceLine['fraction'];
261 // Only street found? If it's in the US we can check TIGER data for nearest housenumber
262 if (CONST_Use_US_Tiger_Data && $bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 ))
264 $fSearchDiam = 0.001;
265 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
266 //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
267 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
268 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')'; //no centroid anymore in Tiger data, now we have lines
269 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
273 $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
276 $aAllHouses = $this->oDB->getAll($sSQL);
277 foreach($aAllHouses as $i)
279 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
283 $aPlaceTiger = $this->oDB->getRow($sSQL);
284 if (PEAR::IsError($aPlaceTiger))
286 failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
290 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
291 $bPlaceIsTiger = true;
292 $aPlace = $aPlaceTiger;
293 $iPlaceID = $aPlaceTiger['place_id'];
294 $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
295 $fFraction = $aPlaceTiger['fraction'];
299 // The point we found might be too small - use the address to find what it is a child of
300 if ($iPlaceID && $iMaxRank < 28)
302 if (($aPlace['rank_search'] > 28 || $bPlaceIsTiger || $bPlaceIsLine) && $iParentPlaceID)
304 $iPlaceID = $iParentPlaceID;
306 $sSQL = 'select address_place_id';
307 $sSQL .= ' FROM place_addressline';
308 $sSQL .= " WHERE place_id = $iPlaceID";
309 $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
311 $iPlaceID = $this->oDB->getOne($sSQL);
312 if (PEAR::IsError($iPlaceID))
314 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
318 $iPlaceID = $aPlace['place_id'];
321 return array('place_id' => $iPlaceID,
322 'type' => $bPlaceIsTiger ? 'tiger' : $bPlaceIsLine ? 'interpolation' : 'osm',
323 'fraction' => ($bPlaceIsTiger || $bPlaceIsLine) ? $fFraction : -1);