Load.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. import { dataService } from "./Service/DataService.js";
  2. import { lineService } from "./Service/LineService.js";
  3. import { roadService } from "./Service/RoadService.js";
  4. import { curveRoadService } from "./Service/CurveRoadService.js";
  5. import { pointService } from "./Service/PointService.js";
  6. import { curveRoadPointService } from "./Service/CurveRoadPointService.js";
  7. import { roadPointService } from "./Service/RoadPointService.js";
  8. import { imageService } from "./Service/ImageService.js";
  9. import VectorCategory from "./enum/VectorCategory.js";
  10. import { coordinate } from "./Coordinate.js";
  11. import Settings from "./Settings";
  12. import { circleService } from "./Service/CircleService.js";
  13. import { magnifierService } from "./Service/MagnifierService.js";
  14. import { textService } from "./Service/TextService.js";
  15. import { svgService } from "./Service/SVGService.js";
  16. import { mathUtil } from "./Util/MathUtil.js";
  17. import { historyService } from "./Service/HistoryService.js";
  18. import { uiService } from "./Service/UIService";
  19. import { crossPointService } from "./Service/CrossPointService.js";
  20. import Road from "./Geometry/Road.js";
  21. import CurveRoad from "./Geometry/CurveRoad.js";
  22. import { edgeService } from "./Service/EdgeService.js";
  23. import { curvePointService } from "./Service/CurvePointService.js";
  24. import { curveEdgeService } from "./Service/CurveEdgeService.js";
  25. import Constant from "./Constant.js";
  26. export default class Load {
  27. constructor(layer) {
  28. this.layer = layer;
  29. this.version = "v1.0";
  30. this.vectorsJson = null;
  31. this.newVectorId = null;
  32. }
  33. async load(dataLocal, data3d) {
  34. this.layer.initLocation();
  35. coordinate.init(this.layer.canvas);
  36. if (dataLocal) {
  37. if (dataLocal.Settings) {
  38. for (let key in dataLocal.Settings) {
  39. Settings[key] = dataLocal.Settings[key];
  40. }
  41. }
  42. if (dataLocal.backgroundImg) {
  43. let bgImg = imageService.createBackgroundImg(
  44. dataLocal.backgroundImg.src,
  45. dataLocal.backgroundImg.vectorId
  46. );
  47. bgImg.setCenter(dataLocal.backgroundImg.center);
  48. // bgImg.setDisplay(dataLocal.backgroundImg.display);
  49. bgImg.setDisplay(true); //背景图始终显示
  50. bgImg.setAngle(dataLocal.backgroundImg.angle);
  51. try {
  52. if (dataLocal.backgroundImg.src) {
  53. await bgImg.setImageData();
  54. bgImg.setBounding();
  55. }
  56. } catch (e) {}
  57. }
  58. if (dataLocal.circles) {
  59. for (let key in dataLocal.circles) {
  60. let circle = circleService.create(
  61. dataLocal.circles[key].center,
  62. dataLocal.circles[key].radius || dataLocal.circles[key].radiusX,
  63. key
  64. );
  65. circle.setRadiusX(dataLocal.circles[key].radiusX);
  66. circle.setRadiusY(dataLocal.circles[key].radiusY);
  67. circle.setPoints(dataLocal.circles[key].points);
  68. circle.setColor(dataLocal.circles[key].color);
  69. circle.setDisplay(dataLocal.circles[key].display);
  70. }
  71. }
  72. if (dataLocal.magnifiers) {
  73. for (let key in dataLocal.magnifiers) {
  74. let magnifier = magnifierService.create(
  75. dataLocal.magnifiers[key].position,
  76. key
  77. );
  78. magnifier.setSrc(dataLocal.magnifiers[key].photoUrl);
  79. magnifier.setDisplay(dataLocal.magnifiers[key].display);
  80. magnifier.setPopPosition(null, dataLocal.magnifiers[key].popPosition);
  81. try {
  82. if (dataLocal.magnifiers[key].photoUrl) {
  83. await magnifier.setImageData();
  84. }
  85. } catch (e) {}
  86. }
  87. }
  88. if (dataLocal.points) {
  89. for (let key in dataLocal.points) {
  90. let point = pointService.create(dataLocal.points[key], key);
  91. point.setCategory(dataLocal.points[key].category);
  92. point.setParent(
  93. JSON.parse(JSON.stringify(dataLocal.points[key].parent))
  94. );
  95. point.setDisplay(dataLocal.points[key].display);
  96. point.setLocationMode(dataLocal.points[key].locationMode);
  97. point.setLinkedBasePointId(dataLocal.points[key].linkedBasePointId);
  98. point.setColor(point.getColor());
  99. point.linkedTextId = dataLocal.points[key].linkedTextId;
  100. }
  101. // let points = dataService.vectorData.points;
  102. let points = dataLocal.points;
  103. Settings.basePointIds = [];
  104. for (let key in points) {
  105. if (points[key].category == VectorCategory.Point.BasePoint) {
  106. Settings.basePointIds.push(points[key].vectorId);
  107. }
  108. }
  109. }
  110. if (dataLocal.svgs) {
  111. for (let key in dataLocal.svgs) {
  112. let svg = svgService.create(
  113. dataLocal.svgs[key].center,
  114. dataLocal.svgs[key].type,
  115. dataLocal.svgs[key].vectorId
  116. );
  117. svg.setPoints(dataLocal.svgs[key].points);
  118. }
  119. }
  120. if (dataLocal.lines) {
  121. for (let key in dataLocal.lines) {
  122. let line = lineService.createByPointId(
  123. dataLocal.lines[key].startId,
  124. dataLocal.lines[key].endId,
  125. dataLocal.lines[key].category,
  126. key
  127. );
  128. if (dataLocal.lines[key].style) {
  129. line.setStyle(dataLocal.lines[key].style);
  130. }
  131. if (dataLocal.lines[key].weight) {
  132. line.setWeight(dataLocal.lines[key].weight);
  133. }
  134. if (dataLocal.lines[key].color) {
  135. line.setColor(dataLocal.lines[key].color);
  136. }
  137. if (dataLocal.lines[key].value) {
  138. line.setValue(dataLocal.lines[key].value);
  139. }
  140. if (dataLocal.lines[key].locationMode) {
  141. line.setLocationMode(dataLocal.lines[key].locationMode);
  142. }
  143. if (dataLocal.lines[key].linkedFixPointId) {
  144. line.setLinkedFixPointId(dataLocal.lines[key].linkedFixPointId);
  145. }
  146. if (dataLocal.lines[key].linkedBasePointId) {
  147. line.setLinkedBasePointId(dataLocal.lines[key].linkedBasePointId);
  148. }
  149. line.setDisplay(dataLocal.lines[key].display);
  150. if (line.getCategory() == VectorCategory.Line.BaseLine) {
  151. uiService.setBaseLineId(key);
  152. }
  153. }
  154. }
  155. if (dataLocal.curvePoints) {
  156. for (let key in dataLocal.curvePoints) {
  157. let curvePointData = dataLocal.curvePoints[key];
  158. let curvePoint = curvePointService.create(
  159. curvePointData,
  160. curvePointData.vectorId
  161. );
  162. curvePoint.setIndex(curvePointData.index);
  163. curvePoint.setPointParent(curvePointData.parent);
  164. dataService.addCurvePoint(curvePoint);
  165. }
  166. }
  167. if (dataLocal.curvelines) {
  168. for (let key in dataLocal.curvelines) {
  169. let curveLineData = dataLocal.curvelines[key];
  170. let curveLine = lineService.createCurveLineByPointIds(
  171. curveLineData.points,
  172. curveLineData.vectorId
  173. );
  174. curveLine.setStyle(curveLineData.style);
  175. curveLine.setWeight(curveLineData.weight);
  176. dataService.addCurveLine(curveLine);
  177. }
  178. }
  179. if (dataLocal.roadPoints) {
  180. for (let key in dataLocal.roadPoints) {
  181. let roadPointData = dataLocal.roadPoints[key];
  182. let roadPoint = roadPointService.create(
  183. dataLocal.roadPoints[key],
  184. dataLocal.roadPoints[key].vectorId
  185. );
  186. roadPoint.parent = JSON.parse(JSON.stringify(roadPointData.parent));
  187. }
  188. }
  189. if (dataLocal.roads) {
  190. for (let key in dataLocal.roads) {
  191. uiService.setWayType(dataLocal.roads[key].way);
  192. uiService.setSingleRoadDrivewayCount(
  193. dataLocal.roads[key].singleRoadDrivewayCount
  194. );
  195. uiService.setRoadLeftDrivewayCount(
  196. dataLocal.roads[key].leftDrivewayCount
  197. );
  198. uiService.setRoadRightDrivewayCount(
  199. dataLocal.roads[key].rightDrivewayCount
  200. );
  201. let road = new Road(
  202. dataLocal.roads[key].startId,
  203. dataLocal.roads[key].endId,
  204. dataLocal.roads[key].vectorId
  205. );
  206. dataService.addRoad(road);
  207. road.setWidth(dataLocal.roads[key].leftWidth, "left");
  208. road.setWidth(dataLocal.roads[key].rightWidth, "right");
  209. road.setWidth(dataLocal.roads[key].singleRoadWidth);
  210. road.setMidDivide(dataLocal.roads[key].midDivide);
  211. road.setLeftEdgeId(dataLocal.roads[key].leftEdgeId);
  212. road.setRightEdgeId(dataLocal.roads[key].rightEdgeId);
  213. road.setLeftLanes(dataLocal.roads[key].leftLanes);
  214. road.setRightLanes(dataLocal.roads[key].rightLanes);
  215. road.setSingleLanes(dataLocal.roads[key].singleLanes);
  216. }
  217. }
  218. if (dataLocal.roadEdges) {
  219. //当roadEdge有样式的时候需要设置
  220. for (let edgeKey in dataLocal.roadEdges) {
  221. let edge = dataLocal.roadEdges[edgeKey];
  222. let newEdge = edgeService.create(
  223. edge.start,
  224. edge.end,
  225. edge.vectorId,
  226. edge.parent
  227. );
  228. if (edge.style) {
  229. newEdge.setStyle(edge.style);
  230. }
  231. if (edge.weight) {
  232. newEdge.setWeight(edge.weight);
  233. }
  234. newEdge.setLineWidth(edge.lineWidth);
  235. newEdge.setParent(edge.parent);
  236. }
  237. }
  238. if (dataLocal.curveRoadPoints) {
  239. for (let key in dataLocal.curveRoadPoints) {
  240. let curveRoadPoint = curveRoadPointService.create(
  241. dataLocal.curveRoadPoints[key],
  242. dataLocal.curveRoadPoints[key].vectorId
  243. );
  244. curveRoadPoint.setIndex(dataLocal.curveRoadPoints[key].index);
  245. curveRoadPoint.setParent(dataLocal.curveRoadPoints[key].parent);
  246. }
  247. }
  248. if (dataLocal.curveRoadEdges) {
  249. for (let key in dataLocal.curveRoadEdges) {
  250. let curveRoadEdgeData = dataLocal.curveRoadEdges[key];
  251. let curveRoadEdge = curveEdgeService.create(
  252. curveRoadEdgeData.start,
  253. curveRoadEdgeData.end,
  254. curveRoadEdgeData.vectorId,
  255. curveRoadEdgeData.parentId,
  256. curveRoadEdgeData.points
  257. );
  258. curveRoadEdge.setStyle(curveRoadEdgeData.style);
  259. curveRoadEdge.setWeight(curveRoadEdgeData.weight);
  260. curveRoadEdge.setParent(curveRoadEdgeData.parent);
  261. curveEdgeService.setCurves(curveRoadEdge);
  262. }
  263. }
  264. if (dataLocal.curveRoads) {
  265. for (let key in dataLocal.curveRoads) {
  266. let curveRoadData = dataLocal.curveRoads[key];
  267. let curveRoad = new CurveRoad(
  268. curveRoadData.startId,
  269. curveRoadData.endId,
  270. curveRoadData.vectorId
  271. );
  272. dataService.addCurveRoad(curveRoad);
  273. curveRoad.leftEdgeId = curveRoadData.leftEdgeId;
  274. curveRoad.rightEdgeId = curveRoadData.rightEdgeId;
  275. curveRoad.points = [];
  276. for (let i = 0; i < curveRoadData.points.length; ++i) {
  277. curveRoad.points[i] = dataService.getCurveRoadPoint(
  278. curveRoadData.points[i].vectorId
  279. );
  280. }
  281. curveRoad.curves = JSON.parse(JSON.stringify(curveRoadData.curves));
  282. curveRoad.way = curveRoadData.way;
  283. curveRoad.singleCurveRoadWidth = curveRoadData.singleCurveRoadWidth;
  284. curveRoad.singleCurveRoadDrivewayCount =
  285. curveRoadData.singleCurveRoadDrivewayCount;
  286. curveRoad.singleLanesCurves = JSON.parse(
  287. JSON.stringify(curveRoadData.singleLanesCurves)
  288. );
  289. curveRoad.singleLanesCurves = JSON.parse(
  290. JSON.stringify(curveRoadData.singleLanes)
  291. );
  292. curveRoad.leftWidth = curveRoadData.leftWidth;
  293. curveRoad.rightWidth = curveRoadData.rightWidth;
  294. curveRoad.leftDrivewayCount = curveRoadData.leftDrivewayCount;
  295. curveRoad.rightDrivewayCount = curveRoadData.rightDrivewayCount;
  296. curveRoad.midDivide = JSON.parse(
  297. JSON.stringify(curveRoadData.midDivide)
  298. );
  299. curveRoad.leftLanesCurves = JSON.parse(
  300. JSON.stringify(curveRoadData.leftLanesCurves)
  301. );
  302. curveRoad.rightLanesCurves = JSON.parse(
  303. JSON.stringify(curveRoadData.rightLanesCurves)
  304. );
  305. curveRoad.leftLanes = JSON.parse(
  306. JSON.stringify(curveRoadData.leftLanes)
  307. );
  308. curveRoad.rightLanes = JSON.parse(
  309. JSON.stringify(curveRoadData.rightLanes)
  310. );
  311. }
  312. }
  313. if (dataLocal.crossPoints) {
  314. for (let key in dataLocal.crossPoints) {
  315. let crossPointData = dataLocal.crossPoints[key];
  316. let crossPoint = crossPointService.createByVectorId(
  317. { x: crossPointData.x, y: crossPointData.y },
  318. crossPointData.edgeInfo1.id,
  319. crossPointData.edgeInfo2.id,
  320. crossPointData.edgeInfo1.dir,
  321. crossPointData.edgeInfo2.dir,
  322. crossPointData.vectorId
  323. );
  324. crossPoint.curves = JSON.parse(JSON.stringify(crossPointData.curves));
  325. crossPoint.extremePoint = JSON.parse(
  326. JSON.stringify(crossPointData.extremePoint)
  327. );
  328. crossPoint.style = crossPointData.style;
  329. crossPoint.weight = crossPointData.weight;
  330. dataService.addCrossPoint(crossPoint);
  331. }
  332. }
  333. if (dataLocal.texts) {
  334. for (let key in dataLocal.texts) {
  335. let text = textService.create(dataLocal.texts[key].center, key);
  336. text.setValue(dataLocal.texts[key].value);
  337. text.setFontSize(dataLocal.texts[key].fontSize);
  338. text.setColor(dataLocal.texts[key].color);
  339. text.setDisplay(dataLocal.texts[key].display);
  340. text.setAngle(dataLocal.texts[key].angle || 0);
  341. text.linkedPointId = dataLocal.texts[key].linkedPointId;
  342. }
  343. }
  344. if (dataLocal.hasOwnProperty("currentId")) {
  345. dataService.setCurrentId(dataLocal.currentId);
  346. }
  347. if (dataLocal.hasOwnProperty("res")) {
  348. coordinate.setRes(dataLocal.res);
  349. }
  350. } else if (data3d) {
  351. if (data3d.backImage) {
  352. let bgImg = imageService.createBackgroundImg(
  353. data3d.backImage,
  354. data3d.vectorId
  355. );
  356. bgImg.setCenter(coordinate.center);
  357. try {
  358. if (bgImg.src) {
  359. await bgImg.setImageData();
  360. bgImg.setBounding();
  361. }
  362. } catch (e) {}
  363. if (data3d.meterPerPixel) {
  364. coordinate.setRes(data3d.meterPerPixel);
  365. }
  366. const width = bgImg.imageData.width;
  367. const height = bgImg.imageData.height;
  368. let angle = 0;
  369. if (data3d.baseLines) {
  370. for (let i = 0; i < data3d.baseLines.length; ++i) {
  371. //理论上基准线只能有一条
  372. let baseLine = lineService.create(
  373. this.getXY(width, height, data3d.baseLines[i][0]),
  374. this.getXY(width, height, data3d.baseLines[i][1]),
  375. VectorCategory.Line.BaseLine
  376. );
  377. Settings.baseLineId = baseLine.vectorId;
  378. const geometryBaseLine = mathUtil.createLine1(
  379. dataService.getPoint(baseLine.startId),
  380. dataService.getPoint(baseLine.endId)
  381. );
  382. //文字要和基准线的方向一致
  383. if (typeof geometryBaseLine.a != "undefined") {
  384. angle = Math.atan(geometryBaseLine.a);
  385. } else if (geometryBaseLine.hasOwnProperty("x")) {
  386. angle = Math.PI / 2;
  387. } else {
  388. angle = 0;
  389. }
  390. }
  391. }
  392. if (data3d.measures) {
  393. for (let i = 0; i < data3d.measures.length; ++i) {
  394. //理论上基准线只能有一条
  395. //
  396. const line = lineService.create(
  397. this.getXY(width, height, data3d.measures[i].pos[0]),
  398. this.getXY(width, height, data3d.measures[i].pos[1]),
  399. VectorCategory.Line.MeasureLine
  400. );
  401. if (line && data3d.measures[i].dis) {
  402. line.value = data3d.measures[i].dis;
  403. }
  404. }
  405. }
  406. if (data3d.basePoints) {
  407. for (let i = 0; i < data3d.basePoints.length; ++i) {
  408. let point = pointService.create(
  409. this.getXY(width, height, data3d.basePoints[i])
  410. );
  411. point.setCategory(VectorCategory.Point.BasePoint);
  412. }
  413. }
  414. if (data3d.fixPoints) {
  415. for (let i = 0; i < data3d.fixPoints.length; ++i) {
  416. let point = pointService.create(
  417. this.getXY(width, height, data3d.fixPoints[i].pos)
  418. );
  419. point.setCategory(VectorCategory.Point.FixPoint);
  420. let text = textService.create(
  421. this.getXY(width, height, data3d.fixPoints[i].pos)
  422. );
  423. text.setValue(data3d.fixPoints[i].text);
  424. // text.setAngle(angle);
  425. // text.setDisplayPoint(true);
  426. point.linkedTextId = text.vectorId;
  427. text.linkedPointId = point.vectorId;
  428. }
  429. }
  430. }
  431. }
  432. uiService.setRoadMidDivideWidth(
  433. Constant.defaultMidDivideWidth / coordinate.res
  434. );
  435. uiService.setCurveRoadMidDivideWidth(
  436. Constant.defaultMidDivideWidth / coordinate.res
  437. );
  438. uiService.setSingleLaneWidth(
  439. Constant.defaultSingleLaneWidth / coordinate.res
  440. );
  441. if (Settings.baseLineId) {
  442. this.layer.updateForLocation();
  443. }
  444. this.layer.history.init();
  445. this.layer.renderer.autoRedraw();
  446. }
  447. getXY(width, height, position) {
  448. const point = {};
  449. point.x = (position.x - width / 2) * coordinate.ratio;
  450. point.y = (height / 2 - position.y) * coordinate.ratio;
  451. return point;
  452. }
  453. save() {
  454. this.layer.exit();
  455. const res = coordinate.getRes();
  456. dataService.setRes(res);
  457. const scale = res / (coordinate.zoom / coordinate.defaultZoom);
  458. dataService.setScale(scale);
  459. console.log({ ...dataService.vectorData, Settings });
  460. return { ...dataService.vectorData, Settings };
  461. }
  462. // 退出页面清除缓存及其他操作
  463. clear() {
  464. console.error("clear");
  465. this.layer.uiControl.menu_clear(true);
  466. console.warn("clear");
  467. }
  468. }