X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/3e871d6ee0212f20fc3f528b3957616b72bddad2..d3ff9600b5e345239f4dc1b5d1b7244f5a25d051:/lib/Geocode.php diff --git a/lib/Geocode.php b/lib/Geocode.php index bb067046..a6ff279c 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -6,6 +6,8 @@ protected $aLangPrefOrder = array(); protected $bIncludeAddressDetails = false; + protected $bIncludeExtraTags = false; + protected $bIncludeNameDetails = false; protected $bIncludePolygonAsPoints = false; protected $bIncludePolygonAsText = false; @@ -68,6 +70,16 @@ return $this->bIncludeAddressDetails; } + function getIncludeExtraTags() + { + return $this->bIncludeExtraTags; + } + + function getIncludeNameDetails() + { + return $this->bIncludeNameDetails; + } + function setIncludePolygonAsPoints($b = true) { $this->bIncludePolygonAsPoints = $b; @@ -214,6 +226,11 @@ function loadParamArray($aParams) { if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails']; + if ((float) CONST_Postgresql_Version > 9.2) + { + if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags']; + if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails']; + } if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded']; if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe']; @@ -389,6 +406,8 @@ $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,"; $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,"; $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,"; + if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,"; + if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,"; $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, "; $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, "; $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; @@ -405,6 +424,8 @@ $sSQL .= ",langaddress "; $sSQL .= ",placename "; $sSQL .= ",ref "; + if ($this->bIncludeExtraTags) $sSQL .= ",extratags"; + if ($this->bIncludeNameDetails) $sSQL .= ",name"; $sSQL .= ",extratags->'place' "; if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank) @@ -414,6 +435,8 @@ $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,"; $sSQL .= "null as placename,"; $sSQL .= "null as ref,"; + if ($this->bIncludeExtraTags) $sSQL .= "null as extra,"; + if ($this->bIncludeNameDetails) $sSQL .= "null as names,"; $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, "; $sSQL .= $sImportanceSQL."-1.15 as importance, "; $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; @@ -427,6 +450,8 @@ $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,"; $sSQL .= "null as placename,"; $sSQL .= "null as ref,"; + if ($this->bIncludeExtraTags) $sSQL .= "null as extra,"; + if ($this->bIncludeNameDetails) $sSQL .= "null as names,"; $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, "; $sSQL .= $sImportanceSQL."-1.10 as importance, "; $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; @@ -453,15 +478,15 @@ function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases) { /* - Calculate all searches using aValidTokens i.e. - 'Wodsworth Road, Sheffield' => + Calculate all searches using aValidTokens i.e. + 'Wodsworth Road, Sheffield' => - Phrase Wordset - 0 0 (wodsworth road) - 0 1 (wodsworth)(road) - 1 0 (sheffield) + Phrase Wordset + 0 0 (wodsworth road) + 0 1 (wodsworth)(road) + 1 0 (sheffield) - Score how good the search is so they can be ordered + Score how good the search is so they can be ordered */ foreach($aPhrases as $iPhrase => $sPhrase) { @@ -730,32 +755,32 @@ /* Perform the actual query lookup. Returns an ordered list of results, each with the following fields: - osm_type: type of corresponding OSM object + osm_type: type of corresponding OSM object N - node W - way R - relation P - postcode (internally computed) - osm_id: id of corresponding OSM object - class: general object class (corresponds to tag key of primary OSM tag) - type: subclass of object (corresponds to tag value of primary OSM tag) - admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level - rank_search: rank in search hierarchy + osm_id: id of corresponding OSM object + class: general object class (corresponds to tag key of primary OSM tag) + type: subclass of object (corresponds to tag value of primary OSM tag) + admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level + rank_search: rank in search hierarchy (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level) - rank_address: rank in address hierarchy (determines orer in address) - place_id: internal key (may differ between different instances) - country_code: ISO country code - langaddress: localized full address - placename: localized name of object - ref: content of ref tag (if available) - lon: longitude - lat: latitude - importance: importance of place based on Wikipedia link count - addressimportance: cumulated importance of address elements - extra_place: type of place (for admin boundaries, if there is a place tag) - aBoundingBox: bounding Box - label: short description of the object class/type (English only) - name: full name (currently the same as langaddress) - foundorder: secondary ordering for places with same importance + rank_address: rank in address hierarchy (determines orer in address) + place_id: internal key (may differ between different instances) + country_code: ISO country code + langaddress: localized full address + placename: localized name of object + ref: content of ref tag (if available) + lon: longitude + lat: latitude + importance: importance of place based on Wikipedia link count + addressimportance: cumulated importance of address elements + extra_place: type of place (for admin boundaries, if there is a place tag) + aBoundingBox: bounding Box + label: short description of the object class/type (English only) + name: full name (currently the same as langaddress) + foundorder: secondary ordering for places with same importance */ function lookup() { @@ -837,9 +862,23 @@ { // Start with a blank search $aSearches = array( - array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(), - 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(), - 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'') + array('iSearchRank' => 0, + 'iNamePhrase' => -1, + 'sCountryCode' => false, + 'aName' => array(), + 'aAddress' => array(), + 'aFullNameAddress' => array(), + 'aNameNonSearch' => array(), + 'aAddressNonSearch' => array(), + 'sOperator' => '', + 'aFeatureName' => array(), + 'sClass' => '', + 'sType' => '', + 'sHouseNumber' => '', + 'fLat' => '', + 'fLon' => '', + 'fRadius' => '' + ) ); // Do we have a radius search? @@ -1169,8 +1208,8 @@ { $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4"; if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)"; - if ($bBoundingBoxSearch) - $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)"; + if ($bBoundingBoxSearch) + $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)"; $sSQL .= " order by st_area(geometry) desc limit 1"; if (CONST_Debug) var_dump($sSQL); $aPlaceIDs = $this->oDB->getCol($sSQL); @@ -1590,7 +1629,7 @@ if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]); } - if (CONST_Debug) { echo 'Recheck words:<\i>'; var_dump($aRecheckWords); } + if (CONST_Debug) { echo 'Recheck words:<\i>'; var_dump($aRecheckWords); } foreach($aSearchResults as $iResNum => $aResult) { @@ -1600,7 +1639,7 @@ if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter']) && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter']) { - $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom']; + $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter']; } elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter']) && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter']) @@ -1651,37 +1690,19 @@ if ($this->bIncludePolygonAsPoints) { - // Translate geometry string to point array - if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch)) - { - preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); - } - elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch)) - { - preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER); - } - elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch)) + $aPolyPoints[] = geometryText2Points($aPointPolygon['astext'],$fRadius); + + // Output data suitable for display (points and a bounding box) + if (isset($aPolyPoints)) { - $iSteps = ($fRadius * 40000)^2; - $fStepSize = (2*pi())/$iSteps; - $aPolyPoints = array(); - for($f = 0; $f < 2*pi(); $f += $fStepSize) + $aResult['aPolyPoints'] = array(); + foreach($aPolyPoints as $aPoint) { - $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f))); + $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]); } } } - // Output data suitable for display (points and a bounding box) - if ($this->bIncludePolygonAsPoints && isset($aPolyPoints)) - { - $aResult['aPolyPoints'] = array(); - foreach($aPolyPoints as $aPoint) - { - $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]); - } - } - if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) { $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius; @@ -1692,7 +1713,7 @@ $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius; $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius; } - $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']); + $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']); } } @@ -1756,6 +1777,30 @@ } } + if ($this->bIncludeExtraTags) + { + if ($aResult['extra']) + { + $aResult['sExtraTags'] = json_decode($aResult['extra']); + } + else + { + $aResult['sExtraTags'] = (object) array(); + } + } + + if ($this->bIncludeNameDetails) + { + if ($aResult['names']) + { + $aResult['sNameDetails'] = json_decode($aResult['names']); + } + else + { + $aResult['sNameDetails'] = (object) array(); + } + } + // Adjust importance for the number of exact string matches in the result $aResult['importance'] = max(0.001,$aResult['importance']); $iCountWords = 0; @@ -1790,6 +1835,7 @@ { $aResult['foundorder'] += 0.01; } + if (CONST_Debug) { var_dump($aResult); } $aSearchResults[$iResNum] = $aResult; } uasort($aSearchResults, 'byImportance');