index.js 13 KB

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