Skip to content

Commit 6dc64dd

Browse files
committed
【feature】webmap 注册 wkt 优化
1 parent 50ef82f commit 6dc64dd

File tree

20 files changed

+291
-196
lines changed

20 files changed

+291
-196
lines changed

src/common/mapping/WebMapBase.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Canvg from 'canvg';
22
import { coordEach } from '@turf/meta';
33
import difference from 'lodash.difference';
44
import { WebMapService } from './WebMapService';
5-
import { epsgDefine } from './utils/epsg-define';
5+
import { getEpsgCodeInfo, getProjection, registerProjection } from './utils/epsg-define';
66
import { ArrayStatistic } from '../util/ArrayStatistic';
77
import { ColorsPickerUtil } from '../util/ColorsPickerUtil';
88
import { Util } from '../commontypes/Util';
@@ -30,6 +30,7 @@ export function createWebMapBaseExtending(SuperClass = Events, fireField = 'trig
3030
this.excludePortalProxyUrl = options.excludePortalProxyUrl;
3131
this.isSuperMapOnline = options.isSuperMapOnline;
3232
this.ignoreBaseProjection = options.ignoreBaseProjection;
33+
this.specifiedProj4 = options.proj4;
3334
this.echartslayer = [];
3435
this.canvgsV = [];
3536
this.webMapService = new WebMapService(id, options);
@@ -244,13 +245,13 @@ export function createWebMapBaseExtending(SuperClass = Events, fireField = 'trig
244245
return;
245246
}
246247
if (result && layer.projection) {
247-
if (!epsgDefine.getProjection(layer.projection)) {
248-
const epsgWKT = await this.webMapService.getEpsgCodeInfo(
248+
if (!getProjection(layer.projection, this.specifiedProj4)) {
249+
const epsgWKT = await getEpsgCodeInfo(
249250
layer.projection.split(':')[1],
250251
this.serverUrl
251252
);
252253
if (epsgWKT) {
253-
epsgDefine.registerProjection(layer.projection, epsgWKT);
254+
registerProjection(layer.projection, epsgWKT, this.specifiedProj4);
254255
}
255256
}
256257
}

src/common/mapping/WebMapService.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ export class WebMapService {
390390
dataSource.url,
391391
[decodeURIComponent(dataSource.dataSourceName) || layer.name],
392392
result => {
393+
if (!result.result.features) {
394+
reject('features must be valid');
395+
return;
396+
}
393397
features = this.parseGeoJsonData2Feature({
394398
allDatas: {
395399
features: result.result.features.features
@@ -1241,23 +1245,6 @@ export class WebMapService {
12411245
getFeatureBySQLService.processAsync(getFeatureBySQLParams);
12421246
}
12431247

1244-
async getEpsgCodeInfo(epsgCode, iPortalUrl) {
1245-
const url = iPortalUrl.slice(-1) === '/' ? iPortalUrl : `${iPortalUrl}/`;
1246-
let codeUrl = `${url}epsgcodes/${epsgCode}.json`;
1247-
const wkt = await FetchRequest.get(codeUrl, null)
1248-
.then(response => {
1249-
return response.json();
1250-
})
1251-
.then(epsgcodeInfo => {
1252-
return epsgcodeInfo.wkt;
1253-
})
1254-
.catch(err => {
1255-
console.error(err);
1256-
return undefined;
1257-
});
1258-
return wkt;
1259-
}
1260-
12611248
statisticsFeatures(features, fields, fieldCaptions, fieldTypes) {
12621249
const data = {
12631250
features,

src/common/mapping/WebMapV2.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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 proj4 from 'proj4';
6-
import { epsgDefine, toEpsgCode } from './utils/epsg-define';
5+
import { getProjection, registerProjection, toEpsgCode, transformCoodinates } from './utils/epsg-define';
76
import { ColorsPickerUtil } from '../util/ColorsPickerUtil';
87
import { Util } from '../commontypes/Util';
98
import { ArrayStatistic } from '../util/ArrayStatistic';
@@ -110,7 +109,7 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
110109
const { projection } = mapInfo;
111110
let bounds, wkt;
112111
this.baseProjection = toEpsgCode(projection);
113-
let defaultWktValue = epsgDefine.getProjection(this.baseProjection);
112+
let defaultWktValue = getProjection(this.baseProjection, this.specifiedProj4);
114113

115114
if (defaultWktValue) {
116115
wkt = defaultWktValue;
@@ -463,7 +462,6 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
463462
if (features && features[0] && features[0].geometry && features[0].geometry.type === 'Polygon') {
464463
features = this._handleMultyPolygon(features);
465464
}
466-
467465
if (
468466
features &&
469467
projection &&
@@ -2607,8 +2605,8 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
26072605
if (sourceProjection === 'EPSG:4326') {
26082606
return point;
26092607
}
2610-
const coor = proj4(sourceProjection, 'EPSG:4326', point);
2611-
const proj = proj4.defs(sourceProjection);
2608+
const coor = transformCoodinates({ coordinates: point, sourceProjection, proj4: this.specifiedProj4 });
2609+
const proj = getProjection(sourceProjection, this.specifiedProj4);
26122610
if (isReverse && proj.axis && proj.axis.indexOf('ne') === 0) {
26132611
coor.reverse();
26142612
}
@@ -2699,25 +2697,13 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo }) {
26992697
}
27002698

27012699
_defineProj4(projection, defaultEpsgCode) {
2702-
let epsgCode = projection;
2703-
let epsgValue;
2704-
if (!projection.split(':')[1]) {
2705-
if (defaultEpsgCode && defaultEpsgCode.split(':')[1]) {
2706-
epsgCode = defaultEpsgCode;
2707-
} else {
2708-
epsgCode = toEpsgCode(projection);
2709-
}
2710-
epsgValue = projection;
2711-
}
2712-
const defaultValue = epsgDefine.getProjection(epsgCode);
2713-
const defValue = epsgValue || defaultValue;
2714-
2715-
if (!defValue) {
2716-
console.error(`${epsgCode} not define`);
2717-
} else {
2718-
!proj4.defs(epsgCode) && proj4.defs(epsgCode, defValue);
2719-
!defaultValue && epsgDefine.registerProjection(epsgCode, defValue);
2700+
let epsgCode = toEpsgCode(projection);
2701+
const reg = /^EPSG:/;
2702+
const defValue = epsgCode && projection.match(reg) ? getProjection(epsgCode, this.specifiedProj4) : projection;
2703+
if (!epsgCode && defaultEpsgCode && defaultEpsgCode.match(reg)) {
2704+
epsgCode = defaultEpsgCode;
27202705
}
2706+
registerProjection(epsgCode, defValue, this.specifiedProj4);
27212707
return epsgCode;
27222708
}
27232709

src/common/mapping/utils/L7LayerUtil.js

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FetchRequest } from '../../util/FetchRequest';
2-
import proj4 from 'proj4';
32
import center from '@turf/center';
43
import * as G2 from '@antv/g2';
54
import { isNumber } from './util';
65
import Color from './Color';
76
import debounce from 'lodash.debounce';
7+
import { getEpsgCodeInfo, getProjection, registerProjection, transformCoodinates } from './epsg-define';
88

99
const SelectStyleTypes = {
1010
basic: 'base',
@@ -186,8 +186,41 @@ export function isL7Layer(layer) {
186186
);
187187
}
188188

189+
export function getL7Filter(filter, featureFilter) {
190+
if (!filter) {
191+
return;
192+
}
193+
const [condition, ...expressions] = filter;
194+
const newExpressions = expressions.filter((exp) => {
195+
const [, f] = exp;
196+
if (['$type', '$id'].includes(f)) {
197+
return false;
198+
}
199+
return true;
200+
});
201+
const field = Array.from(new Set(getFilterFields(newExpressions)));
202+
const fFilter = featureFilter([condition, ...newExpressions]);
203+
const filterFunc = fFilter.filter.bind(fFilter);
204+
return {
205+
field,
206+
values: (...args) => {
207+
const properties = {};
208+
field.forEach((f, idx) => {
209+
properties[f] = args[idx];
210+
});
211+
const result = filterFunc(
212+
{},
213+
{
214+
properties
215+
}
216+
);
217+
return result;
218+
}
219+
};
220+
}
221+
189222
export function L7LayerUtil(config) {
190-
const { featureFilter, expression, spec, L7Layer, L7 } = config;
223+
const { featureFilter, expression, spec, L7Layer, L7, proj4 } = config;
191224

192225
/**
193226
* @param {string} url
@@ -487,11 +520,21 @@ export function L7LayerUtil(config) {
487520
}
488521
// proj4缺陷,EPSG:4214的坐标x为180,转换后变成-179.
489522
if (fromProjection === 'EPSG:4214' && toProjection === 'EPSG:4326' && coordinates[0] === 180) {
490-
const newCoordinate = proj4(fromProjection, toProjection, coordinates);
523+
const newCoordinate = transformCoodinates({
524+
coordinates,
525+
sourceProjection: fromProjection,
526+
destProjection: toProjection,
527+
proj4
528+
});
491529
newCoordinate[0] = 180;
492530
return newCoordinate;
493531
}
494-
return proj4(fromProjection, toProjection, coordinates);
532+
return transformCoodinates({
533+
coordinates,
534+
sourceProjection: fromProjection,
535+
destProjection: toProjection,
536+
proj4
537+
});
495538
}
496539

497540
/**
@@ -581,6 +624,10 @@ export function L7LayerUtil(config) {
581624
const result = await resultRes.json();
582625
const projection = `EPSG:${result.epsgCode}`;
583626
if (projection !== 'EPSG:4326') {
627+
if (!getProjection(projection, proj4)) {
628+
const epsgWKT = await getEpsgCodeInfo(projection, options.server);
629+
registerProjection(projection, epsgWKT, proj4);
630+
}
584631
const newFeatures = transformGeometryCoordinates(allFeature.features, projection);
585632
newFeatures && (allFeature.features = newFeatures);
586633
}
@@ -679,23 +726,6 @@ export function L7LayerUtil(config) {
679726
return rules[type] && rules[type](source, sourceLayer, options);
680727
}
681728

682-
function getFilterFields(filter) {
683-
const result = [];
684-
for (const exp of filter) {
685-
if (exp instanceof Array && exp[1] && typeof exp[1] === 'string') {
686-
result.push(exp[1]);
687-
continue;
688-
}
689-
if (exp instanceof Array) {
690-
const subResult = getFilterFields(exp);
691-
if (subResult && subResult.length > 0) {
692-
result.push(...subResult);
693-
}
694-
}
695-
}
696-
return result;
697-
}
698-
699729
/**
700730
* 获取L7图层所需要的共有的图层信息
701731
* @param layer
@@ -716,7 +746,7 @@ export function L7LayerUtil(config) {
716746
maxZoom: maxzoom,
717747
visible: layout.visibility === 'none' ? false : true
718748
},
719-
filter: getL7Filter(filter)
749+
filter: getL7Filter(filter, featureFilter)
720750
};
721751
}
722752

@@ -1997,41 +2027,7 @@ export function L7LayerUtil(config) {
19972027
return layer;
19982028
}
19992029

2000-
function getL7Filter(filter) {
2001-
if (!filter) {
2002-
return;
2003-
}
2004-
const [condition, ...expressions] = filter;
2005-
const newExpressions = expressions.filter((exp) => {
2006-
const [, f] = exp;
2007-
if (['$type', '$id'].includes(f)) {
2008-
return false;
2009-
}
2010-
return true;
2011-
});
2012-
const field = Array.from(new Set(getFilterFields(newExpressions)));
2013-
const fFilter = featureFilter([condition, ...newExpressions]);
2014-
const filterFunc = fFilter.filter.bind(fFilter);
2015-
return {
2016-
field,
2017-
values: (...args) => {
2018-
const properties = {};
2019-
field.forEach((f, idx) => {
2020-
properties[f] = args[idx];
2021-
});
2022-
const result = filterFunc(
2023-
{},
2024-
{
2025-
properties
2026-
}
2027-
);
2028-
return result;
2029-
}
2030-
};
2031-
}
2032-
20332030
return {
2034-
getL7Filter,
20352031
async addL7Layers({ map, webMapInfo, l7Layers, spriteDatas, options }) {
20362032
// 添加L7图层
20372033
const { layers, sources, sprite } = webMapInfo;
@@ -2081,3 +2077,20 @@ export function L7LayerUtil(config) {
20812077
}
20822078

20832079

2080+
function getFilterFields(filter) {
2081+
const result = [];
2082+
for (const exp of filter) {
2083+
if (exp instanceof Array && exp[1] && typeof exp[1] === 'string') {
2084+
result.push(exp[1]);
2085+
continue;
2086+
}
2087+
if (exp instanceof Array) {
2088+
const subResult = getFilterFields(exp);
2089+
if (subResult && subResult.length > 0) {
2090+
result.push(...subResult);
2091+
}
2092+
}
2093+
}
2094+
return result;
2095+
}
2096+

0 commit comments

Comments
 (0)