]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
7e96e10e629ce7a208687190af3dc64477b25852
[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         $sSuggestionURL = false;
17
18         $oGeocode =& new Geocode($oDB);
19
20         $aLangPrefOrder = getPreferredLanguages();
21         $oGeocode->setLanguagePreference($aLangPrefOrder);
22
23         if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
24         if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
25         if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
26         if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
27
28         function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
29         {
30                 if (isset($aParams['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$aParams['addressdetails']);
31                 if (isset($aParams['bounded'])) $oGeocode->setBounded((bool)$aParams['bounded']);
32                 if (isset($aParams['dedupe'])) $oGeocode->setDedupe((bool)$aParams['dedupe']);
33
34                 if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
35                 if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
36
37                 // List of excluded Place IDs - used for more acurate pageing
38                 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
39                 {
40                         foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
41                         {
42                                 $iExcludedPlaceID = (int)$iExcludedPlaceID;
43                                 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
44                         }
45                         $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
46                 }
47
48                 // Only certain ranks of feature
49                 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
50                 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
51
52                 // Country code list
53                 if (isset($aParams['countrycodes']))
54                 {
55                         $aCountryCodes = array();
56                         foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
57                         {
58                                 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
59                                 {
60                                         $aCountryCodes[] = strtolower($sCountryCode);
61                                 }
62                         }
63                         $oGeocode->setCountryCodesList($aCountryCodes);
64                 }
65
66                 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
67                 {
68                         $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
69                         $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
70                 }
71                 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
72                 {
73                         $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
74                         $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
75                 }
76
77                 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
78                 {
79                         $aPoints = explode(',',$aParams['route']);
80                         if (sizeof($aPoints) % 2 != 0)
81                         {
82                                 userError("Uneven number of points");
83                                 exit;
84                         }
85                         $fPrevCoord = false;
86                         $aRoute = array();
87                         foreach($aPoints as $i => $fPoint)
88                         {
89                                 if ($i%2)
90                                 {
91                                         $aRoute[] = array((float)$fPoint, $fPrevCoord);
92                                 }
93                                 else
94                                 {
95                                         $fPrevCoord = (float)$fPoint;
96                                 }
97                         }
98                         $oGeocode->setRoute($aRoute);
99                 }
100
101                 // Search query
102                 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
103                 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
104                 {
105                         $sQuery = substr($_SERVER['PATH_INFO'], 1);
106
107                         // reverse order of '/' separated string
108                         $aPhrases = explode('/', $sQuery);
109                         $aPhrases = array_reverse($aPhrases);
110                         $sQuery = join(', ',$aPhrases);
111                 }
112                 if (!$sQuery)
113                 {
114                         $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
115                         $oGeocode->setReverseInPlan(false);
116                 }
117                 else
118                 {
119                         $oGeocode->setQuery($sQuery);
120                 }
121
122         }
123
124         // Format for output
125         $sOutputFormat = 'html';
126         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
127         {
128                 $sOutputFormat = $_GET['format'];
129         }
130
131         // Show / use polygons
132         if ($sOutputFormat == 'html')
133         {
134                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
135         }
136         else
137         {
138                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
139                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
140                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
141                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
142                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
143                 if ( ( ($bAsGeoJSON?1:0)
144                      + ($bAsKML?1:0)
145                      + ($bAsSVG?1:0)
146                      + ($bAsText?1:0)
147                      + ($bAsPoints?1:0)
148                      ) > CONST_PolygonOutput_MaximumTypes)
149                 {
150                         if (CONST_PolygonOutput_MaximumTypes)
151                         {
152                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
153                         }
154                         else
155                         {
156                                 userError("Polygon output is disabled");
157                         }
158                         exit;
159                 }
160                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
161                 $oGeocode->setIncludePolygonAsText($bAsText);
162                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
163                 $oGeocode->setIncludePolygonAsKML($bAsKML);
164                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
165         }
166
167         loadParamsToGeocode($oGeocode, $_GET, false);
168
169         if (CONST_Search_BatchMode && isset($_GET['batch']))
170         {
171                 $aBatch = json_decode($_GET['batch'], true);
172                 $aBatchResults = array();
173                 foreach($aBatch as $aBatchParams)
174                 {
175                         $oBatchGeocode = clone $oGeocode;
176                         loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
177                         $aSearchResults = $oBatchGeocode->lookup();
178                         $aBatchResults[] = $aSearchResults;
179                 }
180                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
181                 exit;
182         }
183
184         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
185
186         $aSearchResults = $oGeocode->lookup();
187         if ($aSearchResults === false) $aSearchResults = array();
188
189         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
190
191         logEnd($oDB, $hLog, sizeof($aSearchResults));
192
193         $bAsText = $oGeocode->getIncludePolygonAsText();
194         $sQuery = $oGeocode->getQueryString();
195         $sViewBox = $oGeocode->getViewBoxString();
196         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
197         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
198
199         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
200         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
201         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
202         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
203         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
204         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
205         $sMoreURL .= '&q='.urlencode($sQuery);
206
207         if (CONST_Debug) exit;
208
209         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');