Search code examples
javascriptarraysgeojsonbabylonjs

how to separate an array and put it in the bracket


I have an array (Geojson file) and I want to split it into three array . there are some multipolygon coordinates and I want to put the coordinates consecutive for each id, I want to get:

 arrGeo = 
  [ 
    [-4.66478, 58.42441, 5127.4,-4.65982, 58.42082, 5074.7],
    [-3.94815, 57.71632, 5000,-3.94812, 57.71576, 4374.1,-3.94216, 57.71541, 4283,-3.93717, 
     57.71583, 5001],
    [-3.93224, 57.71476, 4048,-3.93261, 57.71456, 3800.4] 
  ]

I try to do it by using for loop but I couldn't separate them, I mean for each ID i need to create separate array of coordinate. what is the problem of my code? and what should I do to solve it?

this is my code:

positions =
       
     [
         {

            "type": "Feature",
            "geometry": {
                "type": "GeometryCollection",
                "geometries": [
                    {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [-4.66478, 58.42441, 5127.4],
                                [-4.65982, 58.42082, 5074.7],

                            ]
                        ]
                    },
                ]
            },
            "id": "kml_1"
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "GeometryCollection",
                "geometries": [
                    {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [-3.94815, 57.71632, 5000],
                                [-3.94812, 57.71576, 4374.1],

                            ]
                        ]
                    },
                    {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [-3.94216, 57.71541, 4283],
                                [-3.93717, 57.71583, 5001],

                            ]
                        ]
                    },

                ]
            },
            "id": "kml_2"
        },

        {
            "type": "Feature",
            "geometry": {
                "type": "GeometryCollection",
                "geometries": [
                    {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [-3.93224, 57.71476, 4048],
                                [-3.93261, 57.71456, 3800.4],

                            ]
                        ]
                    },
                ]
            },
            "id": "kml_3"
        },
    ];

    var customMesh = new BABYLON.Mesh("custom", scene);
    var customMesh2 = new BABYLON.Mesh("custom", scene);
    let arrGeo = [];
    let indices = [];



    for (let i = 0; i < positions.length; i++) {
        let fGeometry = positions[i].geometry.geometries;

        console.log("i", fGeometry);

        for (let j = 0; j < fGeometry.length; j++) {
            console.log("j", j);
            // console.log("p", fGeometry[1]);
            for (let k = 0; k < 1; k++) {
                for (let m = 0; m < 3; m++) {
                    for (let n = 0; n < 3; n++) {
                        let fGCoordinate = fGeometry[j].coordinates[k][m][n];
                        console.log("p", fGCoordinate);

                        arrGeo.push(fGCoordinate);
                    }
                }
            }
        }

    }

Solution

  • Please find below the code and demo link

    let final = [];
    for (var i = 0, len = positions.length; i < len; i++) {
      let a = [];
      for (var j = 0, jlen = positions[i]["geometry"]["geometries"].length; j < jlen; j++) {
        
        for (var k = 0, klen = positions[i]["geometry"]["geometries"][j]["coordinates"].length; k < klen; k++) {
        
          for (l = 0, llen = positions[i]["geometry"]["geometries"][j]["coordinates"][k].length; l < llen; l++) {
            a = a.concat(positions[i]["geometry"]["geometries"][j]["coordinates"][k][l]);
          }
        }
      }
      final.push(a);
    }
    console.log(final);
    

    https://jsbin.com/hehurajoje/edit?js,console