index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. if (tmpCircle.airspaceType == Global.airspaceType.circle && currentAirspaceIndex != i) {
  31. let coordinate = {};
  32. coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
  33. coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
  34. let radius = tmpCircle.radius;
  35. let circle = {
  36. lineWidth: circleStyle.lineWidth ? circleStyle.lineWidth : Global.amapLineWidth,
  37. strokeColor: circleStyle.strokeColor ? circleStyle.strokeColor : Global.amapStrokeColor,
  38. fillColor: circleStyle.fillColor ? circleStyle.fillColor : Global.amapFillColor,
  39. radius,
  40. coordinate
  41. }
  42. circles.push(circle);
  43. }
  44. }
  45. return circles;
  46. }
  47. function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  48. return createSelector(
  49. airspaceInfos,
  50. () => setStyle,
  51. currentAirspaceIndex,
  52. getCircles
  53. );
  54. }
  55. function drawLineConfig(lat, lng) {
  56. let coordinate = {};
  57. coordinate.latitude = latLngDegreesToDecimal(lat);
  58. coordinate.longitude = latLngDegreesToDecimal(lng);
  59. return coordinate;
  60. }
  61. function addOvalPointConfig(lat, lng, imageName) {
  62. let coordinate = {};
  63. coordinate.latitude = latLngDegreesToDecimal(lat);
  64. coordinate.longitude = latLngDegreesToDecimal(lng);
  65. let annotationConfig = {};
  66. annotationConfig.coordinate = coordinate;
  67. annotationConfig.imageName = imageName;
  68. return annotationConfig
  69. }
  70. function getAirwayLine(airway, pointBefore, pointAfter, lineAndMarkerStyle) {
  71. let found = 0
  72. let points = []
  73. for (let point of airway.points) {
  74. if (pointBefore.point_id == point.point_id || pointAfter.point_id == point.point_id) {
  75. found++
  76. points.push(Object.assign({}, point))
  77. continue
  78. }
  79. if (found == 1) {
  80. points.push(Object.assign({}, point))
  81. }
  82. }
  83. if (!(points.length > 0 && found == 2)) {
  84. // 如果两个点不全在航线上面,那么画全部航线
  85. points = airway.points
  86. }
  87. let line = {
  88. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  89. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  90. coordinates: points
  91. }
  92. return { line };
  93. }
  94. function getLinesRouter(lineProps, lineAndMarkerStyle) {
  95. let coordinates = new Array();
  96. let markers = new Array();
  97. let lines = []
  98. if (hasLine(lineProps.dep)) {
  99. coordinates.push(drawLineConfig(lineProps.dep.lat, lineProps.dep.lng));
  100. markers.push(addOvalPointConfig(lineProps.dep.lat, lineProps.dep.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  101. }
  102. if (Array.isArray(lineProps.passPoints)) {
  103. for (let i = 0; i < lineProps.passPoints.length; i++) {
  104. let obj = lineProps.passPoints[i]
  105. if (isObject(obj)) {
  106. continue;
  107. }
  108. if (obj.pointType == Global.pointTypes.point
  109. || obj.pointType == Global.pointTypes.nav) {
  110. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  111. markers.push(addOvalPointConfig(obj.lat, obj.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  112. } else {
  113. // 遇到一个航线,不需要和前前面的点连起来
  114. if (coordinates.length > 1) {
  115. lines.push({
  116. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  117. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  118. coordinates: coordinates
  119. })
  120. }
  121. coordinates = []
  122. const pointBefore = i == 0 ? lineProps.dep : lineProps.passPoints[i - 1]
  123. const pointAfter = i == lineProps.passPoints.length - 1 ? lineProps.arrive : lineProps.passPoints[i + 1]
  124. lines.push(getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle))
  125. }
  126. }
  127. }
  128. if (hasLine(lineProps.arrive)) {
  129. coordinates.push(drawLineConfig(lineProps.arrive.lat, lineProps.arrive.lng));
  130. markers.push(addOvalPointConfig(lineProps.arrive.lat, lineProps.arrive.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval'));
  131. }
  132. if (coordinates.length > 1) {
  133. lines.push({
  134. lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
  135. strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  136. coordinates: coordinates
  137. });
  138. }
  139. return { lines, markers };
  140. }
  141. function getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  142. let retLines = [];
  143. let retMarkers = [];
  144. if (!Array.isArray(airspaceInfos)) {
  145. return { lines: retLines, markers: retMarkers };
  146. }
  147. let lineStyle = setStyle('line');
  148. for (let i = 0; i < airspaceInfos.length; i++) {
  149. let tmpLine = airspaceInfos[i]
  150. if (tmpLine.airspaceType == Global.airspaceType.line && currentAirspaceIndex != i) {
  151. let { lines, markers } = getLinesRouter(tmpLine, lineStyle);
  152. retMarkers.push(...markers);
  153. retLines.push(...lines);
  154. }
  155. }
  156. return { lines: retLines, markers: retMarkers };
  157. }
  158. function getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  159. return createSelector(
  160. airspaceInfos,
  161. () => setStyle,
  162. currentAirspaceIndex,
  163. getLinesAndMarkers
  164. );
  165. }
  166. function getPolygon(polygonProps, polygonAndMarkerStyle) {
  167. let coordinates = new Array();
  168. let markers = new Array();
  169. if (Array.isArray(polygonProps.polygonPoints)) {
  170. for (let obj of polygonProps.polygonPoints) {
  171. if (!hasPoint(obj)) {
  172. continue;
  173. }
  174. coordinates.push(drawLineConfig(obj.lat, obj.lng));
  175. markers.push(addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval'));
  176. }
  177. }
  178. if (coordinates.length > 0) {
  179. let polygon = {
  180. lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth,
  181. strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor,
  182. fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor,
  183. coordinates: coordinates
  184. };
  185. return { markers, polygon };
  186. }
  187. }
  188. function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
  189. let markers = [];
  190. let polygons = [];
  191. if (!Array.isArray(airspaceInfos)) {
  192. return { markers, polygons };
  193. }
  194. let polygonAndMarkerStyle = setStyle('polygon');
  195. for (let i = 0; i < airspaceInfos.length; i++) {
  196. let polygon = airspaceInfos[i]
  197. if (polygon.airspaceType == Global.airspaceType.polygon && currentAirspaceIndex != i) {
  198. let retObj = getPolygon(polygon, polygonAndMarkerStyle);
  199. markers.push(...retObj.markers);
  200. polygons.push(retObj.polygon);
  201. }
  202. }
  203. return { markers, polygons };
  204. }
  205. function getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  206. return createSelector(
  207. airspaceInfos,
  208. () => setStyle,
  209. currentAirspaceIndex,
  210. getPolygonsAndMarkers
  211. );
  212. }
  213. function getMarkers(polygonAndMarkers, lineAndMarkers) {
  214. let markers = [];
  215. if (polygonAndMarkers) {
  216. markers = [...polygonAndMarkers.markers]
  217. }
  218. if (lineAndMarkers) {
  219. markers = [...markers, ...lineAndMarkers.markers]
  220. }
  221. return markers
  222. }
  223. function getMarkerSelector(polygonAndMarkers, lineAndMarkers) {
  224. return createSelector(
  225. polygonAndMarkers,
  226. lineAndMarkers,
  227. getMarkers
  228. )
  229. }
  230. function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) {
  231. let regionPoints = new Array();
  232. for (let i = 0; i < circles.length; i++) {
  233. regionPoints.push(getCircleRegions(circles[i]));
  234. }
  235. if(Array.isArray(lineAndMarkers.lines)) {
  236. let lines = lineAndMarkers.lines;
  237. for (let i = 0; i < lines.length; i++) {
  238. regionPoints.push(lines[i].coordinates);
  239. }
  240. }
  241. if(Array.isArray(lineAndMarkers.polygons)) {
  242. let polygons = getPolygonsAndMarkers.polygons;
  243. for (let i = 0; i < polygons.length; i++) {
  244. regionPoints.push(polygons.coordinates);
  245. }
  246. }
  247. return regionPoints;
  248. }
  249. function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) {
  250. return createSelector(
  251. circles,
  252. lineAndMarkers,
  253. polygonAndMarkers,
  254. getRegionPoints
  255. );
  256. }
  257. function getLines(lineAndMarker) {
  258. return lineAndMarker.lines;
  259. }
  260. function getLineSelector(lineAndMarker) {
  261. return createSelector(
  262. lineAndMarker,
  263. getLines
  264. );
  265. }
  266. function getPolygons(polygonAndMarkers) {
  267. return polygonAndMarkers.polygons;
  268. }
  269. function getPolygonSelector(polygonAndMarkers) {
  270. return createSelector(
  271. polygonAndMarkers,
  272. getPolygons
  273. );
  274. }
  275. //获取selector
  276. export function getShapesSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
  277. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  278. let circles = getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  279. let lineAndMarkers = getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  280. let lines = getLineSelector(lineAndMarkers);
  281. let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex);
  282. let polygons = getPolygonSelector(polygonAndMarkers);
  283. let markers = getMarkerSelector(polygonAndMarkers, lineAndMarkers);
  284. let regionPoints = getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers);
  285. return {
  286. markers,
  287. circles,
  288. lines,
  289. polygons,
  290. regionPoints
  291. }
  292. }
  293. //获取数组
  294. export function getShapes(airspaceInfos, setStyle, currentAirspaceIndex) {
  295. currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
  296. let circles = getCircles(airspaceInfos, setStyle, currentAirspaceIndex);
  297. let lineAndMarkers = getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex);
  298. let lines = getLines(lineAndMarkers);
  299. let polygonAndMarkers = getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex);
  300. let polygons = getPolygons(getPolygonsAndMarkers);
  301. let markers = getMarkers(polygonAndMarkers, lineAndMarkers);
  302. let regionPoints = getRegionPoints(circles, lineAndMarkers, polygonAndMarkers);
  303. return {
  304. markers,
  305. circles,
  306. lines,
  307. polygons,
  308. regionPoints
  309. }
  310. }