Adding tiled WMS source

As shown in the example, this adds support for tiled WMS layers.
This commit is contained in:
ahocevar
2012-10-26 21:36:30 +02:00
parent d0e532eadf
commit 084506cdb5
6 changed files with 246 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../style.css" type="text/css">
<style type="text/css">
html, body, #map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
</style>
<link rel="stylesheet" href="../../css/ol.css" type="text/css">
<title>ol3 full-screen demo</title>
<script src="../../build/ol.js" type="text/javascript"></script>
</head>
<body>
<div id="map"></div>
<script src="../wms.js" type="text/javascript"></script>
</body>
</html>

47
examples/wms.html Normal file
View File

@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
html, body, #map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#text {
position: absolute;
top: 8px;
right: 8px;
z-index: 20000;
background-color: white;
padding: 0 0.5em 0.5em 0.5em;
border-radius: 4px;
}
@media only screen and (max-width: 600px) {
#text {
display: none;
}
}
</style>
<title>wms example</title>
</head>
<body>
<div id="map">
<div id="text">
<h1 id="title">Tiled WMS example</h1>
<div id="shortdesc">Example of a tiled WMS layer.</div>
<div id="docs">
<p>See the
<a href="wms.js" target="_blank">wms.js source</a>
to see how this is done.</p>
</div>
</div>
</div>
<div id="tags">wms, tile, tilelayer</div>
<script src="loader.js?id=wms" type="text/javascript"></script>
</body>
</html>

33
examples/wms.js Normal file
View File

@@ -0,0 +1,33 @@
goog.require('goog.debug.Console');
goog.require('goog.debug.Logger');
goog.require('goog.debug.Logger.Level');
goog.require('ol.Collection');
goog.require('ol.Coordinate');
goog.require('ol.Map');
goog.require('ol.source.MapQuestOpenAerial');
goog.require('ol.source.TiledWMS');
if (goog.DEBUG) {
goog.debug.Console.autoInstall();
goog.debug.Logger.getLogger('ol').setLevel(goog.debug.Logger.Level.INFO);
}
var layers = new ol.Collection([
new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
}),
new ol.layer.TileLayer({
source: new ol.source.TiledWMS({
url: 'http://demo.opengeo.org/geoserver/wms',
params: {'LAYERS': 'topp:states', 'TILED': true}
})
})
]);
var map = new ol.Map({
center: new ol.Coordinate(-10997148, 4569099),
layers: layers,
target: 'map',
zoom: 4
});

View File

@@ -59,3 +59,14 @@
@exportObjectLiteralProperty ol.source.BingMapsOptions.culture string|undefined
@exportObjectLiteralProperty ol.source.BingMapsOptions.key string
@exportObjectLiteralProperty ol.source.BingMapsOptions.style ol.BingMapsStyle
@exportObjectLiteral ol.source.TiledWMSOptions
@exportObjectLiteralProperty ol.source.TiledWMSOptions.params Object
@exportObjectLiteralProperty ol.source.TiledWMSOptions.version string|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.crossOrigin string|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.extent ol.Extent|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.tileGrid ol.tilegrid.TileGrid|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.maxZoom number|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.projection ol.Projection|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.url string|undefined
@exportObjectLiteralProperty ol.source.TiledWMSOptions.urls Array.<string>|undefined

View File

@@ -0,0 +1 @@
@exportSymbol ol.source.TiledWMS

View File

@@ -0,0 +1,131 @@
// FIXME add minZoom support
goog.provide('ol.source.TiledWMS');
goog.provide('ol.source.TiledWMSOptions');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Attribution');
goog.require('ol.Projection');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.source.TileSource');
goog.require('ol.tilegrid.TileGrid');
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* params: Object,
* version: (string|undefined),
* crossOrigin: (string|undefined),
* extent: (ol.Extent|undefined),
* tileGrid: (ol.tilegrid.TileGrid|undefined),
* maxZoom: (number|undefined),
* projection: (ol.Projection|undefined),
* url: (string|undefined),
* urls: (Array.<string>|undefined)}}
*/
ol.source.TiledWMSOptions;
/**
* @constructor
* @extends {ol.source.TileSource}
* @param {ol.source.TiledWMSOptions} tiledWMSOptions options.
*/
ol.source.TiledWMS = function(tiledWMSOptions) {
goog.asserts.assert(tiledWMSOptions.params);
var projection = goog.isDef(tiledWMSOptions.projection) ?
tiledWMSOptions.projection : ol.Projection.getFromCode('EPSG:3857');
var extent = goog.isDef(tiledWMSOptions.extent) ?
tiledWMSOptions.extent : projection.getExtent();
var version = goog.isDef(tiledWMSOptions.version) ?
tiledWMSOptions.version : '1.3';
var tileGrid = tiledWMSOptions.tileGrid;
if (!goog.isDef(tileGrid)) {
// FIXME Factor this out to a more central/generic place.
var size = Math.max(extent.maxX - extent.minX, extent.maxY - extent.minY);
var maxZoom = goog.isDef(tiledWMSOptions.maxZoom) ?
tiledWMSOptions.maxZoom : 18;
var resolutions = new Array(maxZoom + 1);
for (var z = 0, zz = resolutions.length; z < zz; ++z) {
resolutions[z] = size / (256 << z);
}
tileGrid = new ol.tilegrid.TileGrid({
origin: extent.getTopLeft(),
resolutions: resolutions
});
}
function tileUrlFunction(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
}
var tileSize = tileGrid.getTileSize();
var tileExtent = tileGrid.getTileCoordExtent(tileCoord);
var params = {
'SERVICE': 'WMS',
'VERSION': version,
'REQUEST': 'GetMap',
'WIDTH': tileSize.width,
'HEIGHT': tileSize.height,
'STYLES': '',
'FORMAT': 'image/png',
'TRANSPARENT': true,
// FIXME Projection dependant axis order.
'BBOX': [
tileExtent.minX, tileExtent.minY, tileExtent.maxX, tileExtent.maxY
].join(',')
};
params[version >= '1.3' ? 'CRS' : 'SRS'] = projection.getCode();
goog.object.extend(params, tiledWMSOptions.params);
var url = tiledWMSOptions.urls ?
tiledWMSOptions.urls[goog.math.modulo(
tileCoord.hash(), tiledWMSOptions.urls.length)] :
tiledWMSOptions.url;
for (var param in params) {
url += (~url.indexOf('?') ? '&' : '?') +
param + '=' + encodeURIComponent(params[param]);
}
return url;
}
function tileCoordTransform(tileCoord) {
if (tileGrid.getResolutions().length <= tileCoord.z) {
return null;
}
var x = tileCoord.x;
var projectionExtent = projection.getExtent();
// FIXME do we want a wrapDateLine param? The code below will break maps
// with projections that do not span the whole world width.
if (extent.minX === projectionExtent.minX &&
extent.maxX === projectionExtent.maxX) {
var n = 1 << tileCoord.z;
x = goog.math.modulo(x, n);
}
var tileExtent = tileGrid.getTileCoordExtent(
new ol.TileCoord(tileCoord.z, x, tileCoord.y));
// FIXME We shouldn't need a typecast here.
if (!tileExtent.intersects(/** @type {ol.Extent} */ (extent))) {
return null;
}
return new ol.TileCoord(tileCoord.z, x, tileCoord.y);
}
goog.base(this, {
attributions: tiledWMSOptions.attributions,
crossOrigin: tiledWMSOptions.crossOrigin,
extent: extent,
tileGrid: tileGrid,
projection: projection,
tileUrlFunction: ol.TileUrlFunction.withTileCoordTransform(
tileCoordTransform, tileUrlFunction)
});
};
goog.inherits(ol.source.TiledWMS, ol.source.TileSource);