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;
 
  47      * returns { place_id =>, type => '(osm|tiger)' }
 
  48      * fails if no place was found
 
  52     public function lookup($fLat, $fLon, $bDoInterpolation = true)
 
  54         $sPointSQL = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
 
  55         $iMaxRank = $this->iMaxRank;
 
  57         // Find the nearest point
 
  58         $fSearchDiam = 0.0004;
 
  61         $fMaxAreaDistance = 1;
 
  62         $bIsInUnitedStates = false;
 
  63         $bPlaceIsTiger = false;
 
  64         $bPlaceIsLine = false;
 
  65         while (!$iPlaceID && $fSearchDiam < $fMaxAreaDistance) {
 
  66             $fSearchDiam = $fSearchDiam * 2;
 
  68             // If we have to expand the search area by a large amount then we need a larger feature
 
  69             // then there is a limit to how small the feature should be
 
  70             if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
 
  71             if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
 
  72             if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
 
  73             if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
 
  74             if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
 
  75             if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
 
  76             if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
 
  77             if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
 
  79             $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code';
 
  80             $sSQL .= ' FROM placex';
 
  81             $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
  82             $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
 
  83             $sSQL .= ' and (name is not null or housenumber is not null)';
 
  84             $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
  85             $sSQL .= ' and indexed_status = 0 ';
 
  86             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
  87             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
  88             $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
 
  89             if (CONST_Debug) var_dump($sSQL);
 
  91                 $this->oDB->getRow($sSQL),
 
  92                 "Could not determine closest place."
 
  94             $iPlaceID = $aPlace['place_id'];
 
  95             $iParentPlaceID = $aPlace['parent_place_id'];
 
  96             $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
 
  98         // if a street or house was found, look in interpolation lines table
 
  99         if ($bDoInterpolation && $this->iMaxRank >= 28 && $aPlace && $aPlace['rank_search'] >= 26) {
 
 100             // if a house was found, search the interpolation line that is at least as close as the house
 
 101             $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
 
 102             $sSQL .= ' FROM location_property_osmline';
 
 103             $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
 
 104             $sSQL .= ' and indexed_status = 0 ';
 
 105             $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
 
 108                 $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
 
 111                 $aAllHouses = chksql($this->oDB->getAll($sSQL));
 
 112                 foreach ($aAllHouses as $i) {
 
 113                     echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
 
 116             $aPlaceLine = chksql(
 
 117                 $this->oDB->getRow($sSQL),
 
 118                 "Could not determine closest housenumber on an osm interpolation line."
 
 121                 if (CONST_Debug) var_dump('found housenumber in interpolation lines table', $aPlaceLine);
 
 122                 if ($aPlace['rank_search'] == 30) {
 
 123                     // if a house was already found in placex, we have to find out,
 
 124                     // if the placex house or the interpolated house are closer to the searched point
 
 125                     // distance between point and placex house
 
 126                     $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry) as distance FROM placex as house WHERE house.place_id='.$iPlaceID;
 
 127                     $aDistancePlacex = chksql(
 
 128                         $this->oDB->getRow($sSQL),
 
 129                         "Could not determine distance between searched point and placex house."
 
 131                     $fDistancePlacex = $aDistancePlacex['distance'];
 
 132                     // distance between point and interpolated house (fraction on interpolation line)
 
 133                     $sSQL = 'SELECT ST_distance('.$sPointSQL.', ST_LineInterpolatePoint(linegeo, '.$aPlaceLine['fraction'].')) as distance';
 
 134                     $sSQL .= ' FROM location_property_osmline WHERE place_id = '.$aPlaceLine['place_id'];
 
 135                     $aDistanceInterpolation = chksql(
 
 136                         $this->oDB->getRow($sSQL),
 
 137                         "Could not determine distance between searched point and interpolated house."
 
 139                     $fDistanceInterpolation = $aDistanceInterpolation['distance'];
 
 140                     if ($fDistanceInterpolation < $fDistancePlacex) {
 
 141                         // interpolation is closer to point than placex house
 
 142                         $bPlaceIsLine = true;
 
 143                         $aPlace = $aPlaceLine;
 
 144                         $iPlaceID = $aPlaceLine['place_id'];
 
 145                         $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
 
 146                         $fFraction = $aPlaceLine['fraction'];
 
 149                     // else: nothing to do, take placex house from above
 
 151                     $bPlaceIsLine = true;
 
 152                     $aPlace = $aPlaceLine;
 
 153                     $iPlaceID = $aPlaceLine['place_id'];
 
 154                     $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
 
 155                     $fFraction = $aPlaceLine['fraction'];
 
 161         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
 
 162         if (CONST_Use_US_Tiger_Data && $bDoInterpolation && $bIsInUnitedStates && $this->iMaxRank >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) {
 
 163             $fSearchDiam = 0.001;
 
 164             $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
 
 165             //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
 
 166             $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
 
 167             $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';  //no centroid anymore in Tiger data, now we have lines
 
 168             $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
 
 171                 $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
 
 174                 $aAllHouses = chksql($this->oDB->getAll($sSQL));
 
 175                 foreach ($aAllHouses as $i) {
 
 176                     echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
 
 180             $aPlaceTiger = chksql(
 
 181                 $this->oDB->getRow($sSQL),
 
 182                 "Could not determine closest Tiger place."
 
 185                 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
 
 186                 $bPlaceIsTiger = true;
 
 187                 $aPlace = $aPlaceTiger;
 
 188                 $iPlaceID = $aPlaceTiger['place_id'];
 
 189                 $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
 
 190                 $fFraction = $aPlaceTiger['fraction'];
 
 195         // The point we found might be too small - use the address to find what it is a child of
 
 196         if ($iPlaceID && $iMaxRank < 28) {
 
 197             if (($aPlace['rank_search'] > 28 || $bPlaceIsTiger || $bPlaceIsLine) && $iParentPlaceID) {
 
 198                 $iPlaceID = $iParentPlaceID;
 
 200             $sSQL  = 'select address_place_id';
 
 201             $sSQL .= ' FROM place_addressline';
 
 202             $sSQL .= " WHERE place_id = $iPlaceID";
 
 203             $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
 
 205             $iPlaceID = chksql($this->oDB->getOne($sSQL), "Could not get parent for place.");
 
 207                 $iPlaceID = $aPlace['place_id'];
 
 211                 'place_id' => $iPlaceID,
 
 212                 'type' => $bPlaceIsTiger ? 'tiger' : ($bPlaceIsLine ? 'interpolation' : 'osm'),
 
 213                 'fraction' => ($bPlaceIsTiger || $bPlaceIsLine) ? $fFraction : -1