Skip to content

Commit cce40e3

Browse files
[fix]标绘去掉id drawType, getSource重写
AI-GEN: 70% Cursor
1 parent 3694e85 commit cce40e3

7 files changed

Lines changed: 88 additions & 7 deletions

File tree

‎examples/mapboxgl/videoMapWithTime.html‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
1717
<script type="text/javascript" include="mapbox-gl-enhance,proj4,turf,videojs,opencv" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
1818
<script type="text/javascript">
19-
var dataUrl = 'http://172.16.14.44:8090/iserver/services/data-video/rest/data';
19+
var host = 'https://iserver.supermap.io';
20+
var dataUrl = host + '/iserver/services/data-video/rest/data';
2021
var videoMap;
2122
var count = 0;
2223
var dynamicLayerIds = [];

‎src/mapboxgl/mapping/GeojsonSource.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export default class GeojsonSource {
5050
this.update();
5151
}
5252

53+
getData() {
54+
return cloneDeep(this.originalData);
55+
}
56+
5357
_transformData(data) {
5458
return transformVideoMapGeoJSON(data, this.videoMap);
5559
}

‎src/mapboxgl/mapping/VideoMap.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,38 @@ export class VideoMap extends mapboxgl.Evented {
350350
this.sourceCache[id] = geojsonSource;
351351
}
352352

353+
/**
354+
* @function VideoMap.prototype.getSource
355+
* @description 获取数据源。视频地图 GeoJSON 数据源的 data 为 EPSG:4326 原始数据,其余行为保持 Mapbox source 兼容。
356+
* @param {string} id - 数据源 id。
357+
* @returns {Object|undefined} Mapbox source 或视频地图 GeoJSON source 代理。
358+
*/
359+
getSource(id) {
360+
if (!this._mapExisted()) {
361+
return undefined;
362+
}
363+
const source = this.map.getSource(id);
364+
const videoSource = this.sourceCache[id];
365+
if (!source || !videoSource || typeof videoSource.getData !== 'function') {
366+
return source;
367+
}
368+
return new Proxy(source, {
369+
get(target, property) {
370+
if (property === '_data') {
371+
return videoSource.getData();
372+
}
373+
if (property === 'getData') {
374+
return videoSource.getData.bind(videoSource);
375+
}
376+
if (property === 'setData') {
377+
return videoSource.setData.bind(videoSource);
378+
}
379+
const value = Reflect.get(target, property, target);
380+
return typeof value === 'function' ? value.bind(target) : value;
381+
}
382+
});
383+
}
384+
353385
/**
354386
* @function VideoMap.prototype.updateAtTime
355387
* @description 使用视频时间更新相机参数并重新投影已添加的 GeoJSON 数据源。

‎src/mapboxgl/mapping/VideoMapDraw.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,9 @@ export default class VideoMapDraw {
523523
this.features.push(feature);
524524
this._setSelectedIds([feature.id]);
525525
this._syncResult();
526-
this.videoMap.fire('drawcreate', { feature, data: this.getData() });
526+
const eventFeature = Object.assign({}, feature);
527+
delete eventFeature.id;
528+
this.videoMap.fire('drawcreate', { feature: eventFeature, data: this.getData() });
527529
this._emitModeChange();
528530
this._emitUpdate();
529531
this._showMeasureLabels(feature);
@@ -760,7 +762,7 @@ export default class VideoMapDraw {
760762
return {
761763
type: 'Feature',
762764
id,
763-
properties: { drawType: type, id },
765+
properties: {},
764766
geometry: { type, coordinates }
765767
};
766768
}
@@ -825,15 +827,15 @@ export default class VideoMapDraw {
825827
features.push({
826828
type: 'Feature',
827829
id: `videomap-draw-draft-p${index}`,
828-
properties: { drawType: 'Point', draft: true },
830+
properties: { draft: true },
829831
geometry: { type: 'Point', coordinates: coord }
830832
});
831833
});
832834
if (this.mode === 'LineString' && vertices.length >= 2) {
833835
features.push({
834836
type: 'Feature',
835837
id: 'videomap-draw-draft-line',
836-
properties: { drawType: 'LineString', draft: true },
838+
properties: { draft: true },
837839
geometry: { type: 'LineString', coordinates: vertices }
838840
});
839841
}
@@ -842,14 +844,14 @@ export default class VideoMapDraw {
842844
features.push({
843845
type: 'Feature',
844846
id: 'videomap-draw-draft-polygon',
845-
properties: { drawType: 'Polygon', draft: true },
847+
properties: { draft: true },
846848
geometry: { type: 'Polygon', coordinates: [vertices.concat([vertices[0]])] }
847849
});
848850
} else {
849851
features.push({
850852
type: 'Feature',
851853
id: 'videomap-draw-draft-line',
852-
properties: { drawType: 'LineString', draft: true },
854+
properties: { draft: true },
853855
geometry: { type: 'LineString', coordinates: vertices }
854856
});
855857
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import VideoMapDraw from '../../../src/mapboxgl/mapping/VideoMapDraw';
2+
3+
describe('VideoMapDraw', () => {
4+
function createVideoMap() {
5+
return {
6+
sourceCache: {},
7+
map: {
8+
getCanvas: () => ({ style: {} }),
9+
doubleClickZoom: {
10+
enable: jasmine.createSpy('enable'),
11+
disable: jasmine.createSpy('disable')
12+
}
13+
},
14+
fire: jasmine.createSpy('fire')
15+
};
16+
}
17+
18+
it('keeps feature id internally without putting it in properties', () => {
19+
const videoMap = createVideoMap();
20+
const draw = new VideoMapDraw(videoMap);
21+
const feature = draw._feature('Point', [116, 39]);
22+
23+
expect(feature.id).toBe('videomap-draw-1');
24+
expect(feature.properties).toEqual({});
25+
});
26+
27+
it('removes feature id from the drawcreate event feature', () => {
28+
const videoMap = createVideoMap();
29+
const draw = new VideoMapDraw(videoMap);
30+
const feature = draw._feature('Point', [116, 39]);
31+
32+
draw._completeFeature(feature);
33+
34+
const drawcreateCall = videoMap.fire.calls.allArgs().find((args) => args[0] === 'drawcreate');
35+
const eventFeature = drawcreateCall[1].feature;
36+
expect(eventFeature.id).toBeUndefined();
37+
expect(eventFeature.properties).toEqual({});
38+
expect(draw.features[0].id).toBe('videomap-draw-1');
39+
});
40+
});

‎test/mapboxgl/mapping/VideoMapSpec.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ describe('mapboxgl_videoMap', () => {
279279
}
280280
});
281281
expect(videoMap.sourceCache['test111']).not.toBe(null);
282+
expect(videoMap.getSource('test111').getData()).toEqual(videoMap.sourceCache['test111'].originalData);
282283
videoMap.removeSource('test111');
283284
expect(videoMap.sourceCache['test111']).toBeUndefined();
284285
done();

‎test/test-main-mapboxgl.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import './mapboxgl/mapping/WebMapV2Spec.js';
8888
import './mapboxgl/mapping/WebMapV3Spec.js';
8989
import './mapboxgl/mapping/InitMapSpec.js';
9090
import './mapboxgl/mapping/VideoMapSpec.js';
91+
import './mapboxgl/mapping/VideoMapDrawSpec.js';
9192
import './mapboxgl/mapping/clipVideoMapGeometrySpec.js';
9293
import './mapboxgl/mapping/VideoMapUtilSpec.js';
9394
import './mapboxgl/mapping/WebMapV22Spec.js';

0 commit comments

Comments
 (0)