index.ts 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. import {
  2. Appearance,
  3. Billboard,
  4. CallbackProperty,
  5. Cartesian3,
  6. Color,
  7. defined,
  8. EllipseGeometry,
  9. Entity,
  10. GeometryInstance,
  11. GroundPrimitive,
  12. HeightReference,
  13. Material,
  14. Primitive,
  15. ScreenSpaceEventHandler,
  16. ScreenSpaceEventType,
  17. VerticalOrigin,
  18. Math as cesiumMath,
  19. PolygonGeometry,
  20. PolygonHierarchy,
  21. Rectangle as cesiumRectangle,
  22. RectangleGeometry,
  23. BillboardCollection,
  24. } from "cesium";
  25. import { getCatesian3FromPX, cartesianToLonlat } from "../../tools";
  26. import type { BaseAreaI, PlotFuncI, PointArr } from "./../../interface";
  27. import { areaPlot } from "./algorithm";
  28. import emitter from "@/mitt";
  29. class BaseArea implements BaseAreaI {
  30. type: string;
  31. baseType: string;
  32. objId: number;
  33. handler: any;
  34. state: number;
  35. step: number;
  36. floatPoint: any;
  37. floatPointArr: any;
  38. areaPrimitive: any;
  39. areaEntity: any;
  40. modifyHandler: any;
  41. pointList: any[];
  42. material: any;
  43. selectPoint: any;
  44. clickStep: number;
  45. constructor(obj: BaseAreaI) {
  46. this.type = obj.type;
  47. this.baseType = "area";
  48. this.objId = obj.objId;
  49. this.handler = obj.handler;
  50. this.areaPrimitive = obj.areaPrimitive;
  51. this.areaEntity = obj.areaEntity;
  52. this.state = obj.state; //state用于区分当前的状态 0 为删除 1为添加 2为编辑
  53. this.step = obj.step;
  54. this.floatPoint = obj.floatPoint;
  55. this.floatPointArr = obj.floatPointArr;
  56. this.modifyHandler = obj.modifyHandler;
  57. this.pointList = obj.pointList;
  58. this.material = obj.material;
  59. this.selectPoint = obj.selectPoint;
  60. this.clickStep = obj.clickStep;
  61. }
  62. disable() {
  63. if (this.areaEntity) {
  64. window.Viewer.entities.remove(this.areaEntity);
  65. window.Viewer.billboards.remove(this.floatPoint);
  66. this.floatPointArr.forEach((item: Billboard) => {
  67. window.Viewer.billboards.remove(item);
  68. });
  69. this.areaEntity = null;
  70. }
  71. this.state = -1;
  72. this.stopDraw();
  73. }
  74. stopDraw() {
  75. if (this.handler) {
  76. this.handler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
  77. this.handler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
  78. this.handler.removeInputAction(ScreenSpaceEventType.RIGHT_CLICK);
  79. this.handler.destroy();
  80. this.handler = null;
  81. }
  82. if (this.modifyHandler) {
  83. this.modifyHandler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
  84. this.modifyHandler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
  85. this.modifyHandler.removeInputAction(ScreenSpaceEventType.RIGHT_CLICK);
  86. this.modifyHandler.destroy();
  87. this.modifyHandler = null;
  88. }
  89. }
  90. creatPoint(cartesian: number[]): Primitive {
  91. if (!window.Viewer.billboards)
  92. window.Viewer.billboards = window.Viewer.scene.primitives.add(
  93. new BillboardCollection({
  94. scene: window.Viewer.scene,
  95. })
  96. );
  97. return window.Viewer.billboards.add({
  98. id: "moveBillboard",
  99. position: cartesian,
  100. image: "/src/assets/icon/point.png",
  101. verticalOrigin: VerticalOrigin.BOTTOM,
  102. heightReference: HeightReference.CLAMP_TO_GROUND,
  103. });
  104. }
  105. }
  106. // * circle 圆
  107. class Circle extends BaseArea implements PlotFuncI {
  108. constructor() {
  109. super({
  110. type: "Circle",
  111. objId: Number(
  112. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  113. ),
  114. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  115. state: -1,
  116. step: -1,
  117. areaPrimitive: null,
  118. areaEntity: null,
  119. floatPoint: null,
  120. floatPointArr: [],
  121. modifyHandler: null,
  122. pointList: [],
  123. material: Material.fromType("Color", {
  124. color: Color.PINK.clone(),
  125. }),
  126. selectPoint: null,
  127. clickStep: 0,
  128. });
  129. }
  130. startDraw() {
  131. this.state = 1;
  132. // * 单击开始绘制,当点击第三次的时候完成绘制
  133. this.handler.setInputAction((evt: any) => {
  134. const cartesian = getCatesian3FromPX(evt.position);
  135. if (!cartesian) return;
  136. this.pointList.push(cartesian.clone());
  137. if (this.pointList.length == 3) {
  138. this.state = -1;
  139. this.pointList.pop();
  140. this.areaPrimitive = this.showPrimitiveOnMap();
  141. window.Viewer.entities.remove(this.areaEntity);
  142. window.Viewer.billboards.remove(this.floatPoint);
  143. this.areaEntity = null;
  144. this.floatPoint = null;
  145. this.stopDraw();
  146. }
  147. }, ScreenSpaceEventType.LEFT_CLICK);
  148. // * 移动时改变物体positions
  149. this.handler.setInputAction((evt: any) => {
  150. const cartesian = getCatesian3FromPX(evt.endPosition);
  151. if (!cartesian) return;
  152. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  153. else this.floatPoint = this.creatPoint(cartesian.clone());
  154. // * 只有当第一次点击之后再给pointList添加值
  155. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  156. // * 若点击了一次就创建entity使用callback进行数据更新
  157. if (this.pointList.length == 2 && !this.areaEntity) {
  158. this.areaEntity = this.createEntity();
  159. } else {
  160. // * 当鼠标移动时,修改最后一个值
  161. if (this.pointList.length >= 2)
  162. this.pointList.splice(
  163. this.pointList.length - 1,
  164. 1,
  165. cartesian.clone()
  166. );
  167. }
  168. }, ScreenSpaceEventType.MOUSE_MOVE);
  169. }
  170. startModify() {
  171. if (!this.modifyHandler)
  172. this.modifyHandler = new ScreenSpaceEventHandler(
  173. window.Viewer.scene.canvas
  174. );
  175. this.state = 2;
  176. this.pointList.forEach((point) => {
  177. const billboard = this.creatPoint(point);
  178. this.floatPointArr.push(billboard);
  179. });
  180. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  181. this.areaEntity = this.createEntity();
  182. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  183. this.areaPrimitive = null;
  184. this.modifyHandler.setInputAction((evt: any) => {
  185. const cartesian = getCatesian3FromPX(evt.position);
  186. if (!cartesian) return;
  187. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  188. if (this.step != -1) {
  189. this.pointList[this.step] = cartesian.clone();
  190. this.step = -1;
  191. this.floatPoint = null;
  192. return;
  193. } else {
  194. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  195. const feature = window.Viewer.scene.pick(evt.position);
  196. if (
  197. defined(feature) &&
  198. feature.primitive instanceof Billboard &&
  199. feature.primitive.id == "moveBillboard"
  200. ) {
  201. this.floatPoint = feature.primitive;
  202. this.step = this.floatPointArr.indexOf(this.floatPoint);
  203. } else {
  204. this.floatPoint = null;
  205. this.state = -1;
  206. this.floatPointArr.forEach((point: Billboard) => {
  207. window.Viewer.billboards.remove(point);
  208. });
  209. this.floatPointArr = [];
  210. this.areaPrimitive = this.showPrimitiveOnMap();
  211. window.Viewer.entities.remove(this.areaEntity);
  212. this.areaEntity = null;
  213. this.stopDraw();
  214. emitter.emit("modifiedEnd");
  215. }
  216. }
  217. }, ScreenSpaceEventType.LEFT_CLICK);
  218. this.modifyHandler.setInputAction((evt: any) => {
  219. if (this.step != -1 && this.floatPoint) {
  220. const cartesian = getCatesian3FromPX(evt.endPosition);
  221. if (!cartesian) return;
  222. this.floatPoint.position = cartesian;
  223. this.pointList[this.step] = cartesian.clone();
  224. }
  225. }, ScreenSpaceEventType.MOUSE_MOVE);
  226. }
  227. createByData(data: PointArr[]) {
  228. this.pointList = data;
  229. this.areaPrimitive = this.showPrimitiveOnMap();
  230. }
  231. getLnglats(): PointArr[] {
  232. const arr = [];
  233. for (let i = 0; i < this.pointList.length; i++) {
  234. const item = cartesianToLonlat(this.pointList[i]);
  235. arr.push(item);
  236. }
  237. return arr;
  238. }
  239. getPositions(): any[] {
  240. return this.pointList;
  241. }
  242. // * 创建primitive进行展示
  243. showPrimitiveOnMap(): Primitive {
  244. const distance = Cartesian3.distance(this.pointList[0], this.pointList[1]);
  245. const instance = new GeometryInstance({
  246. id: this.objId,
  247. geometry: new EllipseGeometry({
  248. center: this.pointList[0],
  249. semiMajorAxis: distance,
  250. semiMinorAxis: distance,
  251. }),
  252. });
  253. const primitive = window.Viewer.scene.groundPrimitives.add(
  254. new GroundPrimitive({
  255. geometryInstances: instance,
  256. appearance: new Appearance({
  257. material: this.material,
  258. }),
  259. })
  260. );
  261. emitter.emit("drawEnd");
  262. return primitive;
  263. }
  264. // * 创建中间entity以适应动态数据
  265. createEntity(): Entity {
  266. const update = () => {
  267. // * 计算曲线,若有两个点则应该直接返回两个点的连线,若有三个点则返回处理后的坐标集合
  268. if (this.pointList.length == 2) {
  269. return this.pointList[0];
  270. }
  271. };
  272. // * 通过坐标计算半径
  273. const computeDistance = () => {
  274. return Cartesian3.distance(this.pointList[0], this.pointList[1]);
  275. };
  276. return window.Viewer.entities.add({
  277. position: new CallbackProperty(update, false),
  278. ellipse: {
  279. material: Color.BLUE,
  280. clampToGround: true,
  281. heightReference: HeightReference.CLAMP_TO_GROUND,
  282. semiMajorAxis: new CallbackProperty(computeDistance, false),
  283. semiMinorAxis: new CallbackProperty(computeDistance, false),
  284. },
  285. });
  286. }
  287. }
  288. // * ellipse 椭圆
  289. class Ellipse extends BaseArea implements PlotFuncI {
  290. constructor() {
  291. super({
  292. type: "Ellipse",
  293. objId: Number(
  294. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  295. ),
  296. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  297. state: -1,
  298. step: -1,
  299. areaPrimitive: null,
  300. areaEntity: null,
  301. floatPoint: null,
  302. floatPointArr: [],
  303. modifyHandler: null,
  304. pointList: [],
  305. material: Material.fromType("Color", {
  306. color: Color.PINK.clone(),
  307. }),
  308. selectPoint: null,
  309. clickStep: 0,
  310. });
  311. }
  312. startDraw() {
  313. this.state = 1;
  314. // * 单击开始绘制,当点击第三次的时候完成绘制
  315. this.handler.setInputAction((evt: any) => {
  316. const cartesian = getCatesian3FromPX(evt.position);
  317. if (!cartesian) return;
  318. this.pointList.push(cartesian.clone());
  319. if (this.pointList.length == 3) {
  320. this.state = -1;
  321. this.pointList.pop();
  322. this.areaPrimitive = this.showPrimitiveOnMap();
  323. window.Viewer.entities.remove(this.areaEntity);
  324. window.Viewer.billboards.remove(this.floatPoint);
  325. this.areaEntity = null;
  326. this.floatPoint = null;
  327. this.stopDraw();
  328. }
  329. }, ScreenSpaceEventType.LEFT_CLICK);
  330. // * 移动时改变物体positions
  331. this.handler.setInputAction((evt: any) => {
  332. const cartesian = getCatesian3FromPX(evt.endPosition);
  333. if (!cartesian) return;
  334. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  335. else this.floatPoint = this.creatPoint(cartesian.clone());
  336. // * 只有当第一次点击之后再给pointList添加值
  337. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  338. // * 若点击了一次就创建entity使用callback进行数据更新
  339. if (this.pointList.length == 2 && !this.areaEntity) {
  340. this.areaEntity = this.createEntity();
  341. } else {
  342. // * 当鼠标移动时,修改最后一个值
  343. if (this.pointList.length >= 2)
  344. this.pointList.splice(
  345. this.pointList.length - 1,
  346. 1,
  347. cartesian.clone()
  348. );
  349. }
  350. }, ScreenSpaceEventType.MOUSE_MOVE);
  351. }
  352. startModify() {
  353. if (!this.modifyHandler)
  354. this.modifyHandler = new ScreenSpaceEventHandler(
  355. window.Viewer.scene.canvas
  356. );
  357. this.state = 2;
  358. this.pointList.forEach((point) => {
  359. const billboard = this.creatPoint(point);
  360. this.floatPointArr.push(billboard);
  361. });
  362. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  363. this.areaEntity = this.createEntity();
  364. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  365. this.areaPrimitive = null;
  366. this.modifyHandler.setInputAction((evt: any) => {
  367. const cartesian = getCatesian3FromPX(evt.position);
  368. if (!cartesian) return;
  369. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  370. if (this.step != -1) {
  371. this.pointList[this.step] = cartesian.clone();
  372. this.step = -1;
  373. this.floatPoint = null;
  374. return;
  375. } else {
  376. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  377. const feature = window.Viewer.scene.pick(evt.position);
  378. if (
  379. defined(feature) &&
  380. feature.primitive instanceof Billboard &&
  381. feature.primitive.id == "moveBillboard"
  382. ) {
  383. this.floatPoint = feature.primitive;
  384. this.step = this.floatPointArr.indexOf(this.floatPoint);
  385. } else {
  386. this.floatPoint = null;
  387. this.state = -1;
  388. this.floatPointArr.forEach((point: Billboard) => {
  389. window.Viewer.billboards.remove(point);
  390. });
  391. this.floatPointArr = [];
  392. this.areaPrimitive = this.showPrimitiveOnMap();
  393. window.Viewer.entities.remove(this.areaEntity);
  394. this.areaEntity = null;
  395. this.stopDraw();
  396. emitter.emit("modifiedEnd");
  397. }
  398. }
  399. }, ScreenSpaceEventType.LEFT_CLICK);
  400. this.modifyHandler.setInputAction((evt: any) => {
  401. if (this.step != -1 && this.floatPoint) {
  402. const cartesian = getCatesian3FromPX(evt.endPosition);
  403. if (!cartesian) return;
  404. this.floatPoint.position = cartesian;
  405. this.pointList[this.step] = cartesian.clone();
  406. }
  407. }, ScreenSpaceEventType.MOUSE_MOVE);
  408. }
  409. createByData(data: PointArr[]) {
  410. this.pointList = data;
  411. this.areaPrimitive = this.showPrimitiveOnMap();
  412. }
  413. getLnglats(): PointArr[] {
  414. const arr = [];
  415. for (let i = 0; i < this.pointList.length; i++) {
  416. const item = cartesianToLonlat(this.pointList[i]);
  417. arr.push(item);
  418. }
  419. return arr;
  420. }
  421. getPositions(): any[] {
  422. return this.pointList;
  423. }
  424. // * 创建primitive进行展示
  425. showPrimitiveOnMap(): Primitive {
  426. const maxDistance = Cartesian3.distance(
  427. this.pointList[0],
  428. this.pointList[1]
  429. );
  430. const instance = new GeometryInstance({
  431. id: this.objId,
  432. geometry: new EllipseGeometry({
  433. center: this.pointList[0],
  434. semiMajorAxis: maxDistance,
  435. semiMinorAxis: maxDistance / 2,
  436. rotation: this.computeRoate(),
  437. }),
  438. });
  439. const primitive = window.Viewer.scene.groundPrimitives.add(
  440. new GroundPrimitive({
  441. geometryInstances: instance,
  442. appearance: new Appearance({
  443. material: this.material,
  444. }),
  445. })
  446. );
  447. emitter.emit("drawEnd");
  448. return primitive;
  449. }
  450. // * 创建中间entity以适应动态数据
  451. createEntity(): Entity {
  452. const update = () => {
  453. // * 计算曲线,若有两个点则应该直接返回两个点的连线,若有三个点则返回处理后的坐标集合
  454. if (this.pointList.length == 2) {
  455. return this.pointList[0];
  456. }
  457. };
  458. // * 通过坐标计算长半轴
  459. const computeMaxDistance = () => {
  460. const maxDistance = Cartesian3.distance(
  461. this.pointList[0],
  462. this.pointList[1]
  463. );
  464. return maxDistance;
  465. };
  466. // * 通过坐标计算短半轴
  467. const computeMinDistance = () => {
  468. const minDistance =
  469. Cartesian3.distance(this.pointList[0], this.pointList[1]) / 2;
  470. return minDistance;
  471. };
  472. // * 计算椭圆朝向
  473. const computeRoate = () => {
  474. return this.computeRoate.apply(this);
  475. };
  476. return window.Viewer.entities.add({
  477. position: new CallbackProperty(update, false),
  478. ellipse: {
  479. material: Color.BLUE,
  480. clampToGround: true,
  481. heightReference: HeightReference.CLAMP_TO_GROUND,
  482. semiMajorAxis: new CallbackProperty(computeMaxDistance, false),
  483. semiMinorAxis: new CallbackProperty(computeMinDistance, false),
  484. rotation: new CallbackProperty(computeRoate, false),
  485. },
  486. });
  487. }
  488. computeRoate() {
  489. if (
  490. (this.pointList[0].x >= this.pointList[1].x &&
  491. this.pointList[0].y >= this.pointList[1].y) ||
  492. (this.pointList[0].x < this.pointList[1].x &&
  493. this.pointList[0].y < this.pointList[1].y)
  494. ) {
  495. return Math.atan2(
  496. Math.abs(this.pointList[0].y - this.pointList[1].y),
  497. Math.abs(this.pointList[0].x - this.pointList[1].x)
  498. );
  499. } else {
  500. return (
  501. cesiumMath.toRadians(cesiumMath.TWO_PI) -
  502. Math.atan2(
  503. Math.abs(this.pointList[0].y - this.pointList[1].y),
  504. Math.abs(this.pointList[0].x - this.pointList[1].x)
  505. )
  506. );
  507. }
  508. }
  509. }
  510. // * lune 弓形
  511. class Lune extends BaseArea implements PlotFuncI {
  512. constructor() {
  513. super({
  514. type: "Lune",
  515. objId: Number(
  516. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  517. ),
  518. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  519. state: -1,
  520. step: -1,
  521. areaPrimitive: null,
  522. areaEntity: null,
  523. floatPoint: null,
  524. floatPointArr: [],
  525. modifyHandler: null,
  526. pointList: [],
  527. material: Material.fromType("Color", {
  528. color: Color.PINK.clone(),
  529. }),
  530. selectPoint: null,
  531. clickStep: 0,
  532. });
  533. }
  534. startDraw() {
  535. this.state = 1;
  536. // * 单击开始绘制,当点击第三次的时候完成绘制
  537. this.handler.setInputAction((evt: any) => {
  538. const cartesian = getCatesian3FromPX(evt.position);
  539. if (!cartesian) return;
  540. this.pointList.push(cartesian.clone());
  541. if (this.pointList.length == 4) {
  542. this.state = -1;
  543. this.pointList.pop();
  544. this.areaPrimitive = this.showPrimitiveOnMap();
  545. window.Viewer.entities.remove(this.areaEntity);
  546. window.Viewer.billboards.remove(this.floatPoint);
  547. this.areaEntity = null;
  548. this.floatPoint = null;
  549. this.stopDraw();
  550. }
  551. }, ScreenSpaceEventType.LEFT_CLICK);
  552. // * 移动时改变物体positions
  553. this.handler.setInputAction((evt: any) => {
  554. const cartesian = getCatesian3FromPX(evt.endPosition);
  555. if (!cartesian) return;
  556. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  557. else this.floatPoint = this.creatPoint(cartesian.clone());
  558. // * 只有当第一次点击之后再给pointList添加值
  559. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  560. // * 若点击了一次就创建entity使用callback进行数据更新
  561. if (this.pointList.length == 2 && !this.areaEntity) {
  562. this.areaEntity = this.createEntity();
  563. } else {
  564. // * 当鼠标移动时,修改最后一个值
  565. if (this.pointList.length >= 2)
  566. this.pointList.splice(
  567. this.pointList.length - 1,
  568. 1,
  569. cartesian.clone()
  570. );
  571. }
  572. }, ScreenSpaceEventType.MOUSE_MOVE);
  573. }
  574. startModify() {
  575. if (!this.modifyHandler)
  576. this.modifyHandler = new ScreenSpaceEventHandler(
  577. window.Viewer.scene.canvas
  578. );
  579. this.state = 2;
  580. this.pointList.forEach((point) => {
  581. const billboard = this.creatPoint(point);
  582. this.floatPointArr.push(billboard);
  583. });
  584. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  585. this.areaEntity = this.createEntity();
  586. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  587. this.areaPrimitive = null;
  588. this.modifyHandler.setInputAction((evt: any) => {
  589. const cartesian = getCatesian3FromPX(evt.position);
  590. if (!cartesian) return;
  591. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  592. if (this.step != -1) {
  593. this.pointList[this.step] = cartesian.clone();
  594. this.step = -1;
  595. this.floatPoint = null;
  596. return;
  597. } else {
  598. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  599. const feature = window.Viewer.scene.pick(evt.position);
  600. if (
  601. defined(feature) &&
  602. feature.primitive instanceof Billboard &&
  603. feature.primitive.id == "moveBillboard"
  604. ) {
  605. this.floatPoint = feature.primitive;
  606. this.step = this.floatPointArr.indexOf(this.floatPoint);
  607. } else {
  608. this.floatPoint = null;
  609. this.state = -1;
  610. this.floatPointArr.forEach((point: Billboard) => {
  611. window.Viewer.billboards.remove(point);
  612. });
  613. this.floatPointArr = [];
  614. this.areaPrimitive = this.showPrimitiveOnMap();
  615. window.Viewer.entities.remove(this.areaEntity);
  616. this.areaEntity = null;
  617. this.stopDraw();
  618. emitter.emit("modifiedEnd");
  619. }
  620. }
  621. }, ScreenSpaceEventType.LEFT_CLICK);
  622. this.modifyHandler.setInputAction((evt: any) => {
  623. if (this.step != -1 && this.floatPoint) {
  624. const cartesian = getCatesian3FromPX(evt.endPosition);
  625. if (!cartesian) return;
  626. this.floatPoint.position = cartesian;
  627. this.pointList[this.step] = cartesian.clone();
  628. }
  629. }, ScreenSpaceEventType.MOUSE_MOVE);
  630. }
  631. createByData(data: PointArr[]) {
  632. this.pointList = data;
  633. this.areaPrimitive = this.showPrimitiveOnMap();
  634. }
  635. getLnglats(): PointArr[] {
  636. const arr = [];
  637. for (let i = 0; i < this.pointList.length; i++) {
  638. const item = cartesianToLonlat(this.pointList[i]);
  639. arr.push(item);
  640. }
  641. return arr;
  642. }
  643. getPositions(): any[] {
  644. return this.pointList;
  645. }
  646. // * 创建primitive进行展示
  647. showPrimitiveOnMap(): Primitive {
  648. const lnglatArr = [];
  649. for (let i = 0; i < this.pointList.length; i++) {
  650. const lnglat = cartesianToLonlat(this.pointList[i]);
  651. lnglatArr.push(lnglat);
  652. }
  653. const instance = new GeometryInstance({
  654. id: this.objId,
  655. geometry: new PolygonGeometry({
  656. polygonHierarchy: new PolygonHierarchy(
  657. areaPlot.algorithm.getArcPositions(lnglatArr)
  658. ),
  659. }),
  660. });
  661. const primitive = window.Viewer.scene.groundPrimitives.add(
  662. new GroundPrimitive({
  663. geometryInstances: instance,
  664. appearance: new Appearance({
  665. material: this.material,
  666. }),
  667. })
  668. );
  669. emitter.emit("drawEnd");
  670. return primitive;
  671. }
  672. // * 创建中间entity以适应动态数据
  673. createEntity(): Entity {
  674. const update = () => {
  675. // * 计算坐标
  676. const lnglatArr = [];
  677. for (let i = 0; i < this.pointList.length; i++) {
  678. const lnglat = cartesianToLonlat(this.pointList[i]);
  679. lnglatArr.push(lnglat);
  680. }
  681. if (lnglatArr.length == 2) {
  682. lnglatArr.push([lnglatArr[1][0] + 0.0000001, lnglatArr[1][1]]);
  683. }
  684. const res = areaPlot.algorithm.getArcPositions(lnglatArr);
  685. return new PolygonHierarchy(res);
  686. };
  687. return window.Viewer.entities.add({
  688. polygon: {
  689. hierarchy: new CallbackProperty(update, false),
  690. material: Color.BLUE,
  691. heightReference: HeightReference.CLAMP_TO_GROUND,
  692. },
  693. });
  694. }
  695. }
  696. // * Sector 扇形
  697. class Sector extends BaseArea implements PlotFuncI {
  698. constructor() {
  699. super({
  700. type: "Sector",
  701. objId: Number(
  702. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  703. ),
  704. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  705. state: -1,
  706. step: -1,
  707. areaPrimitive: null,
  708. areaEntity: null,
  709. floatPoint: null,
  710. floatPointArr: [],
  711. modifyHandler: null,
  712. pointList: [],
  713. material: Material.fromType("Color", {
  714. color: Color.PINK.clone(),
  715. }),
  716. selectPoint: null,
  717. clickStep: 0,
  718. });
  719. }
  720. startDraw() {
  721. this.state = 1;
  722. // * 单击开始绘制,当点击第三次的时候完成绘制
  723. this.handler.setInputAction((evt: any) => {
  724. const cartesian = getCatesian3FromPX(evt.position);
  725. if (!cartesian) return;
  726. this.pointList.push(cartesian.clone());
  727. if (this.pointList.length == 4) {
  728. this.state = -1;
  729. this.pointList.pop();
  730. this.areaPrimitive = this.showPrimitiveOnMap();
  731. window.Viewer.entities.remove(this.areaEntity);
  732. window.Viewer.billboards.remove(this.floatPoint);
  733. this.areaEntity = null;
  734. this.floatPoint = null;
  735. this.stopDraw();
  736. }
  737. }, ScreenSpaceEventType.LEFT_CLICK);
  738. // * 移动时改变物体positions
  739. this.handler.setInputAction((evt: any) => {
  740. const cartesian = getCatesian3FromPX(evt.endPosition);
  741. if (!cartesian) return;
  742. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  743. else this.floatPoint = this.creatPoint(cartesian.clone());
  744. // * 只有当第一次点击之后再给pointList添加值
  745. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  746. // * 若点击了一次就创建entity使用callback进行数据更新
  747. if (this.pointList.length == 2 && !this.areaEntity) {
  748. this.areaEntity = this.createEntity();
  749. } else {
  750. // * 当鼠标移动时,修改最后一个值
  751. if (this.pointList.length >= 2)
  752. this.pointList.splice(
  753. this.pointList.length - 1,
  754. 1,
  755. cartesian.clone()
  756. );
  757. }
  758. }, ScreenSpaceEventType.MOUSE_MOVE);
  759. }
  760. startModify() {
  761. if (!this.modifyHandler)
  762. this.modifyHandler = new ScreenSpaceEventHandler(
  763. window.Viewer.scene.canvas
  764. );
  765. this.state = 2;
  766. this.pointList.forEach((point) => {
  767. const billboard = this.creatPoint(point);
  768. this.floatPointArr.push(billboard);
  769. });
  770. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  771. this.areaEntity = this.createEntity();
  772. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  773. this.areaPrimitive = null;
  774. this.modifyHandler.setInputAction((evt: any) => {
  775. const cartesian = getCatesian3FromPX(evt.position);
  776. if (!cartesian) return;
  777. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  778. if (this.step != -1) {
  779. this.pointList[this.step] = cartesian.clone();
  780. this.step = -1;
  781. this.floatPoint = null;
  782. return;
  783. } else {
  784. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  785. const feature = window.Viewer.scene.pick(evt.position);
  786. if (
  787. defined(feature) &&
  788. feature.primitive instanceof Billboard &&
  789. feature.primitive.id == "moveBillboard"
  790. ) {
  791. this.floatPoint = feature.primitive;
  792. this.step = this.floatPointArr.indexOf(this.floatPoint);
  793. } else {
  794. this.floatPoint = null;
  795. this.state = -1;
  796. this.floatPointArr.forEach((point: Billboard) => {
  797. window.Viewer.billboards.remove(point);
  798. });
  799. this.floatPointArr = [];
  800. this.areaPrimitive = this.showPrimitiveOnMap();
  801. window.Viewer.entities.remove(this.areaEntity);
  802. this.areaEntity = null;
  803. this.stopDraw();
  804. emitter.emit("modifiedEnd");
  805. }
  806. }
  807. }, ScreenSpaceEventType.LEFT_CLICK);
  808. this.modifyHandler.setInputAction((evt: any) => {
  809. if (this.step != -1 && this.floatPoint) {
  810. const cartesian = getCatesian3FromPX(evt.endPosition);
  811. if (!cartesian) return;
  812. this.floatPoint.position = cartesian;
  813. this.pointList[this.step] = cartesian.clone();
  814. }
  815. }, ScreenSpaceEventType.MOUSE_MOVE);
  816. }
  817. createByData(data: PointArr[]) {
  818. this.pointList = data;
  819. this.areaPrimitive = this.showPrimitiveOnMap();
  820. }
  821. getLnglats(): PointArr[] {
  822. const arr = [];
  823. for (let i = 0; i < this.pointList.length; i++) {
  824. const item = cartesianToLonlat(this.pointList[i]);
  825. arr.push(item);
  826. }
  827. return arr;
  828. }
  829. getPositions(): any[] {
  830. return this.pointList;
  831. }
  832. // * 创建primitive进行展示
  833. showPrimitiveOnMap(): Primitive {
  834. const lnglatArr = [];
  835. for (let i = 0; i < this.pointList.length; i++) {
  836. const lnglat = cartesianToLonlat(this.pointList[i]);
  837. lnglatArr.push(lnglat);
  838. }
  839. const instance = new GeometryInstance({
  840. id: this.objId,
  841. geometry: new PolygonGeometry({
  842. polygonHierarchy: new PolygonHierarchy(
  843. areaPlot.algorithm.getSectorPositions(lnglatArr)
  844. ),
  845. }),
  846. });
  847. const primitives = window.Viewer.scene.groundPrimitives.add(
  848. new GroundPrimitive({
  849. geometryInstances: instance,
  850. appearance: new Appearance({
  851. material: this.material,
  852. }),
  853. })
  854. );
  855. emitter.emit("drawEnd");
  856. return primitives;
  857. }
  858. // * 创建中间entity以适应动态数据
  859. createEntity(): Entity {
  860. const update = () => {
  861. // * 计算坐标
  862. const lnglatArr = [];
  863. for (let i = 0; i < this.pointList.length; i++) {
  864. const lnglat = cartesianToLonlat(this.pointList[i]);
  865. lnglatArr.push(lnglat);
  866. }
  867. if (lnglatArr.length == 2) {
  868. lnglatArr.push([lnglatArr[1][0] + 0.0000001, lnglatArr[1][1]]);
  869. }
  870. const res = areaPlot.algorithm.getSectorPositions(lnglatArr);
  871. return new PolygonHierarchy(res);
  872. };
  873. return window.Viewer.entities.add({
  874. polygon: {
  875. hierarchy: new CallbackProperty(update, false),
  876. material: Color.BLUE,
  877. heightReference: HeightReference.CLAMP_TO_GROUND,
  878. },
  879. });
  880. }
  881. }
  882. // * Rectangle 矩形
  883. class Rectangle extends BaseArea implements PlotFuncI {
  884. constructor() {
  885. super({
  886. type: "Rectangle",
  887. objId: Number(
  888. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  889. ),
  890. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  891. state: -1,
  892. step: -1,
  893. areaPrimitive: null,
  894. areaEntity: null,
  895. floatPoint: null,
  896. floatPointArr: [],
  897. modifyHandler: null,
  898. pointList: [],
  899. material: Material.fromType("Color", {
  900. color: Color.PINK.clone(),
  901. }),
  902. selectPoint: null,
  903. clickStep: 0,
  904. });
  905. }
  906. startDraw() {
  907. this.state = 1;
  908. // * 单击开始绘制,当点击第三次的时候完成绘制
  909. this.handler.setInputAction((evt: any) => {
  910. const cartesian = getCatesian3FromPX(evt.position);
  911. if (!cartesian) return;
  912. this.pointList.push(cartesian.clone());
  913. if (this.pointList.length == 3) {
  914. this.state = -1;
  915. this.pointList.pop();
  916. this.areaPrimitive = this.showPrimitiveOnMap();
  917. window.Viewer.entities.remove(this.areaEntity);
  918. window.Viewer.billboards.remove(this.floatPoint);
  919. this.areaEntity = null;
  920. this.floatPoint = null;
  921. this.stopDraw();
  922. }
  923. }, ScreenSpaceEventType.LEFT_CLICK);
  924. // * 移动时改变物体positions
  925. this.handler.setInputAction((evt: any) => {
  926. const cartesian = getCatesian3FromPX(evt.endPosition);
  927. if (!cartesian) return;
  928. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  929. else this.floatPoint = this.creatPoint(cartesian.clone());
  930. // * 只有当第一次点击之后再给pointList添加值
  931. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  932. // * 若点击了一次就创建entity使用callback进行数据更新
  933. if (this.pointList.length == 2 && !this.areaEntity) {
  934. this.areaEntity = this.createEntity();
  935. } else {
  936. // * 当鼠标移动时,修改最后一个值
  937. if (this.pointList.length >= 2)
  938. this.pointList.splice(
  939. this.pointList.length - 1,
  940. 1,
  941. cartesian.clone()
  942. );
  943. }
  944. }, ScreenSpaceEventType.MOUSE_MOVE);
  945. }
  946. startModify() {
  947. if (!this.modifyHandler)
  948. this.modifyHandler = new ScreenSpaceEventHandler(
  949. window.Viewer.scene.canvas
  950. );
  951. this.state = 2;
  952. this.pointList.forEach((point) => {
  953. const billboard = this.creatPoint(point);
  954. this.floatPointArr.push(billboard);
  955. });
  956. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  957. this.areaEntity = this.createEntity();
  958. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  959. this.areaPrimitive = null;
  960. this.modifyHandler.setInputAction((evt: any) => {
  961. const cartesian = getCatesian3FromPX(evt.position);
  962. if (!cartesian) return;
  963. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  964. if (this.step != -1) {
  965. this.pointList[this.step] = cartesian.clone();
  966. this.step = -1;
  967. this.floatPoint = null;
  968. return;
  969. } else {
  970. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  971. const feature = window.Viewer.scene.pick(evt.position);
  972. if (
  973. defined(feature) &&
  974. feature.primitive instanceof Billboard &&
  975. feature.primitive.id == "moveBillboard"
  976. ) {
  977. this.floatPoint = feature.primitive;
  978. this.step = this.floatPointArr.indexOf(this.floatPoint);
  979. } else {
  980. this.floatPoint = null;
  981. this.state = -1;
  982. this.floatPointArr.forEach((point: Billboard) => {
  983. window.Viewer.billboards.remove(point);
  984. });
  985. this.floatPointArr = [];
  986. this.areaPrimitive = this.showPrimitiveOnMap();
  987. window.Viewer.entities.remove(this.areaEntity);
  988. this.areaEntity = null;
  989. this.stopDraw();
  990. emitter.emit("modifiedEnd");
  991. }
  992. }
  993. }, ScreenSpaceEventType.LEFT_CLICK);
  994. this.modifyHandler.setInputAction((evt: any) => {
  995. if (this.step != -1 && this.floatPoint) {
  996. const cartesian = getCatesian3FromPX(evt.endPosition);
  997. if (!cartesian) return;
  998. this.floatPoint.position = cartesian;
  999. this.pointList[this.step] = cartesian.clone();
  1000. }
  1001. }, ScreenSpaceEventType.MOUSE_MOVE);
  1002. }
  1003. createByData(data: PointArr[]) {
  1004. this.pointList = data;
  1005. this.areaPrimitive = this.showPrimitiveOnMap();
  1006. }
  1007. getLnglats(): PointArr[] {
  1008. const arr = [];
  1009. for (let i = 0; i < this.pointList.length; i++) {
  1010. const item = cartesianToLonlat(this.pointList[i]);
  1011. arr.push(item);
  1012. }
  1013. return arr;
  1014. }
  1015. getPositions(): any[] {
  1016. return this.pointList;
  1017. }
  1018. // * 创建primitive进行展示
  1019. showPrimitiveOnMap(): Primitive {
  1020. const lnglatArr = [];
  1021. for (let i = 0; i < this.pointList.length; i++) {
  1022. const lnglat = cartesianToLonlat(this.pointList[i]);
  1023. lnglatArr.push(lnglat);
  1024. }
  1025. const res = areaPlot.algorithm.getRectanglePositions(
  1026. lnglatArr[0],
  1027. lnglatArr[1]
  1028. );
  1029. const instance = new GeometryInstance({
  1030. id: this.objId,
  1031. geometry: new RectangleGeometry({
  1032. rectangle: cesiumRectangle.fromDegrees(res[0], res[2], res[1], res[3]),
  1033. }),
  1034. });
  1035. const primitive = window.Viewer.scene.groundPrimitives.add(
  1036. new GroundPrimitive({
  1037. geometryInstances: instance,
  1038. appearance: new Appearance({
  1039. material: this.material,
  1040. }),
  1041. })
  1042. );
  1043. emitter.emit("drawEnd");
  1044. return primitive;
  1045. }
  1046. // * 创建中间entity以适应动态数据
  1047. createEntity(): Entity {
  1048. const update = () => {
  1049. // * 计算坐标
  1050. const lnglatArr = [];
  1051. for (let i = 0; i < this.pointList.length; i++) {
  1052. const lnglat = cartesianToLonlat(this.pointList[i]);
  1053. lnglatArr.push(lnglat);
  1054. }
  1055. const res = areaPlot.algorithm.getRectanglePositions(
  1056. lnglatArr[0],
  1057. lnglatArr[1]
  1058. );
  1059. return cesiumRectangle.fromDegrees(res[0], res[2], res[1], res[3]);
  1060. };
  1061. return window.Viewer.entities.add({
  1062. rectangle: {
  1063. coordinates: new CallbackProperty(update, false),
  1064. material: Color.BLUE,
  1065. heightReference: HeightReference.CLAMP_TO_GROUND,
  1066. },
  1067. });
  1068. }
  1069. }
  1070. // * closedCurve 曲线面
  1071. class ClosedCurve extends BaseArea implements PlotFuncI {
  1072. constructor() {
  1073. super({
  1074. type: "ClosedCurve",
  1075. objId: Number(
  1076. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  1077. ),
  1078. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  1079. state: -1,
  1080. step: -1,
  1081. areaPrimitive: null,
  1082. areaEntity: null,
  1083. floatPoint: null,
  1084. floatPointArr: [],
  1085. modifyHandler: null,
  1086. pointList: [],
  1087. material: Material.fromType("Color", {
  1088. color: Color.PINK.clone(),
  1089. }),
  1090. selectPoint: null,
  1091. clickStep: 0,
  1092. });
  1093. }
  1094. startDraw() {
  1095. this.state = 1;
  1096. // * 单击开始绘制,当点击第三次的时候完成绘制
  1097. this.handler.setInputAction((evt: any) => {
  1098. const cartesian = getCatesian3FromPX(evt.position);
  1099. if (!cartesian) return;
  1100. this.pointList.push(cartesian.clone());
  1101. }, ScreenSpaceEventType.LEFT_CLICK);
  1102. // * 移动时改变物体positions
  1103. this.handler.setInputAction((evt: any) => {
  1104. const cartesian = getCatesian3FromPX(evt.endPosition);
  1105. if (!cartesian) return;
  1106. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  1107. else this.floatPoint = this.creatPoint(cartesian.clone());
  1108. // * 只有当第一次点击之后再给pointList添加值
  1109. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  1110. // * 若点击了一次就创建entity使用callback进行数据更新
  1111. if (this.pointList.length == 2 && !this.areaEntity) {
  1112. this.areaEntity = this.createEntity();
  1113. } else {
  1114. // * 当鼠标移动时,修改最后一个值
  1115. if (this.pointList.length >= 2)
  1116. this.pointList.splice(
  1117. this.pointList.length - 1,
  1118. 1,
  1119. cartesian.clone()
  1120. );
  1121. }
  1122. }, ScreenSpaceEventType.MOUSE_MOVE);
  1123. // * 鼠标右击完成绘制
  1124. this.handler.setInputAction(() => {
  1125. if (this.pointList.length < 3) return;
  1126. this.state = -1;
  1127. this.pointList.pop();
  1128. this.areaPrimitive = this.showPrimitiveOnMap();
  1129. window.Viewer.entities.remove(this.areaEntity);
  1130. window.Viewer.billboards.remove(this.floatPoint);
  1131. this.areaEntity = null;
  1132. this.floatPoint = null;
  1133. this.stopDraw();
  1134. }, ScreenSpaceEventType.RIGHT_CLICK);
  1135. }
  1136. startModify() {
  1137. if (!this.modifyHandler)
  1138. this.modifyHandler = new ScreenSpaceEventHandler(
  1139. window.Viewer.scene.canvas
  1140. );
  1141. this.state = 2;
  1142. this.pointList.forEach((point) => {
  1143. const billboard = this.creatPoint(point);
  1144. this.floatPointArr.push(billboard);
  1145. });
  1146. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  1147. this.areaEntity = this.createEntity();
  1148. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  1149. this.areaPrimitive = null;
  1150. this.modifyHandler.setInputAction((evt: any) => {
  1151. const cartesian = getCatesian3FromPX(evt.position);
  1152. if (!cartesian) return;
  1153. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  1154. if (this.step != -1) {
  1155. this.pointList[this.step] = cartesian.clone();
  1156. this.step = -1;
  1157. this.floatPoint = null;
  1158. return;
  1159. } else {
  1160. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  1161. const feature = window.Viewer.scene.pick(evt.position);
  1162. if (
  1163. defined(feature) &&
  1164. feature.primitive instanceof Billboard &&
  1165. feature.primitive.id == "moveBillboard"
  1166. ) {
  1167. this.floatPoint = feature.primitive;
  1168. this.step = this.floatPointArr.indexOf(this.floatPoint);
  1169. } else {
  1170. this.floatPoint = null;
  1171. this.state = -1;
  1172. this.floatPointArr.forEach((point: Billboard) => {
  1173. window.Viewer.billboards.remove(point);
  1174. });
  1175. this.floatPointArr = [];
  1176. this.areaPrimitive = this.showPrimitiveOnMap();
  1177. window.Viewer.entities.remove(this.areaEntity);
  1178. this.areaEntity = null;
  1179. this.stopDraw();
  1180. emitter.emit("modifiedEnd");
  1181. }
  1182. }
  1183. }, ScreenSpaceEventType.LEFT_CLICK);
  1184. this.modifyHandler.setInputAction((evt: any) => {
  1185. if (this.step != -1 && this.floatPoint) {
  1186. const cartesian = getCatesian3FromPX(evt.endPosition);
  1187. if (!cartesian) return;
  1188. this.floatPoint.position = cartesian;
  1189. this.pointList[this.step] = cartesian.clone();
  1190. }
  1191. }, ScreenSpaceEventType.MOUSE_MOVE);
  1192. }
  1193. createByData(data: PointArr[]) {
  1194. this.pointList = data;
  1195. this.areaPrimitive = this.showPrimitiveOnMap();
  1196. }
  1197. getLnglats(): PointArr[] {
  1198. const arr = [];
  1199. for (let i = 0; i < this.pointList.length; i++) {
  1200. const item = cartesianToLonlat(this.pointList[i]);
  1201. arr.push(item);
  1202. }
  1203. return arr;
  1204. }
  1205. getPositions(): any[] {
  1206. return this.pointList;
  1207. }
  1208. // * 创建primitive进行展示
  1209. showPrimitiveOnMap(): Primitive {
  1210. const lnglatArr = [];
  1211. for (let i = 0; i < this.pointList.length; i++) {
  1212. const lnglat = cartesianToLonlat(this.pointList[i]);
  1213. lnglatArr.push(lnglat);
  1214. }
  1215. const res = areaPlot.algorithm.getClosedCurvePositions(lnglatArr);
  1216. const instance = new GeometryInstance({
  1217. id: this.objId,
  1218. geometry: new PolygonGeometry({
  1219. polygonHierarchy: new PolygonHierarchy(res),
  1220. }),
  1221. });
  1222. const primitive = window.Viewer.scene.groundPrimitives.add(
  1223. new GroundPrimitive({
  1224. geometryInstances: instance,
  1225. appearance: new Appearance({
  1226. material: this.material,
  1227. }),
  1228. })
  1229. );
  1230. emitter.emit("drawEnd");
  1231. return primitive;
  1232. }
  1233. // * 创建中间entity以适应动态数据
  1234. createEntity(): Entity {
  1235. const update = () => {
  1236. // * 计算坐标
  1237. const lnglatArr = [];
  1238. for (let i = 0; i < this.pointList.length; i++) {
  1239. const lnglat = cartesianToLonlat(this.pointList[i]);
  1240. lnglatArr.push(lnglat);
  1241. }
  1242. const res = areaPlot.algorithm.getClosedCurvePositions(lnglatArr);
  1243. return new PolygonHierarchy(res);
  1244. };
  1245. return window.Viewer.entities.add({
  1246. polygon: {
  1247. hierarchy: new CallbackProperty(update, false),
  1248. material: Color.BLUE,
  1249. heightReference: HeightReference.CLAMP_TO_GROUND,
  1250. },
  1251. });
  1252. }
  1253. }
  1254. // * polygon 多边形
  1255. class Polygon extends BaseArea implements PlotFuncI {
  1256. constructor() {
  1257. super({
  1258. type: "Polygon",
  1259. objId: Number(
  1260. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  1261. ),
  1262. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  1263. state: -1,
  1264. step: -1,
  1265. areaPrimitive: null,
  1266. areaEntity: null,
  1267. floatPoint: null,
  1268. floatPointArr: [],
  1269. modifyHandler: null,
  1270. pointList: [],
  1271. material: Material.fromType("Color", {
  1272. color: Color.PINK.clone(),
  1273. }),
  1274. selectPoint: null,
  1275. clickStep: 0,
  1276. });
  1277. }
  1278. startDraw() {
  1279. this.state = 1;
  1280. // * 单击开始绘制,当点击第三次的时候完成绘制
  1281. this.handler.setInputAction((evt: any) => {
  1282. const cartesian = getCatesian3FromPX(evt.position);
  1283. if (!cartesian) return;
  1284. this.pointList.push(cartesian.clone());
  1285. }, ScreenSpaceEventType.LEFT_CLICK);
  1286. // * 移动时改变物体positions
  1287. this.handler.setInputAction((evt: any) => {
  1288. const cartesian = getCatesian3FromPX(evt.endPosition);
  1289. if (!cartesian) return;
  1290. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  1291. else this.floatPoint = this.creatPoint(cartesian.clone());
  1292. // * 只有当第一次点击之后再给pointList添加值
  1293. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  1294. // * 若点击了一次就创建entity使用callback进行数据更新
  1295. if (this.pointList.length == 2 && !this.areaEntity) {
  1296. this.areaEntity = this.createEntity();
  1297. } else {
  1298. // * 当鼠标移动时,修改最后一个值
  1299. if (this.pointList.length >= 2)
  1300. this.pointList.splice(
  1301. this.pointList.length - 1,
  1302. 1,
  1303. cartesian.clone()
  1304. );
  1305. }
  1306. }, ScreenSpaceEventType.MOUSE_MOVE);
  1307. // * 鼠标右击完成绘制
  1308. this.handler.setInputAction(() => {
  1309. if (this.pointList.length < 3) return;
  1310. this.state = -1;
  1311. this.pointList.pop();
  1312. this.areaPrimitive = this.showPrimitiveOnMap();
  1313. window.Viewer.entities.remove(this.areaEntity);
  1314. window.Viewer.billboards.remove(this.floatPoint);
  1315. this.areaEntity = null;
  1316. this.floatPoint = null;
  1317. this.stopDraw();
  1318. }, ScreenSpaceEventType.RIGHT_CLICK);
  1319. }
  1320. startModify() {
  1321. if (!this.modifyHandler)
  1322. this.modifyHandler = new ScreenSpaceEventHandler(
  1323. window.Viewer.scene.canvas
  1324. );
  1325. this.state = 2;
  1326. this.pointList.forEach((point) => {
  1327. const billboard = this.creatPoint(point);
  1328. this.floatPointArr.push(billboard);
  1329. });
  1330. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  1331. this.areaEntity = this.createEntity();
  1332. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  1333. this.areaPrimitive = null;
  1334. this.modifyHandler.setInputAction((evt: any) => {
  1335. const cartesian = getCatesian3FromPX(evt.position);
  1336. if (!cartesian) return;
  1337. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  1338. if (this.step != -1) {
  1339. this.pointList[this.step] = cartesian.clone();
  1340. this.step = -1;
  1341. this.floatPoint = null;
  1342. return;
  1343. } else {
  1344. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  1345. const feature = window.Viewer.scene.pick(evt.position);
  1346. if (
  1347. defined(feature) &&
  1348. feature.primitive instanceof Billboard &&
  1349. feature.primitive.id == "moveBillboard"
  1350. ) {
  1351. this.floatPoint = feature.primitive;
  1352. this.step = this.floatPointArr.indexOf(this.floatPoint);
  1353. } else {
  1354. this.floatPoint = null;
  1355. this.state = -1;
  1356. this.floatPointArr.forEach((point: Billboard) => {
  1357. window.Viewer.billboards.remove(point);
  1358. });
  1359. this.floatPointArr = [];
  1360. this.areaPrimitive = this.showPrimitiveOnMap();
  1361. window.Viewer.entities.remove(this.areaEntity);
  1362. this.areaEntity = null;
  1363. this.stopDraw();
  1364. emitter.emit("modifiedEnd");
  1365. }
  1366. }
  1367. }, ScreenSpaceEventType.LEFT_CLICK);
  1368. this.modifyHandler.setInputAction((evt: any) => {
  1369. if (this.step != -1 && this.floatPoint) {
  1370. const cartesian = getCatesian3FromPX(evt.endPosition);
  1371. if (!cartesian) return;
  1372. this.floatPoint.position = cartesian;
  1373. this.pointList[this.step] = cartesian.clone();
  1374. }
  1375. }, ScreenSpaceEventType.MOUSE_MOVE);
  1376. }
  1377. createByData(data: PointArr[]) {
  1378. this.pointList = data;
  1379. this.areaPrimitive = this.showPrimitiveOnMap();
  1380. }
  1381. getLnglats(): PointArr[] {
  1382. const arr = [];
  1383. for (let i = 0; i < this.pointList.length; i++) {
  1384. const item = cartesianToLonlat(this.pointList[i]);
  1385. arr.push(item);
  1386. }
  1387. return arr;
  1388. }
  1389. getPositions(): any[] {
  1390. return this.pointList;
  1391. }
  1392. // * 创建primitive进行展示
  1393. showPrimitiveOnMap(): Primitive {
  1394. const instance = new GeometryInstance({
  1395. id: this.objId,
  1396. geometry: new PolygonGeometry({
  1397. polygonHierarchy: new PolygonHierarchy(this.pointList),
  1398. }),
  1399. });
  1400. const primitive = window.Viewer.scene.groundPrimitives.add(
  1401. new GroundPrimitive({
  1402. geometryInstances: instance,
  1403. appearance: new Appearance({
  1404. material: this.material,
  1405. }),
  1406. })
  1407. );
  1408. emitter.emit("drawEnd");
  1409. return primitive;
  1410. }
  1411. // * 创建中间entity以适应动态数据
  1412. createEntity(): Entity {
  1413. const update = () => {
  1414. return new PolygonHierarchy(this.pointList);
  1415. };
  1416. return window.Viewer.entities.add({
  1417. polygon: {
  1418. hierarchy: new CallbackProperty(update, false),
  1419. material: Color.BLUE,
  1420. heightReference: HeightReference.CLAMP_TO_GROUND,
  1421. },
  1422. });
  1423. }
  1424. }
  1425. // * freeHandPolygon 自由面
  1426. class FreeHandPolygon extends BaseArea implements PlotFuncI {
  1427. constructor() {
  1428. super({
  1429. type: "FreeHandPolygon",
  1430. objId: Number(
  1431. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  1432. ),
  1433. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  1434. state: -1,
  1435. step: -1,
  1436. areaPrimitive: null,
  1437. areaEntity: null,
  1438. floatPoint: null,
  1439. floatPointArr: [],
  1440. modifyHandler: null,
  1441. pointList: [],
  1442. material: Material.fromType("Color", {
  1443. color: Color.PINK.clone(),
  1444. }),
  1445. selectPoint: null,
  1446. clickStep: 0,
  1447. });
  1448. }
  1449. startDraw() {
  1450. this.state = 1;
  1451. // * 单击开始绘制,当点击第三次的时候完成绘制
  1452. this.handler.setInputAction((evt: any) => {
  1453. const cartesian = getCatesian3FromPX(evt.position);
  1454. if (!cartesian) return;
  1455. this.pointList.push(cartesian.clone());
  1456. }, ScreenSpaceEventType.LEFT_CLICK);
  1457. // * 移动时改变物体positions
  1458. this.handler.setInputAction((evt: any) => {
  1459. const cartesian = getCatesian3FromPX(evt.endPosition);
  1460. if (!cartesian) return;
  1461. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  1462. else this.floatPoint = this.creatPoint(cartesian.clone());
  1463. // * 只有当第一次点击之后再给pointList添加值
  1464. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  1465. // * 若点击了一次就创建entity使用callback进行数据更新
  1466. if (this.pointList.length == 2 && !this.areaEntity) {
  1467. this.areaEntity = this.createEntity();
  1468. } else if (this.pointList.length >= 2) {
  1469. // * 随着鼠标移动添加数据
  1470. this.pointList.push(cartesian.clone());
  1471. }
  1472. }, ScreenSpaceEventType.MOUSE_MOVE);
  1473. // * 鼠标右击完成绘制
  1474. this.handler.setInputAction(() => {
  1475. if (this.pointList.length < 3) return;
  1476. this.state = -1;
  1477. this.pointList.pop();
  1478. this.areaPrimitive = this.showPrimitiveOnMap();
  1479. window.Viewer.entities.remove(this.areaEntity);
  1480. window.Viewer.billboards.remove(this.floatPoint);
  1481. this.areaEntity = null;
  1482. this.floatPoint = null;
  1483. this.stopDraw();
  1484. }, ScreenSpaceEventType.RIGHT_CLICK);
  1485. }
  1486. startModify() {
  1487. if (!this.modifyHandler)
  1488. this.modifyHandler = new ScreenSpaceEventHandler(
  1489. window.Viewer.scene.canvas
  1490. );
  1491. this.state = 2;
  1492. this.pointList.forEach((point) => {
  1493. const billboard = this.creatPoint(point);
  1494. this.floatPointArr.push(billboard);
  1495. });
  1496. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  1497. this.areaEntity = this.createEntity();
  1498. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  1499. this.areaPrimitive = null;
  1500. this.modifyHandler.setInputAction((evt: any) => {
  1501. const cartesian = getCatesian3FromPX(evt.position);
  1502. if (!cartesian) return;
  1503. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  1504. if (this.step != -1) {
  1505. this.pointList[this.step] = cartesian.clone();
  1506. this.step = -1;
  1507. this.floatPoint = null;
  1508. return;
  1509. } else {
  1510. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  1511. const feature = window.Viewer.scene.pick(evt.position);
  1512. if (
  1513. defined(feature) &&
  1514. feature.primitive instanceof Billboard &&
  1515. feature.primitive.id == "moveBillboard"
  1516. ) {
  1517. this.floatPoint = feature.primitive;
  1518. this.step = this.floatPointArr.indexOf(this.floatPoint);
  1519. } else {
  1520. this.floatPoint = null;
  1521. this.state = -1;
  1522. this.floatPointArr.forEach((point: Billboard) => {
  1523. window.Viewer.billboards.remove(point);
  1524. });
  1525. this.floatPointArr = [];
  1526. this.areaPrimitive = this.showPrimitiveOnMap();
  1527. window.Viewer.entities.remove(this.areaEntity);
  1528. this.areaEntity = null;
  1529. this.stopDraw();
  1530. emitter.emit("modifiedEnd");
  1531. }
  1532. }
  1533. }, ScreenSpaceEventType.LEFT_CLICK);
  1534. this.modifyHandler.setInputAction((evt: any) => {
  1535. if (this.step != -1 && this.floatPoint) {
  1536. const cartesian = getCatesian3FromPX(evt.endPosition);
  1537. if (!cartesian) return;
  1538. this.floatPoint.position = cartesian;
  1539. this.pointList[this.step] = cartesian.clone();
  1540. }
  1541. }, ScreenSpaceEventType.MOUSE_MOVE);
  1542. }
  1543. createByData(data: PointArr[]) {
  1544. this.pointList = data;
  1545. this.areaPrimitive = this.showPrimitiveOnMap();
  1546. }
  1547. getLnglats(): PointArr[] {
  1548. const arr = [];
  1549. for (let i = 0; i < this.pointList.length; i++) {
  1550. const item = cartesianToLonlat(this.pointList[i]);
  1551. arr.push(item);
  1552. }
  1553. return arr;
  1554. }
  1555. getPositions(): any[] {
  1556. return this.pointList;
  1557. }
  1558. // * 创建primitive进行展示
  1559. showPrimitiveOnMap(): Primitive {
  1560. const instance = new GeometryInstance({
  1561. id: this.objId,
  1562. geometry: new PolygonGeometry({
  1563. polygonHierarchy: new PolygonHierarchy(this.pointList),
  1564. }),
  1565. });
  1566. const primitive = window.Viewer.scene.groundPrimitives.add(
  1567. new GroundPrimitive({
  1568. geometryInstances: instance,
  1569. appearance: new Appearance({
  1570. material: this.material,
  1571. }),
  1572. })
  1573. );
  1574. emitter.emit("drawEnd");
  1575. return primitive;
  1576. }
  1577. // * 创建中间entity以适应动态数据
  1578. createEntity(): Entity {
  1579. const update = () => {
  1580. return new PolygonHierarchy(this.pointList);
  1581. };
  1582. return window.Viewer.entities.add({
  1583. polygon: {
  1584. hierarchy: new CallbackProperty(update, false),
  1585. material: Color.BLUE,
  1586. heightReference: HeightReference.CLAMP_TO_GROUND,
  1587. },
  1588. });
  1589. }
  1590. }
  1591. // * gatheringPlace 聚集地
  1592. class GatheringPlace extends BaseArea implements PlotFuncI {
  1593. constructor() {
  1594. super({
  1595. type: "GatheringPlace",
  1596. objId: Number(
  1597. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  1598. ),
  1599. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  1600. state: -1,
  1601. step: -1,
  1602. areaPrimitive: null,
  1603. areaEntity: null,
  1604. floatPoint: null,
  1605. floatPointArr: [],
  1606. modifyHandler: null,
  1607. pointList: [],
  1608. material: Material.fromType("Color", {
  1609. color: Color.PINK.clone(),
  1610. }),
  1611. selectPoint: null,
  1612. clickStep: 0,
  1613. });
  1614. }
  1615. startDraw() {
  1616. this.state = 1;
  1617. // * 单击开始绘制,当点击第三次的时候完成绘制
  1618. this.handler.setInputAction((evt: any) => {
  1619. const cartesian = getCatesian3FromPX(evt.position);
  1620. if (!cartesian) return;
  1621. this.pointList.push(cartesian.clone());
  1622. if (this.pointList.length == 4) {
  1623. this.state = -1;
  1624. this.pointList.pop();
  1625. this.areaPrimitive = this.showPrimitiveOnMap();
  1626. window.Viewer.entities.remove(this.areaEntity);
  1627. window.Viewer.billboards.remove(this.floatPoint);
  1628. this.areaEntity = null;
  1629. this.floatPoint = null;
  1630. this.stopDraw();
  1631. }
  1632. }, ScreenSpaceEventType.LEFT_CLICK);
  1633. // * 移动时改变物体positions
  1634. this.handler.setInputAction((evt: any) => {
  1635. const cartesian = getCatesian3FromPX(evt.endPosition);
  1636. if (!cartesian) return;
  1637. if (this.floatPoint) this.floatPoint.position = cartesian.clone();
  1638. else this.floatPoint = this.creatPoint(cartesian.clone());
  1639. // * 只有当第一次点击之后再给pointList添加值
  1640. if (this.pointList.length == 1) this.pointList.push(cartesian.clone());
  1641. // * 若点击了一次就创建entity使用callback进行数据更新
  1642. if (this.pointList.length == 2 && !this.areaEntity) {
  1643. this.areaEntity = this.createEntity();
  1644. } else {
  1645. // * 当鼠标移动时,修改最后一个值
  1646. if (this.pointList.length >= 2)
  1647. this.pointList.splice(
  1648. this.pointList.length - 1,
  1649. 1,
  1650. cartesian.clone()
  1651. );
  1652. }
  1653. }, ScreenSpaceEventType.MOUSE_MOVE);
  1654. }
  1655. startModify() {
  1656. if (!this.modifyHandler)
  1657. this.modifyHandler = new ScreenSpaceEventHandler(
  1658. window.Viewer.scene.canvas
  1659. );
  1660. this.state = 2;
  1661. this.pointList.forEach((point) => {
  1662. const billboard = this.creatPoint(point);
  1663. this.floatPointArr.push(billboard);
  1664. });
  1665. // 进入编辑之后将创建好的primitive删除,添加entity用以适应动态数据变化
  1666. this.areaEntity = this.createEntity();
  1667. window.Viewer.scene.groundPrimitives.remove(this.areaPrimitive);
  1668. this.areaPrimitive = null;
  1669. this.modifyHandler.setInputAction((evt: any) => {
  1670. const cartesian = getCatesian3FromPX(evt.position);
  1671. if (!cartesian) return;
  1672. // 如果有移动点在跟随鼠标移动,则在单击的时候固定此点的位置
  1673. if (this.step != -1) {
  1674. this.pointList[this.step] = cartesian.clone();
  1675. this.step = -1;
  1676. this.floatPoint = null;
  1677. return;
  1678. } else {
  1679. // 如果没有移动点跟随鼠标移动,则在单击的时候查看是否有要素,如有则设置跟随点
  1680. const feature = window.Viewer.scene.pick(evt.position);
  1681. if (
  1682. defined(feature) &&
  1683. feature.primitive instanceof Billboard &&
  1684. feature.primitive.id == "moveBillboard"
  1685. ) {
  1686. this.floatPoint = feature.primitive;
  1687. this.step = this.floatPointArr.indexOf(this.floatPoint);
  1688. } else {
  1689. this.floatPoint = null;
  1690. this.state = -1;
  1691. this.floatPointArr.forEach((point: Billboard) => {
  1692. window.Viewer.billboards.remove(point);
  1693. });
  1694. this.floatPointArr = [];
  1695. this.areaPrimitive = this.showPrimitiveOnMap();
  1696. window.Viewer.entities.remove(this.areaEntity);
  1697. this.areaEntity = null;
  1698. this.stopDraw();
  1699. emitter.emit("modifiedEnd");
  1700. }
  1701. }
  1702. }, ScreenSpaceEventType.LEFT_CLICK);
  1703. this.modifyHandler.setInputAction((evt: any) => {
  1704. if (this.step != -1 && this.floatPoint) {
  1705. const cartesian = getCatesian3FromPX(evt.endPosition);
  1706. if (!cartesian) return;
  1707. this.floatPoint.position = cartesian;
  1708. this.pointList[this.step] = cartesian.clone();
  1709. }
  1710. }, ScreenSpaceEventType.MOUSE_MOVE);
  1711. }
  1712. createByData(data: PointArr[]) {
  1713. this.pointList = data;
  1714. this.areaPrimitive = this.showPrimitiveOnMap();
  1715. }
  1716. getLnglats(): PointArr[] {
  1717. const arr = [];
  1718. for (let i = 0; i < this.pointList.length; i++) {
  1719. const item = cartesianToLonlat(this.pointList[i]);
  1720. arr.push(item);
  1721. }
  1722. return arr;
  1723. }
  1724. getPositions(): any[] {
  1725. return this.pointList;
  1726. }
  1727. // * 创建primitive进行展示
  1728. showPrimitiveOnMap(): Primitive {
  1729. // * 计算坐标
  1730. const lnglatArr = [];
  1731. for (let i = 0; i < this.pointList.length; i++) {
  1732. const lnglat = cartesianToLonlat(this.pointList[i]);
  1733. lnglatArr.push(lnglat);
  1734. }
  1735. const res = areaPlot.algorithm.getGatheringPlacePositions(lnglatArr);
  1736. const instance = new GeometryInstance({
  1737. id: this.objId,
  1738. geometry: new PolygonGeometry({
  1739. polygonHierarchy: new PolygonHierarchy(res),
  1740. }),
  1741. });
  1742. const primitive = window.Viewer.scene.groundPrimitives.add(
  1743. new GroundPrimitive({
  1744. geometryInstances: instance,
  1745. appearance: new Appearance({
  1746. material: this.material,
  1747. }),
  1748. })
  1749. );
  1750. emitter.emit("drawEnd");
  1751. return primitive;
  1752. }
  1753. // * 创建中间entity以适应动态数据
  1754. createEntity(): Entity {
  1755. const update = () => {
  1756. // * 计算坐标
  1757. const lnglatArr = [];
  1758. for (let i = 0; i < this.pointList.length; i++) {
  1759. const lnglat = cartesianToLonlat(this.pointList[i]);
  1760. lnglatArr.push(lnglat);
  1761. }
  1762. if (lnglatArr.length == 2) {
  1763. lnglatArr.push([lnglatArr[1][0] + 0.0000001, lnglatArr[1][1]]);
  1764. }
  1765. const res = areaPlot.algorithm.getGatheringPlacePositions(lnglatArr);
  1766. return new PolygonHierarchy(res);
  1767. };
  1768. return window.Viewer.entities.add({
  1769. polygon: {
  1770. hierarchy: new CallbackProperty(update, false),
  1771. material: Color.BLUE,
  1772. heightReference: HeightReference.CLAMP_TO_GROUND,
  1773. },
  1774. });
  1775. }
  1776. }
  1777. export {
  1778. Circle,
  1779. Ellipse,
  1780. Lune,
  1781. Sector,
  1782. Rectangle,
  1783. ClosedCurve,
  1784. Polygon,
  1785. FreeHandPolygon,
  1786. GatheringPlace,
  1787. };