gisviewer-vue3-arcgis
Version:
在应用中引入私服的包, 项目根目录下 新建文件 .npmrc 在 .npmrc 中对包源进行配置:
103 lines (102 loc) • 3.18 kB
JavaScript
import * as h from "@turf/turf";
import d from "ol/Feature";
import { LineString as p } from "ol/geom";
import g from "ol/layer/Vector";
import { unByKey as y } from "ol/Observable";
import S from "ol/source/Vector";
import w from "ol/style/Stroke";
import L from "ol/style/Style";
const F = (a, o) => {
try {
const e = h.lineString(a);
return h.lineOffset(e, o, { units: "meters" }).geometry.coordinates;
} catch {
return a;
}
}, P = (a) => a < 5 ? "#f50b00" : a < 10 ? "#f7a030" : a < 20 ? "#f2df1a" : "#46a914";
class z {
constructor(o) {
this.lastParams = null, this.resolutionListenerKey = null, this.hasGpu = !0;
const e = localStorage.getItem("gpu");
this.hasGpu = e !== "Unknown", this.map = o, this.roadSource = new S(), this.roadLayer = new g({ source: this.roadSource }), this.map.addLayer(this.roadLayer);
}
_normalizeParams(o) {
const e = o.roadSegmentList.reduce((t, s) => {
let r = s.shape;
if (!r)
return t;
if (typeof r == "string")
try {
r = JSON.parse(r);
} catch {
return t;
}
if (!Array.isArray(r) || r.length < 2)
return t;
const l = r.filter(
(n) => Array.isArray(n) && n.length >= 2 && Number.isFinite(n[0]) && Number.isFinite(n[1])
);
return l.length < 2 || t.push({
...s,
shape: l
}), t;
}, []);
return {
...o,
roadSegmentList: e
};
}
showRoadFlow(o) {
var t;
const e = this._normalizeParams(o);
if (this.lastParams = e, this.resolutionListenerKey || (this.resolutionListenerKey = this.map.getView().on("change:resolution", () => {
this.lastParams && (this.roadSource.clear(), this._renderSegments(this.lastParams));
})), this.roadSource.clear(), this._renderSegments(e), (t = e.options) != null && t.needZoom) {
const s = e.roadSegmentList.flatMap(
(r) => r.shape
);
if (s.length >= 2) {
const r = h.bbox(
h.lineString(s)
);
this.map.getView().fit(r, {
padding: [10, 10, 10, 10],
duration: this.hasGpu ? 500 : 0
});
}
}
}
_renderSegments(o) {
const e = o.roadSegmentList;
if (!e.length)
return;
const t = e.map((i) => i.flow), s = Math.min(...t), l = Math.max(...t) - s || 1, n = this.map.getView().getResolution() ?? 1;
for (const i of e) {
const m = 2 + (i.flow - s) / l * 8, u = m * n * 3, c = F(i.shape, u), f = new d({
geometry: new p(c),
roadId: i.roadId,
flow: i.flow,
avgSpeed: i.avgSpeed,
type: "roadFlowSegment"
});
f.setStyle(
new L({
stroke: new w({
color: P(i.avgSpeed),
width: m,
lineCap: "square"
})
})
), this.roadSource.addFeature(f);
}
}
clearRoadFlow() {
this.lastParams = null, this.resolutionListenerKey && (y(this.resolutionListenerKey), this.resolutionListenerKey = null), this.roadSource.clear();
}
destroy() {
this.clearRoadFlow(), this.map.removeLayer(this.roadLayer), this.roadLayer.dispose();
}
}
export {
z as default
};