From 282c6777ee11360b1b5bd8e60196d81f90480c79 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Mon, 23 Oct 2017 23:28:00 +0200 Subject: [PATCH] use PlaceLookup::loadParamArray in search and lookup --- lib/Geocode.php | 94 ++++++++----------------------------------- lib/PlaceLookup.php | 97 ++++++++++++++++++++++----------------------- website/lookup.php | 5 +-- website/reverse.php | 1 - website/search.php | 32 +-------------- 5 files changed, 68 insertions(+), 161 deletions(-) diff --git a/lib/Geocode.php b/lib/Geocode.php index bf359caf..ae518d51 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -12,21 +12,13 @@ class Geocode { protected $oDB; + protected $oPlaceLookup; + protected $aLangPrefOrder = array(); protected $bIncludeAddressDetails = false; - protected $bIncludeExtraTags = false; - protected $bIncludeNameDetails = false; - - protected $bIncludePolygonAsPoints = false; - protected $bIncludePolygonAsText = false; - protected $bIncludePolygonAsGeoJSON = false; - protected $bIncludePolygonAsKML = false; - protected $bIncludePolygonAsSVG = false; - protected $fPolygonSimplificationThreshold = 0.0; protected $aExcludePlaceIDs = array(); - protected $bDeDupe = true; protected $bReverseInPlan = false; protected $iLimit = 20; @@ -57,6 +49,7 @@ class Geocode public function __construct(&$oDB) { $this->oDB =& $oDB; + $this->oPlaceLookup = new PlaceLookup($this->oDB); $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules); } @@ -87,26 +80,14 @@ class Geocode $aParams = array('q' => $this->sQuery); } + $aParams = array_merge($aParams, $this->oPlaceLookup->getMoreUrlParams()); + if ($this->aExcludePlaceIDs) { $aParams['exclude_place_ids'] = implode(',', $this->aExcludePlaceIDs); } if ($this->bIncludeAddressDetails) $aParams['addressdetails'] = '1'; - if ($this->bIncludeExtraTags) $aParams['extratags'] = '1'; - if ($this->bIncludeNameDetails) $aParams['namedetails'] = '1'; - - if ($this->bIncludePolygonAsPoints) $aParams['polygon'] = '1'; - if ($this->bIncludePolygonAsText) $aParams['polygon_text'] = '1'; - if ($this->bIncludePolygonAsGeoJSON) $aParams['polygon_geojson'] = '1'; - if ($this->bIncludePolygonAsKML) $aParams['polygon_kml'] = '1'; - if ($this->bIncludePolygonAsSVG) $aParams['polygon_svg'] = '1'; - - if ($this->fPolygonSimplificationThreshold > 0.0) { - $aParams['polygon_threshold'] = $this->fPolygonSimplificationThreshold; - } - if ($this->bBoundedSearch) $aParams['bounded'] = '1'; - if (!$this->bDeDupe) $aParams['dedupe'] = '0'; if ($this->aCountryCodes) { $aParams['countrycodes'] = implode(',', $this->aCountryCodes); @@ -119,36 +100,6 @@ class Geocode return $aParams; } - public function setIncludePolygonAsPoints($b = true) - { - $this->bIncludePolygonAsPoints = $b; - } - - public function setIncludePolygonAsText($b = true) - { - $this->bIncludePolygonAsText = $b; - } - - public function setIncludePolygonAsGeoJSON($b = true) - { - $this->bIncludePolygonAsGeoJSON = $b; - } - - public function setIncludePolygonAsKML($b = true) - { - $this->bIncludePolygonAsKML = $b; - } - - public function setIncludePolygonAsSVG($b = true) - { - $this->bIncludePolygonAsSVG = $b; - } - - public function setPolygonSimplificationThreshold($f) - { - $this->fPolygonSimplificationThreshold = $f; - } - public function setLimit($iLimit = 10) { if ($iLimit > 50) $iLimit = 50; @@ -229,17 +180,12 @@ class Geocode } - public function loadParamArray($oParams) + public function loadParamArray($oParams, $sForceGeometryType = null) { $this->bIncludeAddressDetails = $oParams->getBool('addressdetails', $this->bIncludeAddressDetails); - $this->bIncludeExtraTags - = $oParams->getBool('extratags', $this->bIncludeExtraTags); - $this->bIncludeNameDetails - = $oParams->getBool('namedetails', $this->bIncludeNameDetails); $this->bBoundedSearch = $oParams->getBool('bounded', $this->bBoundedSearch); - $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe); $this->setLimit($oParams->getInt('limit', $this->iFinalLimit)); $this->iOffset = $oParams->getInt('offset', $this->iOffset); @@ -298,6 +244,10 @@ class Geocode } } } + + $this->oPlaceLookup->loadParamArray($oParams, $sForceGeometryType); + $this->oPlaceLookup->setIncludeAddressDetails(false); + $this->oPlaceLookup->setIncludePolygonAsPoints($oParams->getBool('polygon')); } public function setQueryFromParams($oParams) @@ -906,26 +856,16 @@ class Geocode return array(); } - $oPlaceLookup = new PlaceLookup($this->oDB); - $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints); - $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText); - $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON); - $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML); - $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG); - $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold); - $oPlaceLookup->setDeDupe($this->bDeDupe); if ($this->aAddressRankList) { - $oPlaceLookup->setAddressRankList($this->aAddressRankList); + $this->oPlaceLookup->setAddressRankList($this->aAddressRankList); } - $oPlaceLookup->setAllowedTypesSQLList($this->sAllowedTypesSQLList); - $oPlaceLookup->setLanguagePreference($this->aLangPrefOrder); - $oPlaceLookup->setIncludeExtraTags($this->bIncludeExtraTags); - $oPlaceLookup->setIncludeNameDetails($this->bIncludeNameDetails); + $this->oPlaceLookup->setAllowedTypesSQLList($this->sAllowedTypesSQLList); + $this->oPlaceLookup->setLanguagePreference($this->aLangPrefOrder); if ($oCtx->hasNearPoint()) { - $oPlaceLookup->setAnchorSql($oCtx->sqlNear); + $this->oPlaceLookup->setAnchorSql($oCtx->sqlNear); } - $aSearchResults = $oPlaceLookup->lookup($aResults); + $aSearchResults = $this->oPlaceLookup->lookup($aResults); $aClassType = getClassTypesWithImportance(); $aRecheckWords = preg_split('/\b[\s,\\-]*/u', $sQuery); @@ -942,7 +882,7 @@ class Geocode // Default $fDiameter = getResultDiameter($aResult); - $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2); + $aOutlineResult = $this->oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2); if ($aOutlineResult) { $aResult = array_merge($aResult, $aOutlineResult); } @@ -1036,7 +976,7 @@ class Geocode if (isset($aResult['zoom'])) $iZoom = $aResult['zoom']; $bFirst = false; } - if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']]) + if (!$this->oPlaceLookup->doDeDupe() || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']]) && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])) ) { $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true; diff --git a/lib/PlaceLookup.php b/lib/PlaceLookup.php index c6a799e5..a48f5597 100644 --- a/lib/PlaceLookup.php +++ b/lib/PlaceLookup.php @@ -32,19 +32,40 @@ class PlaceLookup $this->oDB =& $oDB; } - public function loadParamArray($oParams) + public function doDeDupe() + { + return $this->bDeDupe; + } + + public function setIncludePolygonAsPoints($b = true) + { + $this->bIncludePolygonAsPoints = $b; + } + + public function loadParamArray($oParams, $sGeomType = null) { $aLangs = $oParams->getPreferredLanguages(); $this->aLangPrefOrderSql = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangs)).']'; + $this->bAddressDetails = $oParams->getBool('addressdetails', true); $this->bExtraTags = $oParams->getBool('extratags', false); $this->bNameDetails = $oParams->getBool('namedetails', false); - $this->bIncludePolygonAsText = $oParams->getBool('polygon_text'); - $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson'); - $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml'); - $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg'); + $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe); + + if ($sGeomType === null || $sGeomType == 'text') { + $this->bIncludePolygonAsText = $oParams->getBool('polygon_text'); + } + if ($sGeomType === null || $sGeomType == 'geojson') { + $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson'); + } + if ($sGeomType === null || $sGeomType == 'kml') { + $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml'); + } + if ($sGeomType === null || $sGeomType == 'svg') { + $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg'); + } $this->fPolygonSimplificationThreshold = $oParams->getFloat('polygon_threshold', 0.0); @@ -62,14 +83,32 @@ class PlaceLookup } } - public function setAnchorSql($sPoint) + public function getMoreUrlParams() { - $this->sAnchorSql = $sPoint; + $aParams = array(); + + if ($this->bAddressDetails) $aParams['addressdetails'] = '1'; + if ($this->bExtraTags) $aParams['extratags'] = '1'; + if ($this->bNameDetails) $aParams['namedetails'] = '1'; + + if ($this->bIncludePolygonAsPoints) $aParams['polygon'] = '1'; + if ($this->bIncludePolygonAsText) $aParams['polygon_text'] = '1'; + if ($this->bIncludePolygonAsGeoJSON) $aParams['polygon_geojson'] = '1'; + if ($this->bIncludePolygonAsKML) $aParams['polygon_kml'] = '1'; + if ($this->bIncludePolygonAsSVG) $aParams['polygon_svg'] = '1'; + + if ($this->fPolygonSimplificationThreshold > 0.0) { + $aParams['polygon_threshold'] = $this->fPolygonSimplificationThreshold; + } + + if (!$this->bDeDupe) $aParams['dedupe'] = '0'; + + return $aParams; } - public function setDeDupe($bDeDupe) + public function setAnchorSql($sPoint) { - $this->bDeDupe = $bDeDupe; + $this->sAnchorSql = $sPoint; } public function setAddressRankList($aList) @@ -93,46 +132,6 @@ class PlaceLookup $this->bAddressDetails = $bAddressDetails; } - public function setIncludeExtraTags($bExtraTags = false) - { - $this->bExtraTags = $bExtraTags; - } - - public function setIncludeNameDetails($bNameDetails = false) - { - $this->bNameDetails = $bNameDetails; - } - - public function setIncludePolygonAsPoints($b = true) - { - $this->bIncludePolygonAsPoints = $b; - } - - public function setIncludePolygonAsText($b = true) - { - $this->bIncludePolygonAsText = $b; - } - - public function setIncludePolygonAsGeoJSON($b = true) - { - $this->bIncludePolygonAsGeoJSON = $b; - } - - public function setIncludePolygonAsKML($b = true) - { - $this->bIncludePolygonAsKML = $b; - } - - public function setIncludePolygonAsSVG($b = true) - { - $this->bIncludePolygonAsSVG = $b; - } - - public function setPolygonSimplificationThreshold($f) - { - $this->fPolygonSimplificationThreshold = $f; - } - private function addressImportanceSql($sGeometry, $sPlaceId) { if ($this->sAnchorSql) { diff --git a/website/lookup.php b/website/lookup.php index 667686d1..578d4c37 100755 --- a/website/lookup.php +++ b/website/lookup.php @@ -24,10 +24,7 @@ $aSearchResults = array(); $aCleanedQueryParts = array(); $oPlaceLookup = new Nominatim\PlaceLookup($oDB); -$oPlaceLookup->setLanguagePreference($aLangPrefOrder); -$oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true)); -$oPlaceLookup->setIncludeExtraTags($oParams->getBool('extratags', false)); -$oPlaceLookup->setIncludeNameDetails($oParams->getBool('namedetails', false)); +$oPlaceLookup->loadParamArray($oParams); $aOsmIds = explode(',', $oParams->getString('osm_ids', '')); diff --git a/website/reverse.php b/website/reverse.php index 269229d3..85ca1981 100755 --- a/website/reverse.php +++ b/website/reverse.php @@ -23,7 +23,6 @@ $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder); $oPlaceLookup = new Nominatim\PlaceLookup($oDB); $oPlaceLookup->loadParamArray($oParams); -$oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true)); $sOsmType = $oParams->getSet('osm_type', array('N', 'W', 'R')); $iOsmId = $oParams->getInt('osm_id', -1); diff --git a/website/search.php b/website/search.php index 4952465e..bf9695ab 100755 --- a/website/search.php +++ b/website/search.php @@ -28,36 +28,8 @@ if (CONST_Search_ReversePlanForAll // Format for output $sOutputFormat = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html'); -// Show / use polygons -if ($sOutputFormat == 'html') { - $oGeocode->setIncludePolygonAsGeoJSON($oParams->getBool('polygon_geojson')); - $bAsGeoJSON = false; -} else { - $bAsPoints = $oParams->getBool('polygon'); - $bAsGeoJSON = $oParams->getBool('polygon_geojson'); - $bAsKML = $oParams->getBool('polygon_kml'); - $bAsSVG = $oParams->getBool('polygon_svg'); - $bAsText = $oParams->getBool('polygon_text'); - $iWantedTypes = ($bAsGeoJSON?1:0) + ($bAsKML?1:0) + ($bAsSVG?1:0) + ($bAsText?1:0) + ($bAsPoints?1:0); - if ($iWantedTypes > CONST_PolygonOutput_MaximumTypes) { - if (CONST_PolygonOutput_MaximumTypes) { - userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option"); - } else { - userError("Polygon output is disabled"); - } - exit; - } - $oGeocode->setIncludePolygonAsPoints($bAsPoints); - $oGeocode->setIncludePolygonAsText($bAsText); - $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON); - $oGeocode->setIncludePolygonAsKML($bAsKML); - $oGeocode->setIncludePolygonAsSVG($bAsSVG); -} - -// Polygon simplification threshold (optional) -$oGeocode->setPolygonSimplificationThreshold($oParams->getFloat('polygon_threshold', 0.0)); - -$oGeocode->loadParamArray($oParams); +$sForcedGeometry = ($sOutputFormat == 'html') ? "geojson" : null; +$oGeocode->loadParamArray($oParams, $sForcedGeometry); if (CONST_Search_BatchMode && isset($_GET['batch'])) { $aBatch = json_decode($_GET['batch'], true); -- 2.45.1