简体   繁体   English

将JSON多边形坐标列表转换为GeoJSON格式的Long / Lat数组

[英]Converting a list of JSON polygon coordinates to Long/Lat arrays in GeoJSON format

I am fetching data from a REST API which results in a list of datasets. 我从REST API获取数据,从而生成数据集列表。 Some of these datasets contain coordinates that I want to project on a map. 其中一些数据集包含我想在地图上投影的坐标。 In order to project the coordinates, I need to convert the JSON output to GeoJSON. 为了投影坐标,我需要将JSON输出转换为GeoJSON。 While the conversion goes well for most parts of the data. 虽然转换适用于大多数数据部分。 I struggle with a long array containing x and y coordinates. 我挣扎着一个包含x和y坐标的长数组。 It is 4+ points combined into polygons (5th endpoint = begin point is missing). 它是4个点组合成多边形(第5个端点=缺少起点)。

How can I convert this one array with 4+ XY coordinates to the right format while also adding the last one that should match the first point? 如何将具有4+ XY坐标的这个数组转换为正确的格式,同时还添加应与第一个点匹配的最后一个?

I thought about slicing the array into new ones for each point but this would result in too much work for polygons with over 20 points. 我想过为每个点切割数组到新的数组,但这会导致超过20个点的多边形工作太多。

Example of the JSON: JSON示例:

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

This is how it should look like after conversion into GeoJSON. 这是转换为GeoJSON后的样子。 I wrote this down myself, but I need a JavaScript code to do this automatically while looping through the results. 我自己写下了这篇文章,但是我需要一个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]
          ]
        ]
      }
    }

i think this function will do the work for you. 我认为这个功能可以帮到你。

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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM