7 * Copyright 2009 Gerhard Koch
8 * modded for RRZE settings - unrz191 2012 + 2016-06-02 + 2022-10-28
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 * @author Gerhard Koch <gerhard.koch AT ymail.com>
26 * staticmap.php?center=40.714728,-73.998672&zoom=14&size=512x512&maptype=mapnik&markers=40.702147,-74.015794,blues|40.711614,-74.012318,greeng|40.718217,-73.998284,redc
31 ini_set('display_errors', 'off');
36 protected $maxWidth = 2048;
37 protected $maxHeight = 2048;
39 protected $tileSize = 256;
40 protected $tileSrcUrl = array( 'osmorg' => 'http://osm.rrze.fau.de/tiles/{Z}/{X}/{Y}.png',
41 'osmde' => 'http://osm.rrze.fau.de/osmde/{Z}/{X}/{Y}.png',
42 'osmhd' => 'http://osm.rrze.fau.de/osmhd/{Z}/{X}/{Y}.png',
43 'osmdehd' => 'http://osm.rrze.fau.de/osmdehd/{Z}/{X}/{Y}.png',
44 'luftbilderl' => 'http://osm.rrze.fau.de/luftbilderl2016/{Z}/{X}/{Y}.png'
47 protected $tileDefaultSrc = 'osmde';
48 protected $markerBaseDir = '/var/www/staticmaplite/images/markers';
49 protected $osmLogo = '/var/www/staticmaplite/images/osm_logo.png';
51 protected $markerPrototypes = array(// found at http://www.mapito.net/map-marker-icons.html
52 'lighblue' => array('regex'=>'/^lightblue([0-9]+)$/',
55 'offsetImage'=>'0,-19',
58 // openlayers std markers
59 'ol-marker'=> array('regex'=>'/^ol-marker(|-blue|-gold|-green)+$/',
61 'shadow'=>'../marker_shadow.png',
62 'offsetImage'=>'-10,-25',
63 'offsetShadow'=>'-1,-13'
65 // taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
66 'ylw'=> array('regex'=>'/^(pink|purple|red|ltblu|ylw)-pushpin$/',
68 'shadow'=>'../marker_shadow.png',
69 'offsetImage'=>'-10,-32',
70 'offsetShadow'=>'-1,-13'
77 # No point in caching - our source is localhost
78 protected $useTileCache = false;
79 protected $tileCacheBaseDir = 'cache/tiles';
81 protected $useMapCache = false;
82 protected $mapCacheBaseDir = 'cache/maps';
83 protected $mapCacheID = '';
84 protected $mapCacheFile = '';
85 protected $mapCacheExtension = 'png';
88 protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype;
89 protected $centerX, $centerY, $offsetX, $offsetY;
91 public function __construct()
98 $this->markers = array();
99 $this->maptype = $this->tileDefaultSrc;
102 public function parseParams()
106 if (!empty($_GET['show'])) {
107 $this->parseOjwParams();
110 $this->parseLiteParams();
114 public function parseLiteParams()
116 // get zoom from GET paramter
117 $this->zoom = $_GET['zoom'] ? intval($_GET['zoom']) : 0;
118 if ($this->zoom > 19) $this->zoom = 19;
120 // get lat and lon from GET paramter
121 list($this->lat, $this->lon) = explode(',', $_GET['center']);
122 $this->lat = floatval($this->lat);
123 $this->lon = floatval($this->lon);
125 // get size from GET paramter
127 list($this->width, $this->height) = explode('x', $_GET['size']);
128 $this->width = intval($this->width);
129 if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
130 $this->height = intval($this->height);
131 if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
133 if (!empty($_GET['markers'])) {
134 $markers = explode('|', $_GET['markers']);
135 foreach ($markers as $marker) {
136 list($markerLat, $markerLon, $markerType) = explode(',', $marker);
137 $markerLat = floatval($markerLat);
138 $markerLon = floatval($markerLon);
139 $markerType = basename($markerType);
140 $this->markers[] = array('lat' => $markerLat, 'lon' => $markerLon, 'type' => $markerType);
144 if ($_GET['maptype']) {
145 if (array_key_exists($_GET['maptype'], $this->tileSrcUrl)) {
146 $this->maptype = $_GET['maptype'];
147 if (($_GET['maptype'] == "osmhd") || ($_GET['maptype'] == "osmdehd")) {
148 $this->tileSize = 512;
154 public function parseOjwParams()
156 $this->lat = floatval($_GET['lat']);
157 $this->lon = floatval($_GET['lon']);
158 $this->zoom = intval($_GET['z']);
160 $this->width = intval($_GET['w']);
161 if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
162 $this->height = intval($_GET['h']);
163 if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
166 if (!empty($_GET['mlat0'])) {
167 $markerLat = floatval($_GET['mlat0']);
168 if (!empty($_GET['mlon0'])) {
169 $markerLon = floatval($_GET['mlon0']);
170 $this->markers[] = array('lat' => $markerLat, 'lon' => $markerLon, 'type' => "bullseye");
175 public function lonToTile($long, $zoom)
177 return (($long + 180) / 360) * pow(2, $zoom);
180 public function latToTile($lat, $zoom)
182 return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
185 public function initCoords()
187 $this->centerX = $this->lonToTile($this->lon, $this->zoom);
188 $this->centerY = $this->latToTile($this->lat, $this->zoom);
189 $this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
190 $this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
193 public function createBaseMap()
195 $this->image = imagecreatetruecolor($this->width, $this->height);
196 $startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
197 $startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
198 $endX = ceil($this->centerX + ($this->width / $this->tileSize) / 2);
199 $endY = ceil($this->centerY + ($this->height / $this->tileSize) / 2);
200 $this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize);
201 $this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize);
202 $this->offsetX += floor($this->width / 2);
203 $this->offsetY += floor($this->height / 2);
204 $this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
205 $this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
207 for ($x = $startX; $x <= $endX; $x++) {
208 for ($y = $startY; $y <= $endY; $y++) {
209 $url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]);
210 $tileData = $this->fetchTile($url);
212 $tileImage = imagecreatefromstring($tileData);
214 $tileImage = imagecreate($this->tileSize, $this->tileSize);
215 $color = imagecolorallocate($tileImage, 255, 255, 255);
216 @imagestring($tileImage, 1, 127, 127, 'err', $color);
218 $destX = ($x - $startX) * $this->tileSize + $this->offsetX;
219 $destY = ($y - $startY) * $this->tileSize + $this->offsetY;
220 imagecopy($this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
226 public function placeMarkers()
228 // loop thru marker array
229 foreach ($this->markers as $marker) {
230 // set some local variables
231 $markerLat = $marker['lat'];
232 $markerLon = $marker['lon'];
233 $markerType = $marker['type'];
234 // clear variables from previous loops
235 $markerFilename = '';
238 // check for marker type, get settings from markerPrototypes
240 foreach ($this->markerPrototypes as $markerPrototype) {
241 if (preg_match($markerPrototype['regex'], $markerType, $matches)) {
242 $markerFilename = $matches[0] . $markerPrototype['extension'];
243 if ($markerPrototype['offsetImage']) {
244 list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
246 $markerShadow = $markerPrototype['shadow'];
248 list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
255 // check required files or set default
256 if ($markerFilename == '' || !file_exists($this->markerBaseDir . '/' . $markerFilename)) {
258 $markerFilename = 'lightblue' . $markerIndex . '.png';
259 $markerImageOffsetX = 0;
260 $markerImageOffsetY = -19;
263 // create img resource
264 if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
265 $markerImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerFilename);
267 $markerImg = imagecreatefrompng($this->markerBaseDir . '/lightblue1.png');
270 // check for shadow + create shadow recource
271 if ($markerShadow && file_exists($this->markerBaseDir . '/' . $markerShadow)) {
272 $markerShadowImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerShadow);
276 $destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($markerLon, $this->zoom)));
277 $destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom)));
279 // copy shadow on basemap
280 if ($markerShadow && $markerShadowImg) {
281 imagecopy($this->image, $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
282 0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
285 // copy marker on basemap above shadow
286 imagecopy($this->image, $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
287 0, 0, imagesx($markerImg), imagesy($markerImg));
293 public function tileUrlToFilename($url)
295 return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url);
298 public function checkTileCache($url)
300 $filename = $this->tileUrlToFilename($url);
301 if (file_exists($filename)) {
302 return file_get_contents($filename);
306 public function checkMapCache()
308 $this->mapCacheID = md5($this->serializeParams());
309 $filename = $this->mapCacheIDToFilename();
310 if (file_exists($filename)) return true;
313 public function serializeParams()
315 return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype));
318 public function mapCacheIDToFilename()
320 if (!$this->mapCacheFile) {
321 $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) . "/" . substr($this->mapCacheID, 4);
323 return $this->mapCacheFile . "." . $this->mapCacheExtension;
327 public function mkdir_recursive($pathname, $mode)
329 is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
330 return is_dir($pathname) || @mkdir($pathname, $mode);
333 public function writeTileToCache($url, $data)
335 $filename = $this->tileUrlToFilename($url);
336 $this->mkdir_recursive(dirname($filename), 0777);
337 file_put_contents($filename, $data);
340 public function fetchTile($url)
342 if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
344 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
345 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
346 curl_setopt($ch, CURLOPT_URL, $url);
347 $tile = curl_exec($ch);
349 if ($tile && $this->useTileCache) {
350 $this->writeTileToCache($url, $tile);
356 public function copyrightNotice()
358 $logoImg = imagecreatefrompng($this->osmLogo);
359 imagecopy($this->image, $logoImg, imagesx($this->image) - imagesx($logoImg), imagesy($this->image) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg));
363 public function sendHeader()
365 header('Content-Type: image/png');
366 $expires = 60 * 60 * 24 * 14;
367 header("Pragma: public");
368 header("Cache-Control: maxage=" . $expires);
369 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
372 public function makeMap()
375 $this->createBaseMap();
376 if (count($this->markers)) $this->placeMarkers();
377 if ($this->osmLogo) $this->copyrightNotice();
380 public function showMap()
382 $this->parseParams();
383 if ($this->useMapCache) {
384 // use map cache, so check cache for map
385 if (!$this->checkMapCache()) {
386 // map is not in cache, needs to be build
388 $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
389 imagepng($this->image, $this->mapCacheIDToFilename(), 9);
391 if (file_exists($this->mapCacheIDToFilename())) {
392 return file_get_contents($this->mapCacheIDToFilename());
394 return imagepng($this->image);
399 return file_get_contents($this->mapCacheIDToFilename());
403 // no cache, make map, send headers and deliver png
406 return imagepng($this->image);
413 $map = new staticMapLite();
414 print $map->showMap();