source: subversion/applications/rendering/wms/class.backend_api.php @ 29350

Last change on this file since 29350 was 9330, checked in by isortega, 11 years ago

WMS now features basic osmarender support (only z17), as well as more bugs.

File size: 2.9 KB
Line 
1<?php
2
3/// @author Iván Sánchez Ortega <ivan@sanchezortega.es>
4
5/**
6    OSM WMS ("OpenStreetMap Web Map Service")
7    Copyright (C) 2008, Iván Sánchez Ortega
8
9    This program is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation, either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21*/   
22
23/// Backend for using the OSM 0.5 API to retrieve the data
24
25
26class backend_api extends backend
27{
28
29        static $base_api_url = "http://www.openstreetmap.org";
30       
31        /// Returns an URL to retrieve the data from. Might be a live HTTP URL, or a local temporary .osm file.
32        /// TODO: projection, projection, projection.
33        function data_url($bbox)
34        {
35                list($left,$bottom,$right,$top) = explode(',',$bbox);
36               
37                if ( !is_numeric($left) ||
38                     !is_numeric($bottom) ||
39                     !is_numeric($right) ||
40                     !is_numeric($top))
41                        trigger_error("Coordinates of the bounding box are not numbers!",E_USER_ERROR);
42               
43                if ( ($top-$bottom > 0.25) || ($right-$left > 0.25) )
44                        trigger_error("OSM API backend won't accept a request greater than 0.25 degrees (measured in latitude-longitude). Please request a smaller area.",E_USER_ERROR);
45               
46               
47                return self::$base_api_url . "/api/0.5/map?bbox=$left,$bottom,$right,$top";
48        }
49       
50        /// Returns the data as a string, corresponding to an .osm file
51        /// TODO: catch any errors encountered when downloading data.
52        /// TODO: use CURL.
53        function get_data_as_osm($bbox)
54        {
55
56                $ch = curl_init();
57
58                // set URL and other appropriate options
59                curl_setopt($ch, CURLOPT_URL, $this->data_url($bbox) );
60                curl_setopt($ch, CURLOPT_HEADER, true);
61                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62                curl_setopt($ch, CURLOPT_USERAGENT, 'OSM WMS');
63//              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
64               
65                $data = curl_exec($ch);
66       
67                $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
68               
69                if ($http_code != 200)
70                {
71                        $headers = explode("\n",substr($data,0,strpos($data,"\r\n\r\n")));
72                        foreach($headers as $header)
73                        {
74                                if (strstr($header,'Error: '))
75                                        trigger_error("OSM server returned HTTP code $http_code.\nOSM server also returned the following error information:\n$header\n(URL was " . $this->data_url($bbox) . ")"  ,E_USER_ERROR);
76                        }
77                       
78                        trigger_error("OSM server returned HTTP code $http_code.\n(URL was " . $this->data_url($bbox) . ")"  ,E_USER_ERROR);
79                }
80       
81                // No errors, cut the header...
82//      var_dump($data);
83//      var_dump( strstr($data,"\r\n\r\n"));
84                $data = strstr($data,"\r\n\r\n");
85                return substr($data,4);
86
87
88//              return $data;
89        }
90       
91
92       
93}
94
95
96
97
Note: See TracBrowser for help on using the repository browser.