Pārlūkot izejas kodu

Merge branch 'fix_airline' into 'master'

修复航线宽度的问题

See merge request BA/amap-shapes-generator!20
guohui.deng 7 gadi atpakaļ
vecāks
revīzija
4a23c7dd79
2 mainītis faili ar 77 papildinājumiem un 77 dzēšanām
  1. 38 0
      .eslintrc
  2. 39 77
      index.js

+ 38 - 0
.eslintrc

@@ -0,0 +1,38 @@
+{
+    "parser": "babel-eslint",
+    "env": {
+        "es6": true
+    },
+    "parserOptions": {
+        "ecmaVersion": 6,
+        "sourceType": "module",
+        "ecmaFeatures": {
+            "jsx": true
+        }
+    },
+    "globals": {
+        "require": false,
+        "module": false,
+        "setInterval": false,
+        "clearInterval": false,
+        "setTimeout": false,
+        "clearTimeout": false,
+        "console": false,
+        "fetch": false,
+        "navigator": false,
+        "await": false,
+        "__DEV__": false,
+        "navigation": false,
+        "FormData": false
+    },
+    "extends": [
+        "eslint:recommended",
+    ],
+    "rules": {
+        // overrides
+        "indent": ["error", 4, "SwitchCase": 1],
+        "no-trailing-spaces": 2,
+        "no-console": 0,
+        "comma-dangle": ["error", "never"]
+    }
+}

+ 39 - 77
index.js

@@ -107,18 +107,32 @@ function addOvalPointConfig(lat, lng, imageName, dataType) {
     };
 }
 
-function pointCompare(point1, point2) {
+function pointCompare(point1, point2, isStrict) {
     const pointId1 = point1.point_id || point1.pointId;
     const pointId2 = point2.point_id || point2.pointId;
 
-    return pointId1 == pointId2;
+    if(pointId1 == pointId2) {
+        return true;
+    } else if(isStrict) {
+        return false;
+    }
+
+    let [point1Lat, point1lng] = getFixedLatLng(point1)
+    let [point2lat, point2lng] = getFixedLatLng(point2)
+
+    if(myRound(point1Lat) == myRound(point2lat)
+       && myRound(point1lng) == myRound(point2lng)) {
+        return true
+    } else {
+        return false
+    }
 }
 
 
 function getCrossPoint(points1, points2) {
     for(let point1 of points1) {
         for(let point2 of points2) {
-            if (pointCompare(point1, point2))
+            if (pointCompare(point1, point2, true))
                 return point1
         }
     }
@@ -243,7 +257,6 @@ function getLinesRouter(lineProps, lineAndMarkerStyle) {
     if (coordinates.length > 1) {
         lines.push({lineWidth, strokeColor, coordinates});
     }
-    lines = combineLines(lines)
     if(airlineWidth > 0) {
         // 有宽度的空域,需要线周围多画宽度的多边形
         let polygons = processAirlineWidth(lines, airlineWidth)
@@ -253,27 +266,6 @@ function getLinesRouter(lineProps, lineAndMarkerStyle) {
     }
 }
 
-function combineLines(lines) {
-    // 合并线段,如果前后两个线段之间有交接,那就合并成一个
-    // 这样操作之后,lines 就只有不交接的线段了
-    let combinedLines = []
-    combinedLines.push(lines[0])
-    for(let i=1; i<lines.length; i++) {
-        let prevLine = lines[i-1]
-        let line = lines[i]
-        if(isLineConnect(prevLine, line)) {
-            let lastIdx = combinedLines.length-1
-            let lastLine = combinedLines[lastIdx]
-            let points = line.coordinates.slice()
-            points.splice(0, 1) //移除第一个点
-            combinedLines[lastIdx].coordinates = [... lastLine.coordinates, ...points]
-        } else {
-            combinedLines.push(line)
-        }
-    }
-    return combinedLines
-}
-
 function getFixedLatLng(point) {
     let lat = point.latitude ? point.latitude : point.lat
     let lng = point.longitude ? point.longitude : point.lng
@@ -282,53 +274,39 @@ function getFixedLatLng(point) {
 
 function processAirlineWidth(lines, airlineWidth) {
     let polygons = []
+    let {strokeColor, fillColor} = getDefaultStyle()
 
     for(let line of lines) {
         let points = line.coordinates
-        let coordinates = []
-        let lastBearing = null
-        for(let i=0; i<points.length; i++) {
+
+        for(let i=0; i<points.length-1; i++) {
             let [lat1, lng1] = getFixedLatLng(points[i])
-            let point = new LatLon(lat1, lng1)
-            let nextPoint = null
-            let bearing = lastBearing
-            if(i < points.length-1) {
-                let [lat2, lng2] = getFixedLatLng(points[i+1])
-                nextPoint = new LatLon(lat2, lng2)
-                bearing = point.bearingTo(nextPoint)
-            }
+            let [lat2, lng2] = getFixedLatLng(points[i+1])
+            let point1 = new LatLon(lat1, lng1)
+            let point2 = new LatLon(lat2, lng2)
 
-            let [leftPoint, rightPoint] = getBearingPoint(point, lastBearing, bearing, airlineWidth)
-            coordinates.splice(0, 0, {lat: leftPoint.lat, lng: leftPoint.lon}) // add top
-            coordinates.push({lat: rightPoint.lat, lng: rightPoint.lon}) // add bottom
-            lastBearing = bearing
+            let coordinates = getCirclePoints(point1, point2, airlineWidth)
+            polygons.push({lineWidth: 1, strokeColor, fillColor, coordinates})
         }
-
-        let {imageName, strokeColor, fillColor} = getDefaultStyle()
-        polygons.push({lineWidth: 1, strokeColor, fillColor, coordinates});
     }
     return polygons
 }
 
-function getBearingPoint(point, lastBearing, bearing, airlineWidth) {
-    if(!lastBearing)
-        lastBearing = bearing
-
-    let halfAngle = (180  + bearing - lastBearing)/2 // 两条线的夹角的一半,普通角度
-    let rightBearing = bearing - halfAngle //这里需要是真北角
-    let leftBearing = 180 + rightBearing
-
-    let leftWidth = airlineWidth
-    let rightWidth = airlineWidth
-    if(halfAngle != 90) { //直线上面是 90 度,不需要计算斜边
-        let angleToDecimal = halfAngle*Math.PI/180
-        leftWidth = Math.abs(airlineWidth/Math.sin(angleToDecimal))
-        rightWidth = Math.abs(airlineWidth/Math.sin(angleToDecimal))
+function getCirclePoints(point1, point2, width) {
+    let percision = 10 // 半圆处理为多边形的时候,半圆上取几个点
+    let step = 180/percision
+    let bearing = (360 + point1.bearingTo(point2) - 90) % 360 // 取正值
+    let points = []
+    for(let diff = 0; diff <= 180; diff += step) {
+        let point = point2.destinationPoint(width, bearing + diff)
+        points.push({lat: point.lat, lng: point.lon})
     }
 
-    let leftPoint = point.destinationPoint(leftWidth, leftBearing)
-    let rightPoint = point.destinationPoint(rightWidth, rightBearing)
-    return [leftPoint, rightPoint]
+    for(let diff = 180; diff <= 360; diff += step) {
+        let point = point1.destinationPoint(width, bearing + diff)
+        points.push({lat: point.lat, lng: point.lon})
+    }
+    return points
 }
 
 function myRound(num, digits) {
@@ -338,22 +316,6 @@ function myRound(num, digits) {
     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 = [];
@@ -533,7 +495,7 @@ function getPolygonSelector(polygonAndMarkers, linePolygonsAndMarkers) {
 
 let setStyle = (style) => {
     if (!style)
-        return (shapeName) => { return null }
+        return () => { return null }
     else
         return (shapeName) => style[shapeName]
 }