Skip to content

Commit af11251

Browse files
committed
【feature】crs 逻辑重构, 优先取map的crs定义; review by sogym
1 parent bf07589 commit af11251

File tree

17 files changed

+1373
-448
lines changed

17 files changed

+1373
-448
lines changed

src/common/mapping/MapStyle.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { WebMapService } from './WebMapService';
22
import { SourceListModelV2 } from './utils/SourceListModelV2';
33
import { createAppreciableLayerId, isSameRasterLayer } from './utils/util';
44

5-
export function createMapStyleExtending(SuperClass, { MapManager, mapRepo }) {
5+
export function createMapStyleExtending(SuperClass, { MapManager, crsManager }) {
66
return class MapStyle extends SuperClass {
77
constructor(id, options = {}, mapOptions = {}) {
88
super();
@@ -11,24 +11,22 @@ export function createMapStyleExtending(SuperClass, { MapManager, mapRepo }) {
1111
this.webMapService = new WebMapService(id, options);
1212
this._layerIdRenameMapList = [];
1313
this._appendLayers = false;
14+
this._baseProjection = '';
1415
}
1516

1617
initializeMap(_, map) {
18+
this._baseProjection = this._registerMapCRS(this.mapOptions);
1719
if (map) {
20+
if (!crsManager.isSameProjection(map, this._baseProjection)) {
21+
this.fire('projectionnotmatch');
22+
return;
23+
}
1824
this._appendLayers = true;
1925
this.map = map;
2026
this._addLayersToMap();
2127
return;
2228
}
2329
this.mapOptions.container = this.options.target;
24-
if (typeof this.mapOptions.crs === 'object' && this.mapOptions.crs.epsgCode) {
25-
this.mapOptions.crs = new mapRepo.CRS(
26-
this.mapOptions.crs.epsgCode,
27-
this.mapOptions.crs.WKT,
28-
this.mapOptions.crs.extent,
29-
this.mapOptions.crs.unit
30-
);
31-
}
3230
if (!this.mapOptions.transformRequest) {
3331
this.mapOptions.transformRequest = (url, resourceType) => {
3432
let proxy = '';
@@ -53,7 +51,7 @@ export function createMapStyleExtending(SuperClass, { MapManager, mapRepo }) {
5351
if (Object.prototype.hasOwnProperty.call(this.mapOptions, 'fadeDuration')) {
5452
fadeDuration = this.mapOptions.fadeDuration;
5553
}
56-
this.map = new MapManager({ ...this.mapOptions, fadeDuration });
54+
this.map = new MapManager({ ...this.mapOptions, fadeDuration, crs: this._baseProjection });
5755
this.fire('mapinitialized', { map: this.map });
5856
this.map.on('load', () => {
5957
this._sendMapToUser();
@@ -71,6 +69,17 @@ export function createMapStyleExtending(SuperClass, { MapManager, mapRepo }) {
7169
}
7270
}
7371

72+
_registerMapCRS(mapOptions) {
73+
const { crs } = mapOptions;
74+
let epsgCode = crs;
75+
if (typeof crs === 'object' && crs.epsgCode) {
76+
const { epsgCode: name, WKT: wkt, extent, unit } = crs;
77+
crsManager.registerCRS({ name, wkt, extent, unit });
78+
epsgCode = name;
79+
}
80+
return epsgCode;
81+
}
82+
7483
_addLayersToMap() {
7584
const { sources, layers, layerIdMapList } = this._setUniqueId(this.mapOptions.style);
7685
layers.forEach((layer) => {

src/common/mapping/WebMapBase.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
172172
this._mapInitializedHandler = this._mapInitializedHandler.bind(this);
173173
this._mapCreateSucceededHandler = this._mapCreateSucceededHandler.bind(this);
174174
this._addLayerChangedHandler = this._addLayerChangedHandler.bind(this);
175-
this._initWebMap(!this.map);
176175
}
177176

178177
/**
@@ -193,33 +192,6 @@ export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
193192
}
194193
}
195194

196-
/**
197-
* @function WebMapBase.prototype.setCRS
198-
* @description 更新地图投影。
199-
* @param {string|Object} crs - 地图 crs。
200-
*/
201-
setCRS(crs) {
202-
if (this.map) {
203-
this.mapOptions.crs = crs;
204-
if (this.mapOptions.crs) {
205-
if (this.map.getCRS(typeof crs === 'string' ? crs : crs.epsgCode)) {
206-
return;
207-
}
208-
if (crs.epsgCode) {
209-
this.mapOptions.crs = new mapRepo.CRS(
210-
this.mapOptions.crs.epsgCode,
211-
this.mapOptions.crs.WKT,
212-
this.mapOptions.crs.extent,
213-
this.mapOptions.crs.unit
214-
);
215-
this.map.setCRS(this.mapOptions.crs);
216-
} else {
217-
this.map.setCRS(mapRepo.CRS.get(crs));
218-
}
219-
}
220-
}
221-
}
222-
223195
/**
224196
* @function WebMapBase.prototype.setCenter
225197
* @description 更新地图中心点。
@@ -500,6 +472,10 @@ export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
500472
this.clean(false);
501473
}
502474

475+
_readyForInitializingWebMap() {
476+
this._initWebMap(!this.map);
477+
}
478+
503479
_initWebMap(clean = true) {
504480
clean && this.clean();
505481
if (this.webMapInfo) {
@@ -660,6 +636,7 @@ export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
660636
};
661637
}
662638
this.type = type;
639+
// initializeMap 完成3个步骤:1. 注册投影 2. 判断投影与存在的map是否一致 3. 创建地图
663640
this._handler.initializeMap(_mapInfo, this.map);
664641
}
665642

src/common/mapping/WebMapV2.js

Lines changed: 70 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* This program are made available under the terms of the Apache License, Version 2.0
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html. */
44
import cloneDeep from 'lodash.clonedeep';
5-
import { getProjection, registerProjection, toEpsgCode, transformCoodinates } from './utils/epsg-define';
5+
import { getProjection, toEpsgCode, transformCoodinates } from './utils/epsg-define';
66
import { ColorsPickerUtil } from '../util/ColorsPickerUtil';
77
import { Util } from '../commontypes/Util';
88
import { ArrayStatistic } from '../util/ArrayStatistic';
@@ -17,7 +17,7 @@ const INTERNET_MAP_BOUNDS = {
1717
BING: [-180, -90, 180, 90]
1818
}
1919

20-
export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataFlowService, GraticuleLayer }) {
20+
export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, crsManager, DataFlowService, GraticuleLayer }) {
2121
return class WebMapV2 extends SuperClass {
2222
constructor(
2323
id,
@@ -49,12 +49,23 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
4949
this._appendLayers = false;
5050
}
5151

52-
initializeMap(mapInfo, map) {
53-
if (map) {
54-
this._appendLayers = true;
55-
this.map = map;
52+
async initializeMap(mapInfo, map) {
53+
try {
54+
this.baseProjection = await this._registerMapCRS(mapInfo);
55+
if (map) {
56+
if (!crsManager.isSameProjection(map, this.baseProjection) && !this.ignoreBaseProjection) {
57+
this.fire('projectionnotmatch');
58+
return;
59+
}
60+
this._appendLayers = true;
61+
this.map = map;
62+
}
63+
this._mapInfo = mapInfo;
64+
this._loadLayers(mapInfo, this._taskID);
65+
} catch (error) {
66+
console.error(error);
67+
this.fire('mapcreatefailed', { error });
5668
}
57-
this._getMapInfo(mapInfo, this._taskID);
5869
}
5970

6071
cleanLayers(layers) {
@@ -95,12 +106,10 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
95106

96107
_initWebMap() {}
97108

109+
_getMapInfo() {}
110+
98111
_loadLayers(mapInfo, _taskID) {
99112
if (this.map) {
100-
if (this.map.getCRS().epsgCode !== this.baseProjection && !this.ignoreBaseProjection) {
101-
this.fire('projectionnotmatch', {});
102-
return;
103-
}
104113
this._handleLayerInfo(mapInfo, _taskID);
105114
} else {
106115
setTimeout(() => {
@@ -112,95 +121,59 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
112121
}
113122
}
114123

115-
_setCRS(baseProjection, wkt, bounds) {
116-
if (mapRepo.CRS.get(baseProjection)) {
117-
return;
118-
}
119-
const crs = new mapRepo.CRS(baseProjection, wkt, bounds, bounds[2] > 180 ? 'meter' : 'degree');
120-
mapRepo.CRS.set(crs);
121-
}
122-
123-
_getMapInfo(mapInfo, _taskID) {
124-
this._mapInfo = mapInfo;
125-
const { projection } = mapInfo;
126-
let bounds, wkt;
127-
this.baseProjection = toEpsgCode(projection);
128-
let defaultWktValue = getProjection(this.baseProjection, this.specifiedProj4);
129-
130-
if (defaultWktValue) {
131-
wkt = defaultWktValue;
132-
}
133-
if (!mapRepo.CRS.get(this.baseProjection)) {
134-
if (mapInfo.baseLayer && mapInfo.baseLayer.layerType === 'MAPBOXSTYLE') {
135-
let url = mapInfo.baseLayer.dataSource.url;
136-
if (url.indexOf('/restjsr/') > -1 && !/\/style\.json$/.test(url)) {
137-
url += '/style.json';
138-
}
139-
this.webMapService.getMapBoxStyle(url).then((res) => {
124+
async _registerMapCRS(mapInfo) {
125+
const { projection, extent, baseLayer = {} } = mapInfo;
126+
const epsgCode = toEpsgCode(projection);
127+
let crs = {
128+
name: epsgCode,
129+
extent: [extent.leftBottom.x, extent.leftBottom.y, extent.rightTop.x, extent.rightTop.y],
130+
wkt: this._getProjectionWKT(projection)
131+
};
132+
if (!crsManager.getCRS(epsgCode)) {
133+
switch (baseLayer.layerType) {
134+
case 'MAPBOXSTYLE': {
135+
let url = baseLayer.dataSource.url;
136+
if (url.indexOf('/restjsr/') > -1 && !/\/style\.json$/.test(url)) {
137+
url += '/style.json';
138+
}
139+
const res = await this.webMapService.getMapBoxStyle(url);
140140
if (res && res.metadata && res.metadata.indexbounds) {
141-
bounds = res.metadata.indexbounds;
142-
} else {
143-
bounds = [
144-
mapInfo.extent.leftBottom.x,
145-
mapInfo.extent.leftBottom.y,
146-
mapInfo.extent.rightTop.x,
147-
mapInfo.extent.rightTop.y
148-
];
141+
crs.extent = res.metadata.indexbounds;
149142
}
150-
this._defineProj4(projection);
151-
this._setCRS(this.baseProjection, wkt, bounds);
152-
this._loadLayers(mapInfo, _taskID);
153-
});
154-
} else if (mapInfo.baseLayer && mapInfo.baseLayer.layerType === 'TILE') {
155-
// 获取地图的wkt
156-
this.getEpsgCodeWKT(`${mapInfo.baseLayer.url}/prjCoordSys.wkt`, {
157-
withoutFormatSuffix: true,
158-
withCredentials: this.webMapService.handleWithCredentials('', mapInfo.baseLayer.url, false)
159-
}).then((res) => {
160-
if (!wkt) {
161-
wkt = res;
143+
break;
144+
}
145+
case 'TILE': {
146+
// 获取地图的wkt
147+
if (!crs.wkt) {
148+
crs.wkt = await this.getEpsgCodeWKT(`${baseLayer.url}/prjCoordSys.wkt`, {
149+
withoutFormatSuffix: true,
150+
withCredentials: this.webMapService.handleWithCredentials('', baseLayer.url, false)
151+
});
162152
}
163-
this.getBounds(`${mapInfo.baseLayer.url}.json`, {
153+
const boundsRes = await this.getBounds(`${baseLayer.url}.json`, {
164154
withoutFormatSuffix: true,
165-
withCredentials: this.webMapService.handleWithCredentials('', mapInfo.baseLayer.url, false)
166-
}).then((res) => {
167-
if (res && res.bounds) {
168-
bounds = [
169-
res.bounds.leftBottom.x,
170-
res.bounds.leftBottom.y,
171-
res.bounds.rightTop.x,
172-
res.bounds.rightTop.y
173-
];
174-
} else {
175-
bounds = [
176-
mapInfo.extent.leftBottom.x,
177-
mapInfo.extent.leftBottom.y,
178-
mapInfo.extent.rightTop.x,
179-
mapInfo.extent.rightTop.y
180-
];
181-
}
182-
this._defineProj4(wkt, projection);
183-
this._setCRS(this.baseProjection, wkt, bounds);
184-
this._loadLayers(mapInfo, _taskID);
155+
withCredentials: this.webMapService.handleWithCredentials('', baseLayer.url, false)
185156
});
186-
});
187-
} else {
188-
const error = new Error('Unsupported coordinate system!');
189-
console.log(error);
190-
this.fire('mapcreatefailed', { error });
157+
if (boundsRes && boundsRes.bounds) {
158+
crs.extent = [
159+
boundsRes.bounds.leftBottom.x,
160+
boundsRes.bounds.leftBottom.y,
161+
boundsRes.bounds.rightTop.x,
162+
boundsRes.bounds.rightTop.y
163+
];
164+
}
165+
break;
166+
}
167+
default:
168+
crs = null;
169+
break;
191170
}
192-
} else {
193-
wkt = mapRepo.CRS.get(this.baseProjection).WKT
194-
this._defineProj4(wkt || projection);
195-
bounds = [
196-
mapInfo.extent.leftBottom.x,
197-
mapInfo.extent.leftBottom.y,
198-
mapInfo.extent.rightTop.x,
199-
mapInfo.extent.rightTop.y
200-
];
201-
this._setCRS(this.baseProjection, wkt, bounds);
202-
this._loadLayers(mapInfo, _taskID);
203171
}
172+
if (!crs) {
173+
throw new Error('Unsupported coordinate system!')
174+
}
175+
crsManager.registerCRS(crs);
176+
return epsgCode;
204177
}
205178

206179
_handleLayerInfo(mapInfo, _taskID) {
@@ -499,7 +472,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
499472
layerInfo.dataSource &&
500473
layerInfo.dataSource.type !== 'REST_DATA'
501474
) {
502-
this._unprojectProjection = this._defineProj4(projection);
475+
this._unprojectProjection = toEpsgCode(projection);
503476
features = this.transformFeatures(features);
504477
}
505478

@@ -2772,15 +2745,10 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
27722745
return tiandituUrls;
27732746
}
27742747

2775-
_defineProj4(projection, defaultEpsgCode) {
2748+
_getProjectionWKT(projection) {
27762749
let epsgCode = toEpsgCode(projection);
27772750
const reg = /^EPSG:/;
2778-
const defValue = epsgCode && projection.match(reg) ? getProjection(epsgCode, this.specifiedProj4) : projection;
2779-
if (!epsgCode && defaultEpsgCode && defaultEpsgCode.match(reg)) {
2780-
epsgCode = defaultEpsgCode;
2781-
}
2782-
registerProjection(epsgCode, defValue, this.specifiedProj4);
2783-
return epsgCode;
2751+
return epsgCode && projection.match(reg) ? getProjection(epsgCode, this.specifiedProj4) : projection;
27842752
}
27852753

27862754
_fetchRequest(url, type, options) {
@@ -2805,7 +2773,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, DataF
28052773

28062774
getBounds(baseUrl, options) {
28072775
if (!baseUrl) {
2808-
return;
2776+
return Promise.resolve(null);
28092777
}
28102778
return this._fetchRequest(baseUrl, 'json', options);
28112779
}

0 commit comments

Comments
 (0)