fork(39) download
  1. <?php
  2.  
  3. // This map would show Germany:
  4. $south = deg2rad(47.2);
  5. $north = deg2rad(55.2);
  6. $west = deg2rad(5.8);
  7. $east = deg2rad(15.2);
  8.  
  9. // This also controls the aspect ratio of the projection
  10. $width = 1000;
  11. $height = 1500;
  12.  
  13. // Formula for mercator projection y coordinate:
  14. function mercY($lat) { return log(tan($lat/2 + M_PI/4)); }
  15.  
  16. // Some constants to relate chosen area to screen coordinates
  17. $ymin = mercY($south);
  18. $ymax = mercY($north);
  19. $xFactor = $width/($east - $west);
  20. $yFactor = $height/($ymax - $ymin);
  21.  
  22. function mapProject($lat, $lon) { // both in radians, use deg2rad if neccessary
  23. global $xFactor, $yFactor, $west, $ymax;
  24. $x = $lon;
  25. $y = mercY($lat);
  26. $x = ($x - $west)*$xFactor;
  27. $y = ($ymax - $y)*$yFactor; // y points south
  28. return array($x, $y);
  29. }
  30.  
  31. $pts = array( // Source: Wikipedia
  32. "Berlin", 52, 31, 13, 24,
  33. "Hamburg", 53, 33, 10, 00,
  34. "Muenchen", 48, 08, 11, 35,
  35. "Koelln", 50, 56, 06, 57,
  36. );
  37.  
  38. for ($i = 0; $i != count($pts); $i += 5) {
  39. $name = $pts[$i];
  40. $lat = deg2rad($pts[$i + 1] + $pts[$i + 2]/60);
  41. $lon = deg2rad($pts[$i + 3] + $pts[$i + 4]/60);
  42. $res = mapProject($lat, $lon);
  43. $x = $res[0];
  44. $y = $res[1];
  45. print("$name: $x, $y\n");
  46. }
  47.  
  48. ?>
  49.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Berlin: 808.51063829787, 532.98349489962
Hamburg: 446.8085106383, 331.75803958474
Muenchen: 615.24822695035, 1361.0717330001
Koelln: 122.34042553191, 832.33199570761