소스 검색

Merge branch 'refactor' into 'master'

修复一下问题顺便重构一下

See merge request BA/amap-shapes-generator!6
haoxinlei 7 년 전
부모
커밋
5d1113add3
1개의 변경된 파일78개의 추가작업 그리고 74개의 파일을 삭제
  1. 78 74
      index.js

+ 78 - 74
index.js

@@ -62,32 +62,43 @@ function getCircleSelector(airspaceInfos, setStyle, currentAirspaceIndex) {
     );
 }
 
-
-function drawLineConfig(lat, lng) {
-    let coordinate = {};
-    coordinate.latitude = latLngDegreesToDecimal(lat);
-    coordinate.longitude = latLngDegreesToDecimal(lng);
-    return coordinate;
+function getLatLng(latlng, dataType) {
+    if(dataType == 1) { // 驼峰模式,新建计划的时候的格式
+        return latLngDegreesToDecimal(latlng)
+    } else {
+        return latlng
+    }
 }
 
-function addOvalPointConfig(lat, lng, imageName) {
-    let coordinate = {};
-    coordinate.latitude = latLngDegreesToDecimal(lat);
-    coordinate.longitude = latLngDegreesToDecimal(lng);
-
-    let annotationConfig = {};
-    annotationConfig.coordinate = coordinate;
-    annotationConfig.imageName = imageName;
+function drawLineConfig(lat, lng, dataType) {
+    return {
+        latitude: getLatLng(lat, dataType),
+        longitude: getLatLng(lng, dataType)
+    };
+}
 
-    return annotationConfig
+function addOvalPointConfig(lat, lng, imageName, dataType) {
+    return {
+        coordinate: {
+            latitude: getLatLng(lat, dataType),
+            longitude: getLatLng(lng, dataType)
+        },
+        imageName: imageName
+    };
 }
 
 
-function getAirwayLine(airway, pointBefore, pointAfter, lineAndMarkerStyle) {
+function getAirwayPoints(airway, pointBefore, pointAfter) {
     let found = 0
     let points = []
 
-    for (let point of airway.points) {
+    let airwayPoints
+    if (points in airway)
+        airwayPoints = airway.points
+    else
+        airwayPoints = airway.airlines
+
+    for (let point of airwayPoints) {
         if (pointBefore.point_id == point.point_id || pointAfter.point_id == point.point_id) {
             found++
             points.push(Object.assign({}, point))
@@ -101,15 +112,10 @@ function getAirwayLine(airway, pointBefore, pointAfter, lineAndMarkerStyle) {
 
     if (!(points.length > 0 && found == 2)) {
         // 如果两个点不全在航线上面,那么画全部航线
-        points = airway.points
+        points = airwayPoints
     }
 
-    let line = {
-        lineWidth: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
-        strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
-        coordinates: points
-    }
-    return { line };
+    return points;
 }
 
 
@@ -119,76 +125,74 @@ function getLinesRouter(lineProps, lineAndMarkerStyle) {
     let markers = new Array();
     let lines = []
 
-    let startPointFix;
-    if (lineProps.start_point)
-        startPointFix = 'start_point';
-    else
-        startPointFix = 'dep';
-    if (lineProps[startPointFix]) {
-        coordinates.push(startPointFix == 'dep' ? drawLineConfig(lineProps[startPointFix].lat, lineProps[startPointFix].lng) : { latitude: lineProps[startPointFix].lat, longitude: lineProps[startPointFix].lng });
-        markers.push(startPointFix == 'dep' ? addOvalPointConfig(lineProps[startPointFix].lat, lineProps[startPointFix].lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval') : { coordinate: { latitude: lineProps[startPointFix].lat, longitude: lineProps[startPointFix].lng }, imageName: lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval' });
+    let imageName = 'BA_oval'
+    let lineWidth =  Global.amapLineWidth
+    let strokeColor = Global.amapStrokeColor
+
+    if (lineAndMarkerStyle) {
+        imageName = lineAndMarkerStyle.imageName
+        lineWidth = lineAndMarkerStyle.lineWidth
+        strokeColor = lineAndMarkerStyle.strokeColor
     }
 
-    let passingPointsFix;
-    if (lineProps.passing_points)
-        passingPointsFix = 'passing_points';
-    else
-        passingPointsFix = 'passPoints';
+    let startPoint, passPoints, endPoint, pointTypeFix, dataType
 
-    let endPointFix;
-    if (lineProps.end_point)
-        endPointFix = 'end_point';
-    else
-        endPointFix = 'arrive'
+    if (lineProps.start_point) {
+        dataType = 2 // 下划线模式
+
+        startPoint = lineProps['start_point']
+        passPoints = lineProps['passing_points']
+        endPoint = lineProps['end_point']
+        pointTypeFix = 'point_type';
+    } else {
+        dataType = 1 // 驼峰模式
 
-    if (Array.isArray(lineProps[passingPointsFix])) {
-        for (let i = 0; i < lineProps[passingPointsFix].length; i++) {
-            let obj = lineProps[passingPointsFix][i]
-            if (isObject(obj)) {
+        startPoint = lineProps['dep']
+        passPoints = lineProps['passPoints']
+        endPoint = lineProps['arrive']
+        pointTypeFix = 'pointType'    
+    }
+
+    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 pointTypeFix;
-            if (obj.point_type)
-                pointTypeFix = 'point_type';
-            else
-                pointTypeFix = 'pointType'
-
-            if (obj[pointTypeFix] == Global.pointTypes.point
-                || obj[pointTypeFix] == Global.pointTypes.nav) {
-                coordinates.push(pointTypeFix == 'pointType' ? drawLineConfig(obj.lat, obj.lng) : { latitude: obj.lat, longitude: obj.lng });
-                markers.push(pointTypeFix == 'pointType' ? addOvalPointConfig(obj.lat, obj.lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval') : { coordinate: { latitude: obj.lat, longitude: obj.lng }, imageName: lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval' });
+            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: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
-                        strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
-                        coordinates: coordinates
-                    })
+                    lines.push({lineWidth, strokeColor, coordinates})
                 }
 
                 coordinates = []
 
-                const pointBefore = i == 0 ? lineProps[startPointFix] : lineProps[passingPointsFix][i - 1]
-                const pointAfter = i == lineProps[passingPointsFix].length - 1 ? lineProps[endPointFix] : lineProps[passingPointsFix][i + 1]
+                const pointBefore = i == 0 ? startPoint : passPoints[i - 1]
+                const pointAfter = i == passPoints.length - 1 ? endPoint : passPoints[i + 1]
 
-                lines.push(getAirwayLine(obj, pointBefore, pointAfter, lineAndMarkerStyle))
+                lines.push({lineWidth, strokeColor, coordinates: getAirwayPoints(obj, pointBefore, pointAfter)})
             }
         }
     }
 
-    if (lineProps[endPointFix]) {
-        coordinates.push(endPointFix == 'arrive' ? drawLineConfig(lineProps[endPointFix].lat, lineProps[endPointFix].lng) : { latitude: lineProps[endPointFix].lat, longitude: lineProps[endPointFix].lng });
-        markers.push(endPointFix == 'arrive' ? addOvalPointConfig(lineProps[endPointFix].lat, lineProps[endPointFix].lng, lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval') : { coordinate: { latitude: lineProps[endPointFix].lat, longitude: lineProps[endPointFix].lng }, imageName: lineAndMarkerStyle.imageName ? lineAndMarkerStyle.imageName : 'BA_oval' });
+    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: lineAndMarkerStyle.lineWidth ? lineAndMarkerStyle.lineWidth : Global.amapLineWidth,
-            strokeColor: lineAndMarkerStyle.strokeColor ? lineAndMarkerStyle.strokeColor : Global.amapStrokeColor,
-            coordinates: coordinates
-        });
+        lines.push({lineWidth, strokeColor, coordinates});
     }
 
     return { lines, markers };
@@ -363,7 +367,7 @@ function getPolygonSelector(polygonAndMarkers) {
 
 let setStyle = (style) => {
     if (!style)
-        return (shapeName) => { return { key: 0 } }
+        return (shapeName) => { return null }
     else
         return (shapeName) => style[shapeName]
 }
@@ -403,4 +407,4 @@ export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
         polygons,
         regionPoints
     }
-}
+}