index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import { createSelector } from 'reselect';
  2. import Global from './Common';
  3. import {
  4. latLngDegreesToDecimal,
  5. isObject
  6. } from './Utils'
  7. let LatLon = require('geodesy').LatLonSpherical;
  8. function getCircleRegions(circle) {
  9. let latlon = new LatLon(circle.coordinate.latitude, circle.coordinate.longitude)
  10. let d1 = latlon.destinationPoint(circle.radius, 0)
  11. let d2 = latlon.destinationPoint(circle.radius, 90)
  12. let d3 = latlon.destinationPoint(circle.radius, 180)
  13. let d4 = latlon.destinationPoint(circle.radius, 270)
  14. 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 }]
  15. }
  16. function getDefaultStyle() {
  17. let imageName = 'BA_oval'
  18. let lineWidth = Global.amapLineWidth
  19. let strokeColor = Global.amapStrokeColor
  20. let fillColor = Global.amapFillColor
  21. return {imageName, lineWidth, strokeColor, fillColor}
  22. }
  23. function getCircles(airspaceInfos, setStyle, currentAirspaceIndex) {
  24. let circles = [];
  25. if (!Array.isArray(airspaceInfos)) {
  26. return circles;
  27. }
  28. let {lineWidth, strokeColor, fillColor} = getDefaultStyle()
  29. //通过该方法获取样式
  30. let circleStyle = setStyle('circle');
  31. if(circleStyle) {
  32. lineWidth = circleStyle.lineWidth
  33. strokeColor = circleStyle.strokeColor
  34. fillColor = circleStyle.fillColor
  35. }
  36. for (let i = 0; i < airspaceInfos.length; i++) {
  37. let tmpCircle = airspaceInfos[i]
  38. let airspaceTypeFix, radiusFix
  39. if (tmpCircle.airspaceType) {
  40. airspaceTypeFix = 'airspaceType';
  41. radiusFix = 'radius'
  42. } else {
  43. airspaceTypeFix = 'airspace_type';
  44. radiusFix = 'radius_of_flying'
  45. }
  46. if (tmpCircle[airspaceTypeFix] == Global.airspaceType.circle && currentAirspaceIndex != i) {
  47. let coordinate = {};
  48. if (tmpCircle.center_point_of_flying) {
  49. coordinate.latitude = tmpCircle.center_point_of_flying.lat;
  50. coordinate.longitude = tmpCircle.center_point_of_flying.lng;
  51. } else {
  52. coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
  53. coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
  54. }
  55. let radius = tmpCircle[radiusFix];
  56. let circle = {lineWidth, strokeColor, fillColor, radius, coordinate}
  57. circles.push(circle);
  58. }
  59. }
  60. return circles;
  61. }
  62. function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  63. return createSelector(
  64. airspaceInfos,
  65. () => setStyle,
  66. currentAirspaceIndex,
  67. getCircles
  68. );
  69. }
  70. function getLatLng(latlng, dataType) {
  71. if(dataType == 1) { // 驼峰模式,新建计划的时候的格式
  72. return latLngDegreesToDecimal(latlng)
  73. } else {
  74. return latlng
  75. }
  76. }
  77. function drawLineConfig(lat, lng, dataType) {
  78. return {
  79. latitude: getLatLng(lat, dataType),
  80. longitude: getLatLng(lng, dataType)
  81. };
  82. }
  83. function addOvalPointConfig(lat, lng, imageName, dataType) {
  84. return {
  85. coordinate: {
  86. latitude: getLatLng(lat, dataType),
  87. longitude: getLatLng(lng, dataType)
  88. },
  89. imageName: imageName
  90. };
  91. }
  92. function getCrossPoint(points1, points2) {
  93. for(let point1 of points1) {
  94. for(let point2 of points2) {
  95. if (point1.point_id == point2.point_id)
  96. return point1
  97. }
  98. }
  99. }
  100. function getAirwayPoints(airway, pointBefore, pointAfter) {
  101. let found = 0
  102. let points = []
  103. let pointTypeFix, pointsFix, pointIdFix
  104. if ('points' in airway) {
  105. pointTypeFix = 'point_type';
  106. pointsFix = 'points'
  107. pointIdFix = 'point_id'
  108. } else {
  109. pointTypeFix = 'pointType'
  110. pointsFix = 'airlines'
  111. pointIdFix = 'pointId'
  112. }
  113. // 如果前后是其他航线,那么找到交叉点作为前后的点
  114. if ( pointBefore[pointTypeFix] == Global.pointTypes.line ) {
  115. pointBefore = getCrossPoint(airway[pointsFix], pointBefore[pointsFix])
  116. }
  117. if(pointAfter[pointTypeFix] == Global.pointTypes.line) {
  118. pointAfter = getCrossPoint(airway[pointsFix], pointAfter[pointsFix])
  119. }
  120. for (let point of airway[pointsFix]) {
  121. if (pointBefore[pointIdFix] == point.point_id || pointAfter[pointIdFix] == point.point_id) {
  122. found++
  123. points.push(Object.assign({}, point))
  124. continue
  125. }
  126. if (found == 1) {
  127. points.push(Object.assign({}, point))
  128. }
  129. }
  130. if (!(points.length > 0 && found == 2)) {
  131. // 如果两个点不全在航线上面,那么画全部航线
  132. points = airway[pointsFix]
  133. }
  134. return points;
  135. }
  136. function getLinesRouter(lineProps, lineAndMarkerStyle) {
  137. let coordinates = new Array();
  138. let markers = new Array();
  139. let lines = []
  140. let {imageName, lineWidth, strokeColor} = getDefaultStyle()
  141. if (lineAndMarkerStyle) {
  142. imageName = lineAndMarkerStyle.imageName
  143. lineWidth = lineAndMarkerStyle.lineWidth
  144. strokeColor = lineAndMarkerStyle.strokeColor
  145. }
  146. let startPoint, passPoints, endPoint, pointTypeFix, dataType, airlineWidth
  147. if (lineProps.start_point) {
  148. dataType = 2 // 下划线模式
  149. startPoint = lineProps['start_point']
  150. passPoints = lineProps['passing_points']
  151. endPoint = lineProps['end_point']
  152. pointTypeFix = 'point_type';
  153. airlineWidth = parseInt(lineProps['airline_width'], 10)
  154. } else {
  155. dataType = 1 // 驼峰模式
  156. startPoint = lineProps['dep']
  157. passPoints = lineProps['passPoints']
  158. endPoint = lineProps['arrive']
  159. pointTypeFix = 'pointType'
  160. airlineWidth = parseInt(lineProps['airlineWidth'], 10)
  161. }
  162. if (startPoint) {
  163. coordinates.push(drawLineConfig(startPoint.lat, startPoint.lng, dataType));
  164. markers.push(addOvalPointConfig(startPoint.lat, startPoint.lng, imageName, dataType));
  165. }
  166. if (Array.isArray(passPoints)) {
  167. for (let i = 0; i < passPoints.length; i++) {
  168. let obj = passPoints[i]
  169. if (!isObject(obj)) { // 所有的 points/airway 都必须是 obj
  170. continue;
  171. }
  172. let pointType = obj[pointTypeFix]
  173. if ( pointType == Global.pointTypes.point
  174. || pointType == Global.pointTypes.nav) {
  175. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  176. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  177. } else {
  178. // 遇到一个航线,不需要和前前面的点连起来
  179. if (coordinates.length > 1) {
  180. lines.push({lineWidth, strokeColor, coordinates})
  181. }
  182. coordinates = []
  183. const pointBefore = i == 0 ? startPoint : passPoints[i - 1]
  184. const pointAfter = i == passPoints.length - 1 ? endPoint : passPoints[i + 1]
  185. lines.push({lineWidth, strokeColor, coordinates: getAirwayPoints(obj, pointBefore, pointAfter)})
  186. }
  187. }
  188. }
  189. if (endPoint) {
  190. coordinates.push(drawLineConfig(endPoint.lat, endPoint.lng, dataType));
  191. markers.push(addOvalPointConfig(endPoint.lat, endPoint.lng, imageName, dataType));
  192. }
  193. if (coordinates.length > 1) {
  194. lines.push({lineWidth, strokeColor, coordinates});
  195. }
  196. lines = combineLines(lines)
  197. if(airlineWidth > 0) {
  198. // 有宽度的空域,需要线周围多画宽度的多边形
  199. let polygons = processAirlineWidth(lines, airlineWidth)
  200. return { lines, markers, polygons };
  201. } else {
  202. return { lines, markers, polygons: [] };
  203. }
  204. }
  205. function combineLines(lines) {
  206. // 合并线段,如果前后两个线段之间有交接,那就合并成一个
  207. // 这样操作之后,lines 就只有不交接的线段了
  208. let combinedLines = []
  209. combinedLines.push(lines[0])
  210. for(let i=1; i<lines.length; i++) {
  211. let prevLine = lines[i-1]
  212. let line = lines[i]
  213. if(isLineConnect(prevLine, line)) {
  214. let lastIdx = combinedLines.length-1
  215. let lastLine = combinedLines[lastIdx]
  216. let points = line.coordinates.slice()
  217. points.splice(0, 1) //移除第一个点
  218. combinedLines[lastIdx].coordinates = [... lastLine.coordinates, ...points]
  219. } else {
  220. combinedLines.push(line)
  221. }
  222. }
  223. return combinedLines
  224. }
  225. function getFixedLatLng(point) {
  226. let lat = point.latitude ? point.latitude : point.lat
  227. let lng = point.longitude ? point.longitude : point.lng
  228. return [lat, lng]
  229. }
  230. function processAirlineWidth(lines, airlineWidth) {
  231. let polygons = []
  232. for(let line of lines) {
  233. let points = line.coordinates
  234. let coordinates = []
  235. let lastBearing = null
  236. for(let i=0; i<points.length; i++) {
  237. let [lat1, lng1] = getFixedLatLng(points[i])
  238. let point = new LatLon(lat1, lng1)
  239. let nextPoint = null
  240. let bearing = lastBearing
  241. if(i < points.length-1) {
  242. let [lat2, lng2] = getFixedLatLng(points[i+1])
  243. nextPoint = new LatLon(lat2, lng2)
  244. bearing = point.bearingTo(nextPoint)
  245. }
  246. let [leftPoint, rightPoint] = getBearingPoint(point, lastBearing, bearing, airlineWidth)
  247. coordinates.splice(0, 0, {lat: leftPoint.lat, lng: leftPoint.lon}) // add top
  248. coordinates.push({lat: rightPoint.lat, lng: rightPoint.lon}) // add bottom
  249. lastBearing = bearing
  250. }
  251. let {imageName, lineWidth, strokeColor, fillColor} = getDefaultStyle()
  252. polygons.push({lineWidth, strokeColor, fillColor, coordinates});
  253. }
  254. return polygons
  255. }
  256. function getBearingPoint(point, lastBearing, bearing, airlineWidth) {
  257. if(!lastBearing)
  258. lastBearing = bearing
  259. let halfAngle = (180 - bearing + lastBearing)/2 // 两条线的夹角的一半
  260. let rightBearing = bearing + halfAngle
  261. let leftBearing = 180 + rightBearing
  262. /* let fixAngle = Math.abs(halfAngle) > 180 ? Math.abs(halfAngle) : halfAngle
  263. * let leftWidth = airlineWidth/Math.sin(fixAngle)
  264. * let rightWidth = airlineWidth/Math.sin(fixAngle)
  265. * console.log('width is', airlineWidth, leftWidth, rightWidth, fixAngle, halfAngle) */
  266. let leftWidth = airlineWidth
  267. let rightWidth = airlineWidth
  268. let leftPoint = point.destinationPoint(leftWidth, leftBearing)
  269. let rightPoint = point.destinationPoint(rightWidth, rightBearing)
  270. return [leftPoint, rightPoint]
  271. }
  272. function isLineConnect(line1, line2) {
  273. // 判断 line1 的终点和 line2 的起点是否相同
  274. let line1LastPoint = line1.coordinates[line1.coordinates.length -1]
  275. let line2FirstPoint = line2.coordinates[0]
  276. let [line1lat, line1lng] = getFixedLatLng(line1LastPoint)
  277. let [line2lat, line2lng] = getFixedLatLng(line2FirstPoint)
  278. let precision = 9 // 比较的精度,经纬度会被经过度分秒方式到浮点方式的转化
  279. if(line1lat.toFixed(precision) == line2lat.toFixed(precision)
  280. && line1lng.toFixed(precision) == line2lng.toFixed(precision)) {
  281. return true
  282. } else {
  283. return false
  284. }
  285. }
  286. function getPolygonFromLine(line, airlineWidth) {
  287. }
  288. function getLinesPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  289. let retLines = [];
  290. let retMarkers = [];
  291. let retPolygons = [];
  292. if (!Array.isArray(airspaceInfos)) {
  293. return { lines: retLines, markers: retMarkers, polygons: retPolygons };
  294. }
  295. let lineStyle = setStyle('line');
  296. for (let i = 0; i < airspaceInfos.length; i++) {
  297. let tmpLine = airspaceInfos[i]
  298. let airspaceTypeFix;
  299. if (tmpLine.airspaceType)
  300. airspaceTypeFix = 'airspaceType';
  301. else
  302. airspaceTypeFix = 'airspace_type';
  303. if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) {
  304. let { lines, markers, polygons } = getLinesRouter(tmpLine, lineStyle);
  305. retMarkers.push(...markers);
  306. retLines.push(...lines);
  307. retPolygons.push(...polygons)
  308. }
  309. }
  310. return { lines: retLines, markers: retMarkers, polygons: retPolygons };
  311. }
  312. function getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  313. return createSelector(
  314. airspaceInfos,
  315. () => setStyle,
  316. currentAirspaceIndex,
  317. getLinesPolygonsAndMarkers
  318. );
  319. }
  320. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  321. let coordinates = new Array();
  322. let markers = new Array();
  323. let {imageName, lineWidth, strokeColor, fillColor} = getDefaultStyle()
  324. if (polygonAndMarkerStyle) {
  325. imageName = polygonAndMarkerStyle.imageName
  326. lineWidth = polygonAndMarkerStyle.lineWidth
  327. strokeColor = polygonAndMarkerStyle.strokeColor
  328. fillColor = polygonAndMarkerStyle.fillColor
  329. }
  330. let pointsFix, dataType;
  331. if (polygonProps.points) {
  332. pointsFix = 'points';
  333. dataType = 2
  334. } else {
  335. pointsFix = 'polygonPoints';
  336. dataType = 1 // 驼峰模式
  337. }
  338. if (Array.isArray(polygonProps[pointsFix])) {
  339. for (let obj of polygonProps[pointsFix]) {
  340. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  341. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  342. }
  343. }
  344. let polygon = {lineWidth, strokeColor, fillColor, coordinates};
  345. return { markers, polygon };
  346. }
  347. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  348. let markers = [];
  349. let polygons = [];
  350. if (!Array.isArray(airspaceInfos)) {
  351. return { markers, polygons };
  352. }
  353. let polygonAndMarkerStyle = setStyle('polygon');
  354. for (let i = 0; i < airspaceInfos.length; i++) {
  355. let polygon = airspaceInfos[i]
  356. let airspaceTypeFix;
  357. if (polygon.airspaceType)
  358. airspaceTypeFix = 'airspaceType';
  359. else
  360. airspaceTypeFix = 'airspace_type';
  361. if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  362. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  363. markers.push(...retObj.markers);
  364. polygons.push(retObj.polygon);
  365. }
  366. }
  367. return { markers, polygons };
  368. }
  369. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  370. return createSelector(
  371. airspaceInfos,
  372. () => setStyle,
  373. currentAirspaceIndex,
  374. getPolygonsAndMarkers
  375. );
  376. }
  377. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  378. let markers = [];
  379. if (polygonAndMarkers) {
  380. markers = [...polygonAndMarkers.markers]
  381. }
  382. if (lineAndMarkers) {
  383. markers = [...markers, ...lineAndMarkers.markers]
  384. }
  385. return markers
  386. }
  387. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  388. return createSelector(
  389. polygonAndMarkers,
  390. lineAndMarkers,
  391. getMarkers
  392. )
  393. }
  394. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  395. let regionPoints = new Array();
  396. for (let i = 0; i < circles.length; i++) {
  397. regionPoints.push(...getCircleRegions(circles[i]));
  398. }
  399. let lines = lineAndMarkers.lines;
  400. for (let i = 0; i < lines.length; i++) {
  401. regionPoints.push(...lines[i].coordinates);
  402. }
  403. let polygons = polygonAndMarkers.polygons;
  404. for (let i = 0; i < polygons.length; i++) {
  405. regionPoints.push(...polygons[i].coordinates);
  406. }
  407. return regionPoints;
  408. }
  409. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  410. return createSelector(
  411. circles,
  412. lineAndMarkers,
  413. polygonAndMarkers,
  414. getRegionPoints
  415. );
  416. }
  417. function getLines(lineAndMarker) {
  418. return lineAndMarker.lines;
  419. }
  420. function getLineSelector(lineAndMarker) {
  421. return createSelector(
  422. lineAndMarker,
  423. getLines
  424. );
  425. }
  426. function getPolygons(polygonAndMarkers, linePolygonsAndMarkers) {
  427. return [...polygonAndMarkers.polygons, ...linePolygonsAndMarkers.polygons];
  428. }
  429. function getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers) {
  430. return createSelector(
  431. polygonAndMarkers,
  432. linePolygonsAndMarkers,
  433. getPolygons
  434. );
  435. }
  436. let setStyle = (style) => {
  437. if (!style)
  438. return (shapeName) => { return null }
  439. else
  440. return (shapeName) => style[shapeName]
  441. }
  442. //获取selector
  443. export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
  444. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  445. let circles = getCircleSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  446. let linePolygonsAndMarkers = getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  447. let lines = getLineSelector(linePolygonsAndMarkers);
  448. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  449. let polygons = getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers);
  450. let markers = getMarkerSelector(polygonAndMarkers, linePolygonsAndMarkers);
  451. let regionPoints = getRegionPointsSelector(circles, linePolygonsAndMarkers, polygonAndMarkers);
  452. return {
  453. markers,
  454. circles,
  455. lines,
  456. polygons,
  457. regionPoints
  458. }
  459. }
  460. //获取数组
  461. export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
  462. let {markers, polygons, circles, lines, regionPoints} =
  463. getShapesSelector((airspaceInfos)=>airspaceInfos, style, (currentAirspaceIndex)=>currentAirspaceIndex)
  464. return {
  465. markers: markers(airspaceInfos),
  466. circles: circles(airspaceInfos),
  467. lines: lines(airspaceInfos),
  468. polygons: polygons(airspaceInfos),
  469. regionPoints: regionPoints(airspaceInfos)
  470. }
  471. }