- $iPlaceID = $aPlace['place_id'];
- $iParentPlaceID = $aPlace['parent_place_id'];
- $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
- }
-
- // If a house was found make sure there isn't an interpolation line
- // that is closer
- if ($bDoInterpolation && !$bPlaceIsLine && $aPlace && $aPlace['rank_search'] == 30) {
- // get the distance of the house to the search point
- $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry)';
- $sSQL .= ' FROM placex as house WHERE house.place_id='.$iPlaceID;
-
- $fDistancePlacex = chksql(
- $this->oDB->getOne($sSQL),
- "Could not determine distance between searched point and placex house."
- );
-
- // look for an interpolation that is closer
- $aPlaceLine = $this->lookupInterpolation($sPointSQL, $fDistancePlacex);
-
- if ($aPlaceLine) {
- // interpolation is closer to point than placex house
- $bPlaceIsLine = true;
- $aPlace = $aPlaceLine;
- $iPlaceID = $aPlaceLine['place_id'];
- $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
- $fFraction = $aPlaceLine['fraction'];
- }
- }
-
- // Only street found? If it's in the US we can check TIGER data for nearest housenumber
- if (CONST_Use_US_Tiger_Data && $bDoInterpolation && $bIsInUnitedStates && $this->iMaxRank >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) {
- $fSearchDiam = 0.001;
- $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction';
- //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
- $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
- $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')'; //no centroid anymore in Tiger data, now we have lines
- $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
-
- if (CONST_Debug) {
- $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
- var_dump($sSQL);
-
- $aAllHouses = chksql($this->oDB->getAll($sSQL));
- foreach ($aAllHouses as $i) {
- echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
+
+ if ($aPlace) {
+ $iPlaceID = $aPlace['place_id'];
+ $oResult = new Result($iPlaceID);
+ $iParentPlaceID = $aPlace['parent_place_id'];
+ // if street and maxrank > streetlevel
+ if (($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27)&& $iMaxRank > 27 ) {
+ // find the closest object (up to a certain radius) of which the street is a parent of
+ $sSQL = ' select place_id,parent_place_id,rank_search,country_code,';
+ $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
+ $sSQL .= ' FROM ';
+ $sSQL .= ' placex';
+ // radius ?
+ $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.003)';
+ $sSQL .= ' AND parent_place_id = '.$iPlaceID;
+ $sSQL .= ' and (name is not null or housenumber is not null)';
+ $sSQL .= ' ORDER BY distance ASC limit 1';
+ if (CONST_Debug) var_dump($sSQL);
+ $aStreet = chksql(
+ $this->oDB->getRow($sSQL),
+ 'Could not determine closest place.'
+ );
+ if ($aStreet) {
+ $iPlaceID = $aStreet['place_id'];
+ $oResult = new Result($iPlaceID);
+ $iParentPlaceID = $aStreet['parent_place_id'];
+ }
+ }
+ }else{
+ $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
+ if ($aPlace) {
+ // if place node is found adress goes over linked_place_id
+ if (!empty($aPlace['linked_place_id'])) {
+ $oResult = new Result($aPlace['linked_place_id']);
+ }else{
+ $oResult = new Result($aPlace['place_id']);
+ }
+ }
+ }
+ // lower than street level ($iMaxRank < 26 )
+ }else{
+ $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
+ if ($aPlace) {
+ // if place node is found adress goes over linked_place_id
+ if (!empty($aPlace['linked_place_id'])) {
+ $oResult = new Result($aPlace['linked_place_id']);
+ }else{
+ $oResult = new Result($aPlace['place_id']);
+ }