index.js 15 KB

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