【问题标题】:Converting a list of JSON polygon coordinates to Long/Lat arrays in GeoJSON format将 JSON 多边形坐标列表转换为 GeoJSON 格式的 Long/Lat 数组
【发布时间】:2019-06-24 13:46:13
【问题描述】:

我正在从 REST API 获取数据,这会产生数据集列表。其中一些数据集包含我想在地图上投影的坐标。为了投影坐标,我需要将 JSON 输出转换为 GeoJSON。虽然大部分数据的转换都很顺利。我与包含 x 和 y 坐标的长数组作斗争。它是 4+ 个点组合成多边形(第 5 个端点 = 缺少起点)。

如何将这个具有 4+ XY 坐标的数组转换为正确的格式,同时添加最后一个应该与第一个点匹配的数组?

我曾考虑为每个点将数组分割成新的数组,但这会导致超过 20 个点的多边形工作量过多。

JSON 示例:

"footprint": {
        "epsgId": 4326,
        "data": [
          [
            5.785569964298996,
            50.94215924789526,
            5.934953425435474,
            50.94154873163077,
            5.9341556116101595,
            50.87413533708443,
            5.784989651500041,
            50.87474468292546
          ]
        ],
      },

这是转换成 GeoJSON 后的样子。这是我自己写下来的,但我需要一个 JavaScript 代码在循环结果时自动执行此操作。

    {
      "type": "Feature",
      "properties": {
        "name": "name"
        "id" : "id"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [5.785569964298996, 50.94215924789526],
            [5.934953425435474, 50.94154873163077],
            [5.9341556116101595, 50.87413533708443],
            [5.784989651500041, 50.87474468292546],
            [5.785569964298996, 50.94215924789526]
          ]
        ]
      }
    }

【问题讨论】:

    标签: javascript json geojson


    【解决方案1】:

    我认为这个功能会为你完成工作。

    d = {"footprint": {
            "epsgId": 4326,
            "data": [
              [
                5.785569964298996,
                50.94215924789526,
                5.934953425435474,
                50.94154873163077,
                5.9341556116101595,
                50.87413533708443,
                5.784989651500041,
                50.87474468292546
              ]
            ],
          }
        }
    console.log(d)
    
    function convert(points)
    {
      geojson = {
          "geometry": {
            "type": "Polygon",
            "coordinates": [
            ]
          }
      }
      var coordinates = new Array();
      for (var i = 0; i < points.length; i+=2) {
        coordinates.push([points[i],points[i+1]])
      }
      //closing the polygon
      coordinates.push([points[0],points[1]])
      // points need to be in zero index of coordinates array
      geojson.geometry.coordinates = new Array(coordinates);
      return geojson;
    }
    
    console.log(convert(d["footprint"]["data"][0]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2021-09-02
      相关资源
      最近更新 更多