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);
23 function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
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']);
29 if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
30 if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
32 // List of excluded Place IDs - used for more acurate pageing
33 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
35 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
37 $iExcludedPlaceID = (int)$iExcludedPlaceID;
38 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
40 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
43 // Only certain ranks of feature
44 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
45 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
48 if (isset($aParams['countrycodes']))
50 $aCountryCodes = array();
51 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
53 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
55 $aCountryCodes[] = strtolower($sCountryCode);
58 $oGeocode->setCountryCodesList($aCountryCodes);
61 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
63 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
64 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
66 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
68 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
69 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
72 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
74 $aPoints = explode(',',$aParams['route']);
75 if (sizeof($aPoints) % 2 != 0)
77 userError("Uneven number of points");
82 foreach($aPoints as $i => $fPoint)
86 $aRoute[] = array((float)$fPoint, $fPrevCoord);
90 $fPrevCoord = (float)$fPoint;
93 $oGeocode->setRoute($aRoute);
97 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
98 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
100 $sQuery = substr($_SERVER['PATH_INFO'], 1);
102 // reverse order of '/' separated string
103 $aPhrases = explode('/', $sQuery);
104 $aPhrases = array_reverse($aPhrases);
105 $sQuery = join(', ',$aPhrases);
109 $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
113 $oGeocode->setQuery($sQuery);
119 $sOutputFormat = 'html';
120 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
122 $sOutputFormat = $_GET['format'];
125 // Show / use polygons
126 if ($sOutputFormat == 'html')
128 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
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)
142 ) > CONST_PolygonOutput_MaximumTypes)
144 if (CONST_PolygonOutput_MaximumTypes)
146 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
150 userError("Polygon output is disabled");
154 $oGeocode->setIncludePolygonAsText($bAsPoints);
155 $oGeocode->setIncludePolygonAsText($bAsText);
156 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
157 $oGeocode->setIncludePolygonAsKML($bAsKML);
158 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
161 loadParamsToGeocode($oGeocode, $_GET, false);
163 if (isset($_GET['batch']))
165 $aBatch = json_decode($_GET['batch'], true);
166 $aBatchResults = array();
167 foreach($aBatch as $aBatchParams)
169 $oBatchGeocode = clone $oGeocode;
170 loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
171 $aSearchResults = $oBatchGeocode->lookup();
172 $aBatchResults[] = $aSearchResults;
174 include(CONST_BasePath.'/lib/template/search-batch-json.php');
178 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
180 $aSearchResults = $oGeocode->lookup();
181 if ($aSearchResults === false) $aSearchResults = array();
183 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
185 logEnd($oDB, $hLog, sizeof($aSearchResults));
187 $bAsText = $oGeocode->getIncludePolygonAsText();
188 $sQuery = $oGeocode->getQueryString();
189 $sViewBox = $oGeocode->getViewBoxString();
190 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
191 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
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);
201 if (CONST_Debug) exit;
203 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');