【问题标题】:How to merge two polygon features using topojson merge如何使用 topojson 合并合并两个多边形特征
【发布时间】:2020-04-05 15:47:40
【问题描述】:

我正在尝试使用 topojson 将两个多边形特征合并在一起。这两个多边形最初不包含我认为 topojson.merge 执行合并所需的相似/共享点。在执行拓扑之前,我遍历多边形并将共享点添加到需要它们的多边形中。生成的 FeatureCollection“集合”如下所示:

{
    "type": "FeatureCollection",
    "features": [
    {
        "type": "Feature",
        "properties":
        {
            "id": "area_1"
        },
        "geometry":
        {
            "type": "Polygon",
            "coordinates": [
                [
                    [148, 105],
                    [160, 105],
                    [160, 113],
                    [148, 113]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties":
        {
            "id": "area_2"
        },
        "geometry":
        {
            "type": "Polygon",
            "coordinates": [
                [
                    [143, 113],
                    [148, 113],
                    [160, 113],
                    [171, 113],
                    [171, 132],
                    [143, 132]
                ]
            ]
        }
    }]
} 

let topology = topojson.topology({ collection: collection });

得到的拓扑是:

{
    "type": "Topology",
    "objects":
    {
        "collection":
        {
            "type": "GeometryCollection",
            "geometries": [
            {
                "type": "Polygon",
                "arcs": [
                    [-1]
                ],
                "properties":
                {
                    "id": "area_1"
                }
            },
            {
                "type": "Polygon",
                "arcs": [
                    [-2]
                ],
                "properties":
                {
                    "id": "area_2"
                }
            }]
        }
    },
    "arcs": [
        [
            [
                [148, 105],
                [160, 105],
                [160, 113],
                [148, 113]
            ]
        ],
        [
            [
                [143, 113],
                [148, 113],
                [160, 113],
                [171, 113],
                [171, 132],
                [143, 132]
            ]
        ]
    ]
}

这是我如何调用合并: topojson.merge(topology, topology.objects.collection.geometries);

结果错误是: Uncaught TypeError: Cannot read property '0' of undefined - stitch.js:13

发生错误的代码行:

arcs.forEach(function (i, j) {
    var arc = topology.arcs[i < 0 ? ~i : i],
        t;

    if (arc.length < 3 && !arc[1][0] && !arc[1][1]) {
      t = arcs[++emptyIndex], arcs[emptyIndex] = i, arcs[j] = t;
    }
  });

似乎发生错误是因为代码正在寻找比它们存在的更远一步的弧...

我做错了什么?

【问题讨论】:

    标签: javascript geojson topojson


    【解决方案1】:

    v3.1.0 对我有用。

    topojson.merge(topology, topology.objects.collection.geometries)
    

    有关调用topojson.merge() 的示例,请参阅the tests

    使用来自 OP 的数据

    const collection = {
      "type": "FeatureCollection",
      "features": [{
          "type": "Feature",
          "properties": {
            "id": "area_1"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [148, 105],
                [160, 105],
                [160, 113],
                [148, 113]
              ]
            ]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "id": "area_2"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [143, 113],
                [148, 113],
                [160, 113],
                [171, 113],
                [171, 132],
                [143, 132]
              ]
            ]
          }
        }
      ]
    };
    
    const topology = topojson.topology({
      collection
    });
    
    const merged = topojson.merge(topology, topology.objects.collection.geometries);
    
    console.log(merged);
    <script src="https://unpkg.com/topojson-server@3.0.1/dist/topojson-server.min.js"></script>
    <script src="https://unpkg.com/topojson-client@3.1.0/dist/topojson-client.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 2018-03-09
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多