基于二维数组中的坐标在地图中绘制多边形

时间:2014-11-14 11:05:48

标签: javascript arrays leaflet geojson

给出一个看起来像这样的二维数组(我将其绘制为网格):
    [0] [0] [0]的 [1] [1] [0] [0]的 [1] [1] [0]
    [0]的 [1] [1] [1] [1] [0]的 [1] [1] [1 ] [0]
    [0]的 [1] [1] [1] [1] [0] [0]的 [1] [1] [0 ]
    [0]的 [1] [1] [1] [1] [0] [0] [0] [0] [0]
    [0] [0] [0] [1] [1] [0] [0] [0] [0] [0]

我想提取大胆的1,并忽略那些斜体。我的1代表gps坐标,我想用它作为GeoJSON文档中MultiPolygon特征的输入。

现在我遍历每一行并创建MultiLineString,但这真的不是我想要的。

var previousValue = -1;
var line = [];
var lines = [];
var points = [];
for (var i = 0; i < data.length; i++) {
    var value = data[i];

    if (value == 0 || (previousValue > -1 && value != previousValue)) {
        // Line interrupted, push it to features and create a new
        if (line.length === 1) { // It's not a line, but a point
            //this.common.log.info("Push point");
            points.push(line);
        } else if (line.length === 2 && previousValue > -1) {
            lines.push(line);
        }
        line = [];
    }
    if (value > 0) {
        if (line.length == 0) {
            // Start of line
            line.push(value);
        } else if (line.length == 1) {
            // Line has only a starting point, add to end
            line.push(value);
        } else {
            // Line has already an endpoint, update it
            line[1] = value;
        }
    }
    previousValue = value;
}

我想有一个库吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试一种行进方块算法(如评论中所示),但我建议使用alpha形状。它是一个没有边缘超过alpha的delaunay三角剖分。行进立方体不适合凹形船体。