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