GeometryCollectionEnumerator.cs :  » GIS » DeepEarth » GisSharpBlog » NetTopologySuite » Geometries » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » DeepEarth 
DeepEarth » GisSharpBlog » NetTopologySuite » Geometries » GeometryCollectionEnumerator.cs
using System;
using System.Collections;
using GeoAPI.Geometries;

namespace GisSharpBlog.NetTopologySuite.Geometries{
    /// <summary>
    /// Iterates over all <c>Geometry</c>'s in a <c>GeometryCollection</c>. 
    /// Implements a pre-order depth-first traversal of the <c>GeometryCollection</c>
    /// (which may be nested). The original <c>GeometryCollection</c> is
    /// returned as well (as the first object), as are all sub-collections. It is
    /// simple to ignore the <c>GeometryCollection</c> objects if they are not
    /// needed.
    /// </summary>    
    public class GeometryCollectionEnumerator : IEnumerator
    {
        /// <summary>
        /// The <c>GeometryCollection</c> being iterated over.
        /// </summary>
        private IGeometryCollection parent;

        /// <summary>
        /// Indicates whether or not the first element (the <c>GeometryCollection</c>)
        /// has been returned.
        /// </summary>
        private bool atStart;

        /// <summary>
        /// The number of <c>Geometry</c>s in the the <c>GeometryCollection</c>.
        /// </summary>
        private int max;

        /// <summary>
        /// The index of the <c>Geometry</c> that will be returned when <c>next</c>
        /// is called.
        /// </summary>
        private int index;

        /// <summary>
        /// The iterator over a nested <c>GeometryCollection</c>, or <c>null</c>
        /// if this <c>GeometryCollectionIterator</c> is not currently iterating
        /// over a nested <c>GeometryCollection</c>.
        /// </summary>
        private GeometryCollectionEnumerator subcollectionEnumerator;

        /// <summary>
        /// Constructs an iterator over the given <c>GeometryCollection</c>.
        /// </summary>
        /// <param name="parent">
        /// The collection over which to iterate; also, the first
        /// element returned by the iterator.
        /// </param>
        public GeometryCollectionEnumerator(IGeometryCollection parent) 
        {
            this.parent = parent;
            atStart = true;
            index = 0;
            max = parent.NumGeometries;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext() 
        {
            if (atStart) 
                return true;
            if (subcollectionEnumerator != null) 
            {
                if (subcollectionEnumerator.MoveNext())  
                    return true;
                subcollectionEnumerator = null;
            }
            if (index >= max) 
                return false;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks> The parent GeometryCollection is the first object returned!</remarks>
        public object Current
        {
            get
            {
                // the parent GeometryCollection is the first object returned
                if (atStart) 
                {
                    atStart = false;
                    return parent;
                }
                if (subcollectionEnumerator != null) 
                {
                    if (subcollectionEnumerator.MoveNext()) 
                        return subcollectionEnumerator.Current;
                    else subcollectionEnumerator = null;
                }
                if (index >= max) 
                    throw new ArgumentOutOfRangeException(); 
                
                IGeometry obj = parent.GetGeometryN(index++);
                if (obj is IGeometryCollection) 
                {
                    subcollectionEnumerator = new GeometryCollectionEnumerator((IGeometryCollection) obj);
                    // there will always be at least one element in the sub-collection
                    return subcollectionEnumerator.Current;
                }
                return obj;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            atStart = true;
            index = 0;            
        }
    }    
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.