NTS-NetTopologySuite中将任意几何对象转换为线段

秦凯旋
2023-12-01
        public static void ConvertGeomToLineSegment(Geometry geom, List<LineSegment> ls)
        {
            if (ls == null) ls = new List<LineSegment>();
            if (geom.GeometryType == "LineString")
            {
                for (var i = 0; i < geom.Coordinates.Length - 1; i++)
                {
                    ls.Add(new LineSegment(geom.Coordinates[i], geom.Coordinates[i + 1]));
                }
            }
            else
            {
                if (geom.GeometryType == "GeometryCollection" || geom.GeometryType.StartsWith("Multi"))
                {
                    for (int i = 0; i < geom.NumGeometries; i++)
                    {
                        ConvertGeomToLineSegment(geom.GetGeometryN(i), ls);
                    }
                }
                else
                {

                    var lsTmp = new List<LineString>();
                    ConvertGeomToLineString(geom, lsTmp);
                    foreach (var g in lsTmp)
                    {
                        ConvertGeomToLineSegment(g,ls);
                    }
                }
            }
        }

总结:递归思想

 类似资料: