MapCache: The basic settings

mapcache enables tile caching capability which speeds up accessing  for wms services.  So if you have MS4W setup correctly according to this post, you should already have mapcache enabled. The following is a sample of the mapcache module setting.

<IfModule mapcache_module>
   <Directory "C:/ms4w/apps/mapcache/">
      Order Allow,Deny
      Allow from all
   </Directory>
   MapCacheAlias /mapcache "C:/ms4w/apps/mapcache/mapcache.xml"
</IfModule>

According to the sample setting block in httpd.conf file. The url of the mapcache service is ‘http://{server_name}:{server_port}/mapcache’ and the configuration file of the mapcache service is ‘C:/ms4w/apps/mapcache/mapcache.xml’. It is able to set multiple mapcache services from one or multiple directories.

Multiple mapcache services stored in one folder.

<IfModule mapcache_module>
   <Directory "C:/ms4w/apps/mapcache/">
      Order Allow,Deny
      Allow from all
   </Directory>
   MapCacheAlias /mapcache "C:/ms4w/apps/mapcache/mapcache.xml"
   MapCacheAlias /mapcache2 "C:/ms4w/apps/mapcache/mapcache_test.xml"
</IfModule>

Multiple mapcache services stored in different folder.

<IfModule mapcache_module>
   <Directory "C:/ms4w/apps/mapcache/">
      Order Allow,Deny
      Allow from all
   </Directory>
   <Directory "C:/ms4w/apps/mapcache2/">
      Order Allow,Deny
      Allow from all
   </Directory>
   MapCacheAlias /mapcache "C:/ms4w/apps/mapcache/mapcache.xml"
   MapCacheAlias /mapcache2 "C:/ms4w/apps/mapcache2/mapcache_test.xml"
</IfModule>

One xml file stores the configuration of a mapcache service. The best start point of creating a mapcache configuration is making a copy of the mapcache.xml.sample file and remove the ‘.sample’ extension. The sample xml file contains detailed comments for each configuration element. In general, the xml file is composite of many entries as outlined,

<mapcache>
   <grid>....</grid>
   <source>....</source>
   <cache>...</cache>
   <format>...</format>
   <tileset>...</tileset>
   <services>...</services>
</mapcache>
  • grid defines the layout of tiles. The mandatory elements of a grid include srs, extent, resolution, units and size.
     <grid name="GDA94Z56">
          <extent>498007 6984998 501007 6987992</extent>
          <srs>EPSG:28356</srs>
          <srsalias>GDA95 Zone56</srsalias>
          <resolutions>4.233341800016934 2.116670900008467 1.0583354500042335 0.5291677250021167 0.26458386250105836 0.13229193125052918</resolutions>
          <units>m</units>
          <size>256 256</size>
    </grid>
    
  • source defines a wms service url to query for obtaining images. It must have getmap and http elements.
    <source name="mymap" type="wms">
      <getmap>
    	 <params>
    		<FORMAT>image/png</FORMAT>
    		<LAYERS>allLayers</LAYERS>
    	 </params>
      </getmap>
      <http>
    	 <url>http://server_name/cgi-bin/mapserv.exe?MAP=C:/ms4w/apps/test/basemap_wms.map</url>
      </http>
    </source>
    
  • cache indicates where and how the tiles will be stored. The example shows how to use a disk cache.
    <cache name="disk" type="disk">
    	<base>E:/mapcache</base>
    </cache>
    
  • format defines a image format to be used to store tile data on disk and return tile data to client.
    <format name="myjpeg" type ="JPEG">
    	<quality>95</quality>
    </format>
    
  • tileset groups grid, source, cache and format. It is the essential item for a configuration file.
    <tileset name="mymap_jpeg">
    	<source>mymap</source>
    	<cache>disk</cache>
    	<format>myjpeg</format>
    	<grid>GDA94Z56</grid>
    </tileset>
    
  • service indicates the type of service to enable or disable.
    <service type="wms" enabled="true"/>
    <service type="wmts" enabled="true"/>
    <service type="tms" enabled="true"/>
    <service type="kml" enabled="true"/>
    <service type="gmaps" enabled="true"/>
    <service type="ve" enabled="true"/>
    <service type="demo" enabled="true"/>
    

For detailed documentation, please refer to mapcache configuration file document.

Once the configuration file is ready, it is time to generate map cache. There is a simple command line tool called mapcache_seed provided along with mapcache. With MS4W installation, the mapcache_seed is located in ‘C:\ms4w\tools\mapcache’. The following are some examples showing the basic usage of the tool,

  • ‘-c’: configuration file. ‘-t’: tileset in the configure file to be seeded. ‘-g’: grid to be seeded. The grid must be referenced by the tileset.
    mapcache_seed -c mapcache.xml -t mbrc_background_jpeg -g GDA94Z56
    
  • ‘-t’: enable multiple threads to run seeding in parallel. ‘-v’: get verbose log.
    mapcache_seed -c mapcache.xml -t mbrc_background_jpeg -g GDA94Z56 -t 4 -v
    
  • ‘-f’: force existing tiles to be recreated. This is helpful when the source data is updated.
    mapcache_seed -c mapcache.xml -t mbrc_background_jpeg -g GDA94Z56 -t 4 -v -f
    
  • ‘-d’: area of interest to seed. this one is similar to ‘Area of Interest’ parameter on ‘Manage Map Server Cache Tiles’ tool in ArcToolbox. The value is any ogr datasource (shapefile, etc). This is helpful when the source data is updated only in a small area.
    mapcache_seed -c mapcache.xml -t mbrc_background_jpeg -g GDA94Z56 -t 4 -v -f -d "c:\temp\cache_aoi.shp"
    

More information about seeder, please refer to this documentation.

Once seeder has done its work. It is time to enjoy the benefits of mapcache. The service url is like http://{server_name}/{mapcache_alias}. so if the server name is ‘gisserver’ and mapcache alias is ‘gismapcache’, the url to access the service is http://gisserver/gismapcache.

2 thoughts on “MapCache: The basic settings

Leave a comment