]>
Raphaƫl G. Git Repositories - cdn/blob - public/osm.php
   3 //Check for parameter availability 
   4 if (!isset($_GET['t'])||!isset($_GET['lt'])||!isset($_GET['ln'])||!isset($_GET['z'])||!isset($_GET['w'])||!isset($_GET['h'])||!isset($_GET['e'])) { 
   5         //Check for uri presence 
   6         if (empty($_SERVER['REQUEST_URI'])) { 
   7                 throw new Exception('No request uri'); 
   9         //Check for script name in request uri 
  10         if (($pos = strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) !== false) { 
  11                 $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 0, $pos).substr($_SERVER['REQUEST_URI'], $pos+
strlen($_SERVER['SCRIPT_NAME'])); 
  14         if (!preg_match('!^/(?<t>osm|osmar|cycle|mapnik)/(?<lt>-?[0-9\.]+),(?<ln>-?[0-9\.]+),(?<z>[0-9]+)/(?<w>[0-9]+)x(?<h>[0-9]+)\.(?<e>gif|jpe?g|png)$!', $_SERVER['REQUEST_URI'], $_GET)) { 
  15                 throw new Exception('No request match'); 
  18         unset($_GET[0], $_GET[1], $_GET[2], $_GET[3], $_GET[4], $_GET[5], $_GET[6], $_GET[7]); 
  21 //Check latitude value 
  22 if (abs($lt = floatval($_GET['lt'])) > 90) { 
  23         throw new Exception('Invalid latitude'); 
  26 //Check longitude value 
  27 if (abs($ln = floatval($_GET['ln'])) > 180) { 
  28         throw new Exception('Invalid longitude'); 
  32 if (($z = abs(intval($_GET['z']))) > 20) { 
  33         throw new Exception('Invalid zoom'); 
  37 if (($w = abs(intval($_GET['w']))) > 1024) { 
  38         throw new Exception('Invalid width'); 
  42 if (($h = abs(intval($_GET['h']))) > 1024) { 
  43         throw new Exception('Invalid height'); 
  47 if (($t = ($_GET['t']=='mapnik'?'osm':$_GET['t'])) != 'osm' && $t != 'osmar' && $t != 'cycle') { 
  48         throw new Exception('Invalid extension'); 
  51 //Check extension value 
  52 if (($e = ($_GET['e']=='jpeg'?'jpg':$_GET['e'])) != 'gif' && $e != 'jpg' && $e != 'png') { 
  53         throw new Exception('Invalid extension'); 
  56 //Check imagick presence 
  57 if (!class_exists('Imagick')) { 
  58         throw new Exception('No imagick class'); 
  65         //The imagick instance 
  69         //XXX: see https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_servers 
  71                 'osm' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png', 
  72                 'osmar' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png', 
  73                 'cycle' => 'http://a.tile.opencyclemap.org/cycle/{Z}/{X}/{Y}.png', 
  76         function __construct($t, $lt, $ln, $z, $w, $h, $e) { 
  78                 $tx = floor($cx = $this->lnToTx($ln, $z)); 
  79                 $ty = floor($cy = $this->ltToTy($lt, $z)); 
  82                 $sx = floor(($tx * self
::tz 
- $w) / self
::tz
); 
  83                 $sy = floor(($ty * self
::tz 
- $h) / self
::tz
); 
  86                 $ex = floor(($tx * self
::tz + 
$w) / self
::tz
); 
  87                 $ey = floor(($ty * self
::tz + 
$h) / self
::tz
); 
  89                 //Create the base image 
  90                 $this->im 
= new Imagick(); 
  91                 $this->im
->newImage($w, $h, new ImagickPixel('white'), $e); 
  93                 for($x = $sx; $x <= $ex; $x++
) { 
  94                         for($y = $sy; $y <= $ey; $y++
) { 
  96                                 $file = '../cache/'.$z.'/'.$x.'/'.$y.'.png'; 
  97                                 //Create cache directory 
  98                                 if (!is_dir($dir = dirname($file))) { 
  99                                         mkdir($dir, 0777, true); 
 101                                 //Fetch file in cache 
 102                                 if (!file_exists($file)) { 
 103                                         $url = str_replace(['{Z}', '{X}', '{Y}'], [$z, $x, $y], $this->uris
[$t]); 
 104                                         file_put_contents($dir.'/'.$y.'.png', file_get_contents($url)); 
 108                                 #$dx = ($x - floor($this->centerX - ($this->width / $this->tileSize) / 2)) * $this->tileSize + floor((floor($this->centerX) - $this->centerX) * $this->tileSize); 
 109                                 $dx = ($x - floor($cx - ($w / self
::tz
) / 2)) * self
::tz + 
floor(($tx - $cx) * self
::tz
); 
 110                                 #$destY = ($y - $startY) * $this->tileSize + $this->offsetY; 
 111                                 $dy = ($y - floor($cy - ($h / self
::tz
) / 2)) * self
::tz + 
floor(($ty - $cy) * self
::tz
); 
 114                                 $tm = new Imagick($file); 
 115                                 //Add it at destination 
 116                                 $this->im
->compositeImage($tm, imagick
::COMPOSITE_COPY
, $dx, $dy); 
 120                 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916 
 121                 $draw = new ImagickDraw(); 
 122                 $draw->setFillColor('#cff'); 
 123                 $draw->setStrokeColor('#00c3f9'); 
 124                 $draw->setStrokeWidth(2); 
 125                 $draw->circle($w/2 - 5, $h/2 - 5, $w/2 + 
5, $h/2 + 
5); 
 126                 $this->im
->drawImage($draw); 
 127                 header('Content-Type: image/'.$e); 
 132          * Convert longitude to tile x number 
 134          * @see https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_5 
 136          * @param $ln The longitude 
 141         public function lnToTx($ln, $z) { 
 142                 return (($ln + 
180) / 360) * pow(2, $z); 
 146          * Convert latitude to tile y number 
 148          * @see https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_5 
 150          * @param $lt The latitude 
 155         public function ltToTy($lt, $z) { 
 156                 return (1 - log(tan(deg2rad($lt)) + 
1 / cos(deg2rad($lt))) / pi()) / 2 * pow(2, $z); 
 160          * Convert tile x to longitude 
 162          * @param $tx The tile x 
 165          * @return $ln The longitude 
 167         public function txToLn($tx, $z) { 
 168                 return $tx / pow(2, $z) * 360.0 - 180.0; 
 172          * Convert tile y to latitude 
 174          * @param $ty The tile y 
 177          * @return $lt The latitude 
 179         public function tyToLt($ty, $z) { 
 180                 return rad2deg(atan(sinh(pi() * (1 - 2 * $ty / pow(2, $z))))); 
 187 header('Content-Type: text/plain'); 
 189 $o = new Osm($t, $lt, $ln, $z, $w, $h, $e); 
 193                 $cx = $this->lnToTile($lt, $z); 
 196                 $cy = $this->ltToTile($ln, $z); 
 199                 $ox = floor((floor($cx) - $cx) * $tz); 
 201                 $oy = floor((floor($cy) - $cy) * $tz); 
 203                 //TODO: see this fucking computing 
 204                 $sx = floor($cx - ($w / $tz) / 2); 
 205                 $sy = floor($cy - ($h / $tz) / 2); 
 206                 $ex = ceil($cx + ($w / $tz) / 2); 
 207                 $ey = ceil($cy + ($h / $tz) / 2); 
 209                 $ox = -floor(($cx - floor($cx)) * $tz); 
 210                 $oy = -floor(($cy - floor($cy)) * $tz); 
 212                 $ox += floor($w / 2); 
 213                 $oy += floor($h / 2); 
 214                 $ox += floor($sx - floor($cx)) * $tz; 
 215                 $oy += floor($sy - floor($cy)) * $tz; 
 218                 #$ox = floor($tx - floor($tx)) * $tz; 
 219                 #$oy = floor($ty - floor($ty)) * $tz; 
 231                 #$im->newImage($w, $h, new ImagickPixel('transparent'), $e); 
 233                 #for($x = $tx; $x <= $tx; $x++) { 
 234                         #for($y = $ty; $y <= $ty; $y++) { 
 235 #                               $my = new Imagick($file); 
 236                                 #$my->newImage(5, 5, new ImagickPixel('black'), $e); 
 238 #$this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize); 
 239 #$this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize); 
 240 #$this->offsetX += floor($this->width / 2); 
 241 #$this->offsetY += floor($this->height / 2); 
 242 #$this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize; 
 243 #$this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize; 
 244 #$destX = ($x - $startX) * $this->tileSize + $this->offsetX; 
 245 #$destY = ($y - $startY) * $this->tileSize + $this->offsetY; 
 246 #        $this->centerX = $this->lonToTile($this->lon, $this->zoom); 
 247 #        $this->centerY = $this->latToTile($this->lat, $this->zoom); 
 248 #        $this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize); 
 249 #        $this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize); 
 251 #                               $im->compositeImage($my, imagick::COMPOSITE_COPY, $dx, $dy); 
 252 #                               #floor($w/2)-floor($tz/2), floor($h/2)-floor($tz/2)); 
 253 #                               header('Content-Type: image/'.$e); 
 257 #                               $dx = ($x - $sx) * $tz + floor($w / 2); 
 258 #                               $dy = ($y - $sy) * $tz + floor($h / 2); 
 262 #                               var_dump(floor($w / 2) - floor(($tx - floor($tx)))); 
 267 #                               $tm = new Imagick($url); 
 268 #                               $im->compositeImage($tm, imagick::COMPOSITE_OVER, $dx, $dy);