import { dataService } from "./Service/DataService.js"; import { lineService } from "./Service/LineService.js"; import { roadService } from "./Service/RoadService.js"; import { curveRoadService } from "./Service/CurveRoadService.js"; import { pointService } from "./Service/PointService.js"; import { curveRoadPointService } from "./Service/CurveRoadPointService.js"; import { roadPointService } from "./Service/RoadPointService.js"; import { imageService } from "./Service/ImageService.js"; import VectorCategory from "./enum/VectorCategory.js"; import { coordinate } from "./Coordinate.js"; import Settings from "./Settings"; import { circleService } from "./Service/CircleService.js"; import { magnifierService } from "./Service/MagnifierService.js"; import { textService } from "./Service/TextService.js"; import { svgService } from "./Service/SVGService.js"; import { mathUtil } from "./Util/MathUtil.js"; import { historyService } from "./Service/HistoryService.js"; import { uiService } from "./Service/UIService"; import { crossPointService } from "./Service/CrossPointService.js"; import Road from "./Geometry/Road.js"; import CurveRoad from "./Geometry/CurveRoad.js"; import { edgeService } from "./Service/EdgeService.js"; import { curvePointService } from "./Service/CurvePointService.js"; import { curveEdgeService } from "./Service/CurveEdgeService.js"; import Constant from "./Constant.js"; export default class Load { constructor(layer) { this.layer = layer; this.version = "v1.0"; this.vectorsJson = null; this.newVectorId = null; } async load(dataLocal, data3d) { this.layer.initLocation(); coordinate.init(this.layer.canvas); if (dataLocal) { if (dataLocal.Settings) { for (let key in dataLocal.Settings) { Settings[key] = dataLocal.Settings[key]; } } if (dataLocal.backgroundImg) { let bgImg = imageService.createBackgroundImg( dataLocal.backgroundImg.src, dataLocal.backgroundImg.vectorId ); bgImg.setCenter(dataLocal.backgroundImg.center); // bgImg.setDisplay(dataLocal.backgroundImg.display); bgImg.setDisplay(true); //背景图始终显示 bgImg.setAngle(dataLocal.backgroundImg.angle); try { if (dataLocal.backgroundImg.src) { await bgImg.setImageData(); bgImg.setBounding(); } } catch (e) {} } if (dataLocal.circles) { for (let key in dataLocal.circles) { let circle = circleService.create( dataLocal.circles[key].center, dataLocal.circles[key].radius || dataLocal.circles[key].radiusX, key ); circle.setRadiusX(dataLocal.circles[key].radiusX); circle.setRadiusY(dataLocal.circles[key].radiusY); circle.setPoints(dataLocal.circles[key].points); circle.setColor(dataLocal.circles[key].color); circle.setDisplay(dataLocal.circles[key].display); } } if (dataLocal.magnifiers) { for (let key in dataLocal.magnifiers) { let magnifier = magnifierService.create( dataLocal.magnifiers[key].position, key ); magnifier.setSrc(dataLocal.magnifiers[key].photoUrl); magnifier.setDisplay(dataLocal.magnifiers[key].display); magnifier.setPopPosition(null, dataLocal.magnifiers[key].popPosition); try { if (dataLocal.magnifiers[key].photoUrl) { await magnifier.setImageData(); } } catch (e) {} } } if (dataLocal.points) { for (let key in dataLocal.points) { let point = pointService.create(dataLocal.points[key], key); point.setCategory(dataLocal.points[key].category); point.setParent( JSON.parse(JSON.stringify(dataLocal.points[key].parent)) ); point.setDisplay(dataLocal.points[key].display); point.setLocationMode(dataLocal.points[key].locationMode); point.setLinkedBasePointId(dataLocal.points[key].linkedBasePointId); point.setColor(point.getColor()); point.linkedTextId = dataLocal.points[key].linkedTextId; } // let points = dataService.vectorData.points; let points = dataLocal.points; Settings.basePointIds = []; for (let key in points) { if (points[key].category == VectorCategory.Point.BasePoint) { Settings.basePointIds.push(points[key].vectorId); } } } if (dataLocal.svgs) { for (let key in dataLocal.svgs) { let svg = svgService.create( dataLocal.svgs[key].center, dataLocal.svgs[key].type, dataLocal.svgs[key].vectorId ); svg.setPoints(dataLocal.svgs[key].points); } } if (dataLocal.lines) { for (let key in dataLocal.lines) { let line = lineService.createByPointId( dataLocal.lines[key].startId, dataLocal.lines[key].endId, dataLocal.lines[key].category, key ); if (dataLocal.lines[key].style) { line.setStyle(dataLocal.lines[key].style); } if (dataLocal.lines[key].weight) { line.setWeight(dataLocal.lines[key].weight); } if (dataLocal.lines[key].color) { line.setColor(dataLocal.lines[key].color); } if (dataLocal.lines[key].value) { line.setValue(dataLocal.lines[key].value); } if (dataLocal.lines[key].locationMode) { line.setLocationMode(dataLocal.lines[key].locationMode); } if (dataLocal.lines[key].linkedFixPointId) { line.setLinkedFixPointId(dataLocal.lines[key].linkedFixPointId); } if (dataLocal.lines[key].linkedBasePointId) { line.setLinkedBasePointId(dataLocal.lines[key].linkedBasePointId); } line.setDisplay(dataLocal.lines[key].display); if (line.getCategory() == VectorCategory.Line.BaseLine) { uiService.setBaseLineId(key); } } } if (dataLocal.curvePoints) { for (let key in dataLocal.curvePoints) { let curvePointData = dataLocal.curvePoints[key]; let curvePoint = curvePointService.create( curvePointData, curvePointData.vectorId ); curvePoint.setIndex(curvePointData.index); curvePoint.setPointParent(curvePointData.parent); dataService.addCurvePoint(curvePoint); } } if (dataLocal.curvelines) { for (let key in dataLocal.curvelines) { let curveLineData = dataLocal.curvelines[key]; let curveLine = lineService.createCurveLineByPointIds( curveLineData.points, curveLineData.vectorId ); curveLine.setStyle(curveLineData.style); curveLine.setWeight(curveLineData.weight); dataService.addCurveLine(curveLine); } } if (dataLocal.roadPoints) { for (let key in dataLocal.roadPoints) { let roadPointData = dataLocal.roadPoints[key]; let roadPoint = roadPointService.create( dataLocal.roadPoints[key], dataLocal.roadPoints[key].vectorId ); roadPoint.parent = JSON.parse(JSON.stringify(roadPointData.parent)); } } if (dataLocal.roads) { for (let key in dataLocal.roads) { uiService.setWayType(dataLocal.roads[key].way); uiService.setSingleRoadDrivewayCount( dataLocal.roads[key].singleRoadDrivewayCount ); uiService.setRoadLeftDrivewayCount( dataLocal.roads[key].leftDrivewayCount ); uiService.setRoadRightDrivewayCount( dataLocal.roads[key].rightDrivewayCount ); let road = new Road( dataLocal.roads[key].startId, dataLocal.roads[key].endId, dataLocal.roads[key].vectorId ); dataService.addRoad(road); road.setWidth(dataLocal.roads[key].leftWidth, "left"); road.setWidth(dataLocal.roads[key].rightWidth, "right"); road.setWidth(dataLocal.roads[key].singleRoadWidth); road.setMidDivide(dataLocal.roads[key].midDivide); road.setLeftEdgeId(dataLocal.roads[key].leftEdgeId); road.setRightEdgeId(dataLocal.roads[key].rightEdgeId); road.setLeftLanes(dataLocal.roads[key].leftLanes); road.setRightLanes(dataLocal.roads[key].rightLanes); road.setSingleLanes(dataLocal.roads[key].singleLanes); } } if (dataLocal.roadEdges) { //当roadEdge有样式的时候需要设置 for (let edgeKey in dataLocal.roadEdges) { let edge = dataLocal.roadEdges[edgeKey]; let newEdge = edgeService.create( edge.start, edge.end, edge.vectorId, edge.parent ); if (edge.style) { newEdge.setStyle(edge.style); } if (edge.weight) { newEdge.setWeight(edge.weight); } newEdge.setLineWidth(edge.lineWidth); newEdge.setParent(edge.parent); } } if (dataLocal.curveRoadPoints) { for (let key in dataLocal.curveRoadPoints) { let curveRoadPoint = curveRoadPointService.create( dataLocal.curveRoadPoints[key], dataLocal.curveRoadPoints[key].vectorId ); curveRoadPoint.setIndex(dataLocal.curveRoadPoints[key].index); curveRoadPoint.setParent(dataLocal.curveRoadPoints[key].parent); } } if (dataLocal.curveRoadEdges) { for (let key in dataLocal.curveRoadEdges) { let curveRoadEdgeData = dataLocal.curveRoadEdges[key]; let curveRoadEdge = curveEdgeService.create( curveRoadEdgeData.start, curveRoadEdgeData.end, curveRoadEdgeData.vectorId, curveRoadEdgeData.parentId, curveRoadEdgeData.points ); curveRoadEdge.setStyle(curveRoadEdgeData.style); curveRoadEdge.setWeight(curveRoadEdgeData.weight); curveRoadEdge.setParent(curveRoadEdgeData.parent); curveEdgeService.setCurves(curveRoadEdge); } } if (dataLocal.curveRoads) { for (let key in dataLocal.curveRoads) { let curveRoadData = dataLocal.curveRoads[key]; let curveRoad = new CurveRoad( curveRoadData.startId, curveRoadData.endId, curveRoadData.vectorId ); dataService.addCurveRoad(curveRoad); curveRoad.leftEdgeId = curveRoadData.leftEdgeId; curveRoad.rightEdgeId = curveRoadData.rightEdgeId; curveRoad.points = []; for (let i = 0; i < curveRoadData.points.length; ++i) { curveRoad.points[i] = dataService.getCurveRoadPoint( curveRoadData.points[i].vectorId ); } curveRoad.curves = JSON.parse(JSON.stringify(curveRoadData.curves)); curveRoad.way = curveRoadData.way; curveRoad.singleCurveRoadWidth = curveRoadData.singleCurveRoadWidth; curveRoad.singleCurveRoadDrivewayCount = curveRoadData.singleCurveRoadDrivewayCount; curveRoad.singleLanesCurves = JSON.parse( JSON.stringify(curveRoadData.singleLanesCurves) ); curveRoad.singleLanesCurves = JSON.parse( JSON.stringify(curveRoadData.singleLanes) ); curveRoad.leftWidth = curveRoadData.leftWidth; curveRoad.rightWidth = curveRoadData.rightWidth; curveRoad.leftDrivewayCount = curveRoadData.leftDrivewayCount; curveRoad.rightDrivewayCount = curveRoadData.rightDrivewayCount; curveRoad.midDivide = JSON.parse( JSON.stringify(curveRoadData.midDivide) ); curveRoad.leftLanesCurves = JSON.parse( JSON.stringify(curveRoadData.leftLanesCurves) ); curveRoad.rightLanesCurves = JSON.parse( JSON.stringify(curveRoadData.rightLanesCurves) ); curveRoad.leftLanes = JSON.parse( JSON.stringify(curveRoadData.leftLanes) ); curveRoad.rightLanes = JSON.parse( JSON.stringify(curveRoadData.rightLanes) ); } } if (dataLocal.crossPoints) { for (let key in dataLocal.crossPoints) { let crossPointData = dataLocal.crossPoints[key]; let crossPoint = crossPointService.createByVectorId( { x: crossPointData.x, y: crossPointData.y }, crossPointData.edgeInfo1.id, crossPointData.edgeInfo2.id, crossPointData.edgeInfo1.dir, crossPointData.edgeInfo2.dir, crossPointData.vectorId ); crossPoint.curves = JSON.parse(JSON.stringify(crossPointData.curves)); crossPoint.extremePoint = JSON.parse( JSON.stringify(crossPointData.extremePoint) ); crossPoint.style = crossPointData.style; crossPoint.weight = crossPointData.weight; dataService.addCrossPoint(crossPoint); } } if (dataLocal.texts) { for (let key in dataLocal.texts) { let text = textService.create(dataLocal.texts[key].center, key); text.setValue(dataLocal.texts[key].value); text.setFontSize(dataLocal.texts[key].fontSize); text.setColor(dataLocal.texts[key].color); text.setDisplay(dataLocal.texts[key].display); text.setAngle(dataLocal.texts[key].angle || 0); text.linkedPointId = dataLocal.texts[key].linkedPointId; } } if (dataLocal.hasOwnProperty("currentId")) { dataService.setCurrentId(dataLocal.currentId); } if (dataLocal.hasOwnProperty("res")) { coordinate.setRes(dataLocal.res); } } else if (data3d) { if (data3d.backImage) { let bgImg = imageService.createBackgroundImg( data3d.backImage, data3d.vectorId ); bgImg.setCenter(coordinate.center); try { if (bgImg.src) { await bgImg.setImageData(); bgImg.setBounding(); } } catch (e) {} if (data3d.meterPerPixel) { coordinate.setRes(data3d.meterPerPixel); } const width = bgImg.imageData.width; const height = bgImg.imageData.height; let angle = 0; if (data3d.baseLines) { for (let i = 0; i < data3d.baseLines.length; ++i) { //理论上基准线只能有一条 let baseLine = lineService.create( this.getXY(width, height, data3d.baseLines[i][0]), this.getXY(width, height, data3d.baseLines[i][1]), VectorCategory.Line.BaseLine ); Settings.baseLineId = baseLine.vectorId; const geometryBaseLine = mathUtil.createLine1( dataService.getPoint(baseLine.startId), dataService.getPoint(baseLine.endId) ); //文字要和基准线的方向一致 if (typeof geometryBaseLine.a != "undefined") { angle = Math.atan(geometryBaseLine.a); } else if (geometryBaseLine.hasOwnProperty("x")) { angle = Math.PI / 2; } else { angle = 0; } } } if (data3d.measures) { for (let i = 0; i < data3d.measures.length; ++i) { //理论上基准线只能有一条 // const line = lineService.create( this.getXY(width, height, data3d.measures[i].pos[0]), this.getXY(width, height, data3d.measures[i].pos[1]), VectorCategory.Line.MeasureLine ); if (line && data3d.measures[i].dis) { line.value = data3d.measures[i].dis; } } } if (data3d.basePoints) { for (let i = 0; i < data3d.basePoints.length; ++i) { let point = pointService.create( this.getXY(width, height, data3d.basePoints[i]) ); point.setCategory(VectorCategory.Point.BasePoint); } } if (data3d.fixPoints) { for (let i = 0; i < data3d.fixPoints.length; ++i) { let point = pointService.create( this.getXY(width, height, data3d.fixPoints[i].pos) ); point.setCategory(VectorCategory.Point.FixPoint); let text = textService.create( this.getXY(width, height, data3d.fixPoints[i].pos) ); text.setValue(data3d.fixPoints[i].text); // text.setAngle(angle); // text.setDisplayPoint(true); point.linkedTextId = text.vectorId; text.linkedPointId = point.vectorId; } } } } uiService.setRoadMidDivideWidth( Constant.defaultMidDivideWidth / coordinate.res ); uiService.setCurveRoadMidDivideWidth( Constant.defaultMidDivideWidth / coordinate.res ); uiService.setSingleLaneWidth( Constant.defaultSingleLaneWidth / coordinate.res ); if (Settings.baseLineId) { this.layer.updateForLocation(); } this.layer.history.init(); this.layer.renderer.autoRedraw(); } getXY(width, height, position) { const point = {}; point.x = (position.x - width / 2) * coordinate.ratio; point.y = (height / 2 - position.y) * coordinate.ratio; return point; } save() { this.layer.exit(); const res = coordinate.getRes(); dataService.setRes(res); const scale = res / (coordinate.zoom / coordinate.defaultZoom); dataService.setScale(scale); console.log({ ...dataService.vectorData, Settings }); return { ...dataService.vectorData, Settings }; } // 退出页面清除缓存及其他操作 clear() { console.error("clear"); this.layer.uiControl.menu_clear(true); console.warn("clear"); } }