index.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. import { createSelector } from 'reselect';
  2. import Global from './Common';
  3. import { latLngDegreesToDecimal, latLngDecimalToDegrees, isObject, isSafeString, hasPoint, isNumber } from './Utils';
  4. let LatLon = require('geodesy').LatLonSpherical;
  5. export function convertAirspaceInfoServerToLocal(airspaceInfo) {
  6. const airspaceType = airspaceInfo.airspace_type;
  7. if (airspaceType == Global.airspaceType.circle) {
  8. const ai = airspaceInfo;
  9. return {
  10. airspaceType,
  11. airspaceId: ai.airspace_id,
  12. name: ai.airspace_name,
  13. note: ai.note,
  14. addr: ai.center_loc,
  15. lat: latLngDecimalToDegrees(ai.center_point_of_flying.lat),
  16. lng: latLngDecimalToDegrees(ai.center_point_of_flying.lng),
  17. radius: ai.radius_of_flying,
  18. height: ai.altitude + "",
  19. heightStandard: Global.heightStandardsById.get(ai.unit)
  20. };
  21. }
  22. if (airspaceType == Global.airspaceType.line) {
  23. const ai = airspaceInfo;
  24. let dep = {
  25. addr: ai.start_loc,
  26. lat: latLngDecimalToDegrees(ai.start_point.lat),
  27. lng: latLngDecimalToDegrees(ai.start_point.lng),
  28. height: ai.start_point.altitude + "",
  29. heightStandard: Global.heightStandardsById.get(ai.start_point.unit)
  30. };
  31. let arrive = {
  32. addr: ai.end_loc,
  33. lat: latLngDecimalToDegrees(ai.end_point.lat),
  34. lng: latLngDecimalToDegrees(ai.end_point.lng)
  35. };
  36. let passPoints = [];
  37. if (Array.isArray(ai.passing_points)) {
  38. for (let obj of ai.passing_points) {
  39. let point;
  40. if (obj.point_type == Global.pointTypes.point) {
  41. const pp = obj;
  42. const lat = latLngDecimalToDegrees(pp.lat);
  43. const lng = latLngDecimalToDegrees(pp.lng);
  44. point = {
  45. pointType: pp.point_type,
  46. addr: pp.point_name,
  47. lat, lng
  48. };
  49. }
  50. else if (obj.point_type == Global.pointTypes.nav) {
  51. const pp = obj;
  52. const lat = latLngDecimalToDegrees(pp.lat);
  53. const lng = latLngDecimalToDegrees(pp.lng);
  54. point = {
  55. pointType: pp.point_type,
  56. pointId: pp.point_id,
  57. pointCode: pp.point_code,
  58. pointName: pp.point_name,
  59. lat, lng
  60. };
  61. }
  62. else {
  63. const pp = obj;
  64. point = {
  65. pointType: pp.point_type,
  66. airRouteId: pp.airway,
  67. airlineCode: pp.air_route_code,
  68. airlines: pp.points
  69. };
  70. }
  71. if (isNumber(obj.altitude) && isNumber(obj.unit)) {
  72. point.height = obj.altitude + "";
  73. point.heightStandard = Global.heightStandardsById.get(obj.unit);
  74. }
  75. else {
  76. point.height = "";
  77. point.heightStandard = [...Global.heightStandards.keys()][0];
  78. }
  79. passPoints.push(point);
  80. }
  81. }
  82. return Object.assign({
  83. airspaceType,
  84. airspaceId: ai.airspace_id,
  85. name: ai.airspace_name,
  86. note: ai.note,
  87. dep,
  88. arrive,
  89. passPoints
  90. }, (ai.airline_width ? { airlineWidth: ai.airline_width } : {}));
  91. }
  92. if (airspaceType == Global.airspaceType.polygon) {
  93. const ai = airspaceInfo;
  94. if (Array.isArray(ai.points)) {
  95. let polygonPoints = new Array();
  96. let defaultPointName = 'A';
  97. for (let obj of ai.points) {
  98. let addr = obj.addr;
  99. if (!addr) {
  100. addr = defaultPointName;
  101. defaultPointName = String.fromCharCode(defaultPointName.charCodeAt(0) + 1);
  102. }
  103. polygonPoints.push({ addr, lat: latLngDecimalToDegrees(obj.lat), lng: latLngDecimalToDegrees(obj.lng) });
  104. }
  105. return {
  106. airspaceType,
  107. airspaceId: ai.airspace_id,
  108. name: ai.airspace_name,
  109. note: ai.note,
  110. height: ai.altitude + '',
  111. heightStandard: Global.heightStandardsById.get(ai.unit),
  112. polygonPoints
  113. };
  114. }
  115. }
  116. return null;
  117. }
  118. export function convertAirspaceInfoLocalToServer(airspace) {
  119. if (airspace.airspaceType == Global.airspaceType.circle) {
  120. const ai = airspace;
  121. const circle = {
  122. airspace_name: ai.name,
  123. airspace_id: ai.airspaceId,
  124. airspace_type: Global.airspaceType.circle,
  125. note: isSafeString(ai.note) ? ai.note : null,
  126. radius_of_flying: parseInt(ai.radius + ''),
  127. center_point_of_flying: {
  128. lng: latLngDegreesToDecimal(ai.lng),
  129. lat: latLngDegreesToDecimal(ai.lat)
  130. },
  131. center_loc: ai.addr,
  132. altitude: parseInt(ai.height),
  133. unit: Global.heightStandards.get(ai.heightStandard)
  134. };
  135. return circle;
  136. }
  137. else if (airspace.airspaceType == Global.airspaceType.line) {
  138. const ai = airspace;
  139. let line = {
  140. airspace_name: ai.name,
  141. airspace_id: ai.airspaceId,
  142. airspace_type: Global.airspaceType.line,
  143. note: ai.note,
  144. start_loc: ai.dep.addr,
  145. start_point: {
  146. lng: latLngDegreesToDecimal(ai.dep.lng),
  147. lat: latLngDegreesToDecimal(ai.dep.lat),
  148. altitude: parseInt(ai.dep.height),
  149. unit: Global.heightStandards.get(ai.dep.heightStandard)
  150. },
  151. end_loc: ai.arrive.addr,
  152. end_point: {
  153. lng: latLngDegreesToDecimal(ai.arrive.lng),
  154. lat: latLngDegreesToDecimal(ai.arrive.lat)
  155. }
  156. };
  157. if (ai.airlineWidth) {
  158. line['airline_width'] = ai.airlineWidth;
  159. }
  160. if (Array.isArray(ai.passPoints) && ai.passPoints.length > 0) {
  161. let passing_points = [];
  162. for (let obj of ai.passPoints) {
  163. let retObj;
  164. if (obj.pointType == Global.pointTypes.point && hasPoint(obj)) {
  165. const pp = obj;
  166. retObj = {
  167. point_type: Global.pointTypes.point,
  168. point_name: pp.addr,
  169. lat: latLngDegreesToDecimal(pp.lat),
  170. lng: latLngDegreesToDecimal(pp.lng)
  171. };
  172. }
  173. else if (obj.pointType == Global.pointTypes.nav) {
  174. const pp = obj;
  175. retObj = {
  176. point_type: Global.pointTypes.nav,
  177. point_name: pp.pointName,
  178. point_id: pp.pointId,
  179. point_code: pp.pointCode,
  180. lat: latLngDegreesToDecimal(pp.lat),
  181. lng: latLngDegreesToDecimal(pp.lng)
  182. };
  183. }
  184. else {
  185. const pp = obj;
  186. retObj = {
  187. point_type: Global.pointTypes.line,
  188. airway: pp.airRouteId,
  189. air_route_code: pp.airlineCode,
  190. points: pp.airlines
  191. };
  192. }
  193. if (isSafeString(obj.height) && isSafeString(obj.heightStandard)) {
  194. retObj.altitude = parseInt(obj.height);
  195. // @ts-ignore
  196. retObj.unit = Global.heightStandards.get(obj.heightStandard);
  197. }
  198. passing_points.push(retObj);
  199. }
  200. line.passing_points = passing_points;
  201. }
  202. else {
  203. line.passing_points = [];
  204. }
  205. return line;
  206. }
  207. else {
  208. const ai = airspace;
  209. let points = [];
  210. for (let obj of ai.polygonPoints) {
  211. points.push({ addr: obj.addr, lat: latLngDegreesToDecimal(obj.lat), lng: latLngDegreesToDecimal(obj.lng) });
  212. }
  213. const polygon = {
  214. airspace_name: ai.name,
  215. airspace_id: ai.airspaceId,
  216. airspace_type: Global.airspaceType.polygon,
  217. note: ai.note,
  218. altitude: parseInt(ai.height),
  219. unit: Global.heightStandards.get(ai.heightStandard),
  220. points
  221. };
  222. return polygon;
  223. }
  224. }
  225. function getCircleRegions(circle) {
  226. let lat, lng;
  227. if (circle.coordinate.latitude) {
  228. let coord = circle.coordinate;
  229. lat = coord.latitude;
  230. lng = coord.longitude;
  231. }
  232. else {
  233. let coord = circle.coordinate;
  234. lat = coord.lat;
  235. lng = coord.lng;
  236. }
  237. let latlon = new LatLon(lat, lng);
  238. let d1 = latlon.destinationPoint(circle.radius, 0);
  239. let d2 = latlon.destinationPoint(circle.radius, 90);
  240. let d3 = latlon.destinationPoint(circle.radius, 180);
  241. let d4 = latlon.destinationPoint(circle.radius, 270);
  242. return [{ lat: d1.lat, lng: d1.lon }, { lat: d2.lat, lng: d2.lon }, { lat: d3.lat, lng: d3.lon }, { lat: d4.lat, lng: d4.lon }];
  243. }
  244. function getDefaultStyle() {
  245. let imageName = 'BA_oval';
  246. let lineWidth = Global.amapLineWidth;
  247. let strokeColor = Global.amapStrokeColor;
  248. let fillColor = Global.amapFillColor;
  249. return { imageName, lineWidth, strokeColor, fillColor };
  250. }
  251. function getCirclesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  252. let circles = [];
  253. let markers = [];
  254. if (!Array.isArray(airspaceInfos)) {
  255. return { circles, markers };
  256. }
  257. let { imageName, lineWidth, strokeColor, fillColor } = getDefaultStyle();
  258. //通过该方法获取样式
  259. let circleStyle = setStyle('circle');
  260. if (circleStyle) {
  261. lineWidth = circleStyle.lineWidth;
  262. strokeColor = circleStyle.strokeColor;
  263. fillColor = circleStyle.fillColor;
  264. imageName = circleStyle.imageName;
  265. }
  266. for (let i = 0; i < airspaceInfos.length; i++) {
  267. let tmpCircle = airspaceInfos[i];
  268. let airspaceTypeFix, radiusFix;
  269. if (tmpCircle.airspaceType) {
  270. airspaceTypeFix = 'airspaceType';
  271. radiusFix = 'radius';
  272. }
  273. else {
  274. airspaceTypeFix = 'airspace_type';
  275. radiusFix = 'radius_of_flying';
  276. }
  277. if (tmpCircle[airspaceTypeFix] == Global.airspaceType.circle && currentAirspaceIndex != i) {
  278. let coordinate = { latitude: 0, longitude: 0 };
  279. if (tmpCircle.center_point_of_flying) {
  280. let ai = tmpCircle;
  281. coordinate.latitude = ai.center_point_of_flying.lat;
  282. coordinate.longitude = ai.center_point_of_flying.lng;
  283. }
  284. else {
  285. let ai = tmpCircle;
  286. coordinate.latitude = latLngDegreesToDecimal(ai.lat);
  287. coordinate.longitude = latLngDegreesToDecimal(ai.lng);
  288. }
  289. let radius = tmpCircle[radiusFix];
  290. if (radius) {
  291. let circle = { lineWidth, strokeColor, fillColor, radius, coordinate };
  292. circles.push(circle);
  293. }
  294. else {
  295. // 这里的经纬度必定是数值, 所以最后一个参数是 2
  296. markers.push(addOvalPointConfig(coordinate.latitude, coordinate.longitude, imageName, 2));
  297. }
  298. }
  299. }
  300. return { circles, markers };
  301. }
  302. function getCircleAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  303. return createSelector(airspaceInfos, () => setStyle, currentAirspaceIndex, getCirclesAndMarkers);
  304. }
  305. function getLatLng(latlng, dataType) {
  306. if (dataType == 1) { // 驼峰模式,新建计划的时候的格式
  307. return latLngDegreesToDecimal(latlng);
  308. }
  309. else {
  310. return latlng;
  311. }
  312. }
  313. function drawLineConfig(lat, lng, dataType) {
  314. return {
  315. latitude: getLatLng(lat, dataType),
  316. longitude: getLatLng(lng, dataType)
  317. };
  318. }
  319. function addOvalPointConfig(lat, lng, imageName, dataType) {
  320. return {
  321. coordinate: {
  322. latitude: getLatLng(lat, dataType),
  323. longitude: getLatLng(lng, dataType)
  324. },
  325. imageName: imageName
  326. };
  327. }
  328. function pointCompare(point1, point2) {
  329. if (!point1 || !point2) {
  330. return false;
  331. }
  332. const pointId1 = point1.point_id;
  333. const pointId2 = point2.point_id;
  334. if (pointId1 == pointId2) {
  335. return true;
  336. }
  337. let [point1Lat, point1lng] = getFixedLatLng(point1);
  338. let [point2lat, point2lng] = getFixedLatLng(point2);
  339. if (myRound(point1Lat) == myRound(point2lat)
  340. && myRound(point1lng) == myRound(point2lng)) {
  341. return true;
  342. }
  343. else {
  344. return false;
  345. }
  346. }
  347. function getCrossPoint(points1, points2) {
  348. for (let point1 of points1) {
  349. for (let point2 of points2) {
  350. if (pointCompare(point1, point2))
  351. return point1;
  352. }
  353. }
  354. return undefined;
  355. }
  356. function getAirwayPoints(airway, pointBefore, pointAfter) {
  357. let found = 0;
  358. let points = [];
  359. let pointTypeFix, pointsFix, airRoutePoint;
  360. if ('points' in airway) {
  361. pointTypeFix = 'point_type';
  362. pointsFix = 'points';
  363. airRoutePoint = airway['points'];
  364. }
  365. else {
  366. pointTypeFix = 'pointType';
  367. pointsFix = 'airlines';
  368. airRoutePoint = airway['airlines'];
  369. }
  370. let crossPointBefore, crossPointAfter;
  371. // 如果前后是其他航线,那么找到交叉点作为前后的点
  372. if (pointBefore[pointTypeFix] == Global.pointTypes.line) {
  373. crossPointBefore = getCrossPoint(airRoutePoint, pointBefore[pointsFix]);
  374. }
  375. if (pointAfter[pointTypeFix] == Global.pointTypes.line) {
  376. crossPointAfter = getCrossPoint(airRoutePoint, pointAfter[pointsFix]);
  377. }
  378. for (let point of airRoutePoint) {
  379. if (pointCompare(crossPointBefore, point) || pointCompare(crossPointAfter, point)) {
  380. found++;
  381. points.push(Object.assign({}, point));
  382. continue;
  383. }
  384. if (found == 1) {
  385. points.push(Object.assign({}, point));
  386. }
  387. }
  388. if (!(points.length > 0 && found == 2)) {
  389. // 如果两个点不全在航线上面,那么画全部航线
  390. points = airRoutePoint;
  391. }
  392. return points;
  393. }
  394. function getLinesRouter(lineProps, lineAndMarkerStyle) {
  395. let coordinates = new Array();
  396. let markers = new Array();
  397. let lines = [];
  398. let { imageName, lineWidth, strokeColor } = getDefaultStyle();
  399. if (lineAndMarkerStyle) {
  400. imageName = lineAndMarkerStyle.imageName;
  401. lineWidth = lineAndMarkerStyle.lineWidth;
  402. strokeColor = lineAndMarkerStyle.strokeColor;
  403. }
  404. let startPoint, passPoints, endPoint, pointTypeFix, dataType, airlineWidth;
  405. if (lineProps.start_point) {
  406. let ll = lineProps;
  407. dataType = 2; // 下划线模式
  408. startPoint = ll['start_point'];
  409. passPoints = ll['passing_points'];
  410. endPoint = ll['end_point'];
  411. pointTypeFix = 'point_type';
  412. airlineWidth = parseInt(ll['airline_width'] + '', 10);
  413. }
  414. else {
  415. let ll = lineProps;
  416. dataType = 1; // 驼峰模式
  417. startPoint = ll['dep'];
  418. passPoints = ll['passPoints'];
  419. endPoint = ll['arrive'];
  420. pointTypeFix = 'pointType';
  421. airlineWidth = parseInt(ll['airlineWidth'] + '', 10);
  422. }
  423. if (startPoint) {
  424. coordinates.push(drawLineConfig(startPoint.lat, startPoint.lng, dataType));
  425. markers.push(addOvalPointConfig(startPoint.lat, startPoint.lng, imageName, dataType));
  426. }
  427. if (Array.isArray(passPoints)) {
  428. for (let i = 0; i < passPoints.length; i++) {
  429. let obj = passPoints[i];
  430. if (!isObject(obj)) { // 所有的 points/airway 都必须是 obj
  431. continue;
  432. }
  433. let pointType = obj[pointTypeFix];
  434. if (pointType == Global.pointTypes.point
  435. || pointType == Global.pointTypes.nav) {
  436. let pp = obj;
  437. coordinates.push(drawLineConfig(pp.lat, pp.lng, dataType));
  438. markers.push(addOvalPointConfig(pp.lat, pp.lng, imageName, dataType));
  439. }
  440. else {
  441. // 遇到一个航线,不需要和前前面的点连起来
  442. let pp = obj;
  443. if (coordinates.length > 1) {
  444. lines.push({ lineWidth, strokeColor, coordinates });
  445. }
  446. coordinates = [];
  447. const pointBefore = i == 0 ? startPoint : passPoints[i - 1];
  448. const pointAfter = i == passPoints.length - 1 ? (endPoint ? endPoint : passPoints[passPoints.length - 1]) : passPoints[i + 1];
  449. lines.push({ lineWidth, strokeColor, coordinates: getAirwayPoints(pp, pointBefore, pointAfter) });
  450. }
  451. }
  452. }
  453. if (endPoint) {
  454. coordinates.push(drawLineConfig(endPoint.lat, endPoint.lng, dataType));
  455. markers.push(addOvalPointConfig(endPoint.lat, endPoint.lng, imageName, dataType));
  456. }
  457. if (coordinates.length > 1) {
  458. lines.push({ lineWidth, strokeColor, coordinates });
  459. }
  460. if (airlineWidth > 0) {
  461. // 有宽度的空域,需要线周围多画宽度的多边形
  462. let polygons = processAirlineWidth(lines, airlineWidth);
  463. return { lines, markers, polygons };
  464. }
  465. else {
  466. return { lines, markers, polygons: [] };
  467. }
  468. }
  469. function getFixedLatLng(point) {
  470. let pp = point;
  471. let lat = pp.latitude ? pp.latitude : pp.lat;
  472. let lng = pp.longitude ? pp.longitude : pp.lng;
  473. // 复制计划的数据,有的点是度数模式如 38°35′17″
  474. if (isSafeString(lat)) {
  475. lat = latLngDegreesToDecimal(lat);
  476. lng = latLngDegreesToDecimal(lng);
  477. }
  478. return [lat, lng];
  479. }
  480. function processAirlineWidth(lines, airlineWidth) {
  481. let polygons = [];
  482. let { strokeColor, fillColor } = getDefaultStyle();
  483. for (let line of lines) {
  484. let points = line.coordinates;
  485. for (let i = 0; i < points.length - 1; i++) {
  486. let [lat1, lng1] = getFixedLatLng(points[i]);
  487. let [lat2, lng2] = getFixedLatLng(points[i + 1]);
  488. let point1 = new LatLon(lat1, lng1);
  489. let point2 = new LatLon(lat2, lng2);
  490. let coordinates = getCirclePoints(point1, point2, airlineWidth);
  491. polygons.push({ lineWidth: 1, strokeColor, fillColor, coordinates });
  492. }
  493. }
  494. return polygons;
  495. }
  496. function getCirclePoints(point1, point2, width) {
  497. let percision = 10; // 半圆处理为多边形的时候,半圆上取几个点
  498. let step = 180 / percision;
  499. let bearing = (360 + point1.bearingTo(point2) - 90) % 360; // 取正值
  500. let points = [];
  501. for (let diff = 0; diff <= 180; diff += step) {
  502. let point = point2.destinationPoint(width, bearing + diff);
  503. points.push({ lat: point.lat, lng: point.lon });
  504. }
  505. for (let diff = 180; diff <= 360; diff += step) {
  506. let point = point1.destinationPoint(width, bearing + diff);
  507. points.push({ lat: point.lat, lng: point.lon });
  508. }
  509. return points;
  510. }
  511. function myRound(num, digits) {
  512. if (digits == null)
  513. digits = 6; // 比较的精度,经纬度会被经过度分秒方式到浮点方式的转化
  514. return Math.round(num * Math.pow(10, digits)) / Math.pow(10, digits);
  515. }
  516. function getLinesPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  517. let retLines = [];
  518. let retMarkers = [];
  519. let retPolygons = [];
  520. if (!Array.isArray(airspaceInfos)) {
  521. return { lines: retLines, markers: retMarkers, polygons: retPolygons };
  522. }
  523. let lineStyle = setStyle('line');
  524. for (let i = 0; i < airspaceInfos.length; i++) {
  525. let tmpLine = airspaceInfos[i];
  526. let airspaceTypeFix;
  527. if (tmpLine.airspaceType)
  528. airspaceTypeFix = 'airspaceType';
  529. else
  530. airspaceTypeFix = 'airspace_type';
  531. if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) {
  532. let lineProps = tmpLine;
  533. let { lines, markers, polygons } = getLinesRouter(lineProps, lineStyle);
  534. retMarkers.push(...markers);
  535. retLines.push(...lines);
  536. retPolygons.push(...polygons);
  537. }
  538. }
  539. return { lines: retLines, markers: retMarkers, polygons: retPolygons };
  540. }
  541. function getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  542. return createSelector(airspaceInfos, () => setStyle, currentAirspaceIndex, getLinesPolygonsAndMarkers);
  543. }
  544. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  545. let coordinates = new Array();
  546. let markers = new Array();
  547. let { imageName, lineWidth, strokeColor, fillColor } = getDefaultStyle();
  548. if (polygonAndMarkerStyle) {
  549. imageName = polygonAndMarkerStyle.imageName;
  550. lineWidth = polygonAndMarkerStyle.lineWidth;
  551. strokeColor = polygonAndMarkerStyle.strokeColor;
  552. fillColor = polygonAndMarkerStyle.fillColor;
  553. }
  554. let pointsFix, dataType;
  555. if (polygonProps.points) {
  556. pointsFix = 'points';
  557. dataType = 2;
  558. }
  559. else {
  560. pointsFix = 'polygonPoints';
  561. dataType = 1; // 驼峰模式
  562. }
  563. if (Array.isArray(polygonProps[pointsFix])) {
  564. for (let obj of polygonProps[pointsFix]) {
  565. if (!obj) {
  566. continue;
  567. }
  568. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  569. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  570. }
  571. }
  572. let polygon = { lineWidth, strokeColor, fillColor, coordinates };
  573. return { markers, polygon };
  574. }
  575. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  576. let markers = [];
  577. let polygons = [];
  578. if (!Array.isArray(airspaceInfos)) {
  579. return { markers, polygons };
  580. }
  581. let polygonAndMarkerStyle = setStyle('polygon');
  582. for (let i = 0; i < airspaceInfos.length; i++) {
  583. let polygon = airspaceInfos[i];
  584. let airspaceTypeFix;
  585. if (polygon.airspaceType)
  586. airspaceTypeFix = 'airspaceType';
  587. else
  588. airspaceTypeFix = 'airspace_type';
  589. if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  590. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  591. markers.push(...retObj.markers);
  592. polygons.push(retObj.polygon);
  593. }
  594. }
  595. return { markers, polygons };
  596. }
  597. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  598. return createSelector(airspaceInfos, () => setStyle, currentAirspaceIndex, getPolygonsAndMarkers);
  599. }
  600. function getMarkers(circlesAndMarkers, polygonAndMarkers, lineAndMarkers) {
  601. let markers = [];
  602. if (circlesAndMarkers) {
  603. markers = [...circlesAndMarkers.markers];
  604. }
  605. if (polygonAndMarkers) {
  606. markers = [...markers, ...polygonAndMarkers.markers];
  607. }
  608. if (lineAndMarkers) {
  609. markers = [...markers, ...lineAndMarkers.markers];
  610. }
  611. return markers;
  612. }
  613. function getMarkerSelector(circlesAndMarkers, polygonAndMarkers, lineAndMarkers) {
  614. return createSelector(circlesAndMarkers, polygonAndMarkers, lineAndMarkers, getMarkers);
  615. }
  616. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  617. let regionPoints = new Array();
  618. for (let i = 0; i < circles.length; i++) {
  619. regionPoints.push(...getCircleRegions(circles[i]));
  620. }
  621. let lines = lineAndMarkers.lines;
  622. for (let i = 0; i < lines.length; i++) {
  623. regionPoints.push(...lines[i].coordinates);
  624. }
  625. let polygons = polygonAndMarkers.polygons;
  626. for (let i = 0; i < polygons.length; i++) {
  627. regionPoints.push(...polygons[i].coordinates);
  628. }
  629. return regionPoints;
  630. }
  631. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  632. return createSelector(circles, lineAndMarkers, polygonAndMarkers, getRegionPoints);
  633. }
  634. function getCircles(circlesAndMarkers) {
  635. return circlesAndMarkers.circles;
  636. }
  637. function getCircleSelector(circlesAndMarkers) {
  638. return createSelector(circlesAndMarkers, getCircles);
  639. }
  640. function getLines(lineAndMarker) {
  641. return lineAndMarker.lines;
  642. }
  643. function getLineSelector(lineAndMarker) {
  644. return createSelector(lineAndMarker, getLines);
  645. }
  646. function getPolygons(polygonAndMarkers, linePolygonsAndMarkers) {
  647. return [...polygonAndMarkers.polygons, ...linePolygonsAndMarkers.polygons];
  648. }
  649. function getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers) {
  650. return createSelector(polygonAndMarkers, linePolygonsAndMarkers, getPolygons);
  651. }
  652. let setStyle = (styles) => {
  653. if (!styles)
  654. return () => null;
  655. else
  656. return (shapeName) => styles[shapeName];
  657. };
  658. //获取selector
  659. export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
  660. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  661. let circlesAndMarkers = getCircleAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  662. let circles = getCircleSelector(circlesAndMarkers);
  663. let linePolygonsAndMarkers = getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  664. let lines = getLineSelector(linePolygonsAndMarkers);
  665. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  666. let polygons = getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers);
  667. let markers = getMarkerSelector(circlesAndMarkers, polygonAndMarkers, linePolygonsAndMarkers);
  668. let regionPoints = getRegionPointsSelector(circles, linePolygonsAndMarkers, polygonAndMarkers);
  669. return {
  670. markers,
  671. circles,
  672. lines,
  673. polygons,
  674. regionPoints
  675. };
  676. }
  677. //获取数组
  678. export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
  679. let { markers, polygons, circles, lines, regionPoints } = getShapesSelector(() => airspaceInfos, style, () => currentAirspaceIndex);
  680. return {
  681. markers: markers(airspaceInfos),
  682. circles: circles(airspaceInfos),
  683. lines: lines(airspaceInfos),
  684. polygons: polygons(airspaceInfos),
  685. regionPoints: regionPoints(airspaceInfos)
  686. };
  687. }
  688. // 总共 5 种格式, http://git.corp.brilliantaero.com/BA/Coco/issues/99#note_6358
  689. // 1. 全输出格式
  690. // 2. 简化格式
  691. // 3. 传真格式
  692. // 4. 用户端用的简化格式
  693. // 5. 极简格式
  694. function getHeight(height, unit, type) {
  695. let shortNum;
  696. const num = +height; // to number
  697. if (num >= 100) {
  698. shortNum = parseInt(num / 100 + '').toString();
  699. if (shortNum.length < 2) {
  700. shortNum = '0' + shortNum;
  701. }
  702. }
  703. let heightStandard = Global.heightStandardsById.get(unit);
  704. if (!heightStandard) {
  705. heightStandard = unit;
  706. }
  707. // 这里统一使用数字判断
  708. let standardUnit = Global.heightStandards.get(heightStandard);
  709. let heightDesc;
  710. switch (standardUnit) {
  711. case 1:
  712. heightDesc = ['H*真', '真高*米'];
  713. break;
  714. case 2:
  715. heightDesc = ['H*标(含以下)', '标高*米(含以下)'];
  716. break;
  717. case 3:
  718. heightDesc = ['H*真(含以下)', '真高*米(含以下)'];
  719. break;
  720. default:
  721. heightDesc = ['H*标', '标高*米'];
  722. }
  723. if (shortNum && (type == 1 || type == 2)) {
  724. // H02真,H02真(含以下)
  725. return heightDesc[0].replace('*', shortNum);
  726. }
  727. else {
  728. // 真高200米,真高200米(含以下)
  729. return heightDesc[1].replace('*', height + '');
  730. }
  731. }
  732. function getAirspaceName(airspaceInfo) {
  733. if (airspaceInfo.airspace_name) {
  734. const ai = airspaceInfo;
  735. return ai.airspace_name;
  736. }
  737. else {
  738. const ai = airspaceInfo;
  739. return ai.name;
  740. }
  741. }
  742. export function circleContent(airspaceInfo, type = 3) {
  743. if (type == 5)
  744. return getAirspaceName(airspaceInfo);
  745. if ('airspace_name' in airspaceInfo) {
  746. const lat = latLngDecimalToDegrees(airspaceInfo.center_point_of_flying.lat);
  747. const lng = latLngDecimalToDegrees(airspaceInfo.center_point_of_flying.lng);
  748. let content = [];
  749. let loc = `以${airspaceInfo.center_loc}`;
  750. if (type == 1 || type == 3)
  751. loc += `(E${lng}, N${lat})`;
  752. content.push(`${loc}为中心`);
  753. content.push(`半径${airspaceInfo.radius_of_flying}米`);
  754. content.push(getHeight(airspaceInfo.altitude, airspaceInfo.unit, type));
  755. if (airspaceInfo.note)
  756. content.push(`备注:${airspaceInfo.note}`);
  757. return content.join(',');
  758. }
  759. else {
  760. let content = [];
  761. let loc = `以${airspaceInfo.addr}`;
  762. if (type == 1 || type == 3)
  763. loc += `(E${airspaceInfo.lng}, N${airspaceInfo.lat})`;
  764. content.push(`${loc}为中心`);
  765. content.push(`半径${airspaceInfo.radius}米`);
  766. content.push(getHeight(airspaceInfo.height, airspaceInfo.heightStandard, type));
  767. if (airspaceInfo.note)
  768. content.push(`备注:${airspaceInfo.note}`);
  769. return content.join(',');
  770. }
  771. }
  772. function flyingCenter(item = {}) {
  773. if (item == {}) {
  774. return "";
  775. }
  776. const pp = item;
  777. return ("(E" + latLngDecimalToDegrees(pp.lng) +
  778. ', ' +
  779. "N" + latLngDecimalToDegrees(pp.lat) + ")");
  780. }
  781. export function lineContent(airspaceInfo, type = 3) {
  782. if (type == 5)
  783. return getAirspaceName(airspaceInfo);
  784. if ('airspace_name' in airspaceInfo) {
  785. let content = [];
  786. content.push(`${airspaceInfo.start_loc}`);
  787. if (type == 1 || type == 3)
  788. content.push(`${flyingCenter(airspaceInfo.start_point)}`);
  789. content.push(` - `);
  790. content.push(getHeight(airspaceInfo.start_point.altitude, airspaceInfo.start_point.unit, type));
  791. const passing_points = airspaceInfo.passing_points;
  792. if (Array.isArray(passing_points)) {
  793. for (let i = 0; i < passing_points.length; i++) {
  794. const obj = passing_points[i];
  795. if (obj.point_type == Global.pointTypes.point) {
  796. let pp = obj;
  797. const lat = latLngDecimalToDegrees(pp.lat);
  798. const lng = latLngDecimalToDegrees(pp.lng);
  799. content.push(` - ${pp.point_name}`);
  800. if (type == 1 || type == 3)
  801. content.push(`(E${lng}, N${lat})`);
  802. }
  803. else if (obj.point_type == Global.pointTypes.nav) {
  804. let pp = obj;
  805. const lat = latLngDecimalToDegrees(pp.lat);
  806. const lng = latLngDecimalToDegrees(pp.lng);
  807. content.push(` - ${pp.point_code}`);
  808. if (type == 1 || type == 3)
  809. content.push(`(E${lng}, N${lat})`);
  810. }
  811. else {
  812. let pp = obj;
  813. content.push(` - ${pp.air_route_code}`);
  814. }
  815. if (obj.altitude) {
  816. content.push(` - ${getHeight(obj.altitude, obj.unit, type)}`);
  817. }
  818. }
  819. }
  820. content.push(` - ${airspaceInfo.end_loc}`);
  821. if (type == 1 || type == 3)
  822. content.push(`${flyingCenter(airspaceInfo.end_point)}`);
  823. if (isSafeString(airspaceInfo.airline_width)) {
  824. content.push(`,宽度:${airspaceInfo.airline_width}米`);
  825. }
  826. if (isSafeString(airspaceInfo.note)) {
  827. content.push(`,备注: ${airspaceInfo.note}`);
  828. }
  829. let result = content.join("");
  830. return result;
  831. }
  832. else {
  833. let content = [];
  834. content.push(`${airspaceInfo.dep.addr}`);
  835. if (type == 1 || type == 3)
  836. content.push(`(E${airspaceInfo.dep.lng}, N${airspaceInfo.dep.lat})`);
  837. content.push(` - ${getHeight(airspaceInfo.dep.height, airspaceInfo.dep.heightStandard, type)}`);
  838. if (Array.isArray(airspaceInfo.passPoints)) {
  839. let length = airspaceInfo.passPoints.length;
  840. for (let i = 0; i < length; i++) {
  841. let obj = airspaceInfo.passPoints[i];
  842. if (obj.pointType == Global.pointTypes.point) {
  843. let pp = obj;
  844. content.push(` - ${pp.addr}`);
  845. if (type == 1 || type == 3)
  846. content.push(`(E${pp.lng}, N${pp.lat})`);
  847. }
  848. else if (obj.pointType == Global.pointTypes.nav) {
  849. let pp = obj;
  850. content.push(` - ${pp.pointCode}`);
  851. if (type == 1 || type == 3)
  852. content.push(`(E${pp.lng}, N${pp.lat})`);
  853. }
  854. else {
  855. let pp = obj;
  856. content.push(` - ${pp.airlineCode}`);
  857. }
  858. if (obj.height) {
  859. content.push(` - ${getHeight(obj.height, obj.heightStandard, type)}`);
  860. }
  861. }
  862. }
  863. content.push(` - ${airspaceInfo.arrive.addr}`);
  864. if (type == 1 || type == 3)
  865. content.push(`(E${airspaceInfo.arrive.lng}, N${airspaceInfo.arrive.lat})`);
  866. if (airspaceInfo.airlineWidth) {
  867. content.push(`,宽度:${airspaceInfo.airlineWidth}米`);
  868. }
  869. if (airspaceInfo.note)
  870. content.push(`,备注:${airspaceInfo.note}`);
  871. return content.join('');
  872. }
  873. }
  874. export function polygonContent(airspaceInfo, type = 3) {
  875. if (type == 5)
  876. return getAirspaceName(airspaceInfo);
  877. if ('airspace_name' in airspaceInfo) {
  878. let res = [];
  879. let points = airspaceInfo.points;
  880. for (let i = 0; i < points.length; i++) {
  881. let c = `${points[i].addr ? points[i].addr : ''}`;
  882. if (type == 1 || type == 3)
  883. c += `(E${latLngDecimalToDegrees(points[i].lng)}, N${latLngDecimalToDegrees(points[i].lat)})`;
  884. res.push(c);
  885. }
  886. let content = [res.join('、')];
  887. content.push(`${airspaceInfo.points.length}点连线范围内`);
  888. content.push(`,${getHeight(airspaceInfo.altitude, airspaceInfo.unit, type)}`);
  889. if (isSafeString(airspaceInfo.note)) {
  890. content.push(`,备注:${airspaceInfo.note}`);
  891. }
  892. return content.join('');
  893. }
  894. else {
  895. let content = [];
  896. let length = airspaceInfo.polygonPoints.length;
  897. for (let i = 0; i < length; i++) {
  898. let obj = airspaceInfo.polygonPoints[i];
  899. let c = `${obj.addr ? obj.addr : ''}`;
  900. if (type == 1 || type == 3)
  901. c += `(E${obj.lng}, N${obj.lat})`;
  902. content.push(c);
  903. }
  904. let cc = content.join('、') + `${length}点连线范围内`;
  905. cc += `,${getHeight(airspaceInfo.height, airspaceInfo.heightStandard, type)}`;
  906. if (airspaceInfo.note)
  907. cc += `,备注:${airspaceInfo.note}`;
  908. return cc;
  909. }
  910. }
  911. export { latLngDegreesToDecimal, latLngDecimalToDegrees };