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 if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
24 if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
25 if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
26 if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
28 function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
30 if (isset($aParams['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$aParams['addressdetails']);
31 if (isset($aParams['bounded'])) $oGeocode->setBounded((bool)$aParams['bounded']);
32 if (isset($aParams['dedupe'])) $oGeocode->setDedupe((bool)$aParams['dedupe']);
34 if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
35 if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
37 // List of excluded Place IDs - used for more acurate pageing
38 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
40 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
42 $iExcludedPlaceID = (int)$iExcludedPlaceID;
43 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
45 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
48 // Only certain ranks of feature
49 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
50 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
53 if (isset($aParams['countrycodes']))
55 $aCountryCodes = array();
56 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
58 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
60 $aCountryCodes[] = strtolower($sCountryCode);
63 $oGeocode->setCountryCodesList($aCountryCodes);
66 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
68 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
69 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
71 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
73 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
74 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
77 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
79 $aPoints = explode(',',$aParams['route']);
80 if (sizeof($aPoints) % 2 != 0)
82 userError("Uneven number of points");
87 foreach($aPoints as $i => $fPoint)
91 $aRoute[] = array((float)$fPoint, $fPrevCoord);
95 $fPrevCoord = (float)$fPoint;
98 $oGeocode->setRoute($aRoute);
102 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
103 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
105 $sQuery = substr($_SERVER['PATH_INFO'], 1);
107 // reverse order of '/' separated string
108 $aPhrases = explode('/', $sQuery);
109 $aPhrases = array_reverse($aPhrases);
110 $sQuery = join(', ',$aPhrases);
114 $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
115 $oGeocode->setReverseInPlan(false);
119 $oGeocode->setQuery($sQuery);
125 $sOutputFormat = 'html';
126 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
128 $sOutputFormat = $_GET['format'];
131 // Show / use polygons
132 if ($sOutputFormat == 'html')
134 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
138 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
139 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
140 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
141 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
142 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
143 if ( ( ($bAsGeoJSON?1:0)
148 ) > CONST_PolygonOutput_MaximumTypes)
150 if (CONST_PolygonOutput_MaximumTypes)
152 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
156 userError("Polygon output is disabled");
160 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
161 $oGeocode->setIncludePolygonAsText($bAsText);
162 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
163 $oGeocode->setIncludePolygonAsKML($bAsKML);
164 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
167 loadParamsToGeocode($oGeocode, $_GET, false);
169 if (CONST_Search_BatchMode && isset($_GET['batch']))
171 $aBatch = json_decode($_GET['batch'], true);
172 $aBatchResults = array();
173 foreach($aBatch as $aBatchParams)
175 $oBatchGeocode = clone $oGeocode;
176 loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
177 $aSearchResults = $oBatchGeocode->lookup();
178 $aBatchResults[] = $aSearchResults;
180 include(CONST_BasePath.'/lib/template/search-batch-json.php');
184 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
186 $aSearchResults = $oGeocode->lookup();
187 if ($aSearchResults === false) $aSearchResults = array();
189 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
191 logEnd($oDB, $hLog, sizeof($aSearchResults));
193 $bAsText = $oGeocode->getIncludePolygonAsText();
194 $sQuery = $oGeocode->getQueryString();
195 $sViewBox = $oGeocode->getViewBoxString();
196 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
197 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
199 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
200 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
201 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
202 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
203 if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
204 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
205 $sMoreURL .= '&q='.urlencode($sQuery);
207 if (CONST_Debug) exit;
209 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');