index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. function getCircles(airspaceInfos, setStyle, currentAirspaceIndex) {
  13. let circles = [];
  14. if (!Array.isArray(airspaceInfos)) {
  15. return { circles };
  16. }
  17. //通过该方法获取样式
  18. let circleStyle = setStyle('circle');
  19. for (let i = 0; i < airspaceInfos.length; i++) {
  20. let tmpCircle = airspaceInfos[i]
  21. if (tmpCircle.airspaceType == Global.airspaceType.circle && currentAirspaceIndex != i) {
  22. let coordinate = {};
  23. coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
  24. coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
  25. let radius = tmpCircle.radius;
  26. let circle = {
  27. lineWidth: circleStyle.lineWidth ? circleStyle.lineWidth : Global.amapLineWidth,
  28. strokeColor: circleStyle.strokeColor ? circleStyle.strokeColor : Global.amapStrokeColor,
  29. fillColor: circleStyle.fillColor ? circleStyle.fillColor : Global.amapFillColor,
  30. radius,
  31. coordinate
  32. }
  33. circles.push(circle);
  34. }
  35. }
  36. return circles;
  37. }
  38. function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  39. return createSelector(
  40. airspaceInfos,
  41. () => setStyle,
  42. currentAirspaceIndex,
  43. getCircles
  44. );
  45. }
  46. function drawLineConfig(lat, lng) {
  47. let coordinate = {};
  48. coordinate.latitude = latLngDegreesToDecimal(lat);
  49. coordinate.longitude = latLngDegreesToDecimal(lng);
  50. return coordinate;
  51. }
  52. function addOvalPointConfig(lat, lng, imageName) {
  53. let coordinate = {};
  54. coordinate.latitude = latLngDegreesToDecimal(lat);
  55. coordinate.longitude = latLngDegreesToDecimal(lng);
  56. let annotationConfig = {};
  57. annotationConfig.coordinate = coordinate;
  58. annotationConfig.imageName = imageName;
  59. return annotationConfig
  60. }
  61. function getAirwayLine(airway, pointBefore, pointAfter, lineAndMarkerStyle) {
  62. let found = 0
  63. let points = []
  64. for (let point of airway.points) {
  65. if (pointBefore.point_id == point.point_id || pointAfter.point_id == point.point_id) {
  66. found++
  67. points.push(Object.assign({}, point))
  68. continue
  69. }
  70. if (found == 1) {
  71. points.push(Object.assign({}, point))
  72. }
  73. }
  74. if (!(points.length > 0 && found == 2)) {
  75. // 如果两个点不全在航线上面,那么画全部航线
  76. points = airway.points
  77. }
  78. let line = {
  79. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  80. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  81. coordinates: points
  82. }
  83. return { line };
  84. }
  85. function getLinesRouter(lineProps, lineAndMarkerStyle) {
  86. let coordinates = new Array();
  87. let markers = new Array();
  88. let lines = []
  89. if (hasLine(lineProps.dep)) {
  90. coordinates.push(drawLineConfig(lineProps.dep.lat, lineProps.dep.lng));
  91. markers.push(addOvalPointConfig(lineProps.dep.lat, lineProps.dep.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  92. }
  93. if (Array.isArray(lineProps.passPoints)) {
  94. for (let i = 0; i < lineProps.passPoints.length; i++) {
  95. let obj = lineProps.passPoints[i]
  96. if (isObject(obj)) {
  97. continue;
  98. }
  99. if (obj.pointType == Global.pointTypes.point
  100. || obj.pointType == Global.pointTypes.nav) {
  101. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  102. markers.push(addOvalPointConfig(obj.lat, obj.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  103. } else {
  104. // 遇到一个航线,不需要和前前面的点连起来
  105. if (coordinates.length > 1) {
  106. lines.push({
  107. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  108. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  109. coordinates: coordinates
  110. })
  111. }
  112. coordinates = []
  113. const pointBefore = i == 0 ? lineProps.dep : lineProps.passPoints[i - 1]
  114. const pointAfter = i == lineProps.passPoints.length - 1 ? lineProps.arrive : lineProps.passPoints[i + 1]
  115. lines.push(getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle))
  116. }
  117. }
  118. }
  119. if (hasLine(lineProps.arrive)) {
  120. coordinates.push(drawLineConfig(lineProps.arrive.lat, lineProps.arrive.lng));
  121. markers.push(addOvalPointConfig(lineProps.arrive.lat, lineProps.arrive.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  122. }
  123. if (coordinates.length > 1) {
  124. lines.push({
  125. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  126. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  127. coordinates: coordinates
  128. });
  129. }
  130. return { lines, markers };
  131. }
  132. function getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  133. let retLines = [];
  134. let retMarkers = [];
  135. if (!Array.isArray(airspaceInfos)) {
  136. return { lines: retLines, markers: retMarkers };
  137. }
  138. let lineStyle = setStyle('line');
  139. for (let i = 0; i < airspaceInfos.length; i++) {
  140. let tmpLine = airspaceInfos[i]
  141. if (tmpLine.airspaceType == Global.airspaceType.line && currentAirspaceIndex != i) {
  142. let { lines, markers } = getLinesRouter(tmpLine, lineStyle);
  143. retMarkers.push(...markers);
  144. retLines.push(...lines);
  145. }
  146. }
  147. return { lines: retLines, markers: retMarkers };
  148. }
  149. function getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  150. return createSelector(
  151. airspaceInfos,
  152. () => setStyle,
  153. currentAirspaceIndex,
  154. getLinesAndMarkers
  155. );
  156. }
  157. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  158. let coordinates = new Array();
  159. let markers = new Array();
  160. if (Array.isArray(polygonProps.polygonPoints)) {
  161. for (let obj of polygonProps.polygonPoints) {
  162. if (!hasPoint(obj)) {
  163. continue;
  164. }
  165. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  166. markers.push(addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval'));
  167. }
  168. }
  169. if (coordinates.length > 0) {
  170. let polygon = {
  171. lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth,
  172. strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  173. fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor,
  174. coordinates: coordinates
  175. };
  176. return { markers, polygon };
  177. }
  178. }
  179. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  180. let markers = [];
  181. let polygons = [];
  182. if (!Array.isArray(airspaceInfos)) {
  183. return { markers, polygons };
  184. }
  185. let polygonAndMarkerStyle = setStyle('polygon');
  186. for (let i = 0; i < airspaceInfos.length; i++) {
  187. let polygon = airspaceInfos[i]
  188. if (polygon.airspaceType == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  189. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  190. markers.push(...retObj.markers);
  191. polygons.push(retObj.polygon);
  192. }
  193. }
  194. return { markers, polygons };
  195. }
  196. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  197. return createSelector(
  198. airspaceInfos,
  199. () => setStyle,
  200. currentAirspaceIndex,
  201. getPolygonsAndMarkers
  202. );
  203. }
  204. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  205. let markers = [];
  206. if (polygonAndMarkers) {
  207. markers = [...polygonAndMarkers.markers]
  208. }
  209. if (lineAndMarkers) {
  210. markers = [...markers, ...lineAndMarkers.markers]
  211. }
  212. return markers
  213. }
  214. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  215. return createSelector(
  216. polygonAndMarkers,
  217. lineAndMarkers,
  218. getMarkers
  219. )
  220. }
  221. export function getShapesSelect(airspaceInfos, setStyle, currentAirspaceIndex) {
  222. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  223. let circles = getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  224. let lineAndMarkers = getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  225. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  226. let markers = getMarkerSelector(polygonAndMarkers, lineAndMarkers);
  227. return {
  228. markers,
  229. circles,
  230. lineAndMarkers,
  231. polygonAndMarkers
  232. }
  233. }
  234. export function getShapes(airspaceInfos, setStyle, currentAirspaceIndex) {
  235. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  236. let circles = getCircles(airspaceInfos, setStyle, currentAirspaceIndex);
  237. let lineAndMarkers = getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex);
  238. let polygonAndMarkers = getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex);
  239. let markers = getMarkers(polygonAndMarkers, lineAndMarkers);
  240. return {
  241. markers,
  242. circles,
  243. lineAndMarkers,
  244. polygonAndMarkers
  245. }
  246. }