]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
Merge pull request #354 from mtmail/configurable-tile-url
[nominatim.git] / website / search.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Search');
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5         require_once(CONST_BasePath.'/lib/log.php');
6         require_once(CONST_BasePath.'/lib/Geocode.php');
7
8         ini_set('memory_limit', '200M');
9
10         $oDB =& getDB();
11
12         // Display defaults
13         $fLat = CONST_Default_Lat;
14         $fLon = CONST_Default_Lon;
15         $iZoom = CONST_Default_Zoom;
16         $sTileURL = CONST_Map_Tile_URL;
17         $sTileAttribution = CONST_Map_Tile_Attribution;
18
19         $oGeocode =& new Geocode($oDB);
20
21         $aLangPrefOrder = getPreferredLanguages();
22         $oGeocode->setLanguagePreference($aLangPrefOrder);
23
24         if (CONST_Search_ReversePlanForAll
25                 || isset($aLangPrefOrder['name:de'])
26                 || isset($aLangPrefOrder['name:ru'])
27                 || isset($aLangPrefOrder['name:ja'])
28                 || isset($aLangPrefOrder['name:pl']))
29         {
30                 $oGeocode->setReverseInPlan(true);
31         }
32
33         // Format for output
34         $sOutputFormat = 'html';
35         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
36         {
37                 $sOutputFormat = $_GET['format'];
38         }
39
40         // Show / use polygons
41         if ($sOutputFormat == 'html')
42         {
43                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
44         }
45         else
46         {
47                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
48                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
49                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
50                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
51                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
52                 if ( ( ($bAsGeoJSON?1:0)
53                                  + ($bAsKML?1:0)
54                                  + ($bAsSVG?1:0)
55                                  + ($bAsText?1:0)
56                                  + ($bAsPoints?1:0)
57                                  ) > CONST_PolygonOutput_MaximumTypes)
58                 {
59                         if (CONST_PolygonOutput_MaximumTypes)
60                         {
61                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
62                         }
63                         else
64                         {
65                                 userError("Polygon output is disabled");
66                         }
67                         exit;
68                 }
69                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
70                 $oGeocode->setIncludePolygonAsText($bAsText);
71                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
72                 $oGeocode->setIncludePolygonAsKML($bAsKML);
73                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
74         }
75
76         // Polygon simplification threshold (optional)
77         $fThreshold = 0.0;
78         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
79         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
80
81         $oGeocode->loadParamArray($_GET);
82
83         if (CONST_Search_BatchMode && isset($_GET['batch']))
84         {
85                 $aBatch = json_decode($_GET['batch'], true);
86                 $aBatchResults = array();
87                 foreach($aBatch as $aBatchParams)
88                 {
89                         $oBatchGeocode = clone $oGeocode;
90                         $oBatchGeocode->loadParamArray($aBatchParams);
91                         $oBatchGeocode->setQueryFromParams($aBatchParams);
92                         $aSearchResults = $oBatchGeocode->lookup();
93                         $aBatchResults[] = $aSearchResults;
94                 }
95                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
96                 exit;
97         }
98         else
99         {
100                 if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
101                 {
102                                 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
103
104                                 // reverse order of '/' separated string
105                                 $aPhrases = explode('/', $sQuery);
106                                 $aPhrases = array_reverse($aPhrases);
107                                 $sQuery = join(', ',$aPhrases);
108                                 $oGeocode->setQuery($sQuery);
109                 }
110                 else
111                 {
112                                 $oGeocode->setQueryFromParams($_GET);
113                 }
114         }
115
116         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
117
118         $aSearchResults = $oGeocode->lookup();
119         if ($aSearchResults === false) $aSearchResults = array();
120
121         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
122
123         logEnd($oDB, $hLog, sizeof($aSearchResults));
124
125         $bAsText = $oGeocode->getIncludePolygonAsText();
126         $sQuery = $oGeocode->getQueryString();
127         $sViewBox = $oGeocode->getViewBoxString();
128         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
129         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
130
131         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
132         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
133         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
134         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
135         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
136         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
137         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
138         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
139         $sMoreURL .= '&q='.urlencode($sQuery);
140
141         if (CONST_Debug) exit;
142
143         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');