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

namespace GisSharpBlog.NetTopologySuite.Simplify{
    /// <summary>
    /// Simplifies a point, ensuring that
    /// the result is a valid point having the
    /// same dimension and number of components as the input.
    /// The simplification uses a maximum distance difference algorithm
    /// similar to the one used in the Douglas-Peucker algorithm.
    /// In particular, if the input is an areal point
    /// ( <c>Polygon</c> or <c>MultiPolygon</c> )
    /// The result has the same number of shells and holes (rings) as the input,
    /// in the same order
    /// The result rings touch at no more than the number of touching point in the input
    /// (although they may touch at fewer points).
    /// </summary>
    public class TopologyPreservingSimplifier
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="distanceTolerance"></param>
        /// <returns></returns>
        public static IGeometry Simplify(IGeometry geom, double distanceTolerance)
        {
            TopologyPreservingSimplifier tss = new TopologyPreservingSimplifier(geom);
            tss.DistanceTolerance = distanceTolerance;
            return tss.GetResultGeometry();
        }

        private IGeometry inputGeom;
        private TaggedLinesSimplifier lineSimplifier = new TaggedLinesSimplifier();
    private Dictionary<IGeometry, TaggedLineString> lineStringMap;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputGeom"></param>
        public TopologyPreservingSimplifier(IGeometry inputGeom)
        {
            this.inputGeom = inputGeom;            
        }

        /// <summary>
        /// 
        /// </summary>
        public double DistanceTolerance
        {
            get
            {
                return lineSimplifier.DistanceTolerance;
            }
            set
            {
                lineSimplifier.DistanceTolerance = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IGeometry GetResultGeometry() 
        {
            lineStringMap = new Dictionary<IGeometry, TaggedLineString>();
            inputGeom.Apply(new LineStringMapBuilderFilter(this));
            lineSimplifier.Simplify(new List<TaggedLineString>(lineStringMap.Values));
            IGeometry result = (new LineStringTransformer(this)).Transform(inputGeom);
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        private class LineStringTransformer : GeometryTransformer
        {
            private TopologyPreservingSimplifier container = null;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="container"></param>
            public LineStringTransformer(TopologyPreservingSimplifier container)            
            {
                this.container = container;
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="coords"></param>
            /// <param name="parent"></param>
            /// <returns></returns>
            protected override ICoordinateSequence TransformCoordinates(ICoordinateSequence coords, IGeometry parent)
            {
                if (parent is ILineString) 
                {
                    TaggedLineString taggedLine = (TaggedLineString) container.lineStringMap[parent];
                    return CreateCoordinateSequence(taggedLine.ResultCoordinates);
                }
                // for anything else (e.g. points) just copy the coordinates
                return base.TransformCoordinates(coords, parent);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private class LineStringMapBuilderFilter : IGeometryComponentFilter
        {
            private TopologyPreservingSimplifier container = null;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="container"></param>
            public LineStringMapBuilderFilter(TopologyPreservingSimplifier container)            
            {
                this.container = container;
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="geom"></param>
            public void Filter(IGeometry geom)
            {
                if (geom is ILinearRing) 
                {
                    TaggedLineString taggedLine = new TaggedLineString((ILineString) geom, 4);
                    container.lineStringMap.Add(geom, taggedLine);
                }
                else if (geom is ILineString) 
                {
                    TaggedLineString taggedLine = new TaggedLineString((ILineString) geom, 2);
                    container.lineStringMap.Add(geom, taggedLine);
                }
            }
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.