Ejemplo n.º 1
0
        private static List <CoordinateSequence> CreateRings(List <CoordinateSequence> sequences)
        {
            var closedRings = new List <CoordinateSequence>();
            CoordinateSequence currentRing = null;

            while (sequences.Any())
            {
                if (currentRing == null)
                {
                    // start a new ring with any remaining node sequence
                    var lastIndex = sequences.Count - 1;
                    currentRing = sequences[lastIndex];
                    sequences.RemoveAt(lastIndex);
                }
                else
                {
                    // try to continue the ring by appending a node sequence
                    CoordinateSequence assignedSequence = null;
                    foreach (CoordinateSequence sequence in sequences)
                    {
                        if (!currentRing.TryAdd(sequence))
                        {
                            continue;
                        }
                        assignedSequence = sequence;
                        break;
                    }

                    if (assignedSequence != null)
                    {
                        sequences.Remove(assignedSequence);
                    }
                    else
                    {
                        return(null);
                    }
                }

                // check whether the ring under construction is closed
                if (currentRing != null && currentRing.IsClosed)
                {
                    // TODO check that it isn't self-intersecting!
                    closedRings.Add(new CoordinateSequence(currentRing));
                    currentRing = null;
                }
            }

            return(currentRing != null ? null : closedRings);
        }