Skip to content

WMTS createLayer method improved #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 83 additions & 33 deletions lib/OpenLayers/Format/WMTSCapabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
* Optional config properties:
* matrixSet - {String} The matrix set identifier, required if there is
* more than one matrix set in the layer capabilities.
* projection - {String} The desired CRS when no matrixSet is specified.
* eg: "EPSG:3857". If the desired projection is not available,
* an error is thrown.
* style - {String} The name of the style
* format - {String} Image format for the layer. Default is the first
* format returned in the GetCapabilities response.
Expand Down Expand Up @@ -114,6 +117,19 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
var matrixSet;
if (config.matrixSet) {
matrixSet = contents.tileMatrixSets[config.matrixSet];
} else if (config.projection) {
for (var i=0,l=layerDef.tileMatrixSetLinks.length;i<l;i++) {
if (contents.tileMatrixSets[
layerDef.tileMatrixSetLinks[i].tileMatrixSet
].supportedCRS.replace(
/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, "$1:$3"
) === config.projection) {

matrixSet = contents.tileMatrixSets[
layerDef.tileMatrixSetLinks[i].tileMatrixSet];
break;
}
}
} else if (layerDef.tileMatrixSetLinks.length >= 1) {
matrixSet = contents.tileMatrixSets[
layerDef.tileMatrixSetLinks[0].tileMatrixSet];
Expand All @@ -140,7 +156,6 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
if (http.get[0].constraints) {
var constraints = http.get[0].constraints;
var allowedValues = constraints.GetEncoding.allowedValues;

// The OGC documentation is not clear if we should use
// REST or RESTful, ArcGis use RESTful,
// and OpenLayers use REST.
Expand All @@ -167,15 +182,47 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
var projection = config.projection || matrixSet.supportedCRS.replace(
/urn:ogc:def:crs:(\w+):(.*:)?(\w+)$/, "$1:$3");
var units = config.units ||
(projection === "EPSG:4326" ? "degrees" : "m");

var resolutions = [];
for (var mid in matrixSet.matrixIds) {
if (matrixSet.matrixIds.hasOwnProperty(mid)) {
resolutions.push(
matrixSet.matrixIds[mid].scaleDenominator * 0.28E-3 /
OpenLayers.METERS_PER_INCH /
OpenLayers.INCHES_PER_UNIT[units]);
(projection === ("EPSG:4326" || "OGC:CRS84") ? "degrees" : "m");

// compute server-supported resolutions array
var resolutions = [], minScaleDenominator, maxScaleDenominator,
reducedMatrixIds = [], tileMatrixSetLink,
tileMatrixSetLinks = layerDef.tileMatrixSetLinks;
var buildResolutionsArray = function(scaleDenominator) {
resolutions.push(
scaleDenominator * 0.28E-3 / OpenLayers.METERS_PER_INCH /
OpenLayers.INCHES_PER_UNIT[units]
);
if (!minScaleDenominator || minScaleDenominator > scaleDenominator) {
minScaleDenominator = scaleDenominator;
}
if (!maxScaleDenominator || maxScaleDenominator < scaleDenominator) {
maxScaleDenominator = scaleDenominator;
}
};
for (var j=0, l=tileMatrixSetLinks.length; j<l; j++) {
tileMatrixSetLink = tileMatrixSetLinks[j];
if (tileMatrixSetLink.tileMatrixSet === matrixSet.identifier) {
if (tileMatrixSetLink.tileMatrixSetLimits) {
// reformat matrixSet.matrixIds so that identifiers become keys
var tmpMatrixIds = {}, mid;
for (var k=0, ll=matrixSet.matrixIds.length; k<ll; k++) {
tmpMatrixIds[matrixSet.matrixIds[k].identifier] = matrixSet.matrixIds[k];
}
// compute resolutions array + scale limits
for (var k=0, ll=tileMatrixSetLink.tileMatrixSetLimits.length; k<ll; k++) {
mid = tmpMatrixIds[tileMatrixSetLink.tileMatrixSetLimits[k].tileMatrix];
reducedMatrixIds.push(mid);
buildResolutionsArray(mid.scaleDenominator);
}
} else {
// if there are no limits in the tileMatrixSetLink,
// use the resolutions from the full tile matrix set
for (var k=0, ll=matrixSet.matrixIds.length; k<ll; k++) {
buildResolutionsArray(matrixSet.matrixIds[k].scaleDenominator);
};
}
break;
}
}

Expand All @@ -190,8 +237,7 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
url.push(resourceUrl.template);
}
}
}
else {
} else {
var httpGet = capabilities.operationsMetadata.GetTile.dcp.http.get;
url = [];
var constraint;
Expand All @@ -203,28 +249,32 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
}
}
}

return new OpenLayers.Layer.WMTS(
OpenLayers.Util.applyDefaults(config, {
url: url,
requestEncoding: requestEncoding,
name: layerDef.title,
style: style.identifier,
format: format,
matrixIds: matrixSet.matrixIds,
matrixSet: matrixSet.identifier,
projection: projection,
units: units,
resolutions: config.isBaseLayer === false ? undefined :
resolutions,
serverResolutions: resolutions,
tileFullExtent: matrixSet.bounds,
dimensions: dimensions,
params: params
})
);

resolutions.sort(function(a,b){
return b-a;
});
var options = OpenLayers.Util.applyDefaults(config, {
url: url,
requestEncoding: requestEncoding,
name: layerDef.title,
style: style && style.identifier || "",
format: format,
matrixIds: reducedMatrixIds.length ?
reducedMatrixIds : matrixSet.matrixIds,
matrixSet: matrixSet.identifier,
projection: projection,
units: units,
tileFullExtent: matrixSet.bounds,
dimensions: dimensions,
params: params,
resolutions: config.isBaseLayer === false ? undefined :
resolutions,
serverResolutions: resolutions,
minScale: 1/Math.ceil(maxScaleDenominator),
maxScale: 1/Math.floor(minScaleDenominator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should let to don't set minScale/maxScale to allows to use the client zoom.

});
return new OpenLayers.Layer.WMTS(options);
},

CLASS_NAME: "OpenLayers.Format.WMTSCapabilities"

});
30 changes: 29 additions & 1 deletion tests/Format/WMTSCapabilities/v1_0_0.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
}

function test_createLayer(t) {
t.plan(43);
t.plan(48);

var format = new OpenLayers.Format.WMTSCapabilities();

Expand Down Expand Up @@ -239,12 +239,29 @@
}
t.ok(!success, "createLayer returns undefined given bad matrixSet identifier");

// bogus projection
try {
layer = format.createLayer(caps, {
layer: "coastlines",
projection: "EPSG:3857"
});
} catch (err) {
success = false;
}
t.ok(!success, "createLayer returns undefined given unsupported projection");

layer = format.createLayer(caps, {
layer: "coastlines",
matrixSet: "BigWorld"
});
t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance");

layer = format.createLayer(caps, {
layer: "coastlines",
projection: "OGC:CRS84"
});
t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance with CRS84 tile matrix set");

// autodetect matrixSet
layer = format.createLayer(caps, {
layer: "coastlines"
Expand Down Expand Up @@ -364,6 +381,17 @@
layer: "WorldTimeZones"
});
t.eq(layer.requestEncoding, "REST", "correct requestEncoding (in RESTfull)");

// test serverresolutions and min/maxScaleDenominator
xml = document.getElementById("French-IGN-GeoPortal").firstChild.nodeValue;
doc = new OpenLayers.Format.XML().read(xml);
caps = format.read(doc);
layer = format.createLayer(caps, {
layer: "ADMINISTRATIVEUNITS.BOUNDARIES"
});
t.eq(layer.serverResolutions.length, 13, "correct number of server resolutions");
t.eq(layer.maxScale, 1/2132, "correct maxScale");
t.eq(layer.minScale, 1/8735661, "correct minScale");
}

function test_parse_projection(t) {
Expand Down