improvements for pull request

This commit is contained in:
gemo1011
2018-06-18 16:19:38 +02:00
parent 07eb108a6d
commit 0996fdfb55

View File

@@ -47,8 +47,9 @@ class ReverseGeocode
/** /**
* Find the closest interpolation with the given search diameter. * Find the closest interpolation with the given search diameter.
* *
* @param string $sPointSQL Reverse geocoding point as SQL * @param string $sPointSQL Reverse geocoding point as SQL
* @param float $fSearchDiam Search diameter * @param float $fSearchDiam Search diameter
* @param integer $iParentPlaceID Id of parent object
* *
* @return Record of the interpolation or null. * @return Record of the interpolation or null.
*/ */
@@ -72,6 +73,22 @@ class ReverseGeocode
); );
} }
protected function polygonFunctions($sPointSQL, $iMaxRank)
{
$oResult = null;
$aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
if ($aPlace) {
$oResult = new Result($aPlace['place_id']);
} elseif (!$aPlace && $iMaxRank > 4) {
$aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
if ($aPlace) {
$oResult = new Result($aPlace['place_id']);
}
}
return $oResult;
}
protected function noPolygonFound($sPointSQL, $iMaxRank) protected function noPolygonFound($sPointSQL, $iMaxRank)
{ {
// searches for polygon in table country_osm_grid which contains the searchpoint // searches for polygon in table country_osm_grid which contains the searchpoint
@@ -177,6 +194,7 @@ class ReverseGeocode
} }
return $aPoly; return $aPoly;
} }
public function lookup($fLat, $fLon, $bDoInterpolation = true) public function lookup($fLat, $fLon, $bDoInterpolation = true)
{ {
@@ -212,11 +230,11 @@ class ReverseGeocode
if ($iMaxRank == 26) { if ($iMaxRank == 26) {
$sSQL .= ' rank_address = 26'; $sSQL .= ' rank_address = 26';
} else { } else {
$sSQL .= ' rank_address >= 26'; $sSQL .= ' rank_address between 26 and '.$iMaxRank;
} }
$sSQL .= ' and (name is not null or housenumber is not null'; $sSQL .= ' and (name is not null or housenumber is not null';
$sSQL .= ' or rank_address between 26 and 27)'; $sSQL .= ' or rank_address between 26 and 27)';
$sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')'; $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
$sSQL .= ' and indexed_status = 0 and linked_place_id is null'; $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
$sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') '; $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
$sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))'; $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
@@ -242,8 +260,9 @@ class ReverseGeocode
$oResult->iHouseNumber = closestHouseNumber($aHouse); $oResult->iHouseNumber = closestHouseNumber($aHouse);
} }
} }
// if street and maxrank > streetlevel // if street and maxrank > streetlevel
if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) { if (($aPlace['rank_address'] <=27)&& $iMaxRank > 27) {
// find the closest object (up to a certain radius) of which the street is a parent of // 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_address,country_code,'; $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
$sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance'; $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
@@ -253,9 +272,8 @@ class ReverseGeocode
$sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)'; $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
$sSQL .= ' AND parent_place_id = '.$iPlaceID; $sSQL .= ' AND parent_place_id = '.$iPlaceID;
$sSQL .= ' and rank_address != 28'; $sSQL .= ' and rank_address != 28';
$sSQL .= ' and (name is not null or housenumber is not null'; $sSQL .= ' and (name is not null or housenumber is not null)';
$sSQL .= ' or rank_address between 26 and 27)'; $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
$sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
$sSQL .= ' and indexed_status = 0 and linked_place_id is null'; $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
$sSQL .= ' ORDER BY distance ASC limit 1'; $sSQL .= ' ORDER BY distance ASC limit 1';
if (CONST_Debug) var_dump($sSQL); if (CONST_Debug) var_dump($sSQL);
@@ -279,29 +297,36 @@ class ReverseGeocode
} }
} }
} }
// if no POI or street is found ...
} else { // In the US we can check TIGER data for nearest housenumber
$aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank); if (CONST_Use_US_Tiger_Data && $aPlace['country_code'] == 'us' && $this->iMaxRank >= 28) {
if ($aPlace) { $fSearchDiam = $aPlace['rank_search'] > 28 ? $aPlace['distance'] : 0.001;
$oResult = new Result($aPlace['place_id']); $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
} elseif (!$aPlace && $iMaxRank > 4) { $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
$aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank); $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
if ($aPlace) { $sSQL .= 'startnumber,endnumber,interpolationtype';
$oResult = new Result($aPlace['place_id']); $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
$sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
$sSQL .= ' ORDER BY distance ASC limit 1';
if (CONST_Debug) var_dump($sSQL);
$aPlaceTiger = chksql(
$this->oDB->getRow($sSQL),
'Could not determine closest Tiger place.'
);
if ($aPlaceTiger) {
if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
$aPlace = $aPlaceTiger;
$oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
$oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
} }
} }
// if no POI or street is found ...
} else {
$oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
} }
// lower than street level ($iMaxRank < 26 ) // lower than street level ($iMaxRank < 26 )
} else { } else {
$aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank); $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
if ($aPlace) {
$oResult = new Result($aPlace['place_id']);
} elseif (!$aPlace && $iMaxRank > 4) {
$aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
if ($aPlace) {
$oResult = new Result($aPlace['place_id']);
}
}
} }
return $oResult; return $oResult;
} }