Skip to content

Commit e11367d

Browse files
[update] 支持带WFS数据的地图点选查询
(review by xiongjiaojiao)
1 parent f8c11b7 commit e11367d

File tree

7 files changed

+861
-133
lines changed

7 files changed

+861
-133
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = {
4343
'!mapboxgl/*/__tests__/**/type.{js,jsx}'
4444
],
4545
transformIgnorePatterns: [
46-
'node_modules/(?!(mapbox-gl|axios|element-ui|ant-design-vue|geographic-coordinate-converter|videojs-flvjs-es6|vue-videojs7|three)/)'
46+
'node_modules/(?!(ol|mapbox-gl|axios|element-ui|ant-design-vue|geographic-coordinate-converter|videojs-flvjs-es6|vue-videojs7|three)/)'
4747
],
4848
modulePaths: ['src', 'node_modules'],
4949
reporters: ["default", "jest-teamcity"],

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"css-vars-ponyfill": "2.2.1",
5555
"echarts": "^4.8.0",
5656
"echarts-liquidfill": "^2.0.5",
57+
"fast-xml-parser": "^5.2.5",
5758
"flush-promises": "^1.0.2",
5859
"flv.js": "^1.5.0",
5960
"geojson": "^0.5.0",
@@ -78,6 +79,7 @@
7879
"mapbox-gl-compare": "^0.4.0",
7980
"mapv": "^2.0.34",
8081
"mux.js": "^5.6.7",
82+
"ol": "^6.15.1",
8183
"omit.js": "^2.0.2",
8284
"proj4": "^2.8.0",
8385
"resize-detector": "^0.2.0",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
let transformCoordinate, transformCoodinates, mockProj4;
2+
3+
beforeEach(() => {
4+
jest.isolateModules(() => {
5+
mockProj4 = jest.fn();
6+
mockProj4.defs = jest.fn(() => 'FAKE_DEF');
7+
8+
jest.mock('proj4', () => mockProj4);
9+
10+
({ transformCoordinate, transformCoodinates } = require('../epsg-define'));
11+
});
12+
});
13+
14+
afterEach(() => {
15+
jest.restoreAllMocks();
16+
jest.clearAllMocks();
17+
});
18+
19+
describe('epsg-define (mock proj4)', () => {
20+
it('transformCoordinate', () => {
21+
const coords = [120, 30];
22+
23+
// same projection
24+
expect(transformCoordinate('EPSG:4326', 'EPSG:4326', coords)).toEqual(coords);
25+
26+
// EPSG:4490 <-> EPSG:4326
27+
expect(transformCoordinate('EPSG:4490', 'EPSG:4326', coords)).toEqual(coords);
28+
expect(transformCoordinate('EPSG:4326', 'EPSG:4490', coords)).toEqual(coords);
29+
30+
// EPSG:4214 -> EPSG:4326 with x=180 should keep 180
31+
mockProj4.mockImplementation(() => [-179, 30]);
32+
const fixed = transformCoordinate('EPSG:4214', 'EPSG:4326', [180, 30]);
33+
expect(fixed[0]).toBe(180);
34+
35+
// normal transform
36+
mockProj4.mockImplementation(() => [121, 31]);
37+
const result = transformCoordinate('EPSG:3857', 'EPSG:4326', coords);
38+
expect(result).toEqual([121, 31]);
39+
});
40+
41+
it('transformCoodinates', () => {
42+
const coords = [120, 30];
43+
44+
// success
45+
mockProj4.mockImplementation(() => [121, 31]);
46+
const result = transformCoodinates({
47+
coordinates: coords,
48+
sourceProjection: 'EPSG:3857',
49+
destProjection: 'EPSG:4326'
50+
});
51+
expect(result).toEqual([121, 31]);
52+
53+
// error
54+
mockProj4.mockImplementation(() => {
55+
throw new Error('proj4 error');
56+
});
57+
expect(() =>
58+
transformCoodinates({
59+
coordinates: coords,
60+
sourceProjection: 'EPSG:9999',
61+
destProjection: 'EPSG:4326'
62+
})
63+
).toThrow('Error: proj4 error is not defined');
64+
});
65+
});

src/common/_utils/epsg-define.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,28 @@ export function transformCoodinates({ coordinates, sourceProjection, destProject
6767
throw errorMsg;
6868
}
6969
}
70+
71+
export function transformCoordinate(fromProjection, toProjection, coordinates) {
72+
if (fromProjection === toProjection) return coordinates;
73+
if (
74+
(fromProjection === 'EPSG:4490' && toProjection === 'EPSG:4326') ||
75+
(fromProjection === 'EPSG:4326' && toProjection === 'EPSG:4490')
76+
) {
77+
return coordinates;
78+
}
79+
// proj4缺陷,EPSG:4214的坐标x为180,转换后变成-179.
80+
if (fromProjection === 'EPSG:4214' && toProjection === 'EPSG:4326' && coordinates[0] === 180) {
81+
const newCoordinate = transformCoodinates({
82+
coordinates,
83+
sourceProjection: fromProjection,
84+
destProjection: toProjection
85+
});
86+
newCoordinate[0] = 180;
87+
return newCoordinate;
88+
}
89+
return transformCoodinates({
90+
coordinates,
91+
sourceProjection: fromProjection,
92+
destProjection: toProjection
93+
});
94+
}

0 commit comments

Comments
 (0)