ThinkGeo.com    |     Documentation    |     Premium Support

Merging LineShapes

Is there anyway to merge multiple LineShapes into either a single LineShape or a MultiLineShape, similar to PolygonShape.Union(...)?


Thanks,


Ryan



Ryan,


The Union of PolygonShape is using the NTS behind, while NTS did not provide the correct logic for Union LineShapes.
 
Hope our following code can help you.

public static MultilineShape Union(IEnumerable<LineShape> targetLines)
        {
            MultilineShape lines = new MultilineShape();
            List<LineShape> linesForMerge = new List<LineShape>(targetLines);
            while (true)
            {
                List<LineShape> isolatedLines = new List<LineShape>();
                LineShape line = GenerateLineShape(ref linesForMerge);
                
                lines.Lines.Add(line);
                if (linesForMerge.Count <= 0)
                {
                    break;
                }
            }
            return lines;
        }

        private static LineShape GenerateLineShape(ref List<LineShape> targetLines)
        {
            LineShape line = new LineShape();
            foreach (Vertex vertex in targetLines[0].Vertices)
            {
                line.Vertices.Add(vertex);
            }
            targetLines.Remove(targetLines[0]);

            while (true)
            {
                bool mergeOneOrMore = MergeLineShape(line, ref targetLines);
                if (mergeOneOrMore == false)
                {
                    break;
                }
            }
            return line;
        }

        private static bool MergeLineShape(LineShape resultLineShape, ref List<LineShape> targetLines)
        {
            bool mergeOneOrMore = false;
            Collection<LineShape> addedLineShapes = new Collection<LineShape>();
            foreach (LineShape lineShape in targetLines)
            {
                if (resultLineShape.Vertices[resultLineShape.Vertices.Count - 1] == lineShape.Vertices[0])
                {
                    for (int t = 1; t < lineShape.Vertices.Count; t++)
                    {
                        resultLineShape.Vertices.Add(lineShape.Vertices[t]);
                        mergeOneOrMore = true;
                        addedLineShapes.Add(lineShape);
                    }
                }
                else if (resultLineShape.Vertices[resultLineShape.Vertices.Count - 1] == lineShape.Vertices[lineShape.Vertices.Count - 1])
                {

                    for (int t = lineShape.Vertices.Count - 2; t >= 0; t--)
                    {
                        resultLineShape.Vertices.Add(lineShape.Vertices[t]);
                        mergeOneOrMore = true;
                        addedLineShapes.Add(lineShape);
                    }
                }
                else if (resultLineShape.Vertices[0] == lineShape.Vertices[0])
                {
                    for (int t = 0; t < lineShape.Vertices.Count - 1; t++)
                    {
                        resultLineShape.Vertices.Insert(t, lineShape.Vertices[lineShape.Vertices.Count - 1 - t]);
                        mergeOneOrMore = true;
                        addedLineShapes.Add(lineShape);
                    }
                }
                else if (resultLineShape.Vertices[0] == lineShape.Vertices[lineShape.Vertices.Count - 1])
                {
                    for (int t = 0; t < lineShape.Vertices.Count - 1; t++)
                    {
                        resultLineShape.Vertices.Insert(t, lineShape.Vertices[t]);
                        mergeOneOrMore = true;
                        addedLineShapes.Add(lineShape);
                    }
                }
            }
            if (addedLineShapes.Count > 0)
            {
                foreach (LineShape lineShape in addedLineShapes)
                {
                    targetLines.Remove(lineShape);
                }
            }
            return mergeOneOrMore;
        }

 

Any more questions please feel free to let me know.
 
Thanks.
 
Yale