import { createSelector } from 'reselect'; import Global from './Common'; import { latLngDegreesToDecimal } from './Utils' export default class AmapShapesManager { _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 } } _getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getCircles ); } _isObject(arg) { return typeof arg === 'object' && arg !== null; } _hasLine(obj) { return (isObject(obj) && isSafeString(obj.addr) && isSafeString(obj.lat) && isSafeString(obj.lng)); } _hasPoint(obj) { return (isObject(obj) && isSafeString(obj.lat) && isSafeString(obj.lng)); } _drawLineConfig(lat, lng) { let coordinate = {}; coordinate.latitude = latLngDegreesToDecimal(lat); coordinate.longitude = latLngDegreesToDecimal(lng); return coordinate; } _addOvalPointConfig(lat, lng, imageName) { let coordinate = {}; coordinate.latitude = latLngDegreesToDecimal(lat); coordinate.longitude = latLngDegreesToDecimal(lng); let annotationConfig = {}; annotationConfig.coordinate = coordinate; annotationConfig.imageName = imageName; return annotationConfig } _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 }; } _getLinesRouter(lineProps, lineAndMarkerStyle) { let coordinates = new Array(); let markers = new Array(); let lines = [] if (this._hasLine(lineProps.dep)) { coordinates.push(this._drawLineConfig(lineProps.dep.lat, lineProps.dep.lng)); markers.push(this._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 (this._isObject(obj)) { continue; } if (obj.pointType == Global.pointTypes.point || obj.pointType == Global.pointTypes.nav) { coordinates.push(this._drawLineConfig(obj.lat, obj.lng)); markers.push(this._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(this._getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle)) } } } if (this._hasLine(lineProps.arrive)) { coordinates.push(this._drawLineConfig(lineProps.arrive.lat, lineProps.arrive.lng)); markers.push(this._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 }; } _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 } = this._getLinesRouter(tmpLine, lineStyle); retMarkers.push(...markers); retLines.push(...lines); } } return { lines: retLines, markers: retMarkers }; } _getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getLinesAndMarkers ); } _getPolygon = (polygonProps, polygonAndMarkerStyle) => { let coordinates = new Array(); let markers = new Array(); if (Array.isArray(polygonProps.polygonPoints)) { for (let obj of polygonProps.polygonPoints) { if (!this._hasPoint(obj)) { continue; } coordinates.push(this._drawLineConfig(obj.lat, obj.lng)); markers.push(this._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 }; } } _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 = this._getPolygon(polygon) markers.push(...retObj.markers); polygons.push(retObj.polygon); } } return { markers, polygons }; } _getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, _getPolygonsAndMarkers ); } _getMarkers(polygonAndMarkers,lineAndMarkers){ let markers = []; if (polygonAndMarkers) { markers = [...polygonAndMarkers.markers] } if (lineAndMarkers) { markers = [...markers, ...lineAndMarkers.markers] } return markers } _getMarkerSelector(polygonAndMarkers,lineAndMarkers) { return createSelector( polygonAndMarkers, lineAndMarkers, _getMarkers ) } static getShapesSelect(airspaceInfos, setStyle, currentAirspaceIndex) { currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1; let circles = this._getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex); let lineAndMarkers = this._getLineAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex); let polygonAndMarkers = this._getPolygonAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex); let markers = this._getMarkerSelector(polygonAndMarkers,lineAndMarkers); return { markers, circles, lineAndMarkers, polygonAndMarkers } } static getShapes(airspaceInfos, setStyle, currentAirspaceIndex) { currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1; let circles = this._getCircles(airspaceInfos, setStyle, currentAirspaceIndex); let lineAndMarkers = this._getLinesAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex); let polygonAndMarkers = this._getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex); let markers = this._getMarkers(polygonAndMarkers,lineAndMarkers); return { markers, circles, lineAndMarkers, polygonAndMarkers } } }