]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
add extratags and name details to PlaceLookup (reverse)
[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($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);
27
28         // Format for output
29         $sOutputFormat = 'html';
30         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
31         {
32                 $sOutputFormat = $_GET['format'];
33         }
34
35         // Show / use polygons
36         if ($sOutputFormat == 'html')
37         {
38                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
39         }
40         else
41         {
42                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
43                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
44                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
45                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
46                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
47                 if ( ( ($bAsGeoJSON?1:0)
48                      + ($bAsKML?1:0)
49                      + ($bAsSVG?1:0)
50                      + ($bAsText?1:0)
51                      + ($bAsPoints?1:0)
52                      ) > CONST_PolygonOutput_MaximumTypes)
53                 {
54                         if (CONST_PolygonOutput_MaximumTypes)
55                         {
56                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
57                         }
58                         else
59                         {
60                                 userError("Polygon output is disabled");
61                         }
62                         exit;
63                 }
64                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
65                 $oGeocode->setIncludePolygonAsText($bAsText);
66                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
67                 $oGeocode->setIncludePolygonAsKML($bAsKML);
68                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
69         }
70
71         // Polygon simplification threshold (optional)
72         $fThreshold = 0.0;
73         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
74         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
75
76         $oGeocode->loadParamArray($_GET);
77
78         if (CONST_Search_BatchMode && isset($_GET['batch']))
79         {
80                 $aBatch = json_decode($_GET['batch'], true);
81                 $aBatchResults = array();
82                 foreach($aBatch as $aBatchParams)
83                 {
84                         $oBatchGeocode = clone $oGeocode;
85                         $oBatchGeocode->loadParamArray($aBatchParams);
86                         $oBatchGeocode->setQueryFromParams($aBatchParams);
87                         $aSearchResults = $oBatchGeocode->lookup();
88                         $aBatchResults[] = $aSearchResults;
89                 }
90                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
91                 exit;
92         } else {
93         if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
94         {
95             $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
96
97             // reverse order of '/' separated string
98             $aPhrases = explode('/', $sQuery);
99             $aPhrases = array_reverse($aPhrases);
100             $sQuery = join(', ',$aPhrases);
101             $oGeocode->setQuery($sQuery);
102         }
103         else
104         {
105             $oGeocode->setQueryFromParams($_GET);
106         }
107
108     }
109
110         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
111
112         $aSearchResults = $oGeocode->lookup();
113         if ($aSearchResults === false) $aSearchResults = array();
114
115         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
116
117         logEnd($oDB, $hLog, sizeof($aSearchResults));
118
119         $bAsText = $oGeocode->getIncludePolygonAsText();
120         $sQuery = $oGeocode->getQueryString();
121         $sViewBox = $oGeocode->getViewBoxString();
122         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
123         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
124
125         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
126         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
127         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
128         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
129         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
130         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
131         $sMoreURL .= '&q='.urlencode($sQuery);
132
133         if (CONST_Debug) exit;
134
135         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');