index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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
  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. } else {
  154. dataType = 1 // 驼峰模式
  155. startPoint = lineProps['dep']
  156. passPoints = lineProps['passPoints']
  157. endPoint = lineProps['arrive']
  158. pointTypeFix = 'pointType'
  159. }
  160. if (startPoint) {
  161. coordinates.push(drawLineConfig(startPoint.lat, startPoint.lng, dataType));
  162. markers.push(addOvalPointConfig(startPoint.lat, startPoint.lng, imageName, dataType));
  163. }
  164. if (Array.isArray(passPoints)) {
  165. for (let i = 0; i < passPoints.length; i++) {
  166. let obj = passPoints[i]
  167. if (!isObject(obj)) { // 所有的 points/airway 都必须是 obj
  168. continue;
  169. }
  170. let pointType = obj[pointTypeFix]
  171. if ( pointType == Global.pointTypes.point
  172. || pointType == Global.pointTypes.nav) {
  173. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  174. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  175. } else {
  176. // 遇到一个航线,不需要和前前面的点连起来
  177. if (coordinates.length > 1) {
  178. lines.push({lineWidth, strokeColor, coordinates})
  179. }
  180. coordinates = []
  181. const pointBefore = i == 0 ? startPoint : passPoints[i - 1]
  182. const pointAfter = i == passPoints.length - 1 ? endPoint : passPoints[i + 1]
  183. lines.push({lineWidth, strokeColor, coordinates: getAirwayPoints(obj, pointBefore, pointAfter)})
  184. }
  185. }
  186. }
  187. if (endPoint) {
  188. coordinates.push(drawLineConfig(endPoint.lat, endPoint.lng, dataType));
  189. markers.push(addOvalPointConfig(endPoint.lat, endPoint.lng, imageName, dataType));
  190. }
  191. if (coordinates.length > 1) {
  192. lines.push({lineWidth, strokeColor, coordinates});
  193. }
  194. return { lines, markers };
  195. }
  196. function getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  197. let retLines = [];
  198. let retMarkers = [];
  199. if (!Array.isArray(airspaceInfos)) {
  200. return { lines: retLines, markers: retMarkers };
  201. }
  202. let lineStyle = setStyle('line');
  203. for (let i = 0; i < airspaceInfos.length; i++) {
  204. let tmpLine = airspaceInfos[i]
  205. let airspaceTypeFix;
  206. if (tmpLine.airspaceType)
  207. airspaceTypeFix = 'airspaceType';
  208. else
  209. airspaceTypeFix = 'airspace_type';
  210. if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) {
  211. let { lines, markers } = getLinesRouter(tmpLine, lineStyle);
  212. retMarkers.push(...markers);
  213. retLines.push(...lines);
  214. }
  215. }
  216. return { lines: retLines, markers: retMarkers };
  217. }
  218. function getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  219. return createSelector(
  220. airspaceInfos,
  221. () => setStyle,
  222. currentAirspaceIndex,
  223. getLinesAndMarkers
  224. );
  225. }
  226. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  227. let coordinates = new Array();
  228. let markers = new Array();
  229. let {imageName, lineWidth, strokeColor, fillColor} = getDefaultStyle()
  230. if (polygonAndMarkerStyle) {
  231. imageName = polygonAndMarkerStyle.imageName
  232. lineWidth = polygonAndMarkerStyle.lineWidth
  233. strokeColor = polygonAndMarkerStyle.strokeColor
  234. fillColor = polygonAndMarkerStyle.fillColor
  235. }
  236. let pointsFix, dataType;
  237. if (polygonProps.points) {
  238. pointsFix = 'points';
  239. dataType = 2
  240. } else {
  241. pointsFix = 'polygonPoints';
  242. dataType = 1 // 驼峰模式
  243. }
  244. if (Array.isArray(polygonProps[pointsFix])) {
  245. for (let obj of polygonProps[pointsFix]) {
  246. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  247. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  248. }
  249. }
  250. let polygon = {lineWidth, strokeColor, fillColor, coordinates};
  251. return { markers, polygon };
  252. }
  253. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  254. let markers = [];
  255. let polygons = [];
  256. if (!Array.isArray(airspaceInfos)) {
  257. return { markers, polygons };
  258. }
  259. let polygonAndMarkerStyle = setStyle('polygon');
  260. for (let i = 0; i < airspaceInfos.length; i++) {
  261. let polygon = airspaceInfos[i]
  262. let airspaceTypeFix;
  263. if (polygon.airspaceType)
  264. airspaceTypeFix = 'airspaceType';
  265. else
  266. airspaceTypeFix = 'airspace_type';
  267. if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  268. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  269. markers.push(...retObj.markers);
  270. polygons.push(retObj.polygon);
  271. }
  272. }
  273. return { markers, polygons };
  274. }
  275. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  276. return createSelector(
  277. airspaceInfos,
  278. () => setStyle,
  279. currentAirspaceIndex,
  280. getPolygonsAndMarkers
  281. );
  282. }
  283. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  284. let markers = [];
  285. if (polygonAndMarkers) {
  286. markers = [...polygonAndMarkers.markers]
  287. }
  288. if (lineAndMarkers) {
  289. markers = [...markers, ...lineAndMarkers.markers]
  290. }
  291. return markers
  292. }
  293. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  294. return createSelector(
  295. polygonAndMarkers,
  296. lineAndMarkers,
  297. getMarkers
  298. )
  299. }
  300. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  301. let regionPoints = new Array();
  302. for (let i = 0; i < circles.length; i++) {
  303. regionPoints.push(...getCircleRegions(circles[i]));
  304. }
  305. let lines = lineAndMarkers.lines;
  306. for (let i = 0; i < lines.length; i++) {
  307. regionPoints.push(...lines[i].coordinates);
  308. }
  309. let polygons = polygonAndMarkers.polygons;
  310. for (let i = 0; i < polygons.length; i++) {
  311. regionPoints.push(...polygons[i].coordinates);
  312. }
  313. return regionPoints;
  314. }
  315. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  316. return createSelector(
  317. circles,
  318. lineAndMarkers,
  319. polygonAndMarkers,
  320. getRegionPoints
  321. );
  322. }
  323. function getLines(lineAndMarker) {
  324. return lineAndMarker.lines;
  325. }
  326. function getLineSelector(lineAndMarker) {
  327. return createSelector(
  328. lineAndMarker,
  329. getLines
  330. );
  331. }
  332. function getPolygons(polygonAndMarkers) {
  333. return polygonAndMarkers.polygons;
  334. }
  335. function getPolygonSelector(polygonAndMarkers) {
  336. return createSelector(
  337. polygonAndMarkers,
  338. getPolygons
  339. );
  340. }
  341. let setStyle = (style) => {
  342. if (!style)
  343. return (shapeName) => { return null }
  344. else
  345. return (shapeName) => style[shapeName]
  346. }
  347. //获取selector
  348. export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
  349. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  350. let circles = getCircleSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  351. let lineAndMarkers = getLineAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  352. let lines = getLineSelector(lineAndMarkers);
  353. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  354. let polygons = getPolygonSelector(polygonAndMarkers);
  355. let markers = getMarkerSelector(polygonAndMarkers, lineAndMarkers);
  356. let regionPoints = getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers);
  357. return {
  358. markers,
  359. circles,
  360. lines,
  361. polygons,
  362. regionPoints
  363. }
  364. }
  365. //获取数组
  366. export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
  367. let {markers, polygons, circles, lines, regionPoints} = getShapesSelector((airspaceInfos)=>airspaceInfos, style, currentAirspaceIndex)
  368. return {
  369. markers: markers(airspaceInfos),
  370. circles: circles(airspaceInfos),
  371. lines: lines(airspaceInfos),
  372. polygons: polygons(airspaceInfos),
  373. regionPoints: regionPoints(airspaceInfos)
  374. }
  375. }