[GIS] openlayers + mapserver: controlling tiles

mapserveropenlayers-2

I have a mapserver serving the following mapfile:

MAP
    NAME "GLOBCOVER MAP"
    # Map image size
    SIZE 1000 1000
    UNITS meters

    EXTENT -30.001389 -30.106069 80.998611 80.108847

    PROJECTION
        'proj=longlat'
        'ellps=WGS84'
        'towgs84=0,0,0,0,0,0,0'
        'no_defs'
    END

    # Background color for the map canvas -- change as desired
    IMAGECOLOR 255 255 255
    IMAGEQUALITY 95
    IMAGETYPE agg

    OUTPUTFORMAT
        NAME agg
        DRIVER AGG/PNG
        IMAGEMODE RGB
    END

    # Legend
    LEGEND
        IMAGECOLOR 255 255 255
        STATUS ON
        KEYSIZE 18 12
        LABEL
        TYPE BITMAP
        SIZE MEDIUM
        COLOR 0 0 89
    END
END

# Web interface definition. Only the template parameter
# is required to display a map. See MapServer documentation
WEB
    # Set IMAGEPATH to the path where MapServer should
    # write its output.
    IMAGEPATH '/tmp/'

    # Set IMAGEURL to the url that points to IMAGEPATH
    # as defined in your web server configuration
    IMAGEURL '/tmp/'

    # WMS server settings
    METADATA
        'ows_title'           'QGIS-MAP'
      'ows_onlineresource'  'http://localhost/cgi-bin/mapserv?map=/opt/local/apache2/htdocs/eth_luc.map'
  'ows_srs'             'EPSG:4326'
END

#Scale range at which web interface will operate
# Template and header/footer settings
# Only the template parameter is required to display a map. See MapServer documentation
TEMPLATE 'fooOnlyForWMSGetFeatureInfo'
END

LAYER
  NAME 'GLOBCOVER_L4_200901_200912_V2.3'
  TYPE RASTER
  DUMP true
  TEMPLATE fooOnlyForWMSGetFeatureInfo
EXTENT -30.001389 -30.106069 80.998611 80.108847
  DATA '/Users/calvin/work/luc/proj_public/media/GLOBCOVER_L4_200901_200912_V2.3.tif'
  METADATA
      'ows_title' 'GLOBCOVER_L4_200901_200912_V2.3'
  END
  STATUS OFF
  TRANSPARENCY 100
  PROJECTION
      'proj=longlat'
      'ellps=WGS84'
      'towgs84=0,0,0,0,0,0,0'
    'no_defs'
   END
END

END

A png is dynamically generated by mapserver and shown on the web browser as expected from what is specified in my mapfile's EXTENT. The url can be seen in the screenshot here:-

enter image description here

However, when I attempt to make use of openlayers to do the same using the same mapfile, by means of this openlayers code snippet:

<script type="text/javascript">
    $(document).ready(function() {
        var lon = 5;
        var lat = 40;
        var zoom = 5;
        var map, layer;
        var defaultProjection = new OpenLayers.Projection("EPSG:4326")

        map = new OpenLayers.Map({
            div: 'map', 
            displayProjection: defaultProjection
        });
        layer = new OpenLayers.Layer.MapServer( 
            "GLOBCOVER_L4_200901_200912_V2.3", 
            "http://localhost/cgi-bin/mapserv",
            {
                map: '/opt/local/apache2/htdocs/eth_luc.map',
                srs: 'EPSG:4326',
                isBaseLayer: true
            });
        console.log(layer);
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
        map.addControl( new OpenLayers.Control.LayerSwitcher() );
        console.log(map)
    });
</script>

all I am getting are 36 instances of white PNGs tiled and displayed on the browser, as seen here:-

enter image description here

What am I missing in order to serve my dynamically generated mapserver tiles (PNGs) on openlayers? (with the same visual result as what was done by the pure mapserver solution)

Best Answer

In your code the first parameter to the OpenLayers.Layer.MapServer constructor is the name of the layer in OpenLayers rather than MapServer - so it can be set to anything. Try adding a layers parameter to the options instead.

    layer = new OpenLayers.Layer.MapServer( 
        "MyLayerName", 
        "http://localhost/cgi-bin/mapserv",
        {
            layers: 'GLOBCOVER_L4_200901_200912_V2.3',
            map: '/opt/local/apache2/htdocs/eth_luc.map',
            srs: 'EPSG:4326'

        }, {isBaseLayer: true} 
        );

Also you may want to use the WMS layers instead http://trac.osgeo.org/openlayers/wiki/Layer/WMS as it is less application specific, and is a good way of testing that your layers could be read by any WMS client.

Related Question