2 @define('CONST_ConnectionBucket_PageType', 'Search');
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');
8 ini_set('memory_limit', '200M');
13 $fLat = CONST_Default_Lat;
14 $fLon = CONST_Default_Lon;
15 $iZoom = CONST_Default_Zoom;
16 $sSuggestionURL = false;
18 $oGeocode =& new Geocode($oDB);
20 $aLangPrefOrder = getPreferredLanguages();
21 $oGeocode->setLanguagePreference($aLangPrefOrder);
24 if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
25 if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
26 if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
27 if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
30 function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
32 if (isset($aParams['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$aParams['addressdetails']);
33 if (isset($aParams['bounded'])) $oGeocode->setBounded((bool)$aParams['bounded']);
34 if (isset($aParams['dedupe'])) $oGeocode->setDedupe((bool)$aParams['dedupe']);
36 if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
37 if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
39 if (isset($aParams['fallback'])) $oGeocode->setFallback((int)$aParams['fallback']);
41 // List of excluded Place IDs - used for more acurate pageing
42 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
44 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
46 $iExcludedPlaceID = (int)$iExcludedPlaceID;
47 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
49 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
52 // Only certain ranks of feature
53 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
54 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
57 if (isset($aParams['countrycodes']))
59 $aCountryCodes = array();
60 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
62 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
64 $aCountryCodes[] = strtolower($sCountryCode);
67 $oGeocode->setCountryCodesList($aCountryCodes);
70 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
72 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
73 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
75 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
77 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
78 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
81 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
83 $aPoints = explode(',',$aParams['route']);
84 if (sizeof($aPoints) % 2 != 0)
86 userError("Uneven number of points");
91 foreach($aPoints as $i => $fPoint)
95 $aRoute[] = array((float)$fPoint, $fPrevCoord);
99 $fPrevCoord = (float)$fPoint;
102 $oGeocode->setRoute($aRoute);
106 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
107 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
109 $sQuery = substr($_SERVER['PATH_INFO'], 1);
111 // reverse order of '/' separated string
112 $aPhrases = explode('/', $sQuery);
113 $aPhrases = array_reverse($aPhrases);
114 $sQuery = join(', ',$aPhrases);
118 $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
119 $oGeocode->setReverseInPlan(false);
123 $oGeocode->setQuery($sQuery);
129 $sOutputFormat = 'html';
130 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
132 $sOutputFormat = $_GET['format'];
135 // Show / use polygons
136 if ($sOutputFormat == 'html')
138 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
142 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
143 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
144 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
145 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
146 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
147 if ( ( ($bAsGeoJSON?1:0)
152 ) > CONST_PolygonOutput_MaximumTypes)
154 if (CONST_PolygonOutput_MaximumTypes)
156 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
160 userError("Polygon output is disabled");
164 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
165 $oGeocode->setIncludePolygonAsText($bAsText);
166 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
167 $oGeocode->setIncludePolygonAsKML($bAsKML);
168 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
171 loadParamsToGeocode($oGeocode, $_GET, false);
173 if (CONST_Search_BatchMode && isset($_GET['batch']))
175 $aBatch = json_decode($_GET['batch'], true);
176 $aBatchResults = array();
177 foreach($aBatch as $aBatchParams)
179 $oBatchGeocode = clone $oGeocode;
180 loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
181 $aSearchResults = $oBatchGeocode->lookup();
182 $aBatchResults[] = $aSearchResults;
184 include(CONST_BasePath.'/lib/template/search-batch-json.php');
188 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
190 $aSearchResults = $oGeocode->lookup();
191 if ($aSearchResults === false) $aSearchResults = array();
193 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
195 logEnd($oDB, $hLog, sizeof($aSearchResults));
197 $bAsText = $oGeocode->getIncludePolygonAsText();
198 $sQuery = $oGeocode->getQueryString();
199 $sViewBox = $oGeocode->getViewBoxString();
200 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
201 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
203 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
204 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
205 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
206 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
207 if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
208 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
209 $sMoreURL .= '&q='.urlencode($sQuery);
211 if (CONST_Debug) exit;
213 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');