index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 getCircles(airspaceInfos, setStyle, currentAirspaceIndex) {
  17. let circles = [];
  18. if (!Array.isArray(airspaceInfos)) {
  19. return circles;
  20. }
  21. //通过该方法获取样式
  22. let circleStyle = setStyle('circle');
  23. for (let i = 0; i < airspaceInfos.length; i++) {
  24. let tmpCircle = airspaceInfos[i]
  25. let airspaceTypeFix;
  26. if (tmpCircle.airspaceType)
  27. airspaceTypeFix = 'airspaceType';
  28. else
  29. airspaceTypeFix = 'airspace_type';
  30. if (tmpCircle[airspaceTypeFix] == Global.airspaceType.circle && currentAirspaceIndex != i) {
  31. let coordinate = {};
  32. if (tmpCircle.center_point_of_flying) {
  33. coordinate.latitude = tmpCircle.center_point_of_flying.lat;
  34. coordinate.longitude = tmpCircle.center_point_of_flying.lng;
  35. } else {
  36. coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
  37. coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
  38. }
  39. let radius = tmpCircle.radius_of_flying;
  40. let circle = {
  41. lineWidth: circleStyle.lineWidth ? circleStyle.lineWidth : Global.amapLineWidth,
  42. strokeColor: circleStyle.strokeColor ? circleStyle.strokeColor : Global.amapStrokeColor,
  43. fillColor: circleStyle.fillColor ? circleStyle.fillColor : Global.amapFillColor,
  44. radius,
  45. coordinate
  46. }
  47. circles.push(circle);
  48. }
  49. }
  50. return circles;
  51. }
  52. function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  53. return createSelector(
  54. airspaceInfos,
  55. () => setStyle,
  56. currentAirspaceIndex,
  57. getCircles
  58. );
  59. }
  60. function getLatLng(latlng, dataType) {
  61. if(dataType == 1) { // 驼峰模式,新建计划的时候的格式
  62. return latLngDegreesToDecimal(latlng)
  63. } else {
  64. return latlng
  65. }
  66. }
  67. function drawLineConfig(lat, lng, dataType) {
  68. return {
  69. latitude: getLatLng(lat, dataType),
  70. longitude: getLatLng(lng, dataType)
  71. };
  72. }
  73. function addOvalPointConfig(lat, lng, imageName, dataType) {
  74. return {
  75. coordinate: {
  76. latitude: getLatLng(lat, dataType),
  77. longitude: getLatLng(lng, dataType)
  78. },
  79. imageName: imageName
  80. };
  81. }
  82. function getCrossPoint(points1, points2) {
  83. for(let point1 of points1) {
  84. for(let point2 of points2) {
  85. if (point1.point_id == point2.point_id)
  86. return point1
  87. }
  88. }
  89. }
  90. function getAirwayPoints(airway, pointBefore, pointAfter) {
  91. let found = 0
  92. let points = []
  93. let pointTypeFix, pointsFix
  94. if (points in airway) {
  95. pointTypeFix = 'point_type';
  96. pointsFix = 'points'
  97. } else {
  98. pointTypeFix = 'pointType'
  99. pointsFix = 'airlines'
  100. }
  101. // 如果前后是其他航线,那么找到交叉点作为前后的点
  102. if ( pointBefore[pointTypeFix] == Global.pointTypes.line ) {
  103. pointBefore = getCrossPoint(airway[pointsFix], pointBefore[pointsFix])
  104. }
  105. if(pointAfter[pointTypeFix] == Global.pointTypes.line) {
  106. pointAfter = getCrossPoint(airway[pointsFix], pointAfter[pointsFix])
  107. }
  108. for (let point of airway[pointsFix]) {
  109. if (pointBefore.point_id == point.point_id || pointAfter.point_id == point.point_id) {
  110. found++
  111. points.push(Object.assign({}, point))
  112. continue
  113. }
  114. if (found == 1) {
  115. points.push(Object.assign({}, point))
  116. }
  117. }
  118. if (!(points.length > 0 && found == 2)) {
  119. // 如果两个点不全在航线上面,那么画全部航线
  120. points = airway[pointsFix]
  121. }
  122. return points;
  123. }
  124. function getLinesRouter(lineProps, lineAndMarkerStyle) {
  125. let coordinates = new Array();
  126. let markers = new Array();
  127. let lines = []
  128. let imageName = 'BA_oval'
  129. let lineWidth = Global.amapLineWidth
  130. let strokeColor = Global.amapStrokeColor
  131. if (lineAndMarkerStyle) {
  132. imageName = lineAndMarkerStyle.imageName
  133. lineWidth = lineAndMarkerStyle.lineWidth
  134. strokeColor = lineAndMarkerStyle.strokeColor
  135. }
  136. let startPoint, passPoints, endPoint, pointTypeFix, dataType
  137. if (lineProps.start_point) {
  138. dataType = 2 // 下划线模式
  139. startPoint = lineProps['start_point']
  140. passPoints = lineProps['passing_points']
  141. endPoint = lineProps['end_point']
  142. pointTypeFix = 'point_type';
  143. } else {
  144. dataType = 1 // 驼峰模式
  145. startPoint = lineProps['dep']
  146. passPoints = lineProps['passPoints']
  147. endPoint = lineProps['arrive']
  148. pointTypeFix = 'pointType'
  149. }
  150. if (startPoint) {
  151. coordinates.push(drawLineConfig(startPoint.lat, startPoint.lng, dataType));
  152. markers.push(addOvalPointConfig(startPoint.lat, startPoint.lng, imageName, dataType));
  153. }
  154. if (Array.isArray(passPoints)) {
  155. for (let i = 0; i < passPoints.length; i++) {
  156. let obj = passPoints[i]
  157. if (!isObject(obj)) { // 所有的 points/airway 都必须是 obj
  158. continue;
  159. }
  160. let pointType = obj[pointTypeFix]
  161. if ( pointType == Global.pointTypes.point
  162. || pointType == Global.pointTypes.nav) {
  163. coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
  164. markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
  165. } else {
  166. // 遇到一个航线,不需要和前前面的点连起来
  167. if (coordinates.length > 1) {
  168. lines.push({lineWidth, strokeColor, coordinates})
  169. }
  170. coordinates = []
  171. const pointBefore = i == 0 ? startPoint : passPoints[i - 1]
  172. const pointAfter = i == passPoints.length - 1 ? endPoint : passPoints[i + 1]
  173. lines.push({lineWidth, strokeColor, coordinates: getAirwayPoints(obj, pointBefore, pointAfter)})
  174. }
  175. }
  176. }
  177. if (endPoint) {
  178. coordinates.push(drawLineConfig(endPoint.lat, endPoint.lng, dataType));
  179. markers.push(addOvalPointConfig(endPoint.lat, endPoint.lng, imageName, dataType));
  180. }
  181. if (coordinates.length > 1) {
  182. lines.push({lineWidth, strokeColor, coordinates});
  183. }
  184. return { lines, markers };
  185. }
  186. function getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  187. let retLines = [];
  188. let retMarkers = [];
  189. if (!Array.isArray(airspaceInfos)) {
  190. return { lines: retLines, markers: retMarkers };
  191. }
  192. let lineStyle = setStyle('line');
  193. for (let i = 0; i < airspaceInfos.length; i++) {
  194. let tmpLine = airspaceInfos[i]
  195. let airspaceTypeFix;
  196. if (tmpLine.airspaceType)
  197. airspaceTypeFix = 'airspaceType';
  198. else
  199. airspaceTypeFix = 'airspace_type';
  200. if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) {
  201. let { lines, markers } = getLinesRouter(tmpLine, lineStyle);
  202. retMarkers.push(...markers);
  203. retLines.push(...lines);
  204. }
  205. }
  206. return { lines: retLines, markers: retMarkers };
  207. }
  208. function getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  209. return createSelector(
  210. airspaceInfos,
  211. () => setStyle,
  212. currentAirspaceIndex,
  213. getLinesAndMarkers
  214. );
  215. }
  216. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  217. let coordinates = new Array();
  218. let markers = new Array();
  219. let pointsFix;
  220. if (polygonProps.points)
  221. pointsFix = 'points';
  222. else
  223. pointsFix = 'polygonPoints';
  224. if (Array.isArray(polygonProps[pointsFix])) {
  225. for (let obj of polygonProps[pointsFix]) {
  226. coordinates.push(pointsFix == 'polygonPoints' ? drawLineConfig(obj.lat, obj.lng) : { latitude: obj.lat, longitude: obj.lng });
  227. markers.push(pointsFix == 'polygonPoints' ? addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval') : { coordinate: { latitude: obj.lat, longitude: obj.lng }, imageName: polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval' });
  228. }
  229. }
  230. let polygon = {
  231. lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth,
  232. strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  233. fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor,
  234. coordinates: coordinates
  235. };
  236. return { markers, polygon };
  237. }
  238. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  239. let markers = [];
  240. let polygons = [];
  241. if (!Array.isArray(airspaceInfos)) {
  242. return { markers, polygons };
  243. }
  244. let polygonAndMarkerStyle = setStyle('polygon');
  245. for (let i = 0; i < airspaceInfos.length; i++) {
  246. let polygon = airspaceInfos[i]
  247. let airspaceTypeFix;
  248. if (polygon.airspaceType)
  249. airspaceTypeFix = 'airspaceType';
  250. else
  251. airspaceTypeFix = 'airspace_type';
  252. if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  253. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  254. markers.push(...retObj.markers);
  255. polygons.push(retObj.polygon);
  256. }
  257. }
  258. return { markers, polygons };
  259. }
  260. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  261. return createSelector(
  262. airspaceInfos,
  263. () => setStyle,
  264. currentAirspaceIndex,
  265. getPolygonsAndMarkers
  266. );
  267. }
  268. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  269. let markers = [];
  270. if (polygonAndMarkers) {
  271. markers = [...polygonAndMarkers.markers]
  272. }
  273. if (lineAndMarkers) {
  274. markers = [...markers, ...lineAndMarkers.markers]
  275. }
  276. return markers
  277. }
  278. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  279. return createSelector(
  280. polygonAndMarkers,
  281. lineAndMarkers,
  282. getMarkers
  283. )
  284. }
  285. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  286. let regionPoints = new Array();
  287. for (let i = 0; i < circles.length; i++) {
  288. regionPoints.push(getCircleRegions(circles[i]));
  289. }
  290. let lines = lineAndMarkers.lines;
  291. for (let i = 0; i < lines.length; i++) {
  292. regionPoints.push(lines[i].coordinates);
  293. }
  294. let polygons = polygonAndMarkers.polygons;
  295. for (let i = 0; i < polygons.length; i++) {
  296. regionPoints.push(polygons.coordinates);
  297. }
  298. return regionPoints;
  299. }
  300. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  301. return createSelector(
  302. circles,
  303. lineAndMarkers,
  304. polygonAndMarkers,
  305. getRegionPoints
  306. );
  307. }
  308. function getLines(lineAndMarker) {
  309. return lineAndMarker.lines;
  310. }
  311. function getLineSelector(lineAndMarker) {
  312. return createSelector(
  313. lineAndMarker,
  314. getLines
  315. );
  316. }
  317. function getPolygons(polygonAndMarkers) {
  318. return polygonAndMarkers.polygons;
  319. }
  320. function getPolygonSelector(polygonAndMarkers) {
  321. return createSelector(
  322. polygonAndMarkers,
  323. getPolygons
  324. );
  325. }
  326. let setStyle = (style) => {
  327. if (!style)
  328. return (shapeName) => { return null }
  329. else
  330. return (shapeName) => style[shapeName]
  331. }
  332. //获取selector
  333. export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
  334. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  335. let circles = getCircleSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  336. let lineAndMarkers = getLineAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  337. let lines = getLineSelector(lineAndMarkers);
  338. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  339. let polygons = getPolygonSelector(polygonAndMarkers);
  340. let markers = getMarkerSelector(polygonAndMarkers, lineAndMarkers);
  341. let regionPoints = getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers);
  342. return {
  343. markers,
  344. circles,
  345. lines,
  346. polygons,
  347. regionPoints
  348. }
  349. }
  350. //获取数组
  351. export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
  352. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  353. let circles = getCircles(airspaceInfos, setStyle(style), currentAirspaceIndex);
  354. let lineAndMarkers = getLinesAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
  355. let lines = getLines(lineAndMarkers);
  356. let polygonAndMarkers = getPolygonsAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
  357. let polygons = getPolygons(polygonAndMarkers);
  358. let markers = getMarkers(polygonAndMarkers, lineAndMarkers);
  359. let regionPoints = getRegionPoints(circles, lineAndMarkers, polygonAndMarkers);
  360. return {
  361. markers,
  362. circles,
  363. lines,
  364. polygons,
  365. regionPoints
  366. }
  367. }