SegmentStringDissolver.cs :  » GIS » DeepEarth » GisSharpBlog » NetTopologySuite » Noding » 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 » Noding » SegmentStringDissolver.cs
using System.Collections;
using GisSharpBlog.NetTopologySuite.Geometries;
using System.Collections.Generic;
using System.Linq;

namespace GisSharpBlog.NetTopologySuite.Noding{

    /// <summary>
    /// Dissolves a noded collection of <see cref="SegmentString" />s to produce
    /// a set of merged linework with unique segments.
    /// A custom merging strategy can be applied when two identical (up to orientation)
    /// strings are dissolved together.
    /// The default merging strategy is simply to discard the merged string.
    ///<para>
    /// A common use for this class is to merge noded edges
    /// while preserving topological labelling.
    /// </para>
    /// </summary>
    public class SegmentStringDissolver
    {
        /// <summary>
        /// 
        /// </summary>
        public interface ISegmentStringMerger
        {
            /// <summary>
            /// Updates the context data of a <see cref="SegmentString" />
            /// when an identical (up to orientation) one is found during dissolving.
            /// </summary>
            /// <param name="mergeTarget">The segment string to update.</param>
            /// <param name="ssToMerge">The segment string being dissolved.</param>
            /// <param name="isSameOrientation">
            /// <c>true</c> if the strings are in the same direction,
            /// <c>false</c> if they are opposite.
            /// </param>
            void Merge(SegmentString mergeTarget, SegmentString ssToMerge, bool isSameOrientation);
        }

        private ISegmentStringMerger merger;
        private Dictionary<OrientedCoordinateArray, SegmentString> ocaMap = new Dictionary<OrientedCoordinateArray, SegmentString>();
        
        /// <summary>
        /// Creates a dissolver with a user-defined merge strategy.
        /// </summary>
        /// <param name="merger"></param>
        public SegmentStringDissolver(ISegmentStringMerger merger)
        {
            this.merger = merger;
        }

        /// <summary>
        /// Creates a dissolver with the default merging strategy.
        /// </summary>
        public SegmentStringDissolver()
            : this(null) { }

        /// <summary>
        /// Dissolve all <see cref="SegmentString" />s in the input <see cref="ICollection"/>.
        /// </summary>
        /// <param name="segStrings"></param>
        public void Dissolve(ICollection segStrings)
        {
            foreach(object obj in segStrings)
                Dissolve((SegmentString)obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="oca"></param>
        /// <param name="segString"></param>
        private void Add(OrientedCoordinateArray oca, SegmentString segString)
        {
            ocaMap.Add(oca, segString);
        }

        /// <summary>
        /// Dissolve the given <see cref="SegmentString" />.
        /// </summary>
        /// <param name="segString"></param>
        public void Dissolve(SegmentString segString)
        {
            OrientedCoordinateArray oca = new OrientedCoordinateArray(segString.Coordinates);
            SegmentString existing = FindMatching(oca, segString);
            if (existing == null)
                Add(oca, segString);            
            else
            {
                if (merger != null)
                {
                    bool isSameOrientation = CoordinateArrays.Equals(existing.Coordinates, segString.Coordinates);
                    merger.Merge(existing, segString, isSameOrientation);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="oca"></param>
        /// <param name="segString"></param>
        /// <returns></returns>
        private SegmentString FindMatching(OrientedCoordinateArray oca, SegmentString segString)
        {
            return (SegmentString)ocaMap[oca];            
        }        

        /// <summary>
        /// Gets the collection of dissolved (i.e. unique) <see cref="SegmentString" />s
        /// </summary>
        public ICollection Dissolved
        {
            get
            {
        return ocaMap.OrderBy(x => x).ToList();
            }
        }

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