Skip to content

Commit 9dfc627

Browse files
authored
feat: support options.tms for TileLayer in 3857 and 4326 projection (#2782)
* feat: add options.tms for TileLayer in 3857 and 4326 projection * refactor: update VectorTileLayer._prepareOptions * add tileSystem static constants * fix TileLayer._prepareOptions
1 parent 064a086 commit 9dfc627

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

packages/maptalks/src/layer/tile/TileLayer.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,26 @@ class TileLayer extends Layer {
256256
return new TileLayer(layerJSON['id'], layerJSON['options']);
257257
}
258258

259+
onAdd() {
260+
this._prepareOptions();
261+
}
262+
263+
_prepareOptions() {
264+
const options = this.options as any;
265+
if (!options.tileSystem && options.tms) {
266+
const sr = this.getSpatialReference();
267+
const projection = sr.getProjection();
268+
const is4326 = projection.code === "EPSG:4326" || projection.code === "EPSG:4490";
269+
const is3857 = projection.code === "EPSG:3857";
270+
271+
if (is3857) {
272+
options.tileSystem = TileSystem['tms-global-mercator'];
273+
} else if (is4326) {
274+
options.tileSystem = TileSystem['tms-global-geodetic'];
275+
}
276+
}
277+
}
278+
259279
/**
260280
* force Reload tilelayer.
261281
* Note that this method will clear all cached tiles and reload them. It shouldn't be called frequently for performance reason.

packages/maptalks/src/layer/tile/tileinfo/TileSystem.ts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { extend } from '../../../core/util';
22

3+
const semiCircum = 6378137 * Math.PI;
4+
35
/**
46
* @classdesc
57
* A class internally used by tile layer helps to descibe tile system used by different tile services.<br>
@@ -20,6 +22,39 @@ class TileSystem {
2022
y: number;
2123
}
2224

25+
/**
26+
* The most common used tile system, used by google maps, bing maps and amap, soso maps in China.
27+
* @see {@link https://en.wikipedia.org/wiki/Web_Mercator}
28+
* @constant
29+
* @static
30+
*/
31+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
32+
static 'web-mercator' = new TileSystem([1, -1, -semiCircum, semiCircum]);
33+
34+
/**
35+
* Predefined tile system for TMS tile system, A tile system published by [OSGEO]{@link http://www.osgeo.org/}. <br>
36+
* Also used by mapbox's [mbtiles]{@link https://github.com/mapbox/mbtiles-spec} specification.
37+
* @see {@link http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification}
38+
* @constant
39+
* @static
40+
*/
41+
static 'tms-global-mercator' = new TileSystem([1, 1, -semiCircum, -semiCircum]);
42+
43+
/**
44+
* Another tile system published by [OSGEO]{@link http://www.osgeo.org/}, based on EPSG:4326 SRS.
45+
* @see {@link http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification#global-geodetic}
46+
* @constant
47+
* @static
48+
*/
49+
static 'tms-global-geodetic' = new TileSystem([1, 1, -180, -90]);
50+
51+
/**
52+
* Tile system used by [baidu]{@link http://map.baidu.com}
53+
* @constant
54+
* @static
55+
*/
56+
static 'baidu' = new TileSystem([1, 1, 0, 0]);
57+
2358
/**
2459
* Similar with [transformation]{@link Transformation}, it contains 4 numbers: sx, sy, ox, oy.<br>
2560
* @see {@link http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification}
@@ -69,41 +104,4 @@ class TileSystem {
69104
}
70105
}
71106

72-
const semiCircum = 6378137 * Math.PI;
73-
74-
extend(TileSystem, /** @lends TileSystem */ {
75-
/**
76-
* The most common used tile system, used by google maps, bing maps and amap, soso maps in China.
77-
* @see {@link https://en.wikipedia.org/wiki/Web_Mercator}
78-
* @constant
79-
* @static
80-
*/
81-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
82-
'web-mercator': new TileSystem([1, -1, -semiCircum, semiCircum]),
83-
84-
/**
85-
* Predefined tile system for TMS tile system, A tile system published by [OSGEO]{@link http://www.osgeo.org/}. <br>
86-
* Also used by mapbox's [mbtiles]{@link https://github.com/mapbox/mbtiles-spec} specification.
87-
* @see {@link http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification}
88-
* @constant
89-
* @static
90-
*/
91-
'tms-global-mercator': new TileSystem([1, 1, -semiCircum, -semiCircum]),
92-
93-
/**
94-
* Another tile system published by [OSGEO]{@link http://www.osgeo.org/}, based on EPSG:4326 SRS.
95-
* @see {@link http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification#global-geodetic}
96-
* @constant
97-
* @static
98-
*/
99-
'tms-global-geodetic': new TileSystem([1, 1, -180, -90]),
100-
101-
/**
102-
* Tile system used by [baidu]{@link http://map.baidu.com}
103-
* @constant
104-
* @static
105-
*/
106-
'baidu': new TileSystem([1, 1, 0, 0])
107-
});
108-
109107
export default TileSystem;

packages/vt/src/layer/layer/VectorTileLayer.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -331,24 +331,12 @@ class VectorTileLayer extends maptalks.TileLayer {
331331
}
332332
}
333333
}
334-
if (!options.tileSystem) {
335-
if (options.tms) {
336-
if (is3857) {
337-
options.tileSystem = [
338-
1,
339-
1,
340-
-6378137 * Math.PI,
341-
-6378137 * Math.PI,
342-
];
343-
} else if (is4326) {
344-
options.tileSystem = [1, 1, -180, -90];
345-
}
346-
} else {
347-
if (is4326) {
348-
options.tileSystem = [1, -1, -180, 90];
349-
}
334+
if (!options.tileSystem && !options.tms) {
335+
if (is4326) {
336+
options.tileSystem = [1, -1, -180, 90];
350337
}
351338
}
339+
super['_prepareOptions']();
352340
}
353341

354342
forceReload(): this {

0 commit comments

Comments
 (0)