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