Skip to content

Commit f48a838

Browse files
committed
【feature】ol.source.TileSuperMapRest 支持overflowTiles参数,在当前视口范围外额外渲染的瓦片圈数
AI-GEN: 60% Claude
1 parent b311d7f commit f48a838

2 files changed

Lines changed: 113 additions & 6 deletions

File tree

‎src/openlayers/mapping/TileSuperMapRest.js‎

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,50 @@ import { Util as CommonUtil } from '@supermapgis/iclient-common/commontypes/Util
77
import { ServerGeometry } from '@supermapgis/iclient-common/iServer/ServerGeometry';
88
import { Util } from '../core/Util';
99
import TileImage from 'ol/source/TileImage';
10+
import TileRange from 'ol/TileRange';
1011
import Geometry from 'ol/geom/Geometry';
1112
import * as olSize from 'ol/size';
1213
import * as olTilegrid from 'ol/tilegrid';
1314
import TileGrid from 'ol/tilegrid/TileGrid';
1415

16+
function getOverflowTileCount(overflowTiles) {
17+
return Math.max(0, Math.floor(Number(overflowTiles) || 0));
18+
}
19+
20+
function wrapTileGridWithOverflow(tileGrid, overflowTiles) {
21+
const overflowTileCount = getOverflowTileCount(overflowTiles);
22+
if (!tileGrid || !overflowTileCount) {
23+
return tileGrid;
24+
}
25+
26+
const wrappedTileGrid = Object.create(tileGrid);
27+
wrappedTileGrid.overflowTiles = overflowTileCount;
28+
wrappedTileGrid.getTileRangeForExtentAndZ = function (extent, z, tempTileRange) {
29+
const tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z, tempTileRange);
30+
if (!tileRange) {
31+
return tileRange;
32+
}
33+
tileRange.minX -= overflowTileCount;
34+
tileRange.maxX += overflowTileCount;
35+
tileRange.minY -= overflowTileCount;
36+
tileRange.maxY += overflowTileCount;
37+
return tileRange;
38+
};
39+
wrappedTileGrid.getFullTileRange = function (z) {
40+
const tileRange = tileGrid.getFullTileRange ? tileGrid.getFullTileRange(z) : null;
41+
if (!tileRange) {
42+
return tileRange;
43+
}
44+
return new TileRange(
45+
tileRange.minX - overflowTileCount,
46+
tileRange.maxX + overflowTileCount,
47+
tileRange.minY - overflowTileCount,
48+
tileRange.maxY + overflowTileCount
49+
);
50+
};
51+
return wrappedTileGrid;
52+
}
53+
1554
/**
1655
* @class TileSuperMapRest
1756
* @browsernamespace ol.source
@@ -36,6 +75,7 @@ import TileGrid from 'ol/tilegrid/TileGrid';
3675
* @param {string} [options.format = 'png'] - 瓦片表述类型,支持 "png" 、"webp"、"bmp" 、"jpg"、"gif" 等图片类型。
3776
* @param {(NDVIParameter|HillshadeParameter)} [options.rasterfunction] - 栅格分析参数。
3877
* @param {ChartSettingS57|ChartSettingS100} [options.chartSetting] - 海图显示参数设置类,用于管理海图显示环境,包括海图的显示模式、显示类型名称、颜色模式、安全水深线等各种显示风格。
78+
* @param {number} [options.overflowTiles = 0] - 在当前视口范围外额外渲染的瓦片圈数。为 1 时上下左右各多请求一圈瓦片,可用于缓解符号在图层范围边缘被裁切的问题。
3979
* @extends {ol.source.TileImage}
4080
* @usage
4181
*/
@@ -46,6 +86,7 @@ export class TileSuperMapRest extends TileImage {
4686
options.attributions || 'Map Data <span>© SuperMap iServer</span> with <span>© SuperMap iClient</span>';
4787

4888
options.format = options.format ? options.format : 'png';
89+
const tileGrid = wrapTileGridWithOverflow(options.tileGrid, options.overflowTiles);
4990

5091
super({
5192
attributions: options.attributions,
@@ -57,7 +98,7 @@ export class TileSuperMapRest extends TileImage {
5798
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
5899
state: options.state,
59100
tileClass: options.tileClass,
60-
tileGrid: options.tileGrid,
101+
tileGrid: tileGrid,
61102
tileLoadFunction: options.tileLoadFunction,
62103
tilePixelRatio: options.tilePixelRatio,
63104
tileUrlFunction: options.tileUrlFunction || tileUrlFunction,
@@ -68,6 +109,7 @@ export class TileSuperMapRest extends TileImage {
68109
if (options.tileProxy) {
69110
this.tileProxy = options.tileProxy;
70111
}
112+
options.tileGrid = tileGrid;
71113
this.options = options;
72114
//当前切片在切片集中的index
73115
this.tileSetsIndex = -1;
@@ -218,19 +260,25 @@ export class TileSuperMapRest extends TileImage {
218260
function tileUrlFunction(tileCoord, pixelRatio, projection) {
219261
if (!me.tileGrid) {
220262
if (options.extent) {
221-
me.tileGrid = TileSuperMapRest.createTileGrid(options.extent);
263+
me.tileGrid = wrapTileGridWithOverflow(TileSuperMapRest.createTileGrid(options.extent), options.overflowTiles);
222264
if (me.resolutions) {
223265
me.tileGrid.resolutions = me.resolutions;
224266
}
225267
} else {
226268
if (projection.getCode() === 'EPSG:3857') {
227-
me.tileGrid = TileSuperMapRest.createTileGrid([
228-
-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892
229-
]);
269+
me.tileGrid = wrapTileGridWithOverflow(
270+
TileSuperMapRest.createTileGrid([
271+
-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892
272+
]),
273+
options.overflowTiles
274+
);
230275
me.extent = [-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892];
231276
}
232277
if (projection.getCode() === 'EPSG:4326') {
233-
me.tileGrid = TileSuperMapRest.createTileGrid([-180, -90, 180, 90]);
278+
me.tileGrid = wrapTileGridWithOverflow(
279+
TileSuperMapRest.createTileGrid([-180, -90, 180, 90]),
280+
options.overflowTiles
281+
);
234282
me.extent = [-180, -90, 180, 90];
235283
}
236284
}

‎test/openlayers/mapping/TileSuperMapRestSpec.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {getQueryValue} from '../../tool/utils';
66

77
import Point from 'ol/geom/Point';
88
import * as olProj from 'ol/proj';
9+
import TileGrid from 'ol/tilegrid/TileGrid';
910

1011
var url = GlobeParameter.imageURL;
1112
describe('openlayers_TileSuperMapRest', () => {
@@ -95,6 +96,64 @@ describe('openlayers_TileSuperMapRest', () => {
9596
expect(tileSourcetile.getTileGrid().getResolution(0)).toEqual(90.0 / 256);
9697
});
9798

99+
it('tileGrid_overflowTiles_internalGrid', () => {
100+
var tileSource = new TileSuperMapRest({
101+
url: url,
102+
extent: [-180, -90, 180, 90],
103+
overflowTiles: 1,
104+
prjCoordSys: {
105+
epsgCode: 4326
106+
}
107+
});
108+
tileSource.tileUrlFunction([0, 0, 0], 1, olProj.get('EPSG:4326'));
109+
const tileRange = tileSource.getTileGrid().getTileRangeForExtentAndZ([-180, -90, 180, 90], 0);
110+
const fullTileRange = tileSource.getTileGrid().getFullTileRange(0);
111+
expect(tileRange.minX).toBe(-1);
112+
expect(tileRange.maxX).toBe(1);
113+
expect(tileRange.minY).toBe(-1);
114+
expect(tileRange.maxY).toBe(1);
115+
expect(fullTileRange.minX).toBe(-1);
116+
expect(fullTileRange.maxX).toBe(1);
117+
expect(fullTileRange.minY).toBe(-1);
118+
expect(fullTileRange.maxY).toBe(1);
119+
});
120+
121+
it('tileGrid_overflowTiles_customTileGrid', () => {
122+
var tileGrid = new TileGrid({
123+
extent: [-180, -90, 180, 90],
124+
origin: [-180, 90],
125+
resolutions: [180 / 256],
126+
tileSize: 256
127+
});
128+
var tileSource = new TileSuperMapRest({
129+
url: url,
130+
tileGrid: tileGrid,
131+
overflowTiles: 1,
132+
prjCoordSys: {
133+
epsgCode: 4326
134+
}
135+
});
136+
const tileRange = tileSource.getTileGrid().getTileRangeForExtentAndZ([-180, -90, 180, 90], 0);
137+
expect(tileRange.minX).toBe(-1);
138+
expect(tileRange.maxX).toBe(1);
139+
expect(tileRange.minY).toBe(-1);
140+
expect(tileRange.maxY).toBe(1);
141+
});
142+
143+
it('tileUrlFunction_overflowTiles_negativeTileCoord', () => {
144+
var tileSource = new TileSuperMapRest({
145+
url: url,
146+
extent: [-180, -90, 180, 90],
147+
overflowTiles: 1,
148+
prjCoordSys: {
149+
epsgCode: 4326
150+
}
151+
});
152+
var tileUrl = tileSource.tileUrlFunction([0, -1, -1], 1, olProj.get('EPSG:4326'));
153+
expect(tileUrl).toContain('&x=-1');
154+
expect(tileUrl).toContain('&y=-1');
155+
});
156+
98157
it('tileUrlFunction_tilePoxy', () => {
99158
var tileOptions = {
100159
url: url,

0 commit comments

Comments
 (0)