index.js 14 KB

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