]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
Merge pull request #447 from lonvia/fix-array-expression
[nominatim.git] / website / reverse.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Reverse');
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/PlaceLookup.php');
8         require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
9
10         if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
11         {
12                 $fLoadAvg = getLoadAverage();
13                 if ($fLoadAvg > 2) sleep(60);
14                 if ($fLoadAvg > 4) sleep(120);
15                 if ($fLoadAvg > 6)
16                 {
17                         echo "Bulk User: Temporary block due to high server load\n";
18                         exit;
19                 }
20         }
21
22
23         $bAsPoints = false;
24         $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
25         $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
26         $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
27         $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
28         if ( ( ($bAsGeoJSON?1:0)
29                          + ($bAsKML?1:0)
30                          + ($bAsSVG?1:0)
31                          + ($bAsText?1:0)
32                          + ($bAsPoints?1:0)
33                          ) > CONST_PolygonOutput_MaximumTypes)
34         {
35                 if (CONST_PolygonOutput_MaximumTypes)
36                 {
37                         userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
38                 }
39                 else
40                 {
41                         userError("Polygon output is disabled");
42                 }
43                 exit;
44         }
45
46
47         // Polygon simplification threshold (optional)
48         $fThreshold = 0.0;
49         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
50
51
52         $oDB =& getDB();
53         ini_set('memory_limit', '200M');
54
55         // Format for output
56         $sOutputFormat = 'xml';
57         if (isset($_GET['format']) && ( $_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
58         {
59                 $sOutputFormat = $_GET['format'];
60         }
61
62         // Preferred language
63         $aLangPrefOrder = getPreferredLanguages();
64
65         $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
66
67
68         if (isset($_GET['osm_type']) && isset($_GET['osm_id']) && (int)$_GET['osm_id'] && ($_GET['osm_type'] == 'N' || $_GET['osm_type'] == 'W' || $_GET['osm_type'] == 'R'))
69         {
70                 $aLookup = array('osm_type' => $_GET['osm_type'], 'osm_id' => $_GET['osm_id']);
71         }
72         else if (isset($_GET['lat']) && isset($_GET['lon']) && preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $_GET['lat']) && preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $_GET['lon']))
73         {
74                 $oReverseGeocode = new ReverseGeocode($oDB);
75                 $oReverseGeocode->setLanguagePreference($aLangPrefOrder);
76
77                 $oReverseGeocode->setLatLon($_GET['lat'], $_GET['lon']);
78                 $oReverseGeocode->setZoom(@$_GET['zoom']);
79
80                 $aLookup = $oReverseGeocode->lookup();
81                 if (CONST_Debug) var_dump($aLookup);
82         }
83         else
84         {
85                 $aLookup = null;
86         }
87
88         if ($aLookup)
89         {
90                 $oPlaceLookup = new PlaceLookup($oDB);
91                 $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
92                 $oPlaceLookup->setIncludeAddressDetails(getParamBool('addressdetails', true));
93                 $oPlaceLookup->setIncludeExtraTags(getParamBool('extratags', false));
94                 $oPlaceLookup->setIncludeNameDetails(getParamBool('namedetails', false));
95
96                 $aPlace = $oPlaceLookup->lookupPlace($aLookup);
97
98                 $oPlaceLookup->setIncludePolygonAsPoints($bAsPoints);
99                 $oPlaceLookup->setIncludePolygonAsText($bAsText);
100                 $oPlaceLookup->setIncludePolygonAsGeoJSON($bAsGeoJSON);
101                 $oPlaceLookup->setIncludePolygonAsKML($bAsKML);
102                 $oPlaceLookup->setIncludePolygonAsSVG($bAsSVG);
103                 $oPlaceLookup->setPolygonSimplificationThreshold($fThreshold);
104
105                 $fRadius = $fDiameter = getResultDiameter($aPlace);
106                 $aOutlineResult = $oPlaceLookup->getOutlines($aPlace['place_id'], $aPlace['lon'], $aPlace['lat'], $fRadius);
107
108                 if ($aOutlineResult)
109                 {
110                         $aPlace = array_merge($aPlace, $aOutlineResult);
111                 }
112         }
113         else
114         {
115                 $aPlace = null;
116         }
117
118
119         if (CONST_Debug)
120         {
121                 var_dump($aPlace);
122                 exit;
123         }
124
125         if ($sOutputFormat=='html')
126         {
127                 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
128                 $sTileURL = CONST_Map_Tile_URL;
129                 $sTileAttribution = CONST_Map_Tile_Attribution;
130         }
131         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');