@@ -2,6 +2,18 @@ import { isString } from '../../core/util';
2
2
import Ajax from '../../core/Ajax' ;
3
3
import SpatialReference from './SpatialReference' ;
4
4
5
+ function getProjectionCode ( code ) {
6
+ let newcode = '' ;
7
+ const codeArray = code . split ( '' ) ;
8
+ for ( let len = codeArray . length , i = len - 1 ; i >= 0 ; i -- ) {
9
+ if ( ! isNaN ( codeArray [ i ] ) ) {
10
+ newcode = codeArray [ i ] + newcode ;
11
+ } else {
12
+ break ;
13
+ }
14
+ }
15
+ return newcode ;
16
+ }
5
17
6
18
function getProjection ( projection ) {
7
19
let prj = ( projection . indexOf ( 'EPSG' ) > - 1 ? projection : 'EPSG:' + projection ) ;
@@ -21,8 +33,46 @@ function strReplace(str, repArray = []) {
21
33
return str ;
22
34
}
23
35
36
+ function getTransformValue ( options ) {
37
+ const { projection, isArcgis, isGeoServer, isSuperMap } = options ;
38
+ //transform value, ArcGIS is different from others
39
+ let transformValue = 0.0002645833333333333 ;
40
+ if ( isArcgis || isGeoServer || isSuperMap ) {
41
+ transformValue = 0.00028 ;
42
+ }
43
+ if ( projection && projection . indexOf ( '4326' ) > - 1 ) {
44
+ transformValue = 2.3767925226029154e-9 ;
45
+ if ( isArcgis || isSuperMap ) {
46
+ transformValue = 2.518101729011901e-9 ;
47
+ }
48
+ if ( isGeoServer ) {
49
+ transformValue = 2.51528279553466e-9 ;
50
+ }
51
+ }
52
+ return transformValue ;
53
+ }
54
+
55
+ function getTileMatrixSet ( TileMatrixSets , TileMatrixSetLink ) {
56
+ for ( let i = 0 ; i < TileMatrixSets . length ; i ++ ) {
57
+ let TileMatrixSet = TileMatrixSets [ i ] ;
58
+ TileMatrixSet = TileMatrixSet . getElementsByTagName ( 'ows:Identifier' ) [ 0 ] ;
59
+ if ( TileMatrixSet ) {
60
+ if ( TileMatrixSet . textContent === TileMatrixSetLink ) {
61
+ return TileMatrixSets [ i ] ;
62
+ }
63
+ }
64
+ }
65
+ return null ;
66
+ }
67
+
24
68
function parseWMTSXML ( str , requestUrl , options ) {
25
69
//IE test success
70
+ if ( options . isArcgis == null ) {
71
+ options . isArcgis = str . indexOf ( 'arcgis' ) > - 1 ;
72
+ }
73
+ if ( options . isSuperMap == null ) {
74
+ options . isSuperMap = str . indexOf ( 'supermap' ) > - 1 ;
75
+ }
26
76
const parser = new DOMParser ( ) ;
27
77
const xmlDoc = parser . parseFromString ( str , 'text/xml' ) ;
28
78
const content = xmlDoc . querySelectorAll ( 'Contents' ) [ 0 ] ;
@@ -56,58 +106,72 @@ function parseWMTSXML(str, requestUrl, options) {
56
106
if ( layerName ) {
57
107
layerName = layerName . textContent ;
58
108
}
59
- const resourceURL = layer . querySelectorAll ( 'ResourceURL' ) [ 0 ] ;
60
- let url = '' ;
61
- if ( resourceURL ) {
62
- url = resourceURL . attributes . template . value ;
63
- }
64
- const { resolutions, tileSize, tileSystem, projection, TileMatrixSet } = parseTileMatrixSet ( TileMatrixSets , i , options ) ;
65
- //not find ServerURL
66
- if ( ! url . length ) {
67
- url = requestUrl . substr ( 0 , requestUrl . lastIndexOf ( '?' ) ) ;
68
- url += '?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={LAYER}&STYLE={Style}&TILEMATRIXSET={TileMatrixSet}&FORMAT=tiles&TILEMATRIX={TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}' ;
109
+ const tileMatrixSetLinks = layer . getElementsByTagName ( 'TileMatrixSetLink' ) ;
110
+ if ( tileMatrixSetLinks . length === 0 ) {
111
+ continue ;
69
112
}
70
- const urlTemplate = strReplace ( url , [
71
- [ '{LAYER}' , layerName ] ,
72
- [ '{Layer}' , layerName ] ,
73
- [ '{layer}' , layerName ] ,
74
- [ '{STYLE}' , style ] ,
75
- [ '{Style}' , style ] ,
76
- [ '{style}' , style ] ,
77
- [ '{TileMatrixSet}' , TileMatrixSet ] ,
78
- [ '{TileMatrix}' , '{z}' ] ,
79
- [ '{TileRow}' , '{y}' ] ,
80
- [ '{TileCol}' , '{x}' ] ,
81
- ] ) ;
82
- result . push ( {
83
- tileSize,
84
- tileSystem,
85
- spatialReference : {
86
- resolutions,
87
- projection
88
- } ,
89
- urlTemplate,
90
- info : {
91
- layerName, TileMatrixSet, style, tileSize, tileSystem, resolutions, projection, urlTemplate
113
+ for ( let j = 0 , len1 = tileMatrixSetLinks . length ; j < len1 ; j ++ ) {
114
+ let tileMatrixSetLink = tileMatrixSetLinks [ j ] ;
115
+ tileMatrixSetLink = tileMatrixSetLink . getElementsByTagName ( 'TileMatrixSet' ) [ 0 ] ;
116
+ if ( tileMatrixSetLink ) {
117
+ tileMatrixSetLink = tileMatrixSetLink . textContent ;
118
+ }
119
+ const tileMatrixSet = getTileMatrixSet ( TileMatrixSets , tileMatrixSetLink ) ;
120
+ if ( ! tileMatrixSet ) {
121
+ continue ;
122
+ }
123
+ const resourceURL = layer . querySelectorAll ( 'ResourceURL' ) [ 0 ] ;
124
+ let url = '' ;
125
+ if ( resourceURL ) {
126
+ url = resourceURL . attributes . template . value ;
92
127
}
93
- } ) ;
128
+ const { resolutions, tileSize, tileSystem, projection, TileMatrixSet, isGeoServer, levelStr } = parseTileMatrixSet ( tileMatrixSet , options ) ;
129
+ //not find ServerURL
130
+ if ( ! url . length ) {
131
+ url = requestUrl . substr ( 0 , requestUrl . lastIndexOf ( '?' ) ) ;
132
+ url += '?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={LAYER}&STYLE={Style}&TILEMATRIXSET={TileMatrixSet}&FORMAT={tiles}&TILEMATRIX={TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}' ;
133
+ }
134
+ const urlTemplate = strReplace ( url , [
135
+ [ '{LAYER}' , layerName ] ,
136
+ [ '{Layer}' , layerName ] ,
137
+ [ '{layer}' , layerName ] ,
138
+ [ '{STYLE}' , style ] ,
139
+ [ '{Style}' , style ] ,
140
+ [ '{style}' , style ] ,
141
+ [ '{TileMatrixSet}' , TileMatrixSet ] ,
142
+ [ '{TileMatrix}' , isGeoServer ? `${ levelStr } :{z}` : '{z}' ] ,
143
+ [ '{TileRow}' , '{y}' ] ,
144
+ [ '{TileCol}' , '{x}' ] ,
145
+ [ '{tiles}' , isGeoServer ? 'image/png' : 'tiles' ] ,
146
+ ] ) ;
147
+ result . push ( {
148
+ tileSize,
149
+ tileSystem,
150
+ spatialReference : {
151
+ resolutions,
152
+ projection
153
+ } ,
154
+ urlTemplate,
155
+ info : {
156
+ layerName, TileMatrixSet, style, tileSize, tileSystem, resolutions, projection, urlTemplate
157
+ }
158
+ } ) ;
159
+ }
160
+
94
161
}
95
162
return result ;
96
163
}
97
164
98
- function parseTileMatrixSet ( TileMatrixSets , index , options = { } ) {
99
- const TileMatrixSet = TileMatrixSets [ index ] ;
165
+ function parseTileMatrixSet ( TileMatrixSet , options = { } ) {
100
166
const TileMatrixs = TileMatrixSet . getElementsByTagName ( 'TileMatrix' ) ;
101
167
const resolutions = [ ] , tileSystem = [ ] , tileSize = [ ] ;
102
- let projection , tset ;
168
+ let projection , tset , isGeoServer = false , levelStr ;
103
169
if ( ! projection ) {
104
170
const supportedCRS = TileMatrixSet . getElementsByTagName ( 'ows:SupportedCRS' ) [ 0 ] ;
105
171
if ( supportedCRS ) {
106
172
projection = supportedCRS . textContent ;
107
173
projection = projection . split ( 'EPSG' ) [ 1 ] ;
108
- while ( projection . indexOf ( ':' ) > - 1 ) {
109
- projection = projection . replace ( ':' , '' ) ;
110
- }
174
+ projection = getProjectionCode ( projection ) ;
111
175
projection = getProjection ( projection ) ;
112
176
}
113
177
}
@@ -117,22 +181,21 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
117
181
tset = tset . textContent ;
118
182
}
119
183
}
120
- //transform value, ArcGIS is different from others
121
- let transformValue = 0.0002645833333333333 ;
122
- if ( options . isArcgis ) {
123
- transformValue = 0.00028 ;
124
- }
125
- if ( projection && projection . indexOf ( '4326' ) > - 1 ) {
126
- transformValue = 2.3767925226029154e-9 ;
127
- if ( options . isArcgis ) {
128
- transformValue = 2.518101729011901e-9 ;
129
- }
130
- }
184
+ options . projection = projection ;
131
185
let minLevel = Infinity ;
132
186
for ( let index = 0 ; index < TileMatrixs . length ; index ++ ) {
133
187
const TileMatrix = TileMatrixs [ index ] ;
134
188
let level = TileMatrix . getElementsByTagName ( 'ows:Identifier' ) [ 0 ] . textContent ;
135
- level = parseInt ( level ) ;
189
+ if ( isNaN ( parseInt ( level ) ) ) {
190
+ levelStr = level . substr ( 0 , level . lastIndexOf ( ':' ) ) ;
191
+ level = level . split ( ':' ) ;
192
+ level = level [ level . length - 1 ] ;
193
+ level = parseInt ( level ) ;
194
+ isGeoServer = true ;
195
+ options . isGeoServer = true ;
196
+ } else {
197
+ level = parseInt ( level ) ;
198
+ }
136
199
minLevel = Math . min ( minLevel , level ) ;
137
200
const ScaleDenominator = TileMatrix . getElementsByTagName ( 'ScaleDenominator' ) [ 0 ] . textContent ;
138
201
const TopLeftCorner = TileMatrix . getElementsByTagName ( 'TopLeftCorner' ) [ 0 ] . textContent ;
@@ -153,6 +216,7 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
153
216
tileSystem . push ( 1 , - 1 , x , y ) ;
154
217
}
155
218
}
219
+ const transformValue = getTransformValue ( options ) ;
156
220
const res = parseFloat ( ScaleDenominator ) * transformValue ;
157
221
resolutions . push ( res ) ;
158
222
}
@@ -166,7 +230,7 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
166
230
}
167
231
}
168
232
return {
169
- resolutions, tileSize, tileSystem, projection, TileMatrixSet : tset
233
+ resolutions, tileSize, tileSystem, projection, TileMatrixSet : tset , isGeoServer , levelStr
170
234
} ;
171
235
}
172
236
0 commit comments