将polyline图形的要素类转为polygon形式的要素类

 前一段时间写的,实现将polyline形式的FeatureClass转为polygon形式的FeatureClass,这部分相关的代码网上很多,采用Geoprocessor的方式使用DataManagementTools中的 FeatureToPolygon会简单一些,这里采用比较原始的方式,根据每个feature的Geometry转换而来,其中GetPathFromFeatureClass和CreatShapefile是另外写的函数。

Private Function FCCoventToPolygon(ByVal pFeatureClass As IFeatureClass) As IFeatureClass

      

       '通过featureclass找到shp文件的路径
        Dim SourceFileFullName As String
        SourceFileFullName = GetPathFromFeatureClass(pFeatureClass)

        Dim FilePath, FileName As String
        FilePath = IO.Path.GetDirectoryName(SourceFileFullName)
        FileName = IO.Path.GetFileNameWithoutExtension(SourceFileFullName)

      

        '创建一个空的shp文件
        Dim TempFeatureClass As IFeatureClass
        TempFeatureClass = CreatShapefile(FilePath, "Temp_" & FileName)

        '创建一个编辑工作区并启动编辑
        Dim pWorkspaceFactory As IWorkspaceFactory
        Dim pWorkspaceEdit As IWorkspaceEdit
        pWorkspaceFactory = New ShapefileWorkspaceFactory
        pWorkspaceEdit = pWorkspaceFactory.OpenFromFile(FilePath, 0)

        pWorkspaceEdit.StartEditOperation()
        pWorkspaceEdit.StartEditing(True)

        '取出featureclass中的一个feature
        Dim pFeatureCur As IFeatureCursor
        pFeatureCur = pFeatureClass.Search(Nothing, False)
        Dim pFeature As IFeature
        pFeature = pFeatureCur.NextFeature()

        Dim pPolyLine As IPolyline
        Dim pPolygon As IPolygon
        Dim pEndFeature As IFeature
        Dim pGeoms As IGeometryCollection
        Dim pClone As IClone
        Dim pSegs As ISegmentCollection
        Dim pGeometryCollection As IGeometryCollection

        '开始遍历
        While (Not pFeature Is Nothing)

            '把一个polyline转存到一个GeometryCollection,以便取得里面的path(polyline的子对象是path)
            pPolyLine = pFeature.ShapeCopy
            pGeometryCollection = New Polygon
            pClone = pPolyLine
            pGeoms = pClone.Clone

           

            '通过Polyline的每个Path创建为一个新的Ring,并把Ring增加到一个新的Polygon
            For i As Long = 0 To pGeoms.GeometryCount - 1

                pSegs = New Ring
                pSegs.AddSegmentCollection(pGeoms.Geometry(i))
                pGeometryCollection.AddGeometry(pSegs)

            Next i

           

           '生成的Polygon旋转的顺序可能不正确,为确保正确调用SimplifyPreserveFromTo
            pPolygon = pGeometryCollection
            pPolygon.SimplifyPreserveFromTo()

            '在featureclass中新建要素并将刚生成的polygon保存进去
            pEndFeature = TempFeatureClass.CreateFeature
            pEndFeature.Shape = pPolygon
            pEndFeature.Store()

            pFeature = pFeatureCur.NextFeature()

        End While

       

       '关闭编辑
        pWorkspaceEdit.StopEditOperation()
        pWorkspaceEdit.StopEditing(True)

        Return TempFeatureClass

    End Function

原文地址:https://www.cnblogs.com/luspa/p/1263248.html