Browse Source

优化点线面绘制,优化测距测面积

lbj
ctw 3 months ago
parent
commit
a7c52546d2
  1. 570
      ruoyi-ui/src/views/cesiumMap/index.vue

570
ruoyi-ui/src/views/cesiumMap/index.vue

@ -212,7 +212,8 @@ export default {
//
drawingMode: null, // 'point', 'line', 'polygon', 'rectangle', 'circle'
drawingHandler: null,
tempEntity: null,
tempEntity: null, //
tempPreviewEntity: null, //
drawingPoints: [],
drawingStartPoint: null,
isDrawing: false,
@ -462,20 +463,26 @@ export default {
stopDrawing() {
if (this.drawingHandler) {
this.drawingHandler.destroy()
this.drawingHandler = null
this.drawingHandler.destroy();
this.drawingHandler = null;
}
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
this.tempEntity = null
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
//
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
}
this.drawingPoints = []
this.drawingStartPoint = null
this.isDrawing = false
this.drawingPoints = [];
this.drawingStartPoint = null;
this.isDrawing = false;
this.viewer.scene.canvas.style.cursor = 'default'
this.viewer.scene.canvas.style.cursor = 'default';
},
// ********************************************************************
//
@ -490,18 +497,66 @@ export default {
},
// 线
// 线
startLineDrawing() {
this.drawingPoints = [];
//
let activeCursorPosition = null;
//
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
if (this.tempPreviewEntity) this.viewer.entities.remove(this.tempPreviewEntity);
this.tempEntity = null;
this.tempPreviewEntity = null;
// 1.
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// 2.
this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position)
const position = this.getClickPosition(click.position);
if (position) {
this.drawingPoints.push(position)
// 线
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
}
if (this.drawingPoints.length > 1) {
this.drawingPoints.push(position);
// === 线 ===
if (this.drawingPoints.length === 1) {
activeCursorPosition = position; //
// 线
this.tempPreviewEntity = this.viewer.entities.add({
polyline: {
// 使 CallbackProperty
positions: new Cesium.CallbackProperty(() => {
//
if (this.drawingPoints.length > 0 && activeCursorPosition) {
//
const lastPoint = this.drawingPoints[this.drawingPoints.length - 1];
// [, ]
return [lastPoint, activeCursorPosition];
}
return [];
}, false),
width: this.defaultStyles.line.width,
// 线
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
dashLength: 16
}),
clampToGround: true //
}
});
}
// === /线 ===
else {
// 线线
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
}
this.tempEntity = this.viewer.entities.add({
polyline: {
positions: this.drawingPoints,
@ -509,95 +564,137 @@ export default {
material: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
clampToGround: true
}
})
});
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
//
// 3.
this.drawingHandler.setInputAction(() => {
// 线
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
}
if (this.drawingPoints.length > 1) {
this.finishLineDrawing()
this.finishLineDrawing();
} else {
this.cancelDrawing()
this.cancelDrawing();
}
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
//
activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
finishLineDrawing() {
const positions = [...this.drawingPoints]
const entity = this.addLineEntity(positions)
//
const length = this.calculateLineLength(positions)
this.measurementResult = {
distance: length,
type: 'line'
// 线线
if (this.drawingPoints.length > 1) {
// 线
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
}
// 线
const entity = this.addLineEntity([...this.drawingPoints]);
//
const length = this.calculateLineLength([...this.drawingPoints]);
this.measurementResult = {
distance: length,
type: 'line'
};
this.stopDrawing();
return entity;
} else {
this.cancelDrawing();
return null;
}
this.stopDrawing()
return entity
},
//
startPolygonDrawing() {
this.drawingPoints = [];
let activeCursorPosition = null; //
// 1.
//
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
if (this.tempPreviewEntity) this.viewer.entities.remove(this.tempPreviewEntity);
this.tempEntity = null;
this.tempPreviewEntity = null;
// 2.
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// 3.
this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position)
const position = this.getClickPosition(click.position);
if (position) {
this.drawingPoints.push(position)
this.drawingPoints.push(position);
//
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
}
// === ===
if (this.drawingPoints.length === 1) {
activeCursorPosition = position; //
if (this.drawingPoints.length > 2) {
//
const polygonPoints = [...this.drawingPoints, this.drawingPoints[0]]
this.tempEntity = this.viewer.entities.add({
// --- ---
polygon: {
hierarchy: new Cesium.PolygonHierarchy(polygonPoints),
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color)
.withAlpha(this.defaultStyles.polygon.opacity),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color),
outlineWidth: this.defaultStyles.polygon.width
}
})
} else if (this.drawingPoints.length === 2) {
// 线
this.tempEntity = this.viewer.entities.add({
// hierarchy 使 CallbackProperty
hierarchy: new Cesium.CallbackProperty(() => {
// +
if (activeCursorPosition) {
return new Cesium.PolygonHierarchy([...this.drawingPoints, activeCursorPosition]);
}
return new Cesium.PolygonHierarchy(this.drawingPoints);
}, false),
// 使便
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
//
perPositionHeight: false
},
// --- 线 ---
polyline: {
positions: this.drawingPoints,
width: this.defaultStyles.polygon.width,
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color),
// positions 使 CallbackProperty 线
positions: new Cesium.CallbackProperty(() => {
if (activeCursorPosition) {
// [, , ]
return [...this.drawingPoints, activeCursorPosition, this.drawingPoints[0]];
}
return this.drawingPoints;
}, false),
width: this.defaultStyles.line.width,
// 使线
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
dashLength: 16
}),
clampToGround: true
}
})
} else if (this.drawingPoints.length === 1) {
//
this.tempEntity = this.viewer.entities.add({
position: this.drawingPoints[0],
point: {
pixelSize: 8,
color: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color)
}
})
});
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
//
this.drawingHandler.setInputAction((click) => {
if (this.drawingPoints.length > 2) {
this.finishPolygonDrawing()
}
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
//
// 4.
this.drawingHandler.setInputAction(() => {
this.cancelDrawing()
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
if (this.drawingPoints.length >= 3) {
this.finishPolygonDrawing(); //
} else {
this.cancelDrawing(); //
}
//
activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
finishPolygonDrawing() {
@ -615,156 +712,255 @@ export default {
return entity
},
//
//
startRectangleDrawing() {
let startPosition = null
this.drawingPoints = []; //
let activeCursorPosition = null; //
let startPoint = null; //
this.drawingHandler.setInputAction((click) => {
startPosition = this.getClickPosition(click.position)
if (startPosition) {
this.drawingStartPoint = startPosition
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN)
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
// 2.
this.drawingHandler.setInputAction((movement) => {
if (startPosition) {
const endPosition = this.getClickPosition(movement.endPosition)
if (endPosition) {
//
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
}
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
const rectangle = this.calculateRectangle(startPosition, endPosition)
// 3.
this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (!startPoint) {
startPoint = position;
this.drawingPoints.push(startPoint);
activeCursorPosition = startPoint; //
//
this.tempEntity = this.viewer.entities.add({
rectangle: {
coordinates: rectangle,
material: Cesium.Color.fromCssColorString(this.defaultStyles.rectangle.color)
.withAlpha(this.defaultStyles.rectangle.opacity),
// 使 CallbackProperty
coordinates: new Cesium.CallbackProperty(() => {
if (startPoint && activeCursorPosition) {
// 使 Cesium
return Cesium.Rectangle.fromCartesianArray([startPoint, activeCursorPosition]);
}
return Cesium.Rectangle.fromDegrees(0, 0, 0, 0);
}, false),
// +
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.rectangle.color),
outlineWidth: this.defaultStyles.rectangle.width
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2,
clampToGround: true //
}
})
});
}
// --- B ---
else {
this.drawingPoints.push(position);
//
activeCursorPosition = null;
//
this.finishRectangleDrawing();
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// 4.
this.drawingHandler.setInputAction(() => {
if (startPosition && this.tempEntity) {
this.finishRectangleDrawing(startPosition)
startPosition = null
}
}, Cesium.ScreenSpaceEventType.LEFT_UP)
//
this.drawingHandler.setInputAction(() => {
this.cancelDrawing()
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
this.cancelDrawing();
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
finishRectangleDrawing(startPosition) {
if (!this.tempEntity) return
const rectangleCoords = this.tempEntity.rectangle.coordinates.getValue()
const entity = this.addRectangleEntity(rectangleCoords)
finishRectangleDrawing() {
// 1.
const rect = Cesium.Rectangle.fromCartesianArray(this.drawingPoints);
// 2.
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
//
const area = this.calculateRectangleArea(rectangleCoords)
// 3.
const finalEntity = this.viewer.entities.add({
id: 'rectangle-' + new Date().getTime(), // ID
rectangle: {
coordinates: rect,
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2,
clampToGround: true
}
});
// 4.
this.allEntities.push(finalEntity);
// 5.
const area = this.calculateRectangleArea(rect);
this.measurementResult = {
area: area,
type: 'rectangle'
}
};
this.stopDrawing()
return entity
// 6.
this.stopDrawing();
},
//
calculateRectangleArea(rectangle) {
//
const ellipsoid = this.viewer.scene.globe.ellipsoid;
// 使 Cesium
// const geometry = new Cesium.RectangleGeometry({ rectangle: rectangle });
// const geometryInstance = Cesium.RectangleGeometry.createGeometry(geometry);
// ...
// 使
//
const west = rectangle.west;
const south = rectangle.south;
const east = rectangle.east;
const north = rectangle.north;
//
const centerLat = (south + north) / 2;
// 西使
// 6378137
const R = 6378137;
const width = (east - west) * R * Math.cos(centerLat);
//
const height = (north - south) * R;
// = *
const area = Math.abs(width * height);
return area;
},
//
startCircleDrawing() {
let centerPosition = null
let radius = 0
this.drawingPoints = []; //
let activeCursorPosition = null; //
let centerPoint = null; //
this.drawingHandler.setInputAction((click) => {
centerPosition = this.getClickPosition(click.position)
if (centerPosition) {
this.drawingStartPoint = centerPosition
//
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
}
this.tempEntity = this.viewer.entities.add({
position: centerPosition,
point: {
pixelSize: 8,
color: Cesium.Color.fromCssColorString(this.defaultStyles.circle.color)
}
})
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN)
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
// 2.
this.drawingHandler.setInputAction((movement) => {
if (centerPosition) {
const currentPosition = this.getClickPosition(movement.endPosition)
if (currentPosition) {
radius = Cesium.Cartesian3.distance(centerPosition, currentPosition)
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
//
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
}
// 3.
this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (!centerPoint) {
centerPoint = position;
this.drawingPoints.push(centerPoint);
activeCursorPosition = centerPoint;
//
const positions = this.calculateCircle(centerPosition, radius, 64)
//
this.tempEntity = this.viewer.entities.add({
polygon: {
hierarchy: new Cesium.PolygonHierarchy(positions),
material: Cesium.Color.fromCssColorString(this.defaultStyles.circle.color)
.withAlpha(this.defaultStyles.circle.opacity),
position: centerPoint, //
ellipse: {
// 使 CallbackProperty
semiMajorAxis: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
return Cesium.Cartesian3.distance(centerPoint, activeCursorPosition);
}
return 0;
}, false),
semiMinorAxis: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
return Cesium.Cartesian3.distance(centerPoint, activeCursorPosition);
}
return 0;
}, false),
//
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.circle.color),
outlineWidth: this.defaultStyles.circle.width
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2,
// height: 0, // 使 heightReference
}
})
});
}
// --- B/ ---
else {
// 便
this.drawingPoints.push(position);
activeCursorPosition = null; //
//
this.finishCircleDrawing(position);
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// 4.
this.drawingHandler.setInputAction(() => {
if (centerPosition && radius > 0) {
this.finishCircleDrawing(centerPosition, radius)
centerPosition = null
radius = 0
}
}, Cesium.ScreenSpaceEventType.LEFT_UP)
//
// this.drawingHandler.setInputAction(() => {
// this.cancelDrawing()
// }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
this.cancelDrawing();
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
finishCircleDrawing(center, radius) {
const positions = this.calculateCircle(center, radius, 64)
const entity = this.addCircleEntity(center, radius, positions)
finishCircleDrawing(edgePosition) {
const centerPoint = this.drawingPoints[0];
// 1.
const radius = Cesium.Cartesian3.distance(centerPoint, edgePosition);
// 2.
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
//
const area = Math.PI * radius * radius
// 3.
const finalEntity = this.viewer.entities.add({
id: 'circle-' + new Date().getTime(),
position: centerPoint,
ellipse: {
semiMajorAxis: radius,
semiMinorAxis: radius,
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2
}
});
// 4.
this.allEntities.push(finalEntity);
// 5. (π * r²)
//
const area = Math.PI * Math.pow(radius, 2);
this.measurementResult = {
radius: radius, //
area: area,
radius: radius,
type: 'circle'
}
};
this.stopDrawing()
return entity
},
cancelDrawing() {
this.stopDrawing()
this.drawingMode = null
// 6.
this.stopDrawing();
},
// ================== ==================
@ -1522,3 +1718,5 @@ export default {
}
}
</style>

Loading…
Cancel
Save