import { createSelector } from 'reselect'; import Global from './Common'; import { latLngDegreesToDecimal, latLngDecimalToDegrees, isObject, isSafeString } from './Utils' let LatLon = require('geodesy').LatLonSpherical; function getCircleRegions(circle) { let latlon = new LatLon(circle.coordinate.latitude, circle.coordinate.longitude) let d1 = latlon.destinationPoint(circle.radius, 0) let d2 = latlon.destinationPoint(circle.radius, 90) let d3 = latlon.destinationPoint(circle.radius, 180) let d4 = latlon.destinationPoint(circle.radius, 270) 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 }] } function getDefaultStyle() { let imageName = 'BA_oval' let lineWidth = Global.amapLineWidth let strokeColor = Global.amapStrokeColor let fillColor = Global.amapFillColor return {imageName, lineWidth, strokeColor, fillColor} } function getCircles(airspaceInfos, setStyle, currentAirspaceIndex) { let circles = []; if (!Array.isArray(airspaceInfos)) { return circles; } let {lineWidth, strokeColor, fillColor} = getDefaultStyle() //通过该方法获取样式 let circleStyle = setStyle('circle'); if(circleStyle) { lineWidth = circleStyle.lineWidth strokeColor = circleStyle.strokeColor fillColor = circleStyle.fillColor } for (let i = 0; i < airspaceInfos.length; i++) { let tmpCircle = airspaceInfos[i] let airspaceTypeFix, radiusFix if (tmpCircle.airspaceType) { airspaceTypeFix = 'airspaceType'; radiusFix = 'radius' } else { airspaceTypeFix = 'airspace_type'; radiusFix = 'radius_of_flying' } if (tmpCircle[airspaceTypeFix] == Global.airspaceType.circle && currentAirspaceIndex != i) { let coordinate = {}; if (tmpCircle.center_point_of_flying) { coordinate.latitude = tmpCircle.center_point_of_flying.lat; coordinate.longitude = tmpCircle.center_point_of_flying.lng; } else { coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat); coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng); } let radius = tmpCircle[radiusFix]; let circle = {lineWidth, strokeColor, fillColor, radius, coordinate} circles.push(circle); } } return circles; } function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, getCircles ); } function getLatLng(latlng, dataType) { if(dataType == 1) { // 驼峰模式,新建计划的时候的格式 return latLngDegreesToDecimal(latlng) } else { return latlng } } function drawLineConfig(lat, lng, dataType) { return { latitude: getLatLng(lat, dataType), longitude: getLatLng(lng, dataType) }; } function addOvalPointConfig(lat, lng, imageName, dataType) { return { coordinate: { latitude: getLatLng(lat, dataType), longitude: getLatLng(lng, dataType) }, imageName: imageName }; } function getCrossPoint(points1, points2) { for(let point1 of points1) { for(let point2 of points2) { if (point1.point_id == point2.point_id) return point1 } } } function getAirwayPoints(airway, pointBefore, pointAfter) { let found = 0 let points = [] let pointTypeFix, pointsFix, pointIdFix if ('points' in airway) { pointTypeFix = 'point_type'; pointsFix = 'points' pointIdFix = 'point_id' } else { pointTypeFix = 'pointType' pointsFix = 'airlines' pointIdFix = 'pointId' } // 如果前后是其他航线,那么找到交叉点作为前后的点 if ( pointBefore[pointTypeFix] == Global.pointTypes.line ) { pointBefore = getCrossPoint(airway[pointsFix], pointBefore[pointsFix]) } if(pointAfter[pointTypeFix] == Global.pointTypes.line) { pointAfter = getCrossPoint(airway[pointsFix], pointAfter[pointsFix]) } for (let point of airway[pointsFix]) { if (pointBefore[pointIdFix] == point.point_id || pointAfter[pointIdFix] == 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[pointsFix] } return points; } function getLinesRouter(lineProps, lineAndMarkerStyle) { let coordinates = new Array(); let markers = new Array(); let lines = [] let {imageName, lineWidth, strokeColor} = getDefaultStyle() if (lineAndMarkerStyle) { imageName = lineAndMarkerStyle.imageName lineWidth = lineAndMarkerStyle.lineWidth strokeColor = lineAndMarkerStyle.strokeColor } let startPoint, passPoints, endPoint, pointTypeFix, dataType, airlineWidth if (lineProps.start_point) { dataType = 2 // 下划线模式 startPoint = lineProps['start_point'] passPoints = lineProps['passing_points'] endPoint = lineProps['end_point'] pointTypeFix = 'point_type'; airlineWidth = parseInt(lineProps['airline_width'], 10) } else { dataType = 1 // 驼峰模式 startPoint = lineProps['dep'] passPoints = lineProps['passPoints'] endPoint = lineProps['arrive'] pointTypeFix = 'pointType' airlineWidth = parseInt(lineProps['airlineWidth'], 10) } if (startPoint) { coordinates.push(drawLineConfig(startPoint.lat, startPoint.lng, dataType)); markers.push(addOvalPointConfig(startPoint.lat, startPoint.lng, imageName, dataType)); } if (Array.isArray(passPoints)) { for (let i = 0; i < passPoints.length; i++) { let obj = passPoints[i] if (!isObject(obj)) { // 所有的 points/airway 都必须是 obj continue; } let pointType = obj[pointTypeFix] if ( pointType == Global.pointTypes.point || pointType == Global.pointTypes.nav) { coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType)); markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType)); } else { // 遇到一个航线,不需要和前前面的点连起来 if (coordinates.length > 1) { lines.push({lineWidth, strokeColor, coordinates}) } coordinates = [] const pointBefore = i == 0 ? startPoint : passPoints[i - 1] const pointAfter = i == passPoints.length - 1 ? endPoint : passPoints[i + 1] lines.push({lineWidth, strokeColor, coordinates: getAirwayPoints(obj, pointBefore, pointAfter)}) } } } if (endPoint) { coordinates.push(drawLineConfig(endPoint.lat, endPoint.lng, dataType)); markers.push(addOvalPointConfig(endPoint.lat, endPoint.lng, imageName, dataType)); } if (coordinates.length > 1) { lines.push({lineWidth, strokeColor, coordinates}); } lines = combineLines(lines) if(airlineWidth > 0) { // 有宽度的空域,需要线周围多画宽度的多边形 let polygons = processAirlineWidth(lines, airlineWidth) return { lines, markers, polygons }; } else { return { lines, markers, polygons: [] }; } } function combineLines(lines) { // 合并线段,如果前后两个线段之间有交接,那就合并成一个 // 这样操作之后,lines 就只有不交接的线段了 let combinedLines = [] combinedLines.push(lines[0]) for(let i=1; i 180 ? Math.abs(halfAngle) : halfAngle * let leftWidth = airlineWidth/Math.sin(fixAngle) * let rightWidth = airlineWidth/Math.sin(fixAngle) * console.log('width is', airlineWidth, leftWidth, rightWidth, fixAngle, halfAngle) */ let leftWidth = airlineWidth let rightWidth = airlineWidth let leftPoint = point.destinationPoint(leftWidth, leftBearing) let rightPoint = point.destinationPoint(rightWidth, rightBearing) return [leftPoint, rightPoint] } function myRound(num, digits) { if(digits == null) digits = 6 // 比较的精度,经纬度会被经过度分秒方式到浮点方式的转化 return Math.round(num * Math.pow(10, digits)) / Math.pow(10, digits) } function isLineConnect(line1, line2) { // 判断 line1 的终点和 line2 的起点是否相同 let line1LastPoint = line1.coordinates[line1.coordinates.length -1] let line2FirstPoint = line2.coordinates[0] let [line1lat, line1lng] = getFixedLatLng(line1LastPoint) let [line2lat, line2lng] = getFixedLatLng(line2FirstPoint) if(myRound(line1lat) == myRound(line2lat) && myRound(line1lng) == myRound(line2lng)) { return true } else { return false } } function getLinesPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) { let retLines = []; let retMarkers = []; let retPolygons = []; if (!Array.isArray(airspaceInfos)) { return { lines: retLines, markers: retMarkers, polygons: retPolygons }; } let lineStyle = setStyle('line'); for (let i = 0; i < airspaceInfos.length; i++) { let tmpLine = airspaceInfos[i] let airspaceTypeFix; if (tmpLine.airspaceType) airspaceTypeFix = 'airspaceType'; else airspaceTypeFix = 'airspace_type'; if (tmpLine[airspaceTypeFix] == Global.airspaceType.line && currentAirspaceIndex != i) { let { lines, markers, polygons } = getLinesRouter(tmpLine, lineStyle); retMarkers.push(...markers); retLines.push(...lines); retPolygons.push(...polygons) } } return { lines: retLines, markers: retMarkers, polygons: retPolygons }; } function getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle, currentAirspaceIndex) { return createSelector( airspaceInfos, () => setStyle, currentAirspaceIndex, getLinesPolygonsAndMarkers ); } function getPolygon(polygonProps, polygonAndMarkerStyle) { let coordinates = new Array(); let markers = new Array(); let {imageName, lineWidth, strokeColor, fillColor} = getDefaultStyle() if (polygonAndMarkerStyle) { imageName = polygonAndMarkerStyle.imageName lineWidth = polygonAndMarkerStyle.lineWidth strokeColor = polygonAndMarkerStyle.strokeColor fillColor = polygonAndMarkerStyle.fillColor } let pointsFix, dataType; if (polygonProps.points) { pointsFix = 'points'; dataType = 2 } else { pointsFix = 'polygonPoints'; dataType = 1 // 驼峰模式 } if (Array.isArray(polygonProps[pointsFix])) { for (let obj of polygonProps[pointsFix]) { coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType)); markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType)); } } let polygon = {lineWidth, strokeColor, fillColor, 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] let airspaceTypeFix; if (polygon.airspaceType) airspaceTypeFix = 'airspaceType'; else airspaceTypeFix = 'airspace_type'; if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) { let retObj = getPolygon(polygon, polygonAndMarkerStyle); 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 ) } function getRegionPoints(circles, lineAndMarkers, polygonAndMarkers) { let regionPoints = new Array(); for (let i = 0; i < circles.length; i++) { regionPoints.push(...getCircleRegions(circles[i])); } let lines = lineAndMarkers.lines; for (let i = 0; i < lines.length; i++) { regionPoints.push(...lines[i].coordinates); } let polygons = polygonAndMarkers.polygons; for (let i = 0; i < polygons.length; i++) { regionPoints.push(...polygons[i].coordinates); } return regionPoints; } function getRegionPointsSelector(circles, lineAndMarkers, polygonAndMarkers) { return createSelector( circles, lineAndMarkers, polygonAndMarkers, getRegionPoints ); } function getLines(lineAndMarker) { return lineAndMarker.lines; } function getLineSelector(lineAndMarker) { return createSelector( lineAndMarker, getLines ); } function getPolygons(polygonAndMarkers, linePolygonsAndMarkers) { return [...polygonAndMarkers.polygons, ...linePolygonsAndMarkers.polygons]; } function getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers) { return createSelector( polygonAndMarkers, linePolygonsAndMarkers, getPolygons ); } let setStyle = (style) => { if (!style) return (shapeName) => { return null } else return (shapeName) => style[shapeName] } //获取selector export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) { currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1; let circles = getCircleSelector(airspaceInfos, setStyle(style), currentAirspaceIndex); let linePolygonsAndMarkers = getLinePolygonsAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex); let lines = getLineSelector(linePolygonsAndMarkers); let polygonAndMarkers = getPolygonAndMarkerSelector(airspaceInfos, setStyle(style), currentAirspaceIndex); let polygons = getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers); let markers = getMarkerSelector(polygonAndMarkers, linePolygonsAndMarkers); let regionPoints = getRegionPointsSelector(circles, linePolygonsAndMarkers, polygonAndMarkers); return { markers, circles, lines, polygons, regionPoints } } //获取数组 export function getShapes(airspaceInfos, style, currentAirspaceIndex) { let {markers, polygons, circles, lines, regionPoints} = getShapesSelector((airspaceInfos)=>airspaceInfos, style, (currentAirspaceIndex)=>currentAirspaceIndex) return { markers: markers(airspaceInfos), circles: circles(airspaceInfos), lines: lines(airspaceInfos), polygons: polygons(airspaceInfos), regionPoints: regionPoints(airspaceInfos) } } // 总共 5 种格式, http://git.corp.brilliantaero.com/BA/Coco/issues/99#note_6358 // 1. 全输出格式 // 2. 简化格式 // 3. 传真格式 // 4. 用户端用的简化格式 // 5. 极简格式 function getHeight(num, unit, type) { let shortNum if(num >= 100) { shortNum = parseInt(num/100).toString() if(shortNum.length <2) { shortNum = '0' + shortNum } } let heightStandard = Global.heightStandardsById.get(unit) if(!heightStandard) { heightStandard = unit } // 这里统一使用数字判断 let standardUnit = Global.heightStandards.get(heightStandard) let heightDesc switch(standardUnit) { case 1: heightDesc = ['H*真', '真高*米'] break; case 2: heightDesc = ['H*标(含以下)', '标高*米(含以下)'] break; case 3: heightDesc = ['H*真(含以下)', '标高*米(含以下)'] break; default: heightDesc = ['H*标', '标高*米'] } if(shortNum && (type == 1 || type == 2)) { // H02真,H02真(含以下) return heightDesc[0].replace('*', shortNum) } else { // 真高200米,真高200米(含以下) return heightDesc[1].replace('*', num) } } function getAirspaceName(airspaceInfo) { if(airspaceInfo.airspace_name) { return airspaceInfo.airspace_name } else { return airspaceInfo.name } } export function circleContent(airspaceInfo, type=3) { if(type == 5) return getAirspaceName(airspaceInfo) if('airspace_name' in airspaceInfo) { const lat = latLngDecimalToDegrees(airspaceInfo.center_point_of_flying.lat); const lng = latLngDecimalToDegrees(airspaceInfo.center_point_of_flying.lng); let content = []; let loc = `以${airspaceInfo.center_loc}` if(type == 1 || type == 3) loc += `(E${lng}, N${lat})`; content.push(`${loc}为中心`) content.push(`半径${airspaceInfo.radius_of_flying}米`); content.push(getHeight(airspaceInfo.altitude, airspaceInfo.unit, type)) if (airspaceInfo.note) content.push(`备注:${airspaceInfo.note}`) let result = content = content.join(','); return result; } else { let content = [] let loc = `以${airspaceInfo.addr}` if(type == 1 || type == 3) loc += `(E${airspaceInfo.lng}, N${airspaceInfo.lat})`; content.push(`${loc}为中心`) content.push(`半径${airspaceInfo.radius}米`); content.push(getHeight(airspaceInfo.height, airspaceInfo.heightStandard, type)) if (airspaceInfo.note) content.push(`备注:${airspaceInfo.note}`) content = content.join(','); return content; } } function flyingCenter(item = {}){ if(item == {}){ return ; } else { return ( "(E" + latLngDecimalToDegrees(item.lng) + ', ' + "N" + latLngDecimalToDegrees(item.lat) + ")" ); } } export function lineContent(airspaceInfo, type=3) { if(type == 5) return getAirspaceName(airspaceInfo) if('airspace_name' in airspaceInfo) { let content = []; content.push(`${airspaceInfo.start_loc}`) if(type == 1 || type == 3) content.push(`${flyingCenter(airspaceInfo.start_point)}`) content.push(` - `) content.push(getHeight(airspaceInfo.start_point.altitude, airspaceInfo.start_point.unit, type)) const passing_points = airspaceInfo.passing_points; if(Array.isArray(passing_points)) { for(let i = 0; i < passing_points.length; i++) { const obj = passing_points[i]; const lat = latLngDecimalToDegrees(obj.lat) const lng = latLngDecimalToDegrees(obj.lng) if (obj.point_type == Global.pointTypes.point) { content.push(` - ${obj.point_name}`) if(type == 1 || type == 3) content.push(`(E${lng}, N${lat})`) } else if (obj.point_type == Global.pointTypes.nav) { content.push(` - ${obj.point_code}`) if(type == 1 || type == 3) content.push(`(E${lng}, N${lat})`) } else { content.push(` - ${obj.air_route_code}`) } if(obj.altitude) { content.push(` - ${getHeight(obj.altitude, obj.unit, type)}`) } } } content.push(` - ${airspaceInfo.end_loc}`) if(type == 1 || type == 3) content.push(`${flyingCenter(airspaceInfo.start_point)}`) if(isSafeString(airspaceInfo.airline_width)) { content.push(`,宽度:${airspaceInfo.airline_width}米`) } if(isSafeString(airspaceInfo.note)) { content.push(`,备注: ${airspaceInfo.note}`) } let result = content.join("") return result; } else { let content = []; content.push(`${airspaceInfo.dep.addr}`) if(type == 1 || type == 3) content.push(`(E${airspaceInfo.dep.lng}, N${airspaceInfo.dep.lat})`) content.push(` - ${getHeight(airspaceInfo.dep.height, airspaceInfo.dep.heightStandard, type)}`); if (Array.isArray(airspaceInfo.passPoints)) { let length = airspaceInfo.passPoints.length; for (let i = 0; i < length; i++) { let obj = airspaceInfo.passPoints[i]; if (obj.pointType == Global.pointTypes.point) { content.push(` - ${obj.addr}`) if(type == 1 || type == 3) content.push(`(E${obj.lng}, N${obj.lat})`); } else if (obj.pointType == Global.pointTypes.nav) { content.push(` - ${obj.pointCode}`) if(type == 1 || type == 3) content.push(`(E${obj.lng}, N${obj.lat})`); } else { content.push(` - ${obj.airlineCode}`); } if(obj.height) { content.push(` - ${getHeight(obj.height, obj.heightStandard, type)}`) } } } content.push(` - ${airspaceInfo.arrive.addr}`) if(type == 1 || type == 3) content.push(`(E${airspaceInfo.arrive.lng}, N${airspaceInfo.arrive.lat})`); if(airspaceInfo.airlineWidth) { content.push(`,宽度:${airspaceInfo.airlineWidth}米`) } if (airspaceInfo.note) content.push(`,备注:${airspaceInfo.note}`) content = content.join(''); return content; } } export function polygonContent(airspaceInfo, type=3) { if(type == 5) return getAirspaceName(airspaceInfo) if('airspace_name' in airspaceInfo) { let res = [] let points = airspaceInfo.points for (let i = 0; i < points.length; i++) { let c = `${points[i].addr ? points[i].addr : ''}` if(type == 1 || type == 3) c += `(E${latLngDecimalToDegrees(points[i].lng)}, N${latLngDecimalToDegrees(points[i].lat)})`; res.push(c) } let content = [res.join('、')] content.push(`${airspaceInfo.points.length}点连线范围内`) content.push(`, ${getHeight(airspaceInfo.altitude, airspaceInfo.unit, type)}`) if(isSafeString(airspaceInfo.note)) { content.push(`,备注:${airspaceInfo.note}`) } return content.join(''); } else { let content = []; let length = airspaceInfo.polygonPoints.length; for (let i = 0; i < length; i++) { let obj = airspaceInfo.polygonPoints[i]; let c = `${obj.addr ? obj.addr : ''}` c += `(E${obj.lng}, N${obj.lat})` content.push(c) } content = content.join('、') + `${length}点连线范围内,${airspaceInfo.heightStandard}${airspaceInfo.height}米` if (airspaceInfo.note) content = `${content},备注:${airspaceInfo.note}` return content; } }