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