source: subversion/applications/rendering/gpx_slippy_map/tile.php @ 30322

Last change on this file since 30322 was 7915, checked in by ojw, 11 years ago

reduce resolution slightly, seems to help with eastern-hemisphere
tracks?

File size: 3.3 KB
Line 
1<?php
2#----------------------------------------------------------------
3# Render a GPX tracklog onto a slippy-map tile
4#
5#------------------------------------------------------
6# Usage:
7#   tile.php?gpx=12345&t=7/8/9
8#  where:
9#   12345 is the ID of the GPX file on openstreetmap.org/traces
10#   7/8/9 is the tile ID (z/x/y)
11#------------------------------------------------------
12# Copyright 2008, Oliver White
13#
14# This program is free software: you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation, either version 3 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program.  If not, see <http://www.gnu.org/licenses/>.
26#------------------------------------------------------
27$time_start = microtime(true);
28
29include_once("gpx.php");
30include_once("projection.php");
31
32# Create a transparent image
33$W = $H = 256;
34$Image = imagecreatetruecolor($W,$H);
35$BG = imagecolorallocate($Image,255,255,0);
36imagecolortransparent($Image, $BG);
37imagefilledrectangle($Image,0,0,$W,$H,$BG);
38
39# Which tile are we?
40if(preg_match("{(\d+)/(\d+)/(\d+)}", $_GET['t'], $Matches))
41{
42 
43  # Load the GPX file
44  $GPX = $_GET["gpx"] + 0;
45  $Filename = getGpx($GPX);
46
47  # Plot the GPX
48  makeTile(
49    $Image, 
50    $W,
51    $H, 
52    $Matches[2] + 0,
53    $Matches[3] + 0,
54    $Matches[1] + 0,
55    $Filename);
56}
57
58
59if($_GET['benchmark'])
60{
61  $time_end = microtime(true);
62  $time = $time_end - $time_start;
63 
64  header("Content-type:text/plain");
65  printf("Tile took %1.2f ms\n", $time * 1000.0);
66  exit;
67}
68
69# Send image to browser
70header("Content-type:image/PNG");
71imagepng($Image);
72
73
74function makeTile($Image,$W,$H,$x,$y,$z,$Filename)
75{
76  #imagestring($Image,4,10,10,"$x,$y,$z", $Image);return;
77 
78  if($z > 19)
79    return;
80  if(!tileValid($x,$y,$z))
81    return;
82
83  list($x1,$y1,$x2,$y2,$dx,$dy) = relativeTileEdges($x,$y,$z);
84
85  $FG = imagecolorallocate($Image,0,0,50);
86 
87  # Get the GPX details
88  $Data = getMeta($Filename, 1); // 1 means leave the filepointer open
89 
90  if(!$Data['exists'] || !$Data['valid'])
91    {
92    print "Invalid";
93    return;
94    }
95   
96  # If the data was valid, then getMeta will have returned an open
97  # filepointer
98  $fp = $Data['fp'];
99 
100  $NothingOnThisTile = (
101    $Data['points'] < 1
102    || $Data['xmin'] > $x2
103    || $Data['xmax'] < $x1
104    || $Data['ymin'] > $y2
105    || $Data['ymax'] < $y1);
106
107  if(!$NothingOnThisTile || 1)
108    {
109   
110    $s = 2; # half-size of GPX points (pixels)
111    $Resolution = 1 / pow(2.0, 31);
112   
113    # Loop through the points
114    for($ii = 0; $ii < $Data['points']; $ii++)
115    {
116      # rx,ry are relative to the mercator projection (0-1)
117      list($spare, $rx, $ry) = unpack("N2", fread($fp, 8));
118      $rx *= $Resolution;
119      $ry *= $Resolution;
120 
121      # Convert to pixel positions on the image
122      $x = floor($W * ($rx - $x1) / $dx);
123      $y = floor($H * ($ry - $y1) / $dy);
124     
125      if($x >= 0 and $y >= 0 and $x < $W and $y < $H)
126        {
127        imagefilledrectangle($Image, $x-$s,$y-$s,$x+$s,$y+$s, $FG);
128        }
129    }
130  }
131 
132  fclose($fp);
133}
134
135?>
Note: See TracBrowser for help on using the repository browser.