forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlowLayer.js
More file actions
148 lines (138 loc) · 4.97 KB
/
DataFlowLayer.js
File metadata and controls
148 lines (138 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import L from "leaflet";
import '../core/Base';
import {DataFlowService} from "../services/DataFlowService";
/**
* @class L.supermap.dataFlowLayer
* @classdesc 实时数据图层源
* @category iServer DataFlow
* @extends L.GeoJSON{@linkdoc-leaflet/#geojson}
* @param url - {string} 实时数据图层服务地址
* @param options - {Object} 设置图层参数。如:<br>
* geometry - {Object} GeoJSON几何对象<br>
* prjCoordSys - {Object} 投影坐标对象。<br>
* excludeField - {string} 排除字段。<br>
* idField - {string} id字段。
*/
export var DataFlowLayer = L.GeoJSON.extend({
options: {
geometry: null,
prjCoordSys: null,
excludeField: null,
idField: "id"
},
initialize: function (url, options) {
options = options || {};
var me = this;
if (options.style && !options.pointToLayer) {
options.pointToLayer = function (geojson, latlng) {
return L.circleMarker(latlng, options.style());
}
}
L.Util.setOptions(me, options);
me._layers = {};
L.stamp(me);
me.url = url;
this.idCache = {};
},
/**
* @private
* @function L.supermap.dataFlowLayer.prototype.onAdd
* @description 添加地图
* @param map - {L.map} 待添加的地图
*/
onAdd: function (map) { // eslint-disable-line no-unused-vars
var me = this;
me.dataService = new DataFlowService(this.url, {
geometry: this.options.geometry,
prjCoordSys: this.options.prjCoordSys,
excludeField: this.options.excludeField
}).initSubscribe();
me.dataService.on('subscribeSocketConnected', function (e) {
me.fire("subscribeSuccessed", e);
});
me.dataService.on('messageSuccessed', function (msg) {
me._onMessageSuccessed(msg);
});
me.dataService.on('setFilterParamSuccessed', function (msg) {
me.fire("setFilterParamSuccessed", msg);
});
},
/**
* @private
* @function L.supermap.dataFlowLayer.prototype.onRemove
* @description 删除指定地图
* @param map - {L.map} 待删除的地图
*/
onRemove: function (map) { // eslint-disable-line no-unused-vars
this.dataService.unSubscribe();
},
/**
* @function L.supermap.dataFlowLayer.prototype.setExcludeField
* @description 设置唯一字段
* @param excludeField - {string} 唯一字段
*/
setExcludeField: function (excludeField) {
this.dataService.setExcludeField(excludeField);
this.options.excludeField = excludeField;
return this;
},
/**
* @function L.supermap.dataFlowLayer.prototype.setGeometry
* @description 设置集合要素
* @param geometry - {Object} 待设置的GeoJSON几何要素对象
*/
setGeometry: function (geometry) {
this.dataService.setGeometry(geometry);
this.options.geometry = geometry;
return this;
},
_onMessageSuccessed: function (msg) {
var geojson = msg.featureResult;
var geoID = msg.featureResult.properties[this.options.idField];
var layer = null;
if (geoID !== undefined && this.idCache[geoID]) {
layer = this.getLayer(this.idCache[geoID]);
this._updateLayerData(layer, geojson);
} else {
layer = L.GeoJSON.geometryToLayer(geojson, this.options);
layer.feature = L.GeoJSON.asFeature(geojson);
this.addLayer(layer);
if (geoID !== undefined) {
this.idCache[geoID] = this.getLayerId(layer);
}
}
if (this.options.onEachFeature) {
this.options.onEachFeature(geojson, layer);
}
this.fire("dataUpdated", {layer: this, updateLayer: layer, data: msg.featureResult});
},
_updateLayerData: function (layer, geojson) {
if (geojson.properties) {
layer.feature.properties = geojson.properties;
}
var latlngs = [];
switch (geojson.geometry.type) {
case 'Point':
latlngs = L.GeoJSON.coordsToLatLng(geojson.geometry.coordinates);
layer.setLatLng(latlngs);
break;
case 'LineString':
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 0);
layer.setLatLngs(latlngs);
break;
case 'MultiLineString':
case 'Polygon':
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 1);
layer.setLatLngs(latlngs);
break;
case 'MultiPolygon':
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 2);
layer.setLatLngs(latlngs);
break;
}
}
});
export var dataFlowLayer = function (url, options) {
return new DataFlowLayer(url, options);
};
L.supermap.dataFlowLayer = dataFlowLayer;