|
|
1 |
<?php |
|
|
2 |
/* |
|
|
3 |
|
|
|
4 |
This is a PHP file to be used as a backend for a ka-Map layer. It requires |
|
|
5 |
PHP with Mapscript and libgd modules installed. The top of the file |
|
|
6 |
is a configuration section: please edit the variables in this configuration |
|
|
7 |
section to meet your needs, then rename this file to tile.php or something |
|
|
8 |
similar and put it in a web accessible directory. More information |
|
|
9 |
on the OpenLayers ka-Map layer is available from: |
|
|
10 |
|
|
|
11 |
http://trac.openlayers.org/wiki/OpenLayers.Layer.KaMap |
|
|
12 |
|
|
|
13 |
*/ |
|
|
14 |
/********************************************************************** |
|
|
15 |
* |
|
|
16 |
* $Id: tile.php,v 1.33 2006/02/07 03:19:55 pspencer Exp $ |
|
|
17 |
* |
|
|
18 |
* purpose: a simple phpmapscript-based tile renderer that implements |
|
|
19 |
* rudimentary caching for reasonable efficiency. Note the |
|
|
20 |
* cache never shrinks in this version so your disk could |
|
|
21 |
* easily fill up! |
|
|
22 |
* |
|
|
23 |
* author: Paul Spencer (pspencer@dmsolutions.ca) |
|
|
24 |
* |
|
|
25 |
* modifications by Daniel Morissette (dmorissette@dmsolutions.ca) |
|
|
26 |
* |
|
|
27 |
* Modified by Christopher Schmidt for OpenLayers redistribution. |
|
|
28 |
* |
|
|
29 |
********************************************************************** |
|
|
30 |
* |
|
|
31 |
* Copyright (c) 2005, DM Solutions Group Inc. |
|
|
32 |
* |
|
|
33 |
* Permission is hereby granted, free of charge, to any person obtaining a |
|
|
34 |
* copy of this software and associated documentation files (the "Software"), |
|
|
35 |
* to deal in the Software without restriction, including without limitation |
|
|
36 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
|
37 |
* and/or sell copies of the Software, and to permit persons to whom the |
|
|
38 |
* Software is furnished to do so, subject to the following conditions: |
|
|
39 |
* |
|
|
40 |
* The above copyright notice and this permission notice shall be included |
|
|
41 |
* in all copies or substantial portions of the Software. |
|
|
42 |
* |
|
|
43 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
|
44 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
|
45 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
|
46 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
|
47 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
|
48 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
|
49 |
* DEALINGS IN THE SOFTWARE. |
|
|
50 |
* |
|
|
51 |
**********************************************************************/ |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/****************************************************************************** |
|
|
55 |
* basic system configuration |
|
|
56 |
* |
|
|
57 |
* kaMap! uses PHP/MapScript and the PHP GD extension to |
|
|
58 |
* render tiles, and uses PHP/MapScript to generate initialization parameters |
|
|
59 |
* a legend, and a keymap from the selected map file. |
|
|
60 |
* |
|
|
61 |
* Make sure to set the correct module names for your PHP extensions. |
|
|
62 |
* |
|
|
63 |
* WINDOWS USERS: you will likely need to use php_gd2.dll instead of php_gd.dll |
|
|
64 |
*/ |
|
|
65 |
$szPHPMapScriptModule = 'php_mapscript.'.PHP_SHLIB_SUFFIX; |
|
|
66 |
$szPHPGDModule = 'php_gd.'.PHP_SHLIB_SUFFIX; |
|
|
67 |
|
|
|
68 |
/****************************************************************************** |
|
|
69 |
* tile generation parameters |
|
|
70 |
* |
|
|
71 |
* kaMap! generates tiles to load in the client application by first rendering |
|
|
72 |
* larger areas from the map file and then slicing them up into smaller tiles. |
|
|
73 |
* This approach reduces the overhead of loading PHP/MapScript and PHP GD and |
|
|
74 |
* drawing the map file. These larger areas are referred to as metaTiles in |
|
|
75 |
* the code. You can set the size of both the small tiles and the metaTiles |
|
|
76 |
* here. A reasonable size for the small tiles seems to be 200 pixels square. |
|
|
77 |
* Smaller tiles seem to cause problems in client browsers by causing too many |
|
|
78 |
* images to be created and thus slowing performance of live dragging. Larger |
|
|
79 |
* tiles take longer to download to the client and are inefficient. |
|
|
80 |
* |
|
|
81 |
* The number of smaller tiles that form a metaTile can also be configured. |
|
|
82 |
* This parameter allows tuning of the tile generator to ensure optimal |
|
|
83 |
* performance and for label placement. MapServer will produce labels only |
|
|
84 |
* within a rendered area. If the area is too small then features may be |
|
|
85 |
* labelled multiple times. If the area is too large, it may exceed MapServer,s |
|
|
86 |
* maximum map size (by default 2000x2000) or be too resource-intensive on the |
|
|
87 |
* server, ultimately reducing performance. |
|
|
88 |
*/ |
|
|
89 |
$tileWidth = 256; |
|
|
90 |
$tileHeight = 256; |
|
|
91 |
$metaWidth = 5; |
|
|
92 |
$metaHeight = 5; |
|
|
93 |
/* $metaBuffer = Buffer size in pixels to add around metatiles to avoid |
|
|
94 |
* rendering issues along the edge of the map image |
|
|
95 |
*/ |
|
|
96 |
$metaBuffer = 10; |
|
|
97 |
|
|
|
98 |
/****************************************************************************** |
|
|
99 |
* in-image debugging information - tile location, outlines etc. |
|
|
100 |
* to use this, you need to remove images from your cache first. This also |
|
|
101 |
* affects the meta tiles - if debug is on, they are not deleted. |
|
|
102 |
*/ |
|
|
103 |
$bDebug = false; |
|
|
104 |
|
|
|
105 |
/****************************************************************************** |
|
|
106 |
* aszMapFiles - an array of map files available to the application. How this |
|
|
107 |
* is used is determined by the application. Each map file is entered into |
|
|
108 |
* this array as a key->value pair. |
|
|
109 |
* |
|
|
110 |
* The key is the name to be used by the tile caching system to store cached |
|
|
111 |
* tiles within the base cache directory. This key should be a single word |
|
|
112 |
* that uniquely identifies the map. |
|
|
113 |
* |
|
|
114 |
* The value associated with each key is an array of three values. The first |
|
|
115 |
* value is a human-readable name to be presented to the user (should the |
|
|
116 |
* application choose to do so) and the second value is the path to the map |
|
|
117 |
* file. It is assumed that the map file is fully configured for use with |
|
|
118 |
* MapServer/MapScript as no error checking or setting of values is done. The |
|
|
119 |
* third value is an array of scale values for zooming. |
|
|
120 |
*/ |
|
|
121 |
|
|
|
122 |
$aszMapFiles = array( |
|
|
123 |
"world" => array( "World", "/path/to/your/mapfile", |
|
|
124 |
array( 10000 ), # in openlayers, the scale array doesn't matter. |
|
|
125 |
"PNG24") |
|
|
126 |
|
|
|
127 |
/* Add more elements to this array to offer multiple mapfiles */ |
|
|
128 |
|
|
|
129 |
); |
|
|
130 |
|
|
|
131 |
/****************************************************************************** |
|
|
132 |
* figure out which map file to use and set up the necessary variables for |
|
|
133 |
* the rest of the code to use. This does need to be done on every page load |
|
|
134 |
* unfortunately. |
|
|
135 |
* |
|
|
136 |
* szMap should be set to the default map file to use but can change if |
|
|
137 |
* this script is called with map=<mapname>. |
|
|
138 |
*/ |
|
|
139 |
$szMap = 'world'; |
|
|
140 |
|
|
|
141 |
/****************************************************************************** |
|
|
142 |
* kaMap! caching |
|
|
143 |
* |
|
|
144 |
* this is the directory within which kaMap! will create its tile cache. The |
|
|
145 |
* directory does NOT have to be web-accessible, but it must be writable by the |
|
|
146 |
* web-server-user and allow creation of both directories AND files. |
|
|
147 |
* |
|
|
148 |
* the tile caching system will create a separate subdirectory within the base |
|
|
149 |
* cache directory for each map file. Within the cache directory for each map |
|
|
150 |
* file, directories will be created for each group of layers. Within the group |
|
|
151 |
* directories, directories will be created at each of the configured scales |
|
|
152 |
* for the application (see mapfile configuration above.) |
|
|
153 |
*/ |
|
|
154 |
$szBaseCacheDir = "/var/cache/kamap/"; |
|
|
155 |
|
|
|
156 |
/***** END OF CONFIGURABLE STUFF - unless you know what you are doing *****/ |
|
|
157 |
/***** *****/ |
|
|
158 |
/***** *****/ |
|
|
159 |
/***** *****/ |
|
|
160 |
/***** END OF CONFIGURABLE STUFF - unless you know what you are doing *****/ |
|
|
161 |
|
|
|
162 |
if (isset($_REQUEST['map']) && isset($aszMapFiles[$_REQUEST['map']])) |
|
|
163 |
{ |
|
|
164 |
$szMap = $_REQUEST['map']; |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
$szMapCacheDir = $szBaseCacheDir.$szMap."/"; |
|
|
168 |
$szMapName = $aszMapFiles[$szMap][0]; |
|
|
169 |
$szMapFile = $aszMapFiles[$szMap][1]; |
|
|
170 |
$anScales = $aszMapFiles[$szMap][2]; |
|
|
171 |
setOutputFormat($aszMapFiles[$szMap][3]); |
|
|
172 |
/****************************************************************************** |
|
|
173 |
* output format of the map and resulting tiles |
|
|
174 |
* |
|
|
175 |
* The output format used with MapServer can greatly affect appearance and |
|
|
176 |
* performance. It is recommended to use an 8 bit format such as PNG |
|
|
177 |
* |
|
|
178 |
* NOTE: the tile caching code in tile.php is not configurable here. It |
|
|
179 |
* currently assumes that it is outputting 8bit PNG files. If you change to |
|
|
180 |
* PNG24 here then you will need to update tile.php to use the gd function |
|
|
181 |
* imagecreatetruecolor. If you change the output format to jpeg then |
|
|
182 |
* you would need to change imagepng() to imagejpeg(). A nice enhancement |
|
|
183 |
* would be to make that fully configurable from here. |
|
|
184 |
*/ |
|
|
185 |
function setOutputFormat($szFormat) |
|
|
186 |
{ |
|
|
187 |
switch($szFormat) { |
|
|
188 |
case "PNG24": |
|
|
189 |
$GLOBALS['szMapImageFormat'] = 'PNG24'; //mapscript format name |
|
|
190 |
$GLOBALS['szMapImageCreateFunction'] = "imagecreatefrompng"; // appropriate GD function |
|
|
191 |
$GLOBALS['szImageExtension'] = '.png'; //file extension |
|
|
192 |
$GLOBALS['szImageCreateFunction'] = "imagecreatetruecolor"; //or imagecreatetruecolor if PNG24 ... |
|
|
193 |
$GLOBALS['szImageOutputFunction'] = "imagepng"; //or imagegif, imagejpeg ... |
|
|
194 |
$GLOBALS['szImageHeader'] = 'image/png'; //the content-type of the image |
|
|
195 |
break; |
|
|
196 |
case "GIF": |
|
|
197 |
$GLOBALS['szMapImageFormat'] = 'GIF'; //mapscript format name |
|
|
198 |
$GLOBALS['szMapImageCreateFunction'] = "imagecreatefromgif"; // appropriate GD function |
|
|
199 |
$GLOBALS['szImageExtension'] = '.gif'; //file extension |
|
|
200 |
$GLOBALS['szImageCreateFunction'] = "imagecreate"; //or imagecreatetruecolor if PNG24 ... |
|
|
201 |
$GLOBALS['szImageOutputFunction'] = "imagegif"; //or imagegif, imagejpeg ... |
|
|
202 |
$GLOBALS['szImageHeader'] = 'image/gif'; //the content-type of the image |
|
|
203 |
break; |
|
|
204 |
case "JPEG": |
|
|
205 |
$GLOBALS['szMapImageFormat'] = 'JPEG'; //mapscript format name |
|
|
206 |
$GLOBALS['szMapImageCreateFunction'] = "imagecreatefromjpeg"; // appropriate GD function |
|
|
207 |
$GLOBALS['szImageExtension'] = '.jpg'; //file extension |
|
|
208 |
$GLOBALS['szImageCreateFunction'] = "imagecreatetruecolor"; //or imagecreatetruecolor if PNG24 ... |
|
|
209 |
$GLOBALS['szImageOutputFunction'] = "imagejpeg"; //or imagegif, imagejpeg ... |
|
|
210 |
$GLOBALS['szImageHeader'] = 'image/jpeg'; //the content-type of the image |
|
|
211 |
break; |
|
|
212 |
case "PNG": |
|
|
213 |
$GLOBALS['szMapImageFormat'] = 'PNG'; //mapscript format name |
|
|
214 |
$GLOBALS['szMapImageCreateFunction'] = "imagecreatefrompng"; // appropriate GD function |
|
|
215 |
$GLOBALS['szImageExtension'] = '.png'; //file extension |
|
|
216 |
$GLOBALS['szImageCreateFunction'] = "imagecreate"; //or imagecreatetruecolor if PNG24 ... |
|
|
217 |
$GLOBALS['szImageOutputFunction'] = "imagepng"; //or imagegif, imagejpeg ... |
|
|
|