1

I'm building an interactive map using Mapbox and would like to draw a polygon over a specific area like is shown here. For this I need to dynamically fill a 3D array with the X,Y and Z coordinates from a database. The array structure I want to achieve is:

[
  [
    [
      [xCoordinate1, yCoordinate1, zCoordinate1],
      [xCoordinate2, yCoordinate2, zCoordinate2],
      [xCoordinate3, yCoordinate3, zCoordinate3]
    ]
  ]
]

I have been trying to accomplish this with C#. In my application I initialized a 3D list, like so:

List<List<List<List<double>>>> coordinates = new List<List<List<List<double>>>>();

Next I iterated over the coordinates that are coming from the database so they would be added to the array:

foreach (var coordinate in Coordinates) {
  coordinates.Add({ coordinate.X, coordinate.Y, coordinate.Z })
}

However this doesn't add the values at the disired position and throws an IndexOutOfBounds exception. I have also tried to initialize the array, like so:

double[, , ,] coordinates = {
  { 
    { 
      { coordinate.X, coordinate.Y, coordinate.Z }, 
      { coordinate.X, coordinate.Y, coordinate.Z }, 
      { coordinate.X, coordinate.Y, coordinate.Z }
    }
  }
};

With this approach i was also unable to format my array the way it should be formatted. Can someone show me how to work with a complex 3D array so that it gets the structure I'm looking for?

To sum up:

int[,,,] array3D = new int[,,,] { 
  { 
    {
      { 1, 2, 3 },
      { 4, 5, 6 } 
      //How can I add more here dynamically?
    }
  }
};

array3D[0, 0, 0, 3] = { 7, 8, 8 }; //This doesn't do the trick :(
3
  • 1
    Try changing the var to the actual type. You need to add a list of lists of lists to coordinate Commented May 20, 2019 at 16:29
  • 1
    1. The linked example has a) only 3 levels and b) only 2D coordinates. Why do you want 4 levels and 3D coordinates? 2. Please, don't use the same name for two different variables (Coordinates and coordinates). That's confusing. Commented May 20, 2019 at 16:38
  • 1. You are right, I think I misunderstood the difference between a 4D array and an array with 3 levels and 2D coordinates. I would like to achieve the structure from the first snippet. 2. Thanks for you tip, I think it's pretty helpful. Commented May 20, 2019 at 17:12

2 Answers 2

1

You cannot change the size of a multidimensional array, but that is ok because your JSON really represents an array of arrays of sorts.

Start with the (expandable) list of coordinates

var coords = new List<double[]>
{
    new double[] { 1,2,3 },
    new double[] { 4,5,6 },
};
// later
coords.Add(new double[] { 7, 8, 9 });

Then convert to the JSON structure for export. You showed an array of array of array of coordinates (array).

var json = new double[][][][] {
    new double[][][] {
        coords.ToArray()
    }
};

This is how you recover the coordinates

foreach (var item in json[0][0])
{
    Debug.WriteLine($"Point=({item[0]}, {item[1]}, {item[2]})");
}

In the output window, you see

// Point=(1, 2, 3)
// Point=(4, 5, 6)
// Point=(7, 8, 9)
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly, you can get away with just 2D list, where the first list contains sets of coordinates (i.e. (x,y,z)) and the second list simply contains a bunch of first lists, like so:

List<List<double>> coords = new List<List<double>>();
coords.Add(new List<double> { 24, 54, 46 });
coords.Add(new List<double> { 32, 45, 48 });
Console.WriteLine(coords[1][1]);
//Outputs 45.
//Note: xCoord = 24, yCoord = 54, zCoord = 46 in the first list entry

You can make it a separate method or an extension method where the coordinates are passed in as an arguments. It's also possible to loop through the lists to get particular x,y or z coordinate (if you need to search through them in your code).

2 Comments

Creating a list and later add that list to the other list did the trick! Thanks for pointing me in the right direction!
@Mitch It's great that we've solved the problem! Good luck with coding!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.