Pre-caching system
From Ka-Map Wiki
The precache script is an utility to pre-generate image tiles. See usage below. These three solutions are useful to adapt to different needing and configuration.
Contents |
[edit] precache.php
The precache.php script is a command line utility to pre-generate image tiles. See usage below. Without any options, precache.php generates tiles for all maps specified in config.php. Tiles are generated for the extent specified by each mapfile's EXTENT unless the max_extents is defined (see MapFileMetadata) - if max_extents is defined, precache.php uses these bounds to generate tiles.
[edit] Usage
php precache.php {<-options>} <tile_url>
Where <tile_url> is the URL to your tile.php script (e.g. http://localhost/ka-map/htdocs/tile.php)
Options:
-f Force tile.php to overwrite existing cached images
-m <map> Limit cache creation to a single map
-s <scale> Limit cache creation to a single scale
[edit] Troubleshooting
[edit] CLI module issue
Under certain configurations (at least one build under Debian 'etch', mapserver 4.8.3, apache2,php-4.4.2)
running `php precache.php http://www.example.com/ka-map/tile.php`
Will yield the following:
Fatal error: This build of mapscript can't be load [sic] as a 'cli' module for stability reason, but only with php as an apache DSO.
To explain: Precache is running as a command line (cli) app, while mapserver (and specifically, php_mapscript) was built to be run in a php module.
One solution: Following this thread http://lists.umn.edu/cgi-bin/wa?A2=ind0508&L=mapserver-users&P=R32139&I=-3&X=32BE8F099C6A2E2350 the following steps were taken to produce a functioning precache.php
I. download a fresh PHP source package.
II. Configure using the following ./configure --with-apxs2=/usr/bin/apxs2 (no other configure arguments are necessary. apxs2 represents the Apache2 DSO) 'make', but do *not* 'make install' If successful, there will thus be a newly-built PHP tree in (for example) /home/me/php-4.4.2
III. Re-configure Mapserver, making only the following change: Where previously the configure switch read --with-php=/usr/lib/php4 (or similar... or may have been absent entirely) Replace with --with-php=/home/me/php-4.4.2 (ie the new php build) `make` (a new Mapserver -- but more specifically a new php_mapscript.so)
IV. Copy the newly-built php_mapscript.so to (e.g.) php_mapscript_dso.so and place it in the same directory as the original (e.g. /usr/lib/php4/20050606)
V. Modify precache.php and config.php to point to point to the new php_mapscript_dso.so (so as not to corrupt anything else)
VI. Proceed as per the original precache.php instructions.
VII. Everything else (ie besides the new php_mapscript.so) in the new PHP and Mapserver builds may be discarded.
[edit] precache2.php
[edit] Usage version 1.4
24/04/2006
php precache2.php [<-options>]
Options:
-f Force tile.php to overwrite existing cached images
-m <map> Limit cache creation to a single map
-s <scale> Limit cache creation to a single scale
-h Display this message
[edit] For Very Large Shp Files
- Add this to your precache2.php file - typical php install will only give you a small amount of memory to work with so do this
//this file is precache2.php - put this in at the very begginning
//set the memory limit in the file instead of in php.ini (memory limit can be set to your choosing)
//make sure you have enough linux:swap windows:virtual memory on your drive if you don't have enough memory
//if you go into swap/virtual memory, expect a slow down on compiling the tiles
//raster images may take a long time to compile depending on your setup
ini_set("memory_limit","600M");
[edit] Windows Usage
php.exe is located at "drive:\ms4w\Apache\cgi-bin\php.exe" in ms4w, and it must be called from wherever it is unless it is in the path. You must cd to the correct directory containing the precache2.php file, in "drive:\path-to-ka-map\htdocs\". Note: The name of the map IS case sensitive.
example, with ms4w located on the c drive:
c:\> cd ms4w\apps\ka-map-1.0.b1\htdocs\ c:\ms4w\apps\ka-map-1.0.b1\htdocs> c:\ms4w\Apache\cgi-bin\php.exe precache2.php -m "GMap 75"
The PROJECTION keyword cannot use the proj4 database properly in the current version(beta 1) but inline projections work fine. The script may exit at the creation of the base directory with no error, if so comment out those lines (90 and 91) if you already have the cache directory, located at "drive:\ms4w\tmp\ms_tmp\kacache\" for ms4w
[edit] Troubleshooting
[edit] precache2.php from a web page
We struggled for quite a while to get the precache2.php script to run from the command line.
Since we are in a university setting, we have less control over actual server settings without going through a lengthy request process. Plus, php really isn't intended to be run as CLI.
By modifying the top of the script as follows we were able to get the script to run from a web browser. The time limit is the obvious factor, as most servers have a maximum of 30 or 60 seconds for a script. In addition, we wrapped an echo "<pre>"; and the closing version around the script to get processing details.
Hope this helps someone else! Percy PSU Geology
set_time_limit(1200);
//if(!isset($argv)) {
// exit('This is a command line utility');
//}
ini_set("memory_limit","600M");
set_time_limit(1200);
echo "<pre>"; --- rest of script here --- echo "</pre>";
[edit] precache_wget.php
Daniel Muller provided this script that helps a lot not to overload the server during precaching. It's useful in many other ways.
using wget you can help managing bandwidth, request timing and much more.
it works on any server configuration tested until now (debian, slackware, os x)
[edit] usage
Call through the browser the
precache_wget.php
- params
* &force=true Force tile.php to overwrite existing cached images * &map=<map> Limit cache creation to a single map * &scale=<scale> Limit cache creation to a single scale (if &map specified)
The precache.url file is generated on the server in kamapcache/wget_precache/precache-Timestamp.url
- es
- /home/myuser/kamapcache/wget_precache/precache-2007-02-19-18-59-08.url
Then launch wget directly on the server, so no bandwidth and download issues will befaced.
#wget -i precache-xxxx-xx-xx-xx-xx-xx.url -b -O /dev/null -o precache.log
[edit] security issue
- remember to change script name or delete it to avoid people to play with your precache_wget.php file.
this solution could be enough:
#cp precache_wget.php precache_wget.php.off
the extension will not be reconized by PHP and commands will not be executed.

