Merge pull request #74 from ahocevar/tiled-wms
Adding tiled WMS source. r=@twpayne,@elemoine
This commit is contained in:
47
examples/wms-custom-proj.html
Normal file
47
examples/wms-custom-proj.html
Normal 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>Tiled WMS with custom projection example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map">
|
||||
<div id="text">
|
||||
<h1 id="title">Tiled WMS with custom projection example</h1>
|
||||
<div id="shortdesc">Example of two tiled WMS layers (Pixelmap 1:1'000'000 and national parks) using the projection EPSG:21781.</div>
|
||||
<div id="docs">
|
||||
<p>See the
|
||||
<a href="wms-custom-proj.js" target="_blank">wms-custom-proj.js source</a>
|
||||
to see how this is done.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="tags">wms, tile, tilelayer, projection</div>
|
||||
<script src="loader.js?id=wms-custom-proj" type="text/javascript"></script>
|
||||
</body>
|
||||
</html>
|
||||
55
examples/wms-custom-proj.js
Normal file
55
examples/wms-custom-proj.js
Normal file
@@ -0,0 +1,55 @@
|
||||
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.Projection');
|
||||
goog.require('ol.source.TiledWMS');
|
||||
|
||||
|
||||
if (goog.DEBUG) {
|
||||
goog.debug.Console.autoInstall();
|
||||
goog.debug.Logger.getLogger('ol').setLevel(goog.debug.Logger.Level.INFO);
|
||||
}
|
||||
|
||||
var extent = new ol.Extent(420000, 30000, 900000, 350000);
|
||||
|
||||
var epsg21781 = new ol.Projection('EPSG:21781',
|
||||
ol.ProjectionUnits.METERS, extent);
|
||||
|
||||
var layers = new ol.Collection([
|
||||
new ol.layer.TileLayer({
|
||||
source: new ol.source.TiledWMS({
|
||||
url: 'http://wms.geo.admin.ch/?',
|
||||
attributions: [new ol.Attribution(
|
||||
'© ' +
|
||||
'<a href="http://www.geo.admin.ch/internet/geoportal/en/home.html">' +
|
||||
'Pixelmap 1:1000000 / geo.admin.ch</a>')],
|
||||
crossOrigin: null,
|
||||
params: {'LAYERS': 'ch.swisstopo.pixelkarte-farbe-pk1000.noscale'},
|
||||
projection: epsg21781
|
||||
})
|
||||
}),
|
||||
new ol.layer.TileLayer({
|
||||
source: new ol.source.TiledWMS({
|
||||
url: 'http://wms.geo.admin.ch/?',
|
||||
attributions: [new ol.Attribution(
|
||||
'© ' +
|
||||
'<a href="http://www.geo.admin.ch/internet/geoportal/en/home.html">' +
|
||||
'National parks / geo.admin.ch</a>')],
|
||||
crossOrigin: null,
|
||||
params: {'LAYERS': 'ch.bafu.schutzgebiete-paerke_nationaler_bedeutung'},
|
||||
projection: epsg21781
|
||||
})
|
||||
})
|
||||
]);
|
||||
|
||||
var map = new ol.Map({
|
||||
renderer: ol.RendererHint.DOM,
|
||||
center: new ol.Coordinate(660000, 190000),
|
||||
projection: epsg21781,
|
||||
layers: layers,
|
||||
target: 'map',
|
||||
zoom: 9
|
||||
});
|
||||
47
examples/wms.html
Normal file
47
examples/wms.html
Normal 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>
|
||||
36
examples/wms.js
Normal file
36
examples/wms.js
Normal file
@@ -0,0 +1,36 @@
|
||||
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',
|
||||
crossOrigin: null,
|
||||
params: {'LAYERS': 'topp:states', 'TILED': true},
|
||||
extent: new ol.Extent(-13884991, 2870341, -7455066, 6338219)
|
||||
})
|
||||
})
|
||||
]);
|
||||
var map = new ol.Map({
|
||||
renderer: ol.RendererHint.DOM,
|
||||
layers: layers,
|
||||
target: 'map',
|
||||
center: new ol.Coordinate(-10997148, 4569099),
|
||||
zoom: 4
|
||||
});
|
||||
@@ -59,3 +59,15 @@
|
||||
@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.attributions Array.<ol.Attribution>|undefined
|
||||
@exportObjectLiteralProperty ol.source.TiledWMSOptions.params Object
|
||||
@exportObjectLiteralProperty ol.source.TiledWMSOptions.version string|undefined
|
||||
@exportObjectLiteralProperty ol.source.TiledWMSOptions.crossOrigin null|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
|
||||
|
||||
1
src/ol/source/tiledwms.exports
Normal file
1
src/ol/source/tiledwms.exports
Normal file
@@ -0,0 +1 @@
|
||||
@exportSymbol ol.source.TiledWMS
|
||||
115
src/ol/source/tiledwmssource.js
Normal file
115
src/ol/source/tiledwmssource.js
Normal file
@@ -0,0 +1,115 @@
|
||||
// FIXME add minZoom support
|
||||
|
||||
goog.provide('ol.source.TiledWMS');
|
||||
|
||||
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');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.source.TileSource}
|
||||
* @param {ol.source.TiledWMSOptions} tiledWMSOptions options.
|
||||
*/
|
||||
ol.source.TiledWMS = function(tiledWMSOptions) {
|
||||
var projection = goog.isDef(tiledWMSOptions.projection) ?
|
||||
tiledWMSOptions.projection : ol.Projection.getFromCode('EPSG:3857');
|
||||
var projectionExtent = projection.getExtent();
|
||||
|
||||
var extent = goog.isDef(tiledWMSOptions.extent) ?
|
||||
tiledWMSOptions.extent : projectionExtent;
|
||||
|
||||
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(
|
||||
projectionExtent.maxX - projectionExtent.minX,
|
||||
projectionExtent.maxY - projectionExtent.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: projectionExtent.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;
|
||||
// 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);
|
||||
@@ -14,7 +14,7 @@ goog.require('ol.tilegrid.TileGrid');
|
||||
|
||||
/**
|
||||
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
|
||||
* crossOrigin: (string|undefined),
|
||||
* crossOrigin: (null|string|undefined),
|
||||
* extent: (ol.Extent|undefined),
|
||||
* projection: (ol.Projection|undefined),
|
||||
* tileGrid: (ol.tilegrid.TileGrid|undefined),
|
||||
|
||||
Reference in New Issue
Block a user