@@ -72,6 +72,9 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
72
72
* Optional config properties:
73
73
* matrixSet - {String} The matrix set identifier, required if there is
74
74
* more than one matrix set in the layer capabilities.
75
+ * projection - {String} The desired CRS when no matrixSet is specified.
76
+ * eg: "EPSG:3857". If the desired projection is not available,
77
+ * an error is thrown.
75
78
* style - {String} The name of the style
76
79
* format - {String} Image format for the layer. Default is the first
77
80
* format returned in the GetCapabilities response.
@@ -114,6 +117,19 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
114
117
var matrixSet ;
115
118
if ( config . matrixSet ) {
116
119
matrixSet = contents . tileMatrixSets [ config . matrixSet ] ;
120
+ } else if ( config . projection ) {
121
+ for ( var i = 0 , l = layerDef . tileMatrixSetLinks . length ; i < l ; i ++ ) {
122
+ if ( contents . tileMatrixSets [
123
+ layerDef . tileMatrixSetLinks [ i ] . tileMatrixSet
124
+ ] . supportedCRS . replace (
125
+ / u r n : o g c : d e f : c r s : ( \w + ) : ( .* : ) ? ( \w + ) $ / , "$1:$3"
126
+ ) === config . projection ) {
127
+
128
+ matrixSet = contents . tileMatrixSets [
129
+ layerDef . tileMatrixSetLinks [ i ] . tileMatrixSet ] ;
130
+ break ;
131
+ }
132
+ }
117
133
} else if ( layerDef . tileMatrixSetLinks . length >= 1 ) {
118
134
matrixSet = contents . tileMatrixSets [
119
135
layerDef . tileMatrixSetLinks [ 0 ] . tileMatrixSet ] ;
@@ -140,7 +156,6 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
140
156
if ( http . get [ 0 ] . constraints ) {
141
157
var constraints = http . get [ 0 ] . constraints ;
142
158
var allowedValues = constraints . GetEncoding . allowedValues ;
143
-
144
159
// The OGC documentation is not clear if we should use
145
160
// REST or RESTful, ArcGis use RESTful,
146
161
// and OpenLayers use REST.
@@ -167,15 +182,47 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
167
182
var projection = config . projection || matrixSet . supportedCRS . replace (
168
183
/ u r n : o g c : d e f : c r s : ( \w + ) : ( .* : ) ? ( \w + ) $ / , "$1:$3" ) ;
169
184
var units = config . units ||
170
- ( projection === "EPSG:4326" ? "degrees" : "m" ) ;
171
-
172
- var resolutions = [ ] ;
173
- for ( var mid in matrixSet . matrixIds ) {
174
- if ( matrixSet . matrixIds . hasOwnProperty ( mid ) ) {
175
- resolutions . push (
176
- matrixSet . matrixIds [ mid ] . scaleDenominator * 0.28E-3 /
177
- OpenLayers . METERS_PER_INCH /
178
- OpenLayers . INCHES_PER_UNIT [ units ] ) ;
185
+ ( projection === ( "EPSG:4326" || "OGC:CRS84" ) ? "degrees" : "m" ) ;
186
+
187
+ // compute server-supported resolutions array
188
+ var resolutions = [ ] , minScaleDenominator , maxScaleDenominator ,
189
+ reducedMatrixIds = [ ] , tileMatrixSetLink ,
190
+ tileMatrixSetLinks = layerDef . tileMatrixSetLinks ;
191
+ var buildResolutionsArray = function ( scaleDenominator ) {
192
+ resolutions . push (
193
+ scaleDenominator * 0.28E-3 / OpenLayers . METERS_PER_INCH /
194
+ OpenLayers . INCHES_PER_UNIT [ units ]
195
+ ) ;
196
+ if ( ! minScaleDenominator || minScaleDenominator > scaleDenominator ) {
197
+ minScaleDenominator = scaleDenominator ;
198
+ }
199
+ if ( ! maxScaleDenominator || maxScaleDenominator < scaleDenominator ) {
200
+ maxScaleDenominator = scaleDenominator ;
201
+ }
202
+ } ;
203
+ for ( var j = 0 , l = tileMatrixSetLinks . length ; j < l ; j ++ ) {
204
+ tileMatrixSetLink = tileMatrixSetLinks [ j ] ;
205
+ if ( tileMatrixSetLink . tileMatrixSet === matrixSet . identifier ) {
206
+ if ( tileMatrixSetLink . tileMatrixSetLimits ) {
207
+ // reformat matrixSet.matrixIds so that identifiers become keys
208
+ var tmpMatrixIds = { } , mid ;
209
+ for ( var k = 0 , ll = matrixSet . matrixIds . length ; k < ll ; k ++ ) {
210
+ tmpMatrixIds [ matrixSet . matrixIds [ k ] . identifier ] = matrixSet . matrixIds [ k ] ;
211
+ }
212
+ // compute resolutions array + scale limits
213
+ for ( var k = 0 , ll = tileMatrixSetLink . tileMatrixSetLimits . length ; k < ll ; k ++ ) {
214
+ mid = tmpMatrixIds [ tileMatrixSetLink . tileMatrixSetLimits [ k ] . tileMatrix ] ;
215
+ reducedMatrixIds . push ( mid ) ;
216
+ buildResolutionsArray ( mid . scaleDenominator ) ;
217
+ }
218
+ } else {
219
+ // if there are no limits in the tileMatrixSetLink,
220
+ // use the resolutions from the full tile matrix set
221
+ for ( var k = 0 , ll = matrixSet . matrixIds . length ; k < ll ; k ++ ) {
222
+ buildResolutionsArray ( matrixSet . matrixIds [ k ] . scaleDenominator ) ;
223
+ } ;
224
+ }
225
+ break ;
179
226
}
180
227
}
181
228
@@ -190,8 +237,7 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
190
237
url . push ( resourceUrl . template ) ;
191
238
}
192
239
}
193
- }
194
- else {
240
+ } else {
195
241
var httpGet = capabilities . operationsMetadata . GetTile . dcp . http . get ;
196
242
url = [ ] ;
197
243
var constraint ;
@@ -203,28 +249,32 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.Vers
203
249
}
204
250
}
205
251
}
206
-
207
- return new OpenLayers . Layer . WMTS (
208
- OpenLayers . Util . applyDefaults ( config , {
209
- url : url ,
210
- requestEncoding : requestEncoding ,
211
- name : layerDef . title ,
212
- style : style . identifier ,
213
- format : format ,
214
- matrixIds : matrixSet . matrixIds ,
215
- matrixSet : matrixSet . identifier ,
216
- projection : projection ,
217
- units : units ,
218
- resolutions : config . isBaseLayer === false ? undefined :
219
- resolutions ,
220
- serverResolutions : resolutions ,
221
- tileFullExtent : matrixSet . bounds ,
222
- dimensions : dimensions ,
223
- params : params
224
- } )
225
- ) ;
252
+
253
+ resolutions . sort ( function ( a , b ) {
254
+ return b - a ;
255
+ } ) ;
256
+ var options = OpenLayers . Util . applyDefaults ( config , {
257
+ url : url ,
258
+ requestEncoding : requestEncoding ,
259
+ name : layerDef . title ,
260
+ style : style && style . identifier || "" ,
261
+ format : format ,
262
+ matrixIds : reducedMatrixIds . length ?
263
+ reducedMatrixIds : matrixSet . matrixIds ,
264
+ matrixSet : matrixSet . identifier ,
265
+ projection : projection ,
266
+ units : units ,
267
+ tileFullExtent : matrixSet . bounds ,
268
+ dimensions : dimensions ,
269
+ params : params ,
270
+ resolutions : config . isBaseLayer === false ? undefined :
271
+ resolutions ,
272
+ serverResolutions : resolutions ,
273
+ minScale : 1 / Math . ceil ( maxScaleDenominator ) ,
274
+ maxScale : 1 / Math . floor ( minScaleDenominator )
275
+ } ) ;
276
+ return new OpenLayers . Layer . WMTS ( options ) ;
226
277
} ,
227
278
228
279
CLASS_NAME : "OpenLayers.Format.WMTSCapabilities"
229
-
230
280
} ) ;
0 commit comments