Search code examples
javascriptarraysangulartypescriptgeojson

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,

enter image description here

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

coordinates is an Array of Arrays:

enter image description here

Could someone show me what I did wrong please?

Best Regards,


Solution

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