0

I have Envelope[][] extents = new Envelope[][]; construction. Each envelope has MinX, MaxX, MinY and MaxY properties and represents one tile of a grid (bottom-left and top-right point). Now I have another Envelope bounds; which contains min and max values for X- and Y-axis. I want to get tiles from extents which intersects with bounds.

Is there any simple way to do this using Envelope.Intersect?

[edit]
For now I did it in this way (brute-force xD):

List<Envelope> intersectedTiles = new List<Envelope>();
for (int i = 0; i < extents.LongLength; i++)
{
    for (int j = 0; j < extents.Length; j++)
    {
        if (extents[i][j].MinX >= bounds.MinX && extents[i][j].MaxX <= bounds.MaxX &&
            extents[i][j].MinY >= bounds.MinY && extents[i][j].MaxY <= bounds.MaxY)
        {
            intersectedTiles.Add(extents[i][j]);
        }
    }
}
Nickon
  • 9,652
  • 12
  • 64
  • 119

1 Answers1

1
private static bool Intersects(Envelope e1, Envelope e2)
{
    return e1.MinX >= e2.MinX && e1.MaxX <= e2.MaxX && e1.MinY >= e2.MinY && e1.MaxY <= e2.MaxY;
}

List<Envelope> intersectedTiles = extents.SelectMany(es => es)
    .Where(e => Intersects(e, bounds))
    .ToList();
Nickon
  • 9,652
  • 12
  • 64
  • 119
Lee
  • 142,018
  • 20
  • 234
  • 287
  • `e` in `Where` clausule is `null` and `Intersects` returns `NullReferenceException`. – Nickon Sep 27 '12 at 13:13
  • @Nickon - Then you have a null element somewhere in one of the arrays in `extents`. You can ignore them by changing the condition to `Where(e => e != null && Intersects(bounds, e))`, although removing nulls would be better. – Lee Sep 27 '12 at 13:23
  • Yeah, u're right... There was a problem with my calculations, that's why `null` appeared:) Now everything works fine, but I had to change `Intersects(bounds, e)` to `Intersects(e, bounds)`. Thanks – Nickon Sep 27 '12 at 13:37