Skip to content

Commit 13e16ad

Browse files
ol initmap 支持 tileset review by luox
1 parent 468dc2f commit 13e16ad

File tree

8 files changed

+493
-122
lines changed

8 files changed

+493
-122
lines changed

src/common/iServer/InitMapServiceBase.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,38 @@ export function getEpsgCode(prjCoordSys) {
114114
return 'EPSG:' + epsgCode;
115115
}
116116

117+
/**
118+
* @private
119+
* @function getTileFormat
120+
* @description 获取瓦片格式。
121+
* @returns {string} 瓦片格式
122+
*/
123+
124+
export function getTileFormat(tileset) {
125+
if (tileset.tileFormat) {
126+
const format = tileset.tileFormat.toLowerCase();
127+
if (['jpg_png', 'default', 'png8'].includes(format)) {
128+
return 'png';
129+
}
130+
return format;
131+
}
132+
return 'png';
133+
}
134+
135+
/**
136+
* @private
137+
* @function getTileset
138+
* @description 获取 tilseset
139+
* @returns {Object}
140+
*/
141+
142+
export function getTileset(tilesets = [], targets) {
143+
const imageTilesets = tilesets.filter((i) => {
144+
return i.metaData.tileType === targets.tileType || 'Image' && getEpsgCode(i.metaData.prjCoordSys) === getEpsgCode(targets.prjCoordSys);
145+
});
146+
return imageTilesets[0] && imageTilesets[0].metaData;
147+
}
148+
117149

118150
/**
119151
* @private

src/leaflet/mapping/initMap.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { MapService } from '../services/MapService';
1010
import {
1111
InitMapServiceBase,
1212
isPlaneProjection,
13-
getEpsgCode
13+
getEpsgCode,
14+
getTileFormat,
15+
getTileset
1416
} from '@supermap/iclient-common/iServer/InitMapServiceBase';
1517
import proj4 from 'proj4';
1618

@@ -73,19 +75,20 @@ export async function initMap(url, options) {
7375
const origin = [tileset.originalPoint.x, tileset.originalPoint.y]
7476
const resolutions = tileset.resolutions;
7577
const scaleDenominators = tileset.scaleDenominators
78+
const coordUnit = mapObject.coordUnit;
7679
maxZoom = resolutions.length - 1;
77-
crs = crsFromMapJSON({ prjCoordSys, bounds, resolutions, origin, dpi: getDpi(1.0 / scaleDenominators[0], resolutions[0]) }, { maxZoom });
80+
crs = crsFromMapJSON({ prjCoordSys, bounds, resolutions, origin, dpi: getDpi(1.0 / scaleDenominators[0], resolutions[0], coordUnit) }, { maxZoom });
7881
zoom = getZoomByResolution(1.0 / scale, scaleDenominators);
7982
} else {
8083
tileFormat = 'webp';
8184
tileSize = 256;
8285
transparent = true;
8386
const { scale, dpi, coordUnit } = mapObject;
8487
const origin = [bounds.left, bounds.top];
85-
const resolutions = scalesToResolutions(mapObject.visibleScales, bounds, dpi, coordUnit, scale);
88+
const resolutions = scalesToResolutions(mapObject.visibleScales, bounds, dpi, coordUnit, mapOptions.maxZoom, scale);
8689
maxZoom = resolutions.length - 1;
8790
crs = crsFromMapJSON({ prjCoordSys, bounds, resolutions, origin, dpi }, { maxZoom });
88-
zoom = getZoomByScale({ scale, dpi, coordUnit }, resolutions)
91+
zoom = getZoomByScale({ scale, dpi, coordUnit }, resolutions);
8992
}
9093

9194
const mapInfoOptions = {
@@ -240,12 +243,6 @@ function getNonEarthCRS(bounds, origin, resolutions) {
240243
};
241244
return new NonEarthCRS(options);
242245
}
243-
function getTileset(tilesets = [], targets) {
244-
const imageTilesets = tilesets.filter((i) => {
245-
return i.metaData.tileType === targets.tileType || 'Image' && getEpsgCode(i.metaData.prjCoordSys) === getEpsgCode(targets.prjCoordSys);
246-
});
247-
return imageTilesets[0] ? imageTilesets[0].metaData : {};
248-
}
249246

250247
function getCRS(epsgCodeStr, { bounds, origin, dpi, resolutions }) {
251248
const wrapLngLeft = proj4(epsgCodeStr, 'EPSG:4326').forward([bounds.left, 0], true);
@@ -262,13 +259,3 @@ function getCRS(epsgCodeStr, { bounds, origin, dpi, resolutions }) {
262259
}
263260
return new CRS(epsgCodeStr, options);
264261
}
265-
function getTileFormat(tileset) {
266-
if (tileset.tileFormat) {
267-
const format = tileset.tileFormat.toLowerCase();
268-
if (['jpg_png', 'default', 'png8'].includes(format)) {
269-
return 'png';
270-
}
271-
return format;
272-
}
273-
return 'png';
274-
}

src/openlayers/core/Util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
44
import { Bounds } from '@supermap/iclient-common/commontypes/Bounds';
55
import { GeoJSON as GeoJSONFormat } from '@supermap/iclient-common/format/GeoJSON';
6-
import { getMeterPerMapUnit, scalesToResolutions, getZoomByResolution } from '@supermap/iclient-common/util/MapCalculateUtil';
6+
import { getMeterPerMapUnit, scalesToResolutions, getZoomByResolution, getDpi } from '@supermap/iclient-common/util/MapCalculateUtil';
77
import * as olUtil from 'ol/util';
88
import Geometry from 'ol/geom/Geometry';
99
import { getVectorContext } from 'ol/render';
@@ -426,6 +426,7 @@
426426
scalesToResolutions(scales, bounds, dpi, unit, mapobj, level) {
427427
return scalesToResolutions(scales, bounds, dpi, unit, mapobj, level);
428428
},
429+
getDpi,
429430
getProjection(prjCoordSys, extent) {
430431
let projection = get(`EPSG:${prjCoordSys.epsgCode}`);
431432
if (prjCoordSys.type == 'PCS_NON_EARTH') {

src/openlayers/mapping/InitMap.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MapService } from '../services/MapService';
2-
import { InitMapServiceBase, isPlaneProjection } from '@supermap/iclient-common/iServer/InitMapServiceBase';
2+
import { InitMapServiceBase, isPlaneProjection, getTileFormat, getTileset } from '@supermap/iclient-common/iServer/InitMapServiceBase';
33
import olMap from 'ol/Map';
44
import View from 'ol/View';
55
import Tile from 'ol/layer/Tile';
@@ -41,29 +41,53 @@ window.proj4 = proj4;
4141
* initMap(url, { mapOptions, viewOptions, layerOptions, sourceOptions })
4242
* ```
4343
* */
44-
export function initMap(url, options = {}) {
44+
export async function initMap(url, options = {}) {
4545
const { mapOptions, viewOptions, layerOptions, sourceOptions } = options;
4646
const initMapService = new InitMapServiceBase(MapService, url, options);
47-
return initMapService.getMapInfo(async function (serviceResult, resolve, reject) {
48-
if (!serviceResult || !serviceResult.result) {
49-
reject('service is not work!');
50-
return;
51-
}
52-
let { prjCoordSys, bounds } = serviceResult.result;
53-
if (!get(`EPSG:${prjCoordSys.epsgCode}`) && !isPlaneProjection(prjCoordSys.type)) {
54-
const wkt = await initMapService.getWKT();
55-
registerProj(prjCoordSys.epsgCode, wkt, bounds);
56-
}
47+
const tilesets = await initMapService.getTilesets();
48+
const result = await initMapService.getMapInfo();
49+
if (!result || !result.result) {
50+
return 'service is not work!';
51+
}
52+
const mapObject = result.result;
53+
const { prjCoordSys, bounds, center, visibleScales, coordUnit, scale, dpi } = mapObject;
54+
// tileset和地图不同投影,优先使用地图
55+
const tileset = getTileset(tilesets.result, { prjCoordSys, tileType: 'Image' });
5756

58-
let map = createMap(serviceResult.result, mapOptions, viewOptions);
59-
let { layer, source } = createLayer(url, serviceResult.result, sourceOptions, layerOptions);
60-
map.addLayer(layer);
61-
resolve({
62-
map,
63-
source,
64-
layer
65-
});
66-
});
57+
const config = {
58+
center,
59+
bounds,
60+
dpi,
61+
visibleScales,
62+
scale,
63+
prjCoordSys,
64+
coordUnit,
65+
tileFormat: 'webp',
66+
tileSize: 256
67+
};
68+
if (tileset) {
69+
config.tileFormat = getTileFormat(tileset);
70+
config.tileSize = tileset.tileWidth || 256;
71+
config.transparent = tileset.transparent || true;
72+
config.origin = [tileset.originalPoint.x, tileset.originalPoint.y];
73+
config.resolutions = tileset.resolutions;
74+
config.scaleDenominators = tileset.scaleDenominators;
75+
config.dpi = Util.getDpi(1.0 / tileset.scaleDenominators[0], tileset.resolutions[0], coordUnit);
76+
}
77+
78+
if (!get(`EPSG:${prjCoordSys.epsgCode}`) && !isPlaneProjection(prjCoordSys.type)) {
79+
const wkt = await initMapService.getWKT();
80+
registerProj(prjCoordSys.epsgCode, wkt, bounds);
81+
}
82+
83+
let map = createMap(config, mapOptions, viewOptions);
84+
let { layer, source } = createLayer(url, config, sourceOptions, layerOptions);
85+
map.addLayer(layer);
86+
return {
87+
map,
88+
source,
89+
layer
90+
};
6791
}
6892

6993
/**
@@ -77,13 +101,14 @@ export function initMap(url, options = {}) {
77101
*/
78102

79103
export function viewOptionsFromMapJSON(mapJSONObj, level = 22) {
80-
let { bounds, dpi, center, visibleScales, scale, coordUnit, prjCoordSys } = mapJSONObj;
104+
let { bounds, dpi, center, visibleScales, scale, coordUnit, prjCoordSys, scaleDenominators, resolutions } = mapJSONObj;
81105
const mapCenter =
82106
center.x && center.y ? [center.x, center.y] : [(bounds.left + bounds.right) / 2, (bounds.bottom + bounds.top) / 2];
83107
const extent = [bounds.left, bounds.bottom, bounds.right, bounds.top];
84108
let projection = Util.getProjection(prjCoordSys, extent);
85-
var resolutions = Util.scalesToResolutions(visibleScales, bounds, dpi, coordUnit, level);
86-
const zoom = Util.getZoomByResolution(Util.scaleToResolution(scale, dpi, coordUnit), resolutions);
109+
resolutions = resolutions || Util.scalesToResolutions(visibleScales, bounds, dpi, coordUnit, level);
110+
const resolution = scaleDenominators ? (1.0 / scale) : Util.scaleToResolution(scale, dpi, coordUnit);
111+
const zoom = Util.getZoomByResolution(resolution, resolutions);
87112
return {
88113
center: mapCenter,
89114
projection,

src/openlayers/mapping/TileSuperMapRest.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,23 @@ export class TileSuperMapRest extends TileImage {
322322
* @param {Object} mapJSONObj - 地图 JSON 对象。
323323
*/
324324
static optionsFromMapJSON(url, mapJSONObj) {
325-
var options = {};
325+
var options = {
326+
};
327+
if (mapJSONObj.tileFormat) {
328+
options.format = mapJSONObj.tileFormat;
329+
}
330+
if (mapJSONObj.origin) {
331+
options.origin = mapJSONObj.origin;
332+
}
326333
options.url = url;
327334
options.crossOrigin = 'anonymous';
328335
var extent = [mapJSONObj.bounds.left, mapJSONObj.bounds.bottom, mapJSONObj.bounds.right, mapJSONObj.bounds.top];
329336
const { visibleScales, bounds, dpi, coordUnit } = mapJSONObj;
330337
var resolutions = Util.scalesToResolutions(visibleScales, bounds, dpi, coordUnit);
331338
options.tileGrid = new TileGrid({
332339
extent: extent,
333-
resolutions: resolutions
340+
resolutions: resolutions,
341+
tileSize: mapJSONObj.tileSize || 256
334342
});
335343
return options;
336344
}

0 commit comments

Comments
 (0)