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 // List of excluded Place IDs - used for more acurate pageing
40 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
42 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
44 $iExcludedPlaceID = (int)$iExcludedPlaceID;
45 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
47 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
50 // Only certain ranks of feature
51 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
52 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
55 if (isset($aParams['countrycodes']))
57 $aCountryCodes = array();
58 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
60 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
62 $aCountryCodes[] = strtolower($sCountryCode);
65 $oGeocode->setCountryCodesList($aCountryCodes);
68 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
70 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
71 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
73 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
75 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
76 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
79 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
81 $aPoints = explode(',',$aParams['route']);
82 if (sizeof($aPoints) % 2 != 0)
84 userError("Uneven number of points");
89 foreach($aPoints as $i => $fPoint)
93 $aRoute[] = array((float)$fPoint, $fPrevCoord);
97 $fPrevCoord = (float)$fPoint;
100 $oGeocode->setRoute($aRoute);
104 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
105 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
107 $sQuery = substr($_SERVER['PATH_INFO'], 1);
109 // reverse order of '/' separated string
110 $aPhrases = explode('/', $sQuery);
111 $aPhrases = array_reverse($aPhrases);
112 $sQuery = join(', ',$aPhrases);
116 $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
117 $oGeocode->setReverseInPlan(false);
121 $oGeocode->setQuery($sQuery);
127 $sOutputFormat = 'html';
128 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
130 $sOutputFormat = $_GET['format'];
133 // Show / use polygons
134 if ($sOutputFormat == 'html')
136 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
140 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
141 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
142 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
143 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
144 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
145 if ( ( ($bAsGeoJSON?1:0)
150 ) > CONST_PolygonOutput_MaximumTypes)
152 if (CONST_PolygonOutput_MaximumTypes)
154 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
158 userError("Polygon output is disabled");
162 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
163 $oGeocode->setIncludePolygonAsText($bAsText);
164 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
165 $oGeocode->setIncludePolygonAsKML($bAsKML);
166 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
169 loadParamsToGeocode($oGeocode, $_GET, false);
171 if (CONST_Search_BatchMode && isset($_GET['batch']))
173 $aBatch = json_decode($_GET['batch'], true);
174 $aBatchResults = array();
175 foreach($aBatch as $aBatchParams)
177 $oBatchGeocode = clone $oGeocode;
178 loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
179 $aSearchResults = $oBatchGeocode->lookup();
180 $aBatchResults[] = $aSearchResults;
182 include(CONST_BasePath.'/lib/template/search-batch-json.php');
186 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
188 $aSearchResults = $oGeocode->lookup();
189 if ($aSearchResults === false) $aSearchResults = array();
191 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
193 logEnd($oDB, $hLog, sizeof($aSearchResults));
195 $bAsText = $oGeocode->getIncludePolygonAsText();
196 $sQuery = $oGeocode->getQueryString();
197 $sViewBox = $oGeocode->getViewBoxString();
198 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
199 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
201 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
202 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
203 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
204 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
205 if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
206 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
207 $sMoreURL .= '&q='.urlencode($sQuery);
209 if (CONST_Debug) exit;
211 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');