Nominatim/website/search.php

212 lines
7.2 KiB
PHP
Raw Normal View History

2010-10-27 18:05:42 +04:00
<?php
@define('CONST_ConnectionBucket_PageType', 'Search');
2010-10-27 18:05:42 +04:00
require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
require_once(CONST_BasePath.'/lib/log.php');
2013-08-27 20:21:20 +04:00
require_once(CONST_BasePath.'/lib/Geocode.php');
2010-10-27 18:05:42 +04:00
ini_set('memory_limit', '200M');
2010-10-27 18:05:42 +04:00
$oDB =& getDB();
// Display defaults
$fLat = CONST_Default_Lat;
$fLon = CONST_Default_Lon;
$iZoom = CONST_Default_Zoom;
2013-08-27 20:21:20 +04:00
$sSuggestionURL = false;
$oGeocode =& new Geocode($oDB);
$aLangPrefOrder = getPreferredLanguages();
$oGeocode->setLanguagePreference($aLangPrefOrder);
if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
2013-08-28 17:58:50 +04:00
function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
{
if (isset($aParams['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$aParams['addressdetails']);
if (isset($aParams['bounded'])) $oGeocode->setBounded((bool)$aParams['bounded']);
if (isset($aParams['dedupe'])) $oGeocode->setDedupe((bool)$aParams['dedupe']);
if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
if (isset($aParams['fallback'])) $oGeocode->setFallback((int)$aParams['fallback']);
2013-08-28 17:58:50 +04:00
// List of excluded Place IDs - used for more acurate pageing
if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
{
foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
{
$iExcludedPlaceID = (int)$iExcludedPlaceID;
if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
}
$oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
}
// Only certain ranks of feature
if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
// Country code list
if (isset($aParams['countrycodes']))
{
$aCountryCodes = array();
foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
{
if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
{
$aCountryCodes[] = strtolower($sCountryCode);
}
}
$oGeocode->setCountryCodesList($aCountryCodes);
2013-08-28 17:58:50 +04:00
}
if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
{
$aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
$oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
}
2013-08-30 16:04:33 +04:00
else if (isset($aParams['viewbox']) && $aParams['viewbox'])
2013-08-28 17:58:50 +04:00
{
$aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
$oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
}
if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
{
$aPoints = explode(',',$aParams['route']);
if (sizeof($aPoints) % 2 != 0)
{
userError("Uneven number of points");
exit;
}
$fPrevCoord = false;
$aRoute = array();
foreach($aPoints as $i => $fPoint)
{
if ($i%2)
{
$aRoute[] = array((float)$fPoint, $fPrevCoord);
}
else
{
$fPrevCoord = (float)$fPoint;
}
}
$oGeocode->setRoute($aRoute);
}
// Search query
$sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
{
$sQuery = substr($_SERVER['PATH_INFO'], 1);
// reverse order of '/' separated string
$aPhrases = explode('/', $sQuery);
$aPhrases = array_reverse($aPhrases);
$sQuery = join(', ',$aPhrases);
}
if (!$sQuery)
{
$oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
$oGeocode->setReverseInPlan(false);
2013-08-28 17:58:50 +04:00
}
else
{
$oGeocode->setQuery($sQuery);
}
}
2010-10-27 18:05:42 +04:00
// Format for output
2013-08-27 20:21:20 +04:00
$sOutputFormat = 'html';
2010-10-27 18:05:42 +04:00
if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
{
$sOutputFormat = $_GET['format'];
}
// Show / use polygons
2013-04-13 00:45:24 +04:00
if ($sOutputFormat == 'html')
{
2013-08-28 16:42:05 +04:00
if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
2013-04-13 00:45:24 +04:00
}
else
{
2013-08-27 20:21:20 +04:00
$bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
$bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
$bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
$bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
$bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
2013-08-27 20:21:20 +04:00
if ( ( ($bAsGeoJSON?1:0)
+ ($bAsKML?1:0)
+ ($bAsSVG?1:0)
+ ($bAsText?1:0)
+ ($bAsPoints?1:0)
) > CONST_PolygonOutput_MaximumTypes)
2013-04-13 00:45:24 +04:00
{
if (CONST_PolygonOutput_MaximumTypes)
{
userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
2013-04-13 00:45:24 +04:00
}
else
{
userError("Polygon output is disabled");
}
exit;
}
$oGeocode->setIncludePolygonAsPoints($bAsPoints);
2013-08-27 20:21:20 +04:00
$oGeocode->setIncludePolygonAsText($bAsText);
$oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
$oGeocode->setIncludePolygonAsKML($bAsKML);
$oGeocode->setIncludePolygonAsSVG($bAsSVG);
}
2010-10-27 18:05:42 +04:00
2013-08-28 17:58:50 +04:00
loadParamsToGeocode($oGeocode, $_GET, false);
2010-10-27 18:05:42 +04:00
if (CONST_Search_BatchMode && isset($_GET['batch']))
2010-10-27 18:05:42 +04:00
{
2013-08-27 20:21:20 +04:00
$aBatch = json_decode($_GET['batch'], true);
2013-08-28 17:58:50 +04:00
$aBatchResults = array();
foreach($aBatch as $aBatchParams)
{
$oBatchGeocode = clone $oGeocode;
loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
$aSearchResults = $oBatchGeocode->lookup();
$aBatchResults[] = $aSearchResults;
2010-10-27 18:05:42 +04:00
}
2013-08-28 17:58:50 +04:00
include(CONST_BasePath.'/lib/template/search-batch-json.php');
2013-08-27 20:21:20 +04:00
exit;
2010-10-27 18:05:42 +04:00
}
2013-08-28 17:58:50 +04:00
$hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
2013-08-27 20:21:20 +04:00
$aSearchResults = $oGeocode->lookup();
if ($aSearchResults === false) $aSearchResults = array();
$sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
2010-10-27 18:05:42 +04:00
2013-08-27 20:21:20 +04:00
logEnd($oDB, $hLog, sizeof($aSearchResults));
2010-10-27 18:05:42 +04:00
2013-08-27 20:21:20 +04:00
$bAsText = $oGeocode->getIncludePolygonAsText();
$sQuery = $oGeocode->getQueryString();
2013-08-30 16:04:33 +04:00
$sViewBox = $oGeocode->getViewBoxString();
2013-09-02 17:10:00 +04:00
$bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
2013-08-30 16:04:33 +04:00
$aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
2013-08-27 20:21:20 +04:00
$sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
2012-12-09 12:35:34 +04:00
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
2013-08-30 16:04:33 +04:00
if ($bShowPolygons) $sMoreURL .= '&polygon=1';
2013-08-27 20:21:20 +04:00
if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
2013-08-30 16:04:33 +04:00
if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
2010-10-27 18:05:42 +04:00
if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
$sMoreURL .= '&q='.urlencode($sQuery);
if (CONST_Debug) exit;
2010-11-15 19:34:27 +03:00
include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');