Skip to content

Commit 24dcf2b

Browse files
authored
wmts support geoserver (#1304)
1 parent 5a3aebd commit 24dcf2b

File tree

1 file changed

+116
-52
lines changed

1 file changed

+116
-52
lines changed

src/map/spatial-reference/SpatialReference.WMTS.js

Lines changed: 116 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { isString } from '../../core/util';
22
import Ajax from '../../core/Ajax';
33
import SpatialReference from './SpatialReference';
44

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+
}
517

618
function getProjection(projection) {
719
let prj = (projection.indexOf('EPSG') > -1 ? projection : 'EPSG:' + projection);
@@ -21,8 +33,46 @@ function strReplace(str, repArray = []) {
2133
return str;
2234
}
2335

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+
2468
function parseWMTSXML(str, requestUrl, options) {
2569
//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+
}
2676
const parser = new DOMParser();
2777
const xmlDoc = parser.parseFromString(str, 'text/xml');
2878
const content = xmlDoc.querySelectorAll('Contents')[0];
@@ -56,58 +106,72 @@ function parseWMTSXML(str, requestUrl, options) {
56106
if (layerName) {
57107
layerName = layerName.textContent;
58108
}
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;
69112
}
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;
92127
}
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+
94161
}
95162
return result;
96163
}
97164

98-
function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
99-
const TileMatrixSet = TileMatrixSets[index];
165+
function parseTileMatrixSet(TileMatrixSet, options = {}) {
100166
const TileMatrixs = TileMatrixSet.getElementsByTagName('TileMatrix');
101167
const resolutions = [], tileSystem = [], tileSize = [];
102-
let projection, tset;
168+
let projection, tset, isGeoServer = false, levelStr;
103169
if (!projection) {
104170
const supportedCRS = TileMatrixSet.getElementsByTagName('ows:SupportedCRS')[0];
105171
if (supportedCRS) {
106172
projection = supportedCRS.textContent;
107173
projection = projection.split('EPSG')[1];
108-
while (projection.indexOf(':') > -1) {
109-
projection = projection.replace(':', '');
110-
}
174+
projection = getProjectionCode(projection);
111175
projection = getProjection(projection);
112176
}
113177
}
@@ -117,22 +181,21 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
117181
tset = tset.textContent;
118182
}
119183
}
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;
131185
let minLevel = Infinity;
132186
for (let index = 0; index < TileMatrixs.length; index++) {
133187
const TileMatrix = TileMatrixs[index];
134188
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+
}
136199
minLevel = Math.min(minLevel, level);
137200
const ScaleDenominator = TileMatrix.getElementsByTagName('ScaleDenominator')[0].textContent;
138201
const TopLeftCorner = TileMatrix.getElementsByTagName('TopLeftCorner')[0].textContent;
@@ -153,6 +216,7 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
153216
tileSystem.push(1, -1, x, y);
154217
}
155218
}
219+
const transformValue = getTransformValue(options);
156220
const res = parseFloat(ScaleDenominator) * transformValue;
157221
resolutions.push(res);
158222
}
@@ -166,7 +230,7 @@ function parseTileMatrixSet(TileMatrixSets, index, options = {}) {
166230
}
167231
}
168232
return {
169-
resolutions, tileSize, tileSystem, projection, TileMatrixSet: tset
233+
resolutions, tileSize, tileSystem, projection, TileMatrixSet: tset, isGeoServer, levelStr
170234
};
171235
}
172236

0 commit comments

Comments
 (0)