index.js 13 KB

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