<?php

// This map would show Germany:
$south = deg2rad(47.2);
$north = deg2rad(55.2);
$west = deg2rad(5.8);
$east = deg2rad(15.2);

// This also controls the aspect ratio of the projection
$width = 1000;
$height = 1500;

// Formula for mercator projection y coordinate:
function mercY($lat) { return log(tan($lat/2 + M_PI/4)); }

// Some constants to relate chosen area to screen coordinates
$ymin = mercY($south);
$ymax = mercY($north);
$xFactor = $width/($east - $west);
$yFactor = $height/($ymax - $ymin);

function mapProject($lat, $lon) { // both in radians, use deg2rad if neccessary
  global $xFactor, $yFactor, $west, $ymax;
  $x = $lon;
  $y = mercY($lat);
  $x = ($x - $west)*$xFactor;
  $y = ($ymax - $y)*$yFactor; // y points south
  return array($x, $y);
}

$pts = array( // Source: Wikipedia
 "Berlin",   52, 31, 13, 24,
 "Hamburg",  53, 33, 10, 00,
 "Muenchen", 48, 08, 11, 35,
 "Koelln",   50, 56, 06, 57,
);

for ($i = 0; $i != count($pts); $i += 5) {
  $name = $pts[$i];
  $lat = deg2rad($pts[$i + 1] + $pts[$i + 2]/60);
  $lon = deg2rad($pts[$i + 3] + $pts[$i + 4]/60);
  $res = mapProject($lat, $lon);
  $x = $res[0];
  $y = $res[1];
  print("$name: $x, $y\n");
}

?>
