]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
introduce Result class in Geocode and SearchDescription
[nominatim.git] / website / search.php
1 <?php
2 @define('CONST_ConnectionBucket_PageType', 'Search');
3
4 require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5 require_once(CONST_BasePath.'/lib/init-website.php');
6 require_once(CONST_BasePath.'/lib/log.php');
7 require_once(CONST_BasePath.'/lib/Geocode.php');
8 require_once(CONST_BasePath.'/lib/output.php');
9 ini_set('memory_limit', '200M');
10
11 $oDB =& getDB();
12 $oParams = new Nominatim\ParameterParser();
13
14 $oGeocode = new Nominatim\Geocode($oDB);
15
16 $aLangPrefOrder = $oParams->getPreferredLanguages();
17 $oGeocode->setLanguagePreference($aLangPrefOrder);
18
19 if (CONST_Search_ReversePlanForAll
20     || isset($aLangPrefOrder['name:de'])
21     || isset($aLangPrefOrder['name:ru'])
22     || isset($aLangPrefOrder['name:ja'])
23     || isset($aLangPrefOrder['name:pl'])
24 ) {
25     $oGeocode->setReverseInPlan(true);
26 }
27
28 // Format for output
29 $sOutputFormat = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html');
30
31 // Show / use polygons
32 if ($sOutputFormat == 'html') {
33     $oGeocode->setIncludePolygonAsGeoJSON($oParams->getBool('polygon_geojson'));
34     $bAsGeoJSON = false;
35 } else {
36     $bAsPoints = $oParams->getBool('polygon');
37     $bAsGeoJSON = $oParams->getBool('polygon_geojson');
38     $bAsKML = $oParams->getBool('polygon_kml');
39     $bAsSVG = $oParams->getBool('polygon_svg');
40     $bAsText = $oParams->getBool('polygon_text');
41     $iWantedTypes = ($bAsGeoJSON?1:0) + ($bAsKML?1:0) + ($bAsSVG?1:0) + ($bAsText?1:0) + ($bAsPoints?1:0);
42     if ($iWantedTypes > CONST_PolygonOutput_MaximumTypes) {
43         if (CONST_PolygonOutput_MaximumTypes) {
44             userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
45         } else {
46             userError("Polygon output is disabled");
47         }
48         exit;
49     }
50     $oGeocode->setIncludePolygonAsPoints($bAsPoints);
51     $oGeocode->setIncludePolygonAsText($bAsText);
52     $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
53     $oGeocode->setIncludePolygonAsKML($bAsKML);
54     $oGeocode->setIncludePolygonAsSVG($bAsSVG);
55 }
56
57 // Polygon simplification threshold (optional)
58 $oGeocode->setPolygonSimplificationThreshold($oParams->getFloat('polygon_threshold', 0.0));
59
60 $oGeocode->loadParamArray($oParams);
61
62 if (CONST_Search_BatchMode && isset($_GET['batch'])) {
63     $aBatch = json_decode($_GET['batch'], true);
64     $aBatchResults = array();
65     foreach ($aBatch as $aBatchParams) {
66         $oBatchGeocode = clone $oGeocode;
67         $oBatchParams = new Nominatim\ParameterParser($aBatchParams);
68         $oBatchGeocode->loadParamArray($oBatchParams);
69         $oBatchGeocode->setQueryFromParams($oBatchParams);
70         $aSearchResults = $oBatchGeocode->lookup();
71         $aBatchResults[] = $aSearchResults;
72     }
73     include(CONST_BasePath.'/lib/template/search-batch-json.php');
74     exit;
75 }
76
77 $oGeocode->setQueryFromParams($oParams);
78
79 if (!$oGeocode->getQueryString()
80     && isset($_SERVER['PATH_INFO'])
81     && $_SERVER['PATH_INFO'][0] == '/'
82 ) {
83     $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
84
85     // reverse order of '/' separated string
86     $aPhrases = explode('/', $sQuery);
87     $aPhrases = array_reverse($aPhrases);
88     $sQuery = join(', ', $aPhrases);
89     $oGeocode->setQuery($sQuery);
90 }
91
92 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
93
94 $aSearchResults = $oGeocode->lookup();
95
96 if ($sOutputFormat=='html') {
97     $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
98 }
99 logEnd($oDB, $hLog, sizeof($aSearchResults));
100
101 $sQuery = $oGeocode->getQueryString();
102
103 $aMoreParams = $oGeocode->getMoreUrlParams();
104 if ($sOutputFormat != 'html') $aMoreParams['format'] = $sOutputFormat;
105 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
106     $aMoreParams['accept-language'] = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
107 }
108 $sMoreURL = CONST_Website_BaseURL.'search.php?'.http_build_query($aMoreParams);
109
110 if (CONST_Debug) exit;
111
112 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');