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