Skip to content

Instantly share code, notes, and snippets.

@yarl
Created January 2, 2014 18:32
Show Gist options
  • Save yarl/8224075 to your computer and use it in GitHub Desktop.
Save yarl/8224075 to your computer and use it in GitHub Desktop.
Geoportal WMS to TMS conversion.
<?php
$zoom = htmlspecialchars($_GET["zoom"]);
$x = htmlspecialchars($_GET["x"]);
$y = htmlspecialchars($_GET["y"]);
//explanation: https://gist.github.com/tmcw/4954720
$y = pow(2, $zoom)-$y-1;
//source: https://github.com/timwaters/whoots/blob/master/whoots.rb
function get_tile_bbox($x,$y,$z) {
$merc1 = get_merc_coords($x * 256, $y * 256, $z);
$merc2 = get_merc_coords(($x + 1) * 256, ($y + 1) * 256, $z);
$min_x = $merc1['merc_x']; $min_y = $merc1['merc_y'];
$max_x = $merc2['merc_x']; $max_y = $merc2['merc_y'];
return $min_x.",".$min_y.",".$max_x.",".$max_y;
}
function get_merc_coords($x,$y,$z) {
$resolution = (2 * pi() * 6378137 / 256) / (pow(2,$z));
$merc_x = ($x * $resolution - 2 * pi() * 6378137 / 2.0);
$merc_y = ($y * $resolution - 2 * pi() * 6378137 / 2.0);
return compact('merc_x', 'merc_y');
}
$bbox = get_tile_bbox($x,$y,$zoom);
header('Location: http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer?FORMAT=image/jpeg&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS=Raster&STYLES=&SRS=EPSG:3857&WIDTH=265&HEIGHT=256&BBOX='.$bbox);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment