index.js 11 KB

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