Retrieving icons for the MapFish layerTree from WMS servers

If you wrote your own layerTree model, you cannot have layer icons generated automatically. In that case, it it always possible to specify an icon URL using a WMS getLegendGraphic request.

As discussed in https://trac.mapfish.org/trac/mapfish/ticket/111, from r462 on, you can use mapfish.Util.getIconUrl

Here's an example of a folder using three sub-layers with getLegendGraphic icons.

                {
                    text: "Reserves biologiques",
                    layerNames: ["Nature:rbi", "Nature:rbd", "Nature:rbm", "Nature:rbib", "Nature:rbdb", "Nature:rbmb"],
                    expanded: false,
                    checked: false,
                    children: [{
                        text: "intégrales",
                        leaf: true,
                        icon: mapfish.Util.getIconUrl(wms_url, {layer: 'rbi', rule: 'rbiclass'})
                    },{
                        text: "dirigées",
                        leaf: true,
                        icon: mapfish.Util.getIconUrl(wms_url, {layer: 'rbd', rule: 'rbdclass'})
                    },{
                        text: "mixtes",
                        leaf: true,
                        icon: mapfish.Util.getIconUrl(wms_url, {layer: 'rbm', rule: 'rbmclass'})
                    }]
                }

The "rule" parameter is the name of a mapserver class in your mapfile for the layer you want to get an icon.

Creating an "empty" OpenLayers layer

If you need to create an empty base layer, use this kind of code:

map.addLayer(new OpenLayers.Layer.Image("No base layer",
                                        'white.png',
                                        map.maxExtent,
                                        new OpenLayers.Size(1, 1)));

Where white.png is a 1x1 pixel white image.

In this example, the user can choose between the aerial map, the city map or a empty background.

Configuring transparency for IE 6

See: http://www.mail-archive.com/mapserver-users@lists.umn.edu/msg14750.html

Solution 1): Use GIF with GD renderer.

Cons: only 1 bit transparency!

OL (untested):

var layer = new OpenLayers.Layer.WMS("boucles", "/wms-lj2009-agg",
                                     {layers: 'boucles,m2_ligne,m2_arrets',
                                      format: 'image/gif',
                                      transparent: true
                                     });

mapfile:

  OUTPUTFORMAT
    NAME gif
    DRIVER "GD/GIF"
    MIMETYPE "image/gif"
    IMAGEMODE PC256
    EXTENSION "gif"
    TRANSPARENT ON
  END

2) Use PNG with alpha transparency: Cons: This is slow

OL:

var layer = new OpenLayers.Layer.WMS("boucles", "/wms-lj2009-agg",
                                     {layers: 'boucles,m2_ligne,m2_arrets',
                                      format: 'image/png',
                                      transparent: true
                                     }, {alpha: true});

mapfile:

 OUTPUTFORMAT
    NAME agg
    DRIVER "AGG/PNG"
    MIMETYPE "image/png"
    IMAGEMODE RGBA
    #FORMATOPTION "INTERLACE=OFF"
  END

Using transparent layers in a tilecache

If you use the transparent: true option in your layer configuration, the TRANSPARENT=true parameter will be set on the TileCache URL, howerver TileCache won't forward that parameter to the target WMS server.

The solution is simply to put the TRANSPARENT=true GET parameter on the TileCache wms url in tilecache.cfg.in:

[background]
url=%WMS_GEOSERVER_URL%?TRANSPARENT=true

Credits to Eric Lemoine (http://openlayers.org/pipermail/tilecache/2007-March/000161.html)

Generating Mapfiles using m4

In your project/configs/default

function post_subst_in_files() {
    if ! type m4 > /dev/null; then
        echo "Error: you need to install m4 package"
        exit 1
    fi
    
    MAPFILE=mymap.map
    echo "Generating ${MAPFILE} with m4"
    (cd $PROJECT_DIR/wms/; m4 ${MAPFILE}.m4 > ${MAPFILE})
}

In your project/wms/mymap.map.m4.in

# %GENERATED_WARNING%
changequote({, })
changecom()

MAPFILE...

# Sample layer repetition
define({_MYPROJECT_LAYER_}, {

LAYER
  NAME "$1"
 
  TYPE POLYGON
  _MYPROJECT_CONNECTION_COMMON_

  DATA "the_geom from $2 as foo using unique oid using SRID=-1"

  PROJECTION
    "init=epsg:21781"
  END

  LABELITEM "label"
  CLASSITEM "gid"

 
  # transparency
  $3
  # maxscale
  $4

  _MYPROJECT_CLASS_COMMON_
 
END

})

define({_MYPROJECT_TRANSPARENCY_}, {TRANSPARENCY 20})

define({_MYPROJECT_LAYER_TWIN_}, {
_MYPROJECT_LAYER_($1, $1, _MYPROJECT_TRANSPARENCY_, $2)
_MYPROJECT_LAYER_($1_outline, $1, , $2)

})

_MYPROJECT_LAYER_TWIN_(cantons, )

Tile caching with apache

See: http://patrick.blog.thus.ch/2008/02/26/an-alternative-to-tilecache

Gzip Javascript and resources

See http://developer.yahoo.net/blog/archives/2007/07/high_performanc_3.html

First (on Debian):

a2enmod deflate
a2enmod headers

Then drop the example "Compress everything except images" from http://httpd.apache.org/docs/2.0/mod/mod_deflate.html in a deflate.conf in your virtual host.

Avoiding pink tiles

If you see pink tiles when using Tilecache, first check that you have a recent Tilecache version installed. A bug about this was corrected recently: http://www.nabble.com/pink-tiles-tp15893763p15897509.html

If the problem still persists, try setting the following variable:

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

Prevent IE from crashing with error on el.ownerDocument.createRange

IE sometimes crashes when body's first node is of 'text' type. See http://trac.mapfish.org/trac/mapfish/ticket/25 for complete tip.

Make browser cache tiles

Use the following Apache config to make the browser can tiles coming from TileCache and MapServer:

<IfModule mod_expires.c>
        <Directory "/path/to/cgi-bin/">
                ExpiresActive On
                ExpiresDefault "access plus 6 hours"
        </Directory>
        <Directory "/path/to/tilecache/dir/">
                ExpiresActive On
                ExpiresDefault "access plus 6 hours"
        </Directory>
</IfModule>

Note: with FF2 if you reload your page using CTRL+SHIFT+R the cache will be disabled. Worse: once you've loaded your page using CTRL+SHIFT+R, FF2 will never activate caching, even if you reload your page using CTRL+R; you'll have to open a new tab.

[perf] Avoid unnecessary image loads when using the layer tree

You should set the "visibility" parameter to false in your layer configuration when using the layer tree. This will prevent the layer from loading an unneeded image if it is not enabled in the layertree. Sample layer configuration:

        new OpenLayers.Layer.WMS(
            "OverviewMap", wmsHost, {
                layers: [
                    'roads'
                ],
                srs: 'EPSG:4326',
                width: '800',
                height: '300',
                format: 'image/png',
                transparent: true
            }, {
                isBaseLayer: true,
                singleTile: true
            })

Generate Tile Cache? resolutions from Open Layers? scales

var resolutions = [];

for (var i = 0; i <= map.numZoomLevels; i++) {
  resolutions.push(map.getResolutionForZoom(i));
}
console.log(resolutions.join(','));