LengthIndexOfPoint.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 » LengthIndexOfPoint.cs
using System;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;
using GisSharpBlog.NetTopologySuite.Utilities;

namespace GisSharpBlog.NetTopologySuite.LinearReferencing{

    /// <summary>
    /// 
    /// </summary>
    public class LengthIndexOfPoint
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="linearGeom"></param>
        /// <param name="inputPt"></param>
        /// <returns></returns>
        public static double IndexOf(IGeometry linearGeom, ICoordinate inputPt)
        {
            LengthIndexOfPoint locater = new LengthIndexOfPoint(linearGeom);
            return locater.IndexOf(inputPt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="linearGeom"></param>
        /// <param name="inputPt"></param>
        /// <param name="minIndex"></param>
        /// <returns></returns>
        public static double IndexOfAfter(IGeometry linearGeom, ICoordinate inputPt, double minIndex)
        {
            LengthIndexOfPoint locater = new LengthIndexOfPoint(linearGeom);
            return locater.IndexOfAfter(inputPt, minIndex);
        }

        private IGeometry linearGeom;

        /// <summary>
        /// Initializes a new instance of the <see cref="LengthIndexOfPoint"/> class.
        /// </summary>
        /// <param name="linearGeom">A linear geometry.</param>
        public LengthIndexOfPoint(IGeometry linearGeom)
        {
            this.linearGeom = linearGeom;
        }

        /// <summary>
        /// Find the nearest location along a linear {@link Geometry} to a given point.
        /// </summary>
        /// <param name="inputPt">The coordinate to locate.</param>
        /// <returns>The location of the nearest point.</returns>
        public double IndexOf(ICoordinate inputPt)
        {
            return IndexOfFromStart(inputPt, -1.0);
        }

        /// <summary>
        /// Finds the nearest index along the linear <see cref="Geometry" />
        /// to a given <see cref="Coordinate"/> after the specified minimum index.
        /// If possible the location returned will be strictly 
        /// greater than the <paramref name="minIndex" />.
        /// If this is not possible, the value returned 
        /// will equal <paramref name="minIndex" />.
        /// (An example where this is not possible is when
        /// <paramref name="minIndex" /> = [end of line] ).
        /// </summary>
        /// <param name="inputPt">The coordinate to locate.</param>
        /// <param name="minIndex">The minimum location for the point location.</param>
        /// <returns>The location of the nearest point.</returns>
        public double IndexOfAfter(ICoordinate inputPt, double minIndex)
        {
            if (minIndex < 0.0) return IndexOf(inputPt);

            // sanity check for minIndex at or past end of line
            double endIndex = linearGeom.Length;
            if (endIndex < minIndex)
                return endIndex;

            double closestAfter = IndexOfFromStart(inputPt, minIndex);

            /*
             * Return the minDistanceLocation found.
             * This will not be null, since it was initialized to minLocation
             */
            Assert.IsTrue(closestAfter > minIndex, "computed index is before specified minimum index");
            return closestAfter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputPt"></param>
        /// <param name="minIndex"></param>
        /// <returns></returns>
        private double IndexOfFromStart(ICoordinate inputPt, double minIndex)
        {
            double minDistance = Double.MaxValue;

            double ptMeasure = minIndex;
            double segmentStartMeasure = 0.0;

            LineSegment seg = new LineSegment();
            foreach(LinearIterator.LinearElement element in new LinearIterator(linearGeom))
            {
                if (!element.IsEndOfLine)
                {
                    seg.P0 = element.SegmentStart;
                    seg.P1 = element.SegmentEnd;
                    double segDistance = seg.Distance(inputPt);
                    double segMeasureToPt = SegmentNearestMeasure(seg, inputPt, segmentStartMeasure);
                    if (segDistance < minDistance && segMeasureToPt > minIndex)
                    {
                        ptMeasure = segMeasureToPt;
                        minDistance = segDistance;
                    }
                    segmentStartMeasure += seg.Length;
                }                
            }
            return ptMeasure;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="seg"></param>
        /// <param name="inputPt"></param>
        /// <param name="segmentStartMeasure"></param>
        /// <returns></returns>
        private double SegmentNearestMeasure(LineSegment seg, ICoordinate inputPt, double segmentStartMeasure)
        {
            // found new minimum, so compute location distance of point
            double projFactor = seg.ProjectionFactor(inputPt);
            if (projFactor <= 0.0)
                return segmentStartMeasure;
            if (projFactor <= 1.0)
                return segmentStartMeasure + projFactor * seg.Length;
            // ASSERT: projFactor > 1.0
            return segmentStartMeasure + seg.Length;
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.