Add MapServer Untiled Layer to OpenLayers. Originally developed by Steve

Woodbridge, tests written by Paul Spencer and some reworking done. Thanks
to both Paul and Steve for this code. (Closes #526.) 


git-svn-id: http://svn.openlayers.org/trunk/openlayers@2891 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-03-25 21:20:14 +00:00
parent 67963c3e86
commit f3c75e2d29
5 changed files with 428 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#map {
width: 800px;
height: 475px;
border: 1px solid black;
}
</style>
<script src="../lib/OpenLayers.js"></script>
<script src="../lib/OpenLayers/Layer/MapServer/Untiled.js"></script>
<script type="text/javascript">
window.onload = function(){
var map = new OpenLayers.Map( 'map', {maxResolution: 'auto'} );
var layer = new OpenLayers.Layer.MapServer.Untiled( "MapServer Untiled",
"http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0, 0), 1);
map.addControl( new OpenLayers.Control.LayerSwitcher() );
}
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>

View File

@@ -79,6 +79,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") {
"OpenLayers/Layer/HTTPRequest.js",
"OpenLayers/Layer/Grid.js",
"OpenLayers/Layer/MapServer.js",
"OpenLayers/Layer/MapServer/Untiled.js",
"OpenLayers/Layer/KaMap.js",
"OpenLayers/Layer/MultiMap.js",
"OpenLayers/Layer/Markers.js",

View File

@@ -0,0 +1,237 @@
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/release-license.txt
* for the full text of the license. */
/* Derived from the WMS/Untiled.js script by Stephen Woodbridge, 2007 */
/**
* @class
*
* @requires OpenLayers/Layer/HTTPRequest.js
* @requires OpenLayers/Layer/MapServer.js
*/
OpenLayers.Layer.MapServer.Untiled = OpenLayers.Class.create();
OpenLayers.Layer.MapServer.Untiled.prototype =
OpenLayers.Class.inherit( OpenLayers.Layer.HTTPRequest, {
/** Hashtable of default parameter key/value pairs
* @final @type Object */
default_params: {
mode: "map",
map_imagetype: "png"
},
reproject: true,
/** the ratio of image/tile size to map size (this is the untiled buffer)
* @type int */
ratio: 1,
/** @type OpenLayers.Tile.Image */
tile: null,
/** did the image finish loading before a new draw was initiated?
* @type Boolean */
doneLoading: false,
/**
* @constructor
*
* @param {String} name
* @param {String} url
* @param {Object} params
*/
initialize: function(name, url, params, options) {
var newArguments = [];
newArguments.push(name, url, params, options);
OpenLayers.Layer.HTTPRequest.prototype.initialize.apply(this,
newArguments);
OpenLayers.Util.applyDefaults(
this.params,
this.default_params
);
// unless explicitly set in options, if the layer is transparent,
// it will be an overlay
if ((options == null) || (options.isBaseLayer == null)) {
this.isBaseLayer = ((this.params.transparent != "true") &&
(this.params.transparent != true));
}
},
/**
*
*/
destroy: function() {
if (this.tile) {
this.tile.destroy();
this.tile = null;
}
OpenLayers.Layer.HTTPRequest.prototype.destroy.apply(this, arguments);
},
/**
* @param {Object} obj
*
* @returns An exact clone of this OpenLayers.Layer.MapServer.Untiled
* @type OpenLayers.Layer.MapServer.Untiled
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.MapServer.Untiled(this.name,
this.url,
this.params,
this.options);
}
//get all additions from superclasses
obj = OpenLayers.Layer.HTTPRequest.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
return obj;
},
/** Once HTTPRequest has set the map, we can load the image div
*
* @param {OpenLayers.Map} map
*/
setMap: function(map) {
OpenLayers.Layer.HTTPRequest.prototype.setMap.apply(this, arguments);
},
/** When it is not a dragging move (ie when done dragging)
* reload and recenter the div.
*
* @param {OpenLayers.Bounds} bounds
* @param {Boolean} zoomChanged
* @param {Boolean} dragging
*/
moveTo:function(bounds, zoomChanged, dragging) {
if (!this.doneLoading) {
this.events.triggerEvent("loadcancel");
this.doneLoading = true;
}
OpenLayers.Layer.HTTPRequest.prototype.moveTo.apply(this,arguments);
if (bounds == null) {
bounds = this.map.getExtent();
}
var firstRendering = (this.tile == null);
//does the new bounds to which we need to move fall outside of the
// current tile's bounds?
var outOfBounds = (!firstRendering &&
!this.tile.bounds.containsBounds(bounds));
if ( zoomChanged || firstRendering || (!dragging && outOfBounds) ) {
//clear out the old tile
if (this.tile) {
this.tile.clear();
}
//determine new tile bounds
var center = bounds.getCenterLonLat();
var tileWidth = bounds.getWidth() * this.ratio;
var tileHeight = bounds.getHeight() * this.ratio;
var tileBounds =
new OpenLayers.Bounds(center.lon - (tileWidth / 2),
center.lat - (tileHeight / 2),
center.lon + (tileWidth / 2),
center.lat + (tileHeight / 2));
//determine new tile size
var tileSize = this.map.getSize();
tileSize.w = tileSize.w * this.ratio;
tileSize.h = tileSize.h * this.ratio;
//formulate request url string
var url = this.getURL(tileBounds);
//determine new position (upper left corner of new bounds)
var ul = new OpenLayers.LonLat(tileBounds.left, tileBounds.top);
var pos = this.map.getLayerPxFromLonLat(ul);
if ( this.tile && !this.tile.size.equals(tileSize)) {
this.tile.destroy();
this.tile = null;
}
this.events.triggerEvent("loadstart");
this.doneLoading = false;
if (!this.tile) {
this.tile = new OpenLayers.Tile.Image(this, pos, tileBounds,
url, tileSize);
this.tile.draw();
var onload = function() {
this.doneLoading = true;
this.events.triggerEvent("loadend");
}
OpenLayers.Event.observe(this.tile.imgDiv, 'load',
onload.bindAsEventListener(this));
} else {
this.tile.moveTo(tileBounds, pos);
}
}
},
getURL: function(bounds) {
var tileSize = this.map.getSize();
tileSize.w = tileSize.w * this.ratio;
tileSize.h = tileSize.h * this.ratio;
var url = this.getFullRequestString(
{mapext:bounds.toBBOX().replace(/,/g," "),
imgext:bounds.toBBOX().replace(/,/g," "),
map_size:tileSize.w+' '+tileSize.h,
imgx: tileSize.w/2,
imgy: tileSize.h/2,
imgxy: tileSize.w+" "+tileSize.h
});
return url;
},
/** Once HTTPRequest has updated the url, reload the image div
* @param {String} newUrl
*/
setUrl: function(newUrl) {
OpenLayers.Layer.HTTPRequest.prototype.setUrl.apply(this, arguments);
this.moveTo();
},
/** Once HTTPRequest has updated new params, reload the image div
* @param {Object} newParams
*/
mergeNewParams:function(newParams) {
OpenLayers.Layer.HTTPRequest.prototype.mergeNewParams.apply(this,
[newParams]);
//redraw
this.moveTo(null, true);
},
/** combine the layer's url with its params and these newParams.
*
* Add the SRS parameter from 'projection' -- this is probably
* more eloquently done via a setProjection() method, but this
* works for now and always.
*
* @param {Object} newParams
*
* @type String
*/
getFullRequestString:function(newParams) {
var projection = this.map.getProjection();
this.params.srs = (projection == "none") ? null : projection;
return OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(
this, arguments);
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Layer.MapServer.Untiled"
});

View File

@@ -0,0 +1,163 @@
<html>
<head>A
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var layer;
var name = 'Test Layer';
var url = "http://labs.metacarta.com/cgi-bin/mapserv";
var params = { map: '/mapdata/vmap_wms.map',
Alayers: 'basic'};
function test_01_Layer_MapServer_Untiled_constructor (t) {
t.plan( 4 );
var url = "http://labs.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params);
t.ok( layer instanceof OpenLayers.Layer.MapServer.Untiled, "new OpenLayers.Layer.MapServer returns object" );
t.eq( layer.url, "http://labs.metacarta.com/cgi-bin/mapserv", "layer.url is correct (HTTPRequest inited)" );
t.eq( layer.params.mode, "map", "default mode param correctly copied");
t.eq( layer.params.map_imagetype, "png", "default imagetype correctly copied");
}
function test_04_Layer_MapServer_Untiled_clone (t) {
t.plan(3);
var url = "http://labs.metacarta.com/cgi-bin/mapserv";
var map = new OpenLayers.Map('map', {});
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params);
map.addLayer(layer);
var clone = layer.clone();
layer.tile = [[1,2],[3,4]];
t.ok( clone.tile != layer.tile, "clone does not copy tile");
layer.ratio += 1;
t.eq( clone.ratio, 1, "changing layer.ratio does not change clone.ratio -- a fresh copy was made, not just copied reference");
t.eq( clone.alpha, layer.alpha, "alpha copied correctly");
layer.tile = null;
}
function test_05_Layer_MapServer_Untiled_isBaseLayer(t) {
t.plan(3);
var url = "http://labs.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params);
t.ok( layer.isBaseLayer, "baselayer is true by default");
var newParams = OpenLayers.Util.extend(new Object(), params);
newParams.transparent = "true";
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, newParams);
t.ok( !layer.isBaseLayer, "baselayer is false when transparent is set to true");
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params, {isBaseLayer: false});
t.ok( !layer.isBaseLayer, "baselayer is false when option is set to false" );
}
function test_06_Layer_MapServer_Untiled_mergeNewParams (t) {
t.plan( 5 );
var map = new OpenLayers.Map("map");
var url = "http://labs.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params);
var newParams = { layers: 'sooper',
chickpeas: 'image/png'};
map.addLayer(layer);
map.zoomToMaxExtent();
t.ok( !layer.tile.url.match("chickpeas"), "chickpeas is not in URL of first tile in grid" );
layer.mergeNewParams(newParams);
t.eq( layer.params.layers, "sooper", "mergeNewParams() overwrites well");
t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() adds well");
t.ok( layer.tile.url.match("chickpeas"), "chickpeas is in URL of first tile in grid" );
newParams.chickpeas = 151;
t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() makes clean copy of hashtable");
}
function test_07_Layer_MapServer_Untiled_getFullRequestString (t) {
t.plan( 1 );
var map = new OpenLayers.Map('map');
tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'png'};
var tLayer = new OpenLayers.Layer.MapServer.Untiled(name, tUrl, tParams);
map.addLayer(tLayer);
str = tLayer.getFullRequestString();
var tParams = {
layers: 'basic',
format: 'png',
mode: 'map',
map_imagetype: 'png',
srs: 'EPSG:4326'
};
var sStr = tUrl + "?" + OpenLayers.Util.getParameterString(tParams);
sStr = sStr.replace(/,/g, "+");
t.eq(str, sStr , "getFullRequestString() works");
}
function test_08_Layer_MapServer_Untiled_setOpacity (t) {
t.plan( 4 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.MapServer.Untiled(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(parseFloat(tLayer.div.firstChild.style.opacity), 0.5, "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(parseFloat(tLayer.div.firstChild.style.opacity), 0.6, "Opacity on tile is changed correctly");
}
function test_99_Layer_MapServer_Untiled_destroy (t) {
t.plan( 1 );
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.MapServer.Untiled(name, url, params);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0), 5);
//grab a reference to one of the tiles
var tile = layer.tile;
layer.destroy();
// checks to make sure superclass (grid) destroy() was called
t.ok( layer.tile == null, "tile set to null");
}
// -->
</script>
</head>
<body>
<div id="map" style="width:256px;height:256px"></div>
</body>
</html>

View File

@@ -35,6 +35,7 @@
<li>Layer/test_KaMap.html</li>
<li>Layer/test_Markers.html</li>
<li>Layer/test_Multimap.html</li>
<li>Layer/test_MapServer_Untiled.html</li>
<li>Layer/test_Text.html</li>
<li>Layer/test_WMS.html</li>
<li>Layer/test_TMS.html</li>