8 protected $iMaxRank = 28;
11 public function __construct(&$oDB)
17 public function setZoom($iZoom)
19 // Zoom to rank, this could probably be calculated but a lookup gives fine control
21 0 => 2, // Continent / Sea
33 12 => 18, // Town / Village
37 16 => 26, // Street, TODO: major street?
39 18 => 30, // or >, Building
40 19 => 30, // or >, Building
42 $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
46 * Find the closest interpolation with the given search diameter.
48 * @param string $sPointSQL Reverse geocoding point as SQL
49 * @param float $fSearchDiam Search diameter
51 * @return Record of the interpolation or null.
53 protected function lookupInterpolation($sPointSQL, $fSearchDiam)
55 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
56 $sSQL .= ' ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction';
57 $sSQL .= ' , ST_Distance(linegeo,'.$sPointSQL.') as distance';
58 $sSQL .= ' FROM location_property_osmline';
59 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
60 $sSQL .= ' and indexed_status = 0 ';
61 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
64 $this->oDB->getRow($sSQL),
65 "Could not determine closest housenumber on an osm interpolation line."
70 * returns { place_id =>, type => '(osm|tiger)' }
71 * fails if no place was found
75 public function lookup($fLat, $fLon, $bDoInterpolation = true)
77 $sPointSQL = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
78 $iMaxRank = $this->iMaxRank;
80 // Find the nearest point
81 $fSearchDiam = 0.0004;
83 $fMaxAreaDistance = 1;
84 $bIsInUnitedStates = false;
85 $bPlaceIsTiger = false;
86 $bPlaceIsLine = false;
87 while (!$iPlaceID && $fSearchDiam < $fMaxAreaDistance) {
88 $fSearchDiam = $fSearchDiam * 2;
90 // If we have to expand the search area by a large amount then we need a larger feature
91 // then there is a limit to how small the feature should be
92 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
93 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
94 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
95 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
96 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
97 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
98 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
99 if ($fSearchDiam > 0.001 && $iMaxRank > 26) {
100 // try with interpolations before continuing
101 if ($bDoInterpolation) {
102 // no house found, try with interpolations
103 $aPlaceLine = $this->lookupInterpolation($sPointSQL, $fSearchDiam/2);
106 // interpolation is closer to point than placex house
107 $bPlaceIsLine = true;
108 $aPlace = $aPlaceLine;
109 $iPlaceID = $aPlaceLine['place_id'];
110 $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
111 $fFraction = $aPlaceLine['fraction'];
117 // no interpolation found, continue search
121 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code';
122 $sSQL .= ' FROM placex';
123 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
124 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
125 $sSQL .= ' and (name is not null or housenumber is not null)';
126 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
127 $sSQL .= ' and indexed_status = 0 ';
128 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
129 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
130 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
131 if (CONST_Debug) var_dump($sSQL);
133 $this->oDB->getRow($sSQL),
134 "Could not determine closest place."
136 $iPlaceID = $aPlace['place_id'];
137 $iParentPlaceID = $aPlace['parent_place_id'];
138 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
141 // If a house was found make sure there isn't an interpolation line
143 if ($bDoInterpolation && !$bPlaceIsLine && $aPlace && $aPlace['rank_search'] == 30) {
144 // get the distance of the house to the search point
145 $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry)';
146 $sSQL .= ' FROM placex as house WHERE house.place_id='.$iPlaceID;
148 $fDistancePlacex = chksql(
149 $this->oDB->getOne($sSQL),
150 "Could not determine distance between searched point and placex house."
153 // look for an interpolation that is closer
154 $aPlaceLine = $this->lookupInterpolation($sPointSQL, $fDistancePlacex);
156 if ($aPlaceLine && (float) $aPlaceLine['distance'] < (float) $fDistancePlacex) {
157 // interpolation is closer to point than placex house
158 $bPlaceIsLine = true;
159 $aPlace = $aPlaceLine;
160 $iPlaceID = $aPlaceLine['place_id'];
161 $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
162 $fFraction = $aPlaceLine['fraction'];
166 // Only street found? If it's in the US we can check TIGER data for nearest housenumber
167 if (CONST_Use_US_Tiger_Data && $bDoInterpolation && $bIsInUnitedStates && $this->iMaxRank >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) {
168 $fSearchDiam = 0.001;
169 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction';
170 //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
171 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
172 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')'; //no centroid anymore in Tiger data, now we have lines
173 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
176 $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
179 $aAllHouses = chksql($this->oDB->getAll($sSQL));
180 foreach ($aAllHouses as $i) {
181 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
185 $aPlaceTiger = chksql(
186 $this->oDB->getRow($sSQL),
187 "Could not determine closest Tiger place."
190 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
191 $bPlaceIsTiger = true;
192 $aPlace = $aPlaceTiger;
193 $iPlaceID = $aPlaceTiger['place_id'];
194 $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
195 $fFraction = $aPlaceTiger['fraction'];
200 // The point we found might be too small - use the address to find what it is a child of
201 if ($iPlaceID && $iMaxRank < 28) {
202 if (($aPlace['rank_search'] > 28 || $bPlaceIsTiger || $bPlaceIsLine) && $iParentPlaceID) {
203 $iPlaceID = $iParentPlaceID;
204 $bPlaceIsLine = false;
205 $bPlaceIsTiger = false;
207 $sSQL = 'select address_place_id';
208 $sSQL .= ' FROM place_addressline';
209 $sSQL .= " WHERE place_id = $iPlaceID";
210 $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
212 $iPlaceID = chksql($this->oDB->getOne($sSQL), "Could not get parent for place.");
214 $iPlaceID = $aPlace['place_id'];
218 'place_id' => $iPlaceID,
219 'type' => $bPlaceIsTiger ? 'tiger' : ($bPlaceIsLine ? 'interpolation' : 'osm'),
220 'fraction' => ($bPlaceIsTiger || $bPlaceIsLine) ? $fFraction : -1