简体   繁体   中英

How to access geoJson coordinates array in typescript?

I have the following GeoJSON output shortened for read reasons:

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

My goal is to get the coordinates array and store it in another more general Array. In the code, I wrote:

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

Whilst feature[0] is defined,

在此处输入图像描述

geometry remains undefined . I cannot access to geometry->type / coordinates.

coordinates is an Array of Arrays:

在此处输入图像描述

Could someone show me what I did wrong please?

Best Regards,

It might be that features is an object and not an array in the definition.

That would mean that, to access the element '0' instead of feature[0], you should use feature['0'] .

Try something like this:

Here is a working proof: 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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