将polyline图形的要素类转为polygon形式的要素类
2008-08-07 21:27
218 查看
前一段时间写的,实现将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
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
相关文章推荐
- Polygon对象和Polyline对象的组成形式
- Polygon对象和Polyline对象的组成形式
- Polygon对象和Polyline对象的组成形式
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- ArcGIS10.0操作之4——线要素转化成面要素(Polyline TO Polygon)
- ArcMap上发布地图服务前,“将图形转为要素的选项”时报“输出名称无效”错误
- 分数形式标注要素
- Ajax的形式(如何将ajax回调值转为全局变量)
- 【Silverlight】Bing Maps学习系列(五):绘制多边形(Polygon)图形
- ArcGIS——图形转要素(标注转注记后发布服务问题)
- 茶道形式、用具及要素
- Geometry到Polyline/Polygon的转换
- Delphi 的绘图功能[6] - Polygon、Polyline
- Cocos2d-x3.3利用TileMap绘制Polygon/PolyLine
- Object转为String的几种形式
- java将掩码数字转为ip形式
- 【华为练习题】十六进制字符串形式转为内存值形式