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' drawingMode: null, // 'point', 'line', 'polygon', 'rectangle', 'circle'
drawingHandler: null, drawingHandler: null,
tempEntity: null, tempEntity: null, //
tempPreviewEntity: null, //
drawingPoints: [], drawingPoints: [],
drawingStartPoint: null, drawingStartPoint: null,
isDrawing: false, isDrawing: false,
@ -462,20 +463,26 @@ export default {
stopDrawing() { stopDrawing() {
if (this.drawingHandler) { if (this.drawingHandler) {
this.drawingHandler.destroy() this.drawingHandler.destroy();
this.drawingHandler = null this.drawingHandler = null;
} }
if (this.tempEntity) { if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity) this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null this.tempEntity = null;
}
//
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
} }
this.drawingPoints = [] this.drawingPoints = [];
this.drawingStartPoint = null this.drawingStartPoint = null;
this.isDrawing = false this.isDrawing = false;
this.viewer.scene.canvas.style.cursor = 'default' this.viewer.scene.canvas.style.cursor = 'default';
}, },
// ******************************************************************** // ********************************************************************
// //
@ -490,18 +497,66 @@ export default {
}, },
// 线 // 线
// 线
startLineDrawing() { 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) => { this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position) const position = this.getClickPosition(click.position);
if (position) { if (position) {
this.drawingPoints.push(position) this.drawingPoints.push(position);
// 线 // === 线 ===
if (this.tempEntity) { if (this.drawingPoints.length === 1) {
this.viewer.entities.remove(this.tempEntity) activeCursorPosition = position; //
}
// 线
if (this.drawingPoints.length > 1) { 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({ this.tempEntity = this.viewer.entities.add({
polyline: { polyline: {
positions: this.drawingPoints, positions: this.drawingPoints,
@ -509,95 +564,137 @@ export default {
material: Cesium.Color.fromCssColorString(this.defaultStyles.line.color), material: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
clampToGround: true clampToGround: true
} }
}) });
} }
} }
}, Cesium.ScreenSpaceEventType.LEFT_CLICK) }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// // 3.
this.drawingHandler.setInputAction(() => { this.drawingHandler.setInputAction(() => {
// 线
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
}
if (this.drawingPoints.length > 1) { if (this.drawingPoints.length > 1) {
this.finishLineDrawing() this.finishLineDrawing();
} else { } else {
this.cancelDrawing() this.cancelDrawing();
} }
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
//
activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}, },
finishLineDrawing() { finishLineDrawing() {
const positions = [...this.drawingPoints] // 线线
const entity = this.addLineEntity(positions) if (this.drawingPoints.length > 1) {
// 线
// if (this.tempPreviewEntity) {
const length = this.calculateLineLength(positions) this.viewer.entities.remove(this.tempPreviewEntity);
this.measurementResult = { this.tempPreviewEntity = null;
distance: length, }
type: 'line'
// 线
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() { 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) => { this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position) const position = this.getClickPosition(click.position);
if (position) { if (position) {
this.drawingPoints.push(position) this.drawingPoints.push(position);
// // === ===
if (this.tempEntity) { if (this.drawingPoints.length === 1) {
this.viewer.entities.remove(this.tempEntity) activeCursorPosition = position; //
}
if (this.drawingPoints.length > 2) {
//
const polygonPoints = [...this.drawingPoints, this.drawingPoints[0]]
this.tempEntity = this.viewer.entities.add({ this.tempEntity = this.viewer.entities.add({
// --- ---
polygon: { polygon: {
hierarchy: new Cesium.PolygonHierarchy(polygonPoints), // hierarchy 使 CallbackProperty
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color) hierarchy: new Cesium.CallbackProperty(() => {
.withAlpha(this.defaultStyles.polygon.opacity), // +
outline: true, if (activeCursorPosition) {
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color), return new Cesium.PolygonHierarchy([...this.drawingPoints, activeCursorPosition]);
outlineWidth: this.defaultStyles.polygon.width }
} return new Cesium.PolygonHierarchy(this.drawingPoints);
}) }, false),
} else if (this.drawingPoints.length === 2) { // 使便
// 线 material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
this.tempEntity = this.viewer.entities.add({ //
perPositionHeight: false
},
// --- 线 ---
polyline: { polyline: {
positions: this.drawingPoints, // positions 使 CallbackProperty 线
width: this.defaultStyles.polygon.width, positions: new Cesium.CallbackProperty(() => {
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color), 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 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);
// // 4.
this.drawingHandler.setInputAction((click) => {
if (this.drawingPoints.length > 2) {
this.finishPolygonDrawing()
}
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
//
this.drawingHandler.setInputAction(() => { this.drawingHandler.setInputAction(() => {
this.cancelDrawing() if (this.drawingPoints.length >= 3) {
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK) this.finishPolygonDrawing(); //
} else {
this.cancelDrawing(); //
}
//
activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}, },
finishPolygonDrawing() { finishPolygonDrawing() {
@ -615,156 +712,255 @@ export default {
return entity return entity
}, },
// //
startRectangleDrawing() { startRectangleDrawing() {
let startPosition = null this.drawingPoints = []; //
let activeCursorPosition = null; //
let startPoint = null; //
this.drawingHandler.setInputAction((click) => { // 1.
startPosition = this.getClickPosition(click.position) if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
if (startPosition) { this.tempEntity = null;
this.drawingStartPoint = startPosition
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN)
// 2.
this.drawingHandler.setInputAction((movement) => { this.drawingHandler.setInputAction((movement) => {
if (startPosition) { const newPosition = this.getClickPosition(movement.endPosition);
const endPosition = this.getClickPosition(movement.endPosition) if (newPosition) {
if (endPosition) { activeCursorPosition = newPosition;
// }
if (this.tempEntity) { }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.viewer.entities.remove(this.tempEntity)
}
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({ this.tempEntity = this.viewer.entities.add({
rectangle: { rectangle: {
coordinates: rectangle, // 使 CallbackProperty
material: Cesium.Color.fromCssColorString(this.defaultStyles.rectangle.color) coordinates: new Cesium.CallbackProperty(() => {
.withAlpha(this.defaultStyles.rectangle.opacity), 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, outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.rectangle.color), outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: this.defaultStyles.rectangle.width 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(() => { this.drawingHandler.setInputAction(() => {
if (startPosition && this.tempEntity) { this.cancelDrawing();
this.finishRectangleDrawing(startPosition) }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
startPosition = null
}
}, Cesium.ScreenSpaceEventType.LEFT_UP)
//
this.drawingHandler.setInputAction(() => {
this.cancelDrawing()
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}, },
finishRectangleDrawing(startPosition) { finishRectangleDrawing() {
if (!this.tempEntity) return // 1.
const rect = Cesium.Rectangle.fromCartesianArray(this.drawingPoints);
const rectangleCoords = this.tempEntity.rectangle.coordinates.getValue()
const entity = this.addRectangleEntity(rectangleCoords) // 2.
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
// // 3.
const area = this.calculateRectangleArea(rectangleCoords) 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 = { this.measurementResult = {
area: area, area: area,
type: 'rectangle' type: 'rectangle'
} };
this.stopDrawing() // 6.
return entity 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() { startCircleDrawing() {
let centerPosition = null this.drawingPoints = []; //
let radius = 0 let activeCursorPosition = null; //
let centerPoint = null; //
this.drawingHandler.setInputAction((click) => { // 1.
centerPosition = this.getClickPosition(click.position) if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
if (centerPosition) { this.tempEntity = null;
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)
// 2.
this.drawingHandler.setInputAction((movement) => { this.drawingHandler.setInputAction((movement) => {
if (centerPosition) { const newPosition = this.getClickPosition(movement.endPosition);
const currentPosition = this.getClickPosition(movement.endPosition) if (newPosition) {
if (currentPosition) { activeCursorPosition = newPosition;
radius = Cesium.Cartesian3.distance(centerPosition, currentPosition) }
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// // 3.
if (this.tempEntity) { this.drawingHandler.setInputAction((click) => {
this.viewer.entities.remove(this.tempEntity) 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({ this.tempEntity = this.viewer.entities.add({
polygon: { position: centerPoint, //
hierarchy: new Cesium.PolygonHierarchy(positions), ellipse: {
material: Cesium.Color.fromCssColorString(this.defaultStyles.circle.color) // 使 CallbackProperty
.withAlpha(this.defaultStyles.circle.opacity), 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, outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.circle.color), outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: this.defaultStyles.circle.width 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(() => { this.drawingHandler.setInputAction(() => {
if (centerPosition && radius > 0) { this.cancelDrawing();
this.finishCircleDrawing(centerPosition, radius) }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
centerPosition = null
radius = 0
}
}, Cesium.ScreenSpaceEventType.LEFT_UP)
//
// this.drawingHandler.setInputAction(() => {
// this.cancelDrawing()
// }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}, },
finishCircleDrawing(center, radius) { finishCircleDrawing(edgePosition) {
const positions = this.calculateCircle(center, radius, 64) const centerPoint = this.drawingPoints[0];
const entity = this.addCircleEntity(center, radius, positions)
// 1.
const radius = Cesium.Cartesian3.distance(centerPoint, edgePosition);
// 2.
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
// // 3.
const area = Math.PI * radius * radius 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 = { this.measurementResult = {
radius: radius, //
area: area, area: area,
radius: radius,
type: 'circle' type: 'circle'
} };
this.stopDrawing() // 6.
return entity this.stopDrawing();
},
cancelDrawing() {
this.stopDrawing()
this.drawingMode = null
}, },
// ================== ================== // ================== ==================
@ -1522,3 +1718,5 @@ export default {
} }
} }
</style> </style>

Loading…
Cancel
Save