LinearGeometryBuilder.cs :  » GIS » DeepEarth » GisSharpBlog » NetTopologySuite » LinearReferencing » 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 » LinearReferencing » LinearGeometryBuilder.cs
using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;

namespace GisSharpBlog.NetTopologySuite.LinearReferencing{
    /// <summary>
    /// Builds a linear geometry (<see cref="LineString" /> or <see cref="MultiLineString" />)
    /// incrementally (point-by-point).
    /// </summary>
    public class LinearGeometryBuilder
    {
        private IGeometryFactory geomFact = null;
        private List<IGeometry> lines = new List<IGeometry>();
        private CoordinateList coordList = null;

        private bool ignoreInvalidLines = false;
        private bool fixInvalidLines = false;

        private ICoordinate lastPt = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="geomFact"></param>
        public LinearGeometryBuilder(IGeometryFactory geomFact)
        {
            this.geomFact = geomFact;
        }

        /// <summary>
        /// Allows invalid lines to be fixed rather than causing Exceptions.
        /// An invalid line is one which has only one unique point.
        /// </summary>
        public bool FixInvalidLines
        {
            get
            {
                return fixInvalidLines;
            }
            set
            {
                fixInvalidLines = value;
            }
        }

        /// <summary>
        /// Allows invalid lines to be ignored rather than causing Exceptions.
        /// An invalid line is one which has only one unique point.
        /// </summary>
        public bool IgnoreInvalidLines
        {
            get
            {
                return ignoreInvalidLines;
            }
            set
            {
                ignoreInvalidLines = value;
            }
        }

        /// <summary>
        /// Adds a point to the current line.
        /// </summary>
        /// <param name="pt">The <see cref="Coordinate" /> to add.</param>
        public void Add(ICoordinate pt)
        {
            Add(pt, true);
        }

        /// <summary>
        /// Adds a point to the current line.
        /// </summary>
        /// <param name="pt">The <see cref="Coordinate" /> to add.</param>
        /// <param name="allowRepeatedPoints">If <c>true</c>, allows the insertions of repeated points.</param>
        public void Add(ICoordinate pt, bool allowRepeatedPoints)
        {
            if (coordList == null)
                coordList = new CoordinateList();
            coordList.Add(pt, allowRepeatedPoints);
            lastPt = pt;
        }

        /// <summary>
        /// 
        /// </summary>
        public ICoordinate LastCoordinate
        {
            get
            {
                return lastPt;
            }
        }

        /// <summary>
        /// Terminate the current <see cref="LineString" />.
        /// </summary>
        public void EndLine()
        {
            if (coordList == null)
                return;
            
            if (ignoreInvalidLines && coordList.Count < 2)
            {
                coordList = null;
                return;
            }

            ICoordinate[] rawPts = coordList.ToCoordinateArray();
            ICoordinate[] pts = rawPts;
            if (FixInvalidLines)
                pts = ValidCoordinateSequence(rawPts);

            coordList = null;
            ILineString line = null;
            try
            {
                line = geomFact.CreateLineString(pts);
            }
            catch (ArgumentException ex)
            {
                // exception is due to too few points in line.
                // only propagate if not ignoring short lines
                if (!IgnoreInvalidLines)
                    throw ex;
            }

            if (line != null) 
                lines.Add(line);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pts"></param>
        /// <returns></returns>
        private ICoordinate[] ValidCoordinateSequence(ICoordinate[] pts)
        {
            if (pts.Length >= 2) 
                return pts;
            ICoordinate[] validPts = new ICoordinate[] { pts[0], pts[0] };
            return validPts;
        }

        /// <summary>
        /// Builds and returns the <see cref="Geometry" />.
        /// </summary>
        /// <returns></returns>
        public IGeometry GetGeometry()
        {
            // end last line in case it was not done by user
            EndLine();
            return geomFact.BuildGeometry(lines);
        }

    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.