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