import { createSelector } from 'reselect'; import Global from './Common'; import { latLngDegreesToDecimal } from './Utils' function _getCircles(airspaceInfos, setStyle, currentAirspaceIndex) { let circles = []; if (!Array.isArray(airspaceInfos)) { return { circles }; } //通过该方法获取样式 let circleStyle = setStyle('circle'); for (let i = 0; i < airspaceInfos.length; i++) { let tmpCircle = airspaceInfos[i] if (tmpCircle.airspaceType == Global.airspaceType.circle && currentAirspaceIndex != i) { let coordinate = {}; coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat); coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng); let radius = tmpCircle.radius; let circle = { lineWidth: circleStyle.lineWidth ? circleStyle.lineWidth : Global.amapLineWidth, strokeColor: circleStyle.strokeColor ? circleStyle.strokeColor : Global.amapStrokeColor, fillColor: circleStyle.fillColor ? circleStyle.fillColor : Global.amapFillColor, radius, coordinate } circles.push(circle); } } return { circles } } function _getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getCircles ); } function _isObject(arg) { return typeof arg === 'object' && arg !== null; } function _hasLine(obj) { return (isObject(obj) && isSafeString(obj.addr) && isSafeString(obj.lat) && isSafeString(obj.lng)); } function _hasPoint(obj) { return (isObject(obj) && isSafeString(obj.lat) && isSafeString(obj.lng)); } function _drawLineConfig(lat, lng) { let coordinate = {}; coordinate.latitude = latLngDegreesToDecimal(lat); coordinate.longitude = latLngDegreesToDecimal(lng); return coordinate; } function _addOvalPointConfig(lat, lng, imageName) { let coordinate = {}; coordinate.latitude = latLngDegreesToDecimal(lat); coordinate.longitude = latLngDegreesToDecimal(lng); let annotationConfig = {}; annotationConfig.coordinate = coordinate; annotationConfig.imageName = imageName; return annotationConfig } function _getAirwayLine(airway, pointBefore, pointAfter, lineAndMarkerStyle) { let found = 0 let points = [] for (let point of airway.points) { if (pointBefore.point_id == point.point_id || pointAfter.point_id == point.point_id) { found++ points.push(Object.assign({}, point)) continue } if (found == 1) { points.push(Object.assign({}, point)) } } if (!(points.length > 0 && found == 2)) { // 如果两个点不全在航线上面,那么画全部航线 points = airway.points } let line = { lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth, strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor, coordinates: points } return { line }; } function _getLinesRouter(lineProps, lineAndMarkerStyle) { let coordinates = new Array(); let markers = new Array(); let lines = [] if (_hasLine(lineProps.dep)) { coordinates.push(_drawLineConfig(lineProps.dep.lat, lineProps.dep.lng)); markers.push(_addOvalPointConfig(lineProps.dep.lat, lineProps.dep.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval')); } if (Array.isArray(lineProps.passPoints)) { for (let i = 0; i < lineProps.passPoints.length; i++) { let obj = lineProps.passPoints[i] if (_isObject(obj)) { continue; } if (obj.pointType == Global.pointTypes.point || obj.pointType == Global.pointTypes.nav) { coordinates.push(_drawLineConfig(obj.lat, obj.lng)); markers.push(_addOvalPointConfig(obj.lat, obj.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval')); } else { // 遇到一个航线,不需要和前前面的点连起来 if (coordinates.length > 1) { lines.push({ lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth, strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor, coordinates: coordinates }) } coordinates = [] const pointBefore = i == 0 ? lineProps.dep : lineProps.passPoints[i - 1] const pointAfter = i == lineProps.passPoints.length - 1 ? lineProps.arrive : lineProps.passPoints[i + 1] lines.push(_getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle)) } } } if (_hasLine(lineProps.arrive)) { coordinates.push(_drawLineConfig(lineProps.arrive.lat, lineProps.arrive.lng)); markers.push(_addOvalPointConfig(lineProps.arrive.lat, lineProps.arrive.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval')); } if (coordinates.length > 1) { lines.push({ lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth, strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor, coordinates: coordinates }); } return { lines, markers }; } function _getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) { let retLines = []; let retMarkers = []; if (!Array.isArray(airspaceInfos)) { return { lines: retLines, markers: retMarkers }; } let lineStyle = setStyle('line'); for (let i = 0; i < airspaceInfos.length; i++) { let tmpLine = airspaceInfos[i] if (tmpLine.airspaceType == Global.airspaceType.line && currentAirspaceIndex != i) { let { lines, markers } = _getLinesRouter(tmpLine, lineStyle); retMarkers.push(...markers); retLines.push(...lines); } } return { lines: retLines, markers: retMarkers }; } function _getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getLinesAndMarkers ); } function _getPolygon(polygonProps, polygonAndMarkerStyle) { let coordinates = new Array(); let markers = new Array(); if (Array.isArray(polygonProps.polygonPoints)) { for (let obj of polygonProps.polygonPoints) { if (!_hasPoint(obj)) { continue; } coordinates.push(_drawLineConfig(obj.lat, obj.lng)); markers.push(_addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval')); } } if (coordinates.length > 0) { let polygon = { lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth, strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor, fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor, coordinates: coordinates }; return { markers, polygon }; } } function _getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) { let markers = []; let polygons = []; if (!Array.isArray(airspaceInfos)) { return { markers, polygons }; } let polygonAndMarkerStyle = setStyle('polygon'); for (let i = 0; i < airspaceInfos.length; i++) { let polygon = airspaceInfos[i] if (polygon.airspaceType == Global.flyingTypes.polygon && currentAirspaceIndex != i) { let retObj = _getPolygon(polygon) markers.push(...retObj.markers); polygons.push(retObj.polygon); } } return { markers, polygons }; } function _getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getPolygonsAndMarkers ); } function _getMarkers(polygonAndMarkers,lineAndMarkers){ let markers = []; if (polygonAndMarkers) { markers = [...polygonAndMarkers.markers] } if (lineAndMarkers) { markers = [...markers, ...lineAndMarkers.markers] } return markers } function _getMarkerSelector(polygonAndMarkers,lineAndMarkers) { return createSelector( polygonAndMarkers, lineAndMarkers, _getMarkers ) } export function getShapesSelect(airspaceInfos, setStyle, currentAirspaceIndex) { currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1; let circles = _getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex); let lineAndMarkers = _getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex); let polygonAndMarkers = _getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex); let markers = _getMarkerSelector(polygonAndMarkers,lineAndMarkers); return { markers, circles, lineAndMarkers, polygonAndMarkers } } export function getShapes(airspaceInfos, setStyle, currentAirspaceIndex) { currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1; let circles = _getCircles(airspaceInfos, setStyle, currentAirspaceIndex); let lineAndMarkers = _getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex); let polygonAndMarkers = _getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex); let markers = _getMarkers(polygonAndMarkers,lineAndMarkers); return { markers, circles, lineAndMarkers, polygonAndMarkers } }