繁体   English   中英

如何访问 typescript 中的 geoJson 坐标数组?

[英]How to access geoJson coordinates array in typescript?

由于读取原因,我缩短了以下 GeoJSON output:

geoDefinition:
    features: Array(1)
    0:
    geometry:
        coordinates: Array(1)
            0: Array(5)
                0: (2) [5.39014, 43.279295]
                1: (2) [5.393069, 43.279249]
                2: (2) [5.391814, 43.278421]
                3: (2) [5.390709, 43.278749]
                4: (2) [5.39014, 43.279295]
                length: 5

我的目标是获取坐标数组并将其存储在另一个更通用的数组中。 在代码中,我写道:

   this.locations.forEach(element => {
      this.polygons = element.geoDefinition.features[0].geometry.coordinates;

虽然定义了特征[0],

在此处输入图像描述

几何仍然未定义 我无法访问几何->类型/坐标。

坐标是 Arrays 的数组:

在此处输入图像描述

有人可以告诉我我做错了什么吗?

此致,

特征可能是 object 而不是定义中的数组。

这意味着,要访问元素 '0' 而不是 feature[0],您应该使用feature['0']

尝试这样的事情:

这是一个工作证明: https://stackblitz.com/edit/js-puskp8

const locations = [
  {
    geoDefinition:
    {
      features:
        [{
          geometry:
          {
            coordinates: [

              [5.39014, 43.279295],
              [5.39014, 43.279295],
              [5.39014, 43.279295],
              [5.39014, 43.279295],


            ]
          }
        }]
    }
  }
]


const newArr = []
locations.forEach(obj => {
  newArr.push(...obj.geoDefinition.features[0].geometry.coordinates)

})

console.log('copied array is', newArr)

暂无
暂无

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

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