]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
Implement geocoding and reverse geocoding with tiger interpolation lines instead...
[nominatim.git] / website / search.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Search');
3
4         require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5         require_once(CONST_BasePath.'/lib/init-website.php');
6         require_once(CONST_BasePath.'/lib/log.php');
7         require_once(CONST_BasePath.'/lib/Geocode.php');
8
9         ini_set('memory_limit', '200M');
10
11         $oDB =& getDB();
12
13         // Display defaults
14         $fLat = CONST_Default_Lat;
15         $fLon = CONST_Default_Lon;
16         $iZoom = CONST_Default_Zoom;
17
18         $oGeocode = new Geocode($oDB);
19
20         $aLangPrefOrder = getPreferredLanguages();
21         $oGeocode->setLanguagePreference($aLangPrefOrder);
22
23         if (CONST_Search_ReversePlanForAll
24                 || isset($aLangPrefOrder['name:de'])
25                 || isset($aLangPrefOrder['name:ru'])
26                 || isset($aLangPrefOrder['name:ja'])
27                 || isset($aLangPrefOrder['name:pl']))
28         {
29                 $oGeocode->setReverseInPlan(true);
30         }
31
32         // Format for output
33         $sOutputFormat = 'html';
34         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
35         {
36                 $sOutputFormat = $_GET['format'];
37         }
38
39         // Show / use polygons
40         if ($sOutputFormat == 'html')
41         {
42                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
43         }
44         else
45         {
46                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
47                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
48                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
49                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
50                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
51                 if ( ( ($bAsGeoJSON?1:0)
52                                  + ($bAsKML?1:0)
53                                  + ($bAsSVG?1:0)
54                                  + ($bAsText?1:0)
55                                  + ($bAsPoints?1:0)
56                                  ) > CONST_PolygonOutput_MaximumTypes)
57                 {
58                         if (CONST_PolygonOutput_MaximumTypes)
59                         {
60                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
61                         }
62                         else
63                         {
64                                 userError("Polygon output is disabled");
65                         }
66                         exit;
67                 }
68                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
69                 $oGeocode->setIncludePolygonAsText($bAsText);
70                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
71                 $oGeocode->setIncludePolygonAsKML($bAsKML);
72                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
73         }
74
75         // Polygon simplification threshold (optional)
76         $fThreshold = 0.0;
77         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
78         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
79
80         $oGeocode->loadParamArray($_GET);
81
82         if (CONST_Search_BatchMode && isset($_GET['batch']))
83         {
84                 $aBatch = json_decode($_GET['batch'], true);
85                 $aBatchResults = array();
86                 foreach($aBatch as $aBatchParams)
87                 {
88                         $oBatchGeocode = clone $oGeocode;
89                         $oBatchGeocode->loadParamArray($aBatchParams);
90                         $oBatchGeocode->setQueryFromParams($aBatchParams);
91                         $aSearchResults = $oBatchGeocode->lookup();
92                         $aBatchResults[] = $aSearchResults;
93                 }
94                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
95                 exit;
96         }
97         else
98         {
99                 if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
100                 {
101                         $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
102
103                         // reverse order of '/' separated string
104                         $aPhrases = explode('/', $sQuery);
105                         $aPhrases = array_reverse($aPhrases);
106                         $sQuery = join(', ',$aPhrases);
107                         $oGeocode->setQuery($sQuery);
108                 }
109                 else
110                 {
111                         $oGeocode->setQueryFromParams($_GET);
112                 }
113         }
114
115         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
116
117         $aSearchResults = $oGeocode->lookup();
118         if ($aSearchResults === false) $aSearchResults = array();
119
120         if ($sOutputFormat=='html')
121         {
122                 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
123                 $sTileURL = CONST_Map_Tile_URL;
124                 $sTileAttribution = CONST_Map_Tile_Attribution;
125         }
126         logEnd($oDB, $hLog, sizeof($aSearchResults));
127
128         $bAsText = $oGeocode->getIncludePolygonAsText();
129         $sQuery = $oGeocode->getQueryString();
130         $sViewBox = $oGeocode->getViewBoxString();
131         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
132         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
133
134         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
135         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
136         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
137         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
138         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
139         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
140         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
141         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
142         $sMoreURL .= '&q='.urlencode($sQuery);
143
144         if (CONST_Debug) exit;
145
146         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');