index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
  38. coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
  39. let radius = tmpCircle.radius;
  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. if (hasLine(lineProps.dep)) {
  104. coordinates.push(drawLineConfig(lineProps.dep.lat, lineProps.dep.lng));
  105. markers.push(addOvalPointConfig(lineProps.dep.lat, lineProps.dep.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  106. }
  107. if (Array.isArray(lineProps.passPoints)) {
  108. for (let i = 0; i < lineProps.passPoints.length; i++) {
  109. let obj = lineProps.passPoints[i]
  110. if (isObject(obj)) {
  111. continue;
  112. }
  113. if (obj.pointType == Global.pointTypes.point
  114. || obj.pointType == Global.pointTypes.nav) {
  115. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  116. markers.push(addOvalPointConfig(obj.lat, obj.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  117. } else {
  118. // 遇到一个航线,不需要和前前面的点连起来
  119. if (coordinates.length > 1) {
  120. lines.push({
  121. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  122. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  123. coordinates: coordinates
  124. })
  125. }
  126. coordinates = []
  127. const pointBefore = i == 0 ? lineProps.dep : lineProps.passPoints[i - 1]
  128. const pointAfter = i == lineProps.passPoints.length - 1 ? lineProps.arrive : lineProps.passPoints[i + 1]
  129. lines.push(getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle))
  130. }
  131. }
  132. }
  133. if (hasLine(lineProps.arrive)) {
  134. coordinates.push(drawLineConfig(lineProps.arrive.lat, lineProps.arrive.lng));
  135. markers.push(addOvalPointConfig(lineProps.arrive.lat, lineProps.arrive.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  136. }
  137. if (coordinates.length > 1) {
  138. lines.push({
  139. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  140. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  141. coordinates: coordinates
  142. });
  143. }
  144. return { lines, markers };
  145. }
  146. function getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  147. let retLines = [];
  148. let retMarkers = [];
  149. if (!Array.isArray(airspaceInfos)) {
  150. return { lines: retLines, markers: retMarkers };
  151. }
  152. let lineStyle = setStyle('line');
  153. for (let i = 0; i < airspaceInfos.length; i++) {
  154. let tmpLine = airspaceInfos[i]
  155. let airspaceTypeFix;
  156. if (tmpCircle.airspaceType)
  157. airspaceTypeFix = 'airspaceType';
  158. else
  159. airspaceTypeFix = 'airspace_type';
  160. if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) {
  161. let { lines, markers } = getLinesRouter(tmpLine, lineStyle);
  162. retMarkers.push(...markers);
  163. retLines.push(...lines);
  164. }
  165. }
  166. return { lines: retLines, markers: retMarkers };
  167. }
  168. function getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  169. return createSelector(
  170. airspaceInfos,
  171. () => setStyle,
  172. currentAirspaceIndex,
  173. getLinesAndMarkers
  174. );
  175. }
  176. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  177. let coordinates = new Array();
  178. let markers = new Array();
  179. if (Array.isArray(polygonProps.polygonPoints)) {
  180. for (let obj of polygonProps.polygonPoints) {
  181. if (!hasPoint(obj)) {
  182. continue;
  183. }
  184. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  185. markers.push(addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval'));
  186. }
  187. }
  188. if (coordinates.length > 0) {
  189. let polygon = {
  190. lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth,
  191. strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  192. fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor,
  193. coordinates: coordinates
  194. };
  195. return { markers, polygon };
  196. }
  197. }
  198. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  199. let markers = [];
  200. let polygons = [];
  201. if (!Array.isArray(airspaceInfos)) {
  202. return { markers, polygons };
  203. }
  204. let polygonAndMarkerStyle = setStyle('polygon');
  205. for (let i = 0; i < airspaceInfos.length; i++) {
  206. let polygon = airspaceInfos[i]
  207. let airspaceTypeFix;
  208. if (tmpCircle.airspaceType)
  209. airspaceTypeFix = 'airspaceType';
  210. else
  211. airspaceTypeFix = 'airspace_type';
  212. if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  213. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  214. markers.push(...retObj.markers);
  215. polygons.push(retObj.polygon);
  216. }
  217. }
  218. return { markers, polygons };
  219. }
  220. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  221. return createSelector(
  222. airspaceInfos,
  223. () => setStyle,
  224. currentAirspaceIndex,
  225. getPolygonsAndMarkers
  226. );
  227. }
  228. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  229. let markers = [];
  230. if (polygonAndMarkers) {
  231. markers = [...polygonAndMarkers.markers]
  232. }
  233. if (lineAndMarkers) {
  234. markers = [...markers, ...lineAndMarkers.markers]
  235. }
  236. return markers
  237. }
  238. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  239. return createSelector(
  240. polygonAndMarkers,
  241. lineAndMarkers,
  242. getMarkers
  243. )
  244. }
  245. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  246. let regionPoints = new Array();
  247. for (let i = 0; i < circles.length; i++) {
  248. regionPoints.push(getCircleRegions(circles[i]));
  249. }
  250. let lines = lineAndMarkers.lines;
  251. for (let i = 0; i < lines.length; i++) {
  252. regionPoints.push(lines[i].coordinates);
  253. }
  254. let polygons = polygonAndMarkers.polygons;
  255. for (let i = 0; i < polygons.length; i++) {
  256. regionPoints.push(polygons.coordinates);
  257. }
  258. return regionPoints;
  259. }
  260. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  261. return createSelector(
  262. circles,
  263. lineAndMarkers,
  264. polygonAndMarkers,
  265. getRegionPoints
  266. );
  267. }
  268. function getLines(lineAndMarker) {
  269. return lineAndMarker.lines;
  270. }
  271. function getLineSelector(lineAndMarker) {
  272. return createSelector(
  273. lineAndMarker,
  274. getLines
  275. );
  276. }
  277. function getPolygons(polygonAndMarkers) {
  278. return polygonAndMarkers.polygons;
  279. }
  280. function getPolygonSelector(polygonAndMarkers) {
  281. return createSelector(
  282. polygonAndMarkers,
  283. getPolygons
  284. );
  285. }
  286. let setStyle = (style) => {
  287. return (shapeName) => style[shapeName]
  288. }
  289. //获取selector
  290. export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
  291. if (!style)
  292. style = {};
  293. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  294. let circles = getCircleSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  295. let lineAndMarkers = getLineAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  296. let lines = getLineSelector(lineAndMarkers);
  297. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex);
  298. let polygons = getPolygonSelector(polygonAndMarkers);
  299. let markers = getMarkerSelector(polygonAndMarkers, lineAndMarkers);
  300. let regionPoints = getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers);
  301. return {
  302. markers,
  303. circles,
  304. lines,
  305. polygons,
  306. regionPoints
  307. }
  308. }
  309. //获取数组
  310. export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
  311. if (!style)
  312. style = {};
  313. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  314. let circles = getCircles(airspaceInfos, setStyle(style), currentAirspaceIndex);
  315. let lineAndMarkers = getLinesAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
  316. let lines = getLines(lineAndMarkers);
  317. let polygonAndMarkers = getPolygonsAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
  318. let polygons = getPolygons(polygonAndMarkers);
  319. let markers = getMarkers(polygonAndMarkers, lineAndMarkers);
  320. let regionPoints = getRegionPoints(circles, lineAndMarkers, polygonAndMarkers);
  321. return {
  322. markers,
  323. circles,
  324. lines,
  325. polygons,
  326. regionPoints
  327. }
  328. }