index.js 11 KB

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