index.js 13 KB

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