]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
batch mode for json only
[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->setCountryCodeList($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
67                 if (isset($aParams['viewbox']) && $aParams['viewbox'])
68                 {
69                         $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
70                         $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
71                 }
72
73                 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
74                 {
75                         $aPoints = explode(',',$aParams['route']);
76                         if (sizeof($aPoints) % 2 != 0)
77                         {
78                                 userError("Uneven number of points");
79                                 exit;
80                         }
81                         $fPrevCoord = false;
82                         $aRoute = array();
83                         foreach($aPoints as $i => $fPoint)
84                         {
85                                 if ($i%2)
86                                 {
87                                         $aRoute[] = array((float)$fPoint, $fPrevCoord);
88                                 }
89                                 else
90                                 {
91                                         $fPrevCoord = (float)$fPoint;
92                                 }
93                         }
94                         $oGeocode->setRoute($aRoute);
95                 }
96
97                 // Search query
98                 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
99                 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
100                 {
101                         $sQuery = substr($_SERVER['PATH_INFO'], 1);
102
103                         // reverse order of '/' separated string
104                         $aPhrases = explode('/', $sQuery);
105                         $aPhrases = array_reverse($aPhrases);
106                         $sQuery = join(', ',$aPhrases);
107                 }
108                 if (!$sQuery)
109                 {
110                         $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
111                 }
112                 else
113                 {
114                         $oGeocode->setQuery($sQuery);
115                 }
116
117         }
118
119         // Format for output
120         $sOutputFormat = 'html';
121         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
122         {
123                 $sOutputFormat = $_GET['format'];
124         }
125
126         // Show / use polygons
127         if ($sOutputFormat == 'html')
128         {
129                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
130         }
131         else
132         {
133                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
134                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
135                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
136                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
137                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
138                 if ( ( ($bAsGeoJSON?1:0)
139                      + ($bAsKML?1:0)
140                      + ($bAsSVG?1:0)
141                      + ($bAsText?1:0)
142                      + ($bAsPoints?1:0)
143                      ) > CONST_PolygonOutput_MaximumTypes)
144                 {
145                         if (CONST_PolygonOutput_MaximumTypes)
146                         {
147                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
148                         }
149                         else
150                         {
151                                 userError("Polygon output is disabled");
152                         }
153                         exit;
154                 }
155                 $oGeocode->setIncludePolygonAsText($bAsPoints);
156                 $oGeocode->setIncludePolygonAsText($bAsText);
157                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
158                 $oGeocode->setIncludePolygonAsKML($bAsKML);
159                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
160         }
161
162         loadParamsToGeocode($oGeocode, $_GET, false);
163
164         if (isset($_GET['batch']))
165         {
166                 $aBatch = json_decode($_GET['batch'], true);
167                 $aBatchResults = array();
168                 foreach($aBatch as $aBatchParams)
169                 {
170                         $oBatchGeocode = clone $oGeocode;
171                         loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
172                         $aSearchResults = $oBatchGeocode->lookup();
173                         $aBatchResults[] = $aSearchResults;
174                 }
175                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
176                 exit;
177         }
178
179         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
180
181         $aSearchResults = $oGeocode->lookup();
182         if ($aSearchResults === false) $aSearchResults = array();
183
184         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
185
186         logEnd($oDB, $hLog, sizeof($aSearchResults));
187
188         $bAsText = $oGeocode->getIncludePolygonAsText();
189         $sQuery = $oGeocode->getQueryString();
190
191         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
192         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
193         if (isset($_GET['polygon']) && isset($_GET['polygon'])) $sMoreURL .= '&polygon=1';
194         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
195         if (isset($_GET['viewbox']) && $_GET['viewbox']) $sMoreURL .= '&viewbox='.urlencode($_GET['viewbox']);
196         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
197         $sMoreURL .= '&q='.urlencode($sQuery);
198
199         if (CONST_Debug) exit;
200
201         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');