0

I am creating a coordinate grid in JSON for a basic helicopter flight simulator I am working on in Three.js.

I have created the repo here: https://github.com/mpaccione/coordinate_generator

The inputted data is one giant flat array of 60KM^2 Shuttle Radar Topography Data at 60M resolution. Every 600 is a new latitude increment. Due to the output of the SRTM data I needed to create a much more usable grid system that I could access quickly and logically.

My approach was to break it into a 60x60 Grid (1KM Resolution) with a 10x10 Subgrid in each (0.1KM Resolution). Save this multidimensional array system to a JSON file. Then I can segments into memory via Indexed DB and access each grid tile of data directly in the browser in the simulator.

[Grid Lat][Grid Long][Subgrid Lat][Subgrid Long]
[0][0][0][0] (Start)
[59][59][9][9] (End)

Each Subgrid object has three properties:

{
   "latitude":28.15079129,
   "longitude":87.32386969333332,
   "elevation":3805
}

Currently the setup to create this final structured JSON file looks like this:

function createGrid(data){
    console.log('createGrid');
    const GRID_SIZE = 60;
    const SUBGRID_SIZE = 10;

    let grid = new Array(GRID_SIZE).fill(new Array(GRID_SIZE));

    //LOOP OVER EVERY GRID SQUARE
    for (let j = 0; j < GRID_SIZE; j++){
        for (let i = 0; i < GRID_SIZE; i++){
            // FOR EACH SQUARE IN GRID
            grid[i][j] = new Array(SUBGRID_SIZE).fill(new Array(SUBGRID_SIZE))
            // MAKE SUBGRID
            for (let z = 0; z < SUBGRID_SIZE; z++){
                for (let y = 0; y < SUBGRID_SIZE; y++){
                    // EACH SQUARE IN SUBGRID
                    const offset = ((j * 10) + z) + (600 * ((10 * i) + y))
                    grid[i][j][y][z] = data["results"][offset];
                    console.log(`${i}-${j}-${y}-${z}: Latitude: ${data["results"][offset]["latitude"]} Longitude: ${data["results"][offset]["longitude"]}`) 
                }
            }
        }
    }

    writeToFile(JSON.stringify(grid), `/grid/Grid_Output_${name}_${GRID_SIZE}`, `/grid/Grid_Info_${name}_${GRID_SIZE}`, {"gridSize": GRID_SIZE, "subgridSize": SUBGRID_SIZE}, 'json', function(){
        console.log("Grid File Write Successful");
    });

}

So great right? Does what it needs to? NO! I can't figure it out why...

but the latitude property of the object is the same value for every single object however in the console logging it appears correctly.

I've tried all sorts of things like parsing and stringifying, manually assigning, etc. Perhaps it is some compiler assignment issue in Javascript?

Michael Paccione
  • 2,467
  • 6
  • 39
  • 74
  • 1
    Don't use `.fill` with non-primitives - use `Array.from` to create the array instead, to create a new array for every iteration. Eg `const grid = Array.from( { length: GRID_SIZE }, () => [] );` (except more nested). When you use `.fill`, you'll be filling the new array with multiple references to the same array in memory, so changing one array item will change all – CertainPerformance Dec 27 '19 at 21:10
  • Okay I will test using Array.from and see if that works... – Michael Paccione Dec 27 '19 at 21:17
  • This totally works wow... thank you! – Michael Paccione Dec 27 '19 at 21:34

0 Answers0