gisviewer-vue3-arcgis
Version:
在应用中引入私服的包, 项目根目录下 新建文件 .npmrc 在 .npmrc 中对包源进行配置:
387 lines (386 loc) • 11.6 kB
JavaScript
import * as y from "@turf/turf";
import h from "ol/Feature";
import v from "ol/format/GeoJSON";
import { Polygon as c, LineString as u, MultiLineString as S, Point as d } from "ol/geom";
import p from "ol/layer/Vector";
import m from "ol/source/Vector";
import b from "ol/style/Fill";
import g from "ol/style/Style";
import { getLineStyle as F } from "../style/line-style.mjs";
import { getPointStyle as w } from "../style/point-style.mjs";
import { getPolygonStyle as _ } from "../style/polygon-style.mjs";
class T {
constructor(e, s) {
this.hiddenStyle = new g({}), this.flashingMap = /* @__PURE__ */ new Map(), this.defaultPointSymbol = {
type: "simple-marker",
style: "circle",
color: "yellow",
size: 10
}, this.map = e, this.vectorSource = new m(), this.overlayLayer = new p({
source: this.vectorSource,
properties: {
id: (s == null ? void 0 : s.id) ?? "overlay-layer"
}
}), this.map.addLayer(this.overlayLayer);
}
/**
* 添加覆盖物
* @param params
*/
addOverlays(e) {
let s = 0, i = 0;
e.defaultVisible = e.defaultVisible ?? !0, e.zIndex !== void 0 && this.overlayLayer.setZIndex(e.zIndex);
for (const t of e.overlays) {
t.properties ? (t.properties.type || (t.properties.type = e.type), t.properties.id || (t.properties.id = t.id)) : t.properties = { type: e.type, id: t.id };
const r = t.symbol || e.defaultSymbol || this.defaultPointSymbol, o = t.visible == null ? e.defaultVisible : t.visible;
this.addOverlay(
t,
r,
t.properties,
o
).message === "add" ? s++ : i++;
}
return e.needZoom === !0 && this.map.getView().fit(this.vectorSource.getExtent(), {
padding: [50, 50, 50, 50]
}), { status: 0, message: `新增${s}, 更新${i}` };
}
/**
* 添加或更新单个覆盖物
* @param overlay 覆盖物对象
* @param symbol 符号
* @param properties 属性
* @param visible 是否可见
*/
addOverlay(e, s, i, t) {
let r, o = !0;
e.id && (r = this.vectorSource.getFeatures().find((n) => n.get("id") === e.id));
const l = this.createGeometry(e.geometry);
if (!l)
return { status: -1, message: "不支持的几何类型" };
r ? (r.setGeometry(l), o = !1) : (r = new h({
geometry: l
}), this.vectorSource.addFeature(r), o = !0);
const a = this.getStyle(e.geometry.type, s, i);
return a && r.setStyle(a), r.setProperties(i), t || r.setStyle(this.hiddenStyle), r.set("_visible", t), r.set("_originalStyle", a), { status: 0, message: o ? "add" : "update" };
}
/**
* 创建几何对象
* @param geometryConfig 几何配置
*/
createGeometry(e) {
var i;
switch ((i = e.type) == null ? void 0 : i.toLowerCase()) {
case "point":
return new d([e.x, e.y]);
case "line":
case "polyline":
if (e.paths && e.paths.length > 0) {
const t = e.paths[0];
return Array.isArray(t) && Array.isArray(t[0]) ? e.paths.length === 1 ? new u(e.paths[0]) : new S(e.paths) : new u(e.paths);
}
return null;
case "polygon":
return e.rings && e.rings.length > 0 ? new c(e.rings) : null;
default:
return null;
}
}
/**
* 获取样式
* @param geometryType 几何类型
* @param symbol 符号配置
* @param attributes 属性
*/
getStyle(e, s, i) {
switch (e == null ? void 0 : e.toLowerCase()) {
case "point":
return w(s, i);
case "line":
case "polyline":
return F(s, i);
case "polygon":
return _(s, i);
default:
return null;
}
}
/**
* 按type移除覆盖物
* @param types
*/
removeOverlaysByType(e) {
const i = this.vectorSource.getFeatures().filter((t) => {
const r = t.get("type");
return Array.isArray(e) ? e.includes(r) : r === e;
});
return i.forEach((t) => {
this.stopFlash({ id: t.get("id") }), this.vectorSource.removeFeature(t);
}), { status: 0, message: `删除${i.length}` };
}
/**
* 按id移除覆盖物
* @param ids
*/
removeOverlaysById(e) {
const i = this.vectorSource.getFeatures().filter((t) => {
const r = t.get("id");
return Array.isArray(e) ? e.includes(r) : r === e;
});
return i.forEach((t) => {
this.stopFlash({ id: t.get("id") }), this.vectorSource.removeFeature(t);
}), { status: 0, message: `删除${i.length}` };
}
/**
* 按id显示或隐藏覆盖物
* @param ids
* @param isShow
*/
isShowOverlaysByIds(e, s) {
this.vectorSource.getFeatures().forEach((i) => {
const t = i.get("id");
if (e.includes(t)) {
if (s) {
const r = i.get("_originalStyle");
r && i.setStyle(r);
} else
i.setStyle(this.hiddenStyle);
i.set("_visible", s);
} else {
if (s)
i.setStyle(this.hiddenStyle);
else {
const r = i.get("_originalStyle");
r && i.setStyle(r);
}
i.set("_visible", !s);
}
});
}
/**
* 显示全部覆盖物
*/
showAllOverlays() {
this.vectorSource.getFeatures().forEach((e) => {
const s = e.get("_originalStyle");
s && e.setStyle(s), e.set("_visible", !0);
});
}
/**
* 删除全部覆盖物
* @returns
*/
removeAllOverlays() {
const e = this.vectorSource.getFeatures().length;
return this.stopFlash(), this.vectorSource.clear(), { status: 0, message: `删除${e}` };
}
/**
* 更新覆盖物符号
* @param ids 覆盖物id数组
* @param symbol 新的符号配置
*/
updateOverlaysSymbol(e, s) {
let i = 0;
return this.vectorSource.getFeatures().forEach((t) => {
const r = t.get("id");
if (e.includes(r)) {
const o = t.getGeometry();
if (o) {
const l = this.getGeometryType(o), a = t.getProperties(), n = this.getStyle(l, s, a);
n && (t.setStyle(n), t.set("_originalStyle", n), i++);
}
}
}), { status: 0, message: `更新${i}个覆盖物符号` };
}
/**
* 按type更新覆盖物符号
* @param types 类型数组
* @param symbol 新的符号配置
*/
updateOverlaysSymbolByType(e, s) {
let i = 0;
return this.vectorSource.getFeatures().forEach((t) => {
const r = t.get("type");
if (e.includes(r)) {
const o = t.getGeometry();
if (o) {
const l = this.getGeometryType(o), a = t.getProperties(), n = this.getStyle(l, s, a);
n && (t.setStyle(n), t.set("_originalStyle", n), i++);
}
}
}), { status: 0, message: `更新${i}个覆盖物符号` };
}
/**
* 获取几何类型字符串
* @param geometry
*/
getGeometryType(e) {
return e instanceof d ? "point" : e instanceof u ? "polyline" : e instanceof c ? "polygon" : "";
}
/**
* 按id获取覆盖物Feature
* @param id
*/
getOverlayById(e) {
return this.vectorSource.getFeatures().find((s) => s.get("id") === e);
}
/**
* 按type获取覆盖物Feature数组
* @param type
*/
getOverlaysByType(e) {
return this.vectorSource.getFeatures().filter((s) => s.get("type") === e);
}
/**
* 隐藏图层
*/
hideLayer() {
this.overlayLayer.setVisible(!1);
}
/**
* 显示图层
*/
showLayer() {
this.overlayLayer.setVisible(!0);
}
/**
* 删除指定的Feature
* @param features
*/
// public removeFeatures(features: Feature<Geometry> | Feature<Geometry>[]) {
// if (!Array.isArray(features)) features = [features];
// features.forEach((feature) => {
// this.vectorSource.removeFeature(feature);
// });
// }
/**
* 查找屏幕坐标处的覆盖物
* @param pixel 屏幕像素坐标 [x, y]
*/
findFeaturesAtPixel(e) {
const s = [];
return this.map.forEachFeatureAtPixel(
e,
(i, t) => {
t === this.overlayLayer && i instanceof h && s.push(i);
},
{
layerFilter: (i) => i === this.overlayLayer
}
), s;
}
/**
* 销毁
*/
destroy() {
this.vectorSource.clear(), this.map.removeLayer(this.overlayLayer), this.overlayLayer.dispose();
}
addMask(e) {
this.removeMask();
const s = e.maskColor || [100, 100, 100, 0.8], i = e.maskSize || 200;
let t = y.polygon([e.geometry.rings[0]]);
const r = y.buffer(t, i, {
units: "kilometers"
});
let o = y.difference(r, t);
if (e.geometry.rings.length > 1)
for (let l = 1; l < e.geometry.rings.length; l++)
t = y.polygon([e.geometry.rings[l]]), o && (o = y.difference(o, t));
if (o)
try {
let l;
o.geometry.type === "MultiPolygon" ? l = y.multiPolygon(
o.geometry.coordinates
) : l = y.polygon(
o.geometry.coordinates
);
const a = new v().readFeature(l);
if (a.setProperties({ type: "mask" }), a.setStyle(
new g({
fill: new b({
color: s
})
})
), this.vectorSource.addFeature(a), e.needZoom === !0) {
const n = new c(e.geometry.rings);
this.map.getView().fit(n.getExtent(), {
padding: [100, 100, 100, 100],
maxZoom: 18
});
}
} catch {
console.error("蒙板几何处理失败", o.geometry.coordinates);
}
}
removeMask() {
this.removeOverlaysByType("mask");
}
/**
* 按id定位覆盖物并闪烁
* @param params 参数对象
* @param params.id 覆盖物id
* @param params.duration 闪烁持续时间(秒),不传则无限期
* @param params.needZoom 是否需要缩放到覆盖物
*/
flashOverlay(e) {
const s = this.getOverlayById(e.id);
if (!s || !s.getGeometry())
return;
e.needZoom === !0 && this.map.getView().fit(s.getGeometry().getExtent(), {
padding: [100, 100, 100, 100],
maxZoom: 18
});
const i = e.duration == null, t = i ? 0 : Math.max(e.duration, 0.1) * 1e3, r = 400, o = s.get("_visible") !== !1, l = s.get("_originalStyle");
(() => {
s.setStyle(l), s.set("_visible", !0);
const n = setInterval(() => {
s.get("_visible") !== !1 ? (s.setStyle(this.hiddenStyle), s.set("_visible", !1)) : (s.setStyle(l), s.set("_visible", !0));
}, r), f = i ? void 0 : setTimeout(() => {
this.stopFlash(e);
}, t);
this.flashingMap.set(e.id, {
timer: n,
timeout: f,
originalVisible: o
});
})();
}
/**
* 停止指定覆盖物闪烁
* @param params 参数对象
* @param params.id 覆盖物id
*/
stopFlash(e) {
const s = e == null ? void 0 : e.id;
if (s) {
const i = this.flashingMap.get(s);
if (!i)
return;
clearInterval(i.timer), i.timeout && clearTimeout(i.timeout);
const t = this.getOverlayById(s);
if (t) {
if (i.originalVisible) {
const r = t.get("_originalStyle");
t.setStyle(r);
} else
t.setStyle(this.hiddenStyle);
t.set("_visible", i.originalVisible);
}
this.flashingMap.delete(s);
} else
this.flashingMap.forEach((i, t) => {
clearInterval(i.timer), i.timeout && clearTimeout(i.timeout);
const r = this.getOverlayById(t);
if (r) {
if (i.originalVisible) {
const o = r.get("_originalStyle");
r.setStyle(o);
} else
r.setStyle(this.hiddenStyle);
r.set("_visible", i.originalVisible);
}
}), this.flashingMap.clear();
}
}
export {
T as default
};