转载 AE创建泰森多边形
ArcEngin提供了TIN组件,可以利用等高线或离散的高程值生成TIN的方法,其实等高线和离散点都可以看作是要素类—feature class
AE的组件TIN提供了一个非常重要的接口ITinEdit利用这个接口,可以方便地生成三维的TIN,
思路:1找到一个数据集,作为生成TIN的数据源,我们用shp文件来做,它必须有一个字段里含用高程值,2.利用ITinEdit接口生成TIN
重要的是这两个方法:InitNew(int pExtent,,IEnvelope)它需要一个IEnvelope接口的对象
以及方法:SetSpatialReference(int pSpatialReference,ISPatialReference);因此我们首先创建一个ISpatialReference接口对象,利用get_SpatialReference属性得到要素类的空间引用并将空间引用赋给ISpatialReference接口对象,再创建一个IEnvelope接口对象,利用putref_ SpatialReference属性将前面创建的ISpatialReference接口对象的空间引用赋给IEnvelope接口对象,再创建一个TIN对象并得到其ITinEdit接口,利用此接口的InitNew方法创建TIN,而前面创建的IEnvelope接口对象作为该方法的输入对象,这样就可以生成TIN了.
3.上面已能生成TIN,但生成的TIN并没有高程信息,要获得高程字段,再生成三维TIN,这要用到IFields接口和IField的指针,用其get_Fields方法获取高程信息.
代码如下:
//创建TIN数据,同时根据以有的要素类来创建泰森多边形
Public Sub CreateTin()
Dim TinSurface As ITin
Set TinSurface = New Tin
Dim FeatClass As IFeatureClass
Dim Pdocment As IMxDocument
Set Pdocment = ThisDocument
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = Pdocment.ActiveView.FocusMap.Layer(0)
Dim pEnv As IEnvelope
Set pEnv = pFeatureLayer.AreaOfInterest.Envelope
Dim pTinEdit As ITinEdit
Set pTinEdit = New Tin
pTinEdit.InitNew pEnv
pTinEdit.SaveAs "H:\New Folder\tin\a3", False '创建放的路径
pTinEdit.StartEditing
Set FeatClass = pFeatureLayer.FeatureClass
Dim pTagFeild As IField
Set pTagFeild = New Field
Dim pHightFeild As IField
Set pHightFeild = FeatClass.Fields.Field(0)
'MsgBox pHightFeild.Name
'//其中FeatClass.Fields.Field(2), 是要素类中的某一字段 作为Tin的高程信息
pTinEdit.AddFromFeatureClass FeatClass, Nothing, FeatClass.Fields.Field(2), FeatClass.Fields.Field(0), esriTinMassPoint
pTinEdit.StopEditing (True)
pTinEdit.Refresh
Dim pTinNodeCollection As ITinNodeCollection
Set pTinNodeCollection = pTinEdit
MsgBox pTinNodeCollection.NodeCount
Dim ptin As ITin
Set ptin = pTinEdit
MsgBox ptin.DataNodeCount
Dim pNewfeatureclass As IFeatureClass
Set pNewfeatureclass = OpenFeatureClass_Example
MsgBox ptin.DataEdgeCount
///创建泰森多边形 OpenFeatureClass_Example是 找到的要素类图层
pTinNodeCollection.ConvertToVoronoiRegions OpenFeatureClass_Example, Nothing, Nothing, "NodeIndex", "asdf"
End Sub
//添加要素类的图层函数
Public Function OpenFeatureClass_Example() As IFeatureClass
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("H:\New Folder", 0)
' Dim pWorkspaceEdit As IWorkspaceEdit
' Set pWorkspaceEdit = pFeatureWorkspace
' pWorkspaceEdit.StartEditing True
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("Vor3")
Set OpenFeatureClass_Example = pFeatureClass
end function
AE的组件TIN提供了一个非常重要的接口ITinEdit利用这个接口,可以方便地生成三维的TIN,
思路:1找到一个数据集,作为生成TIN的数据源,我们用shp文件来做,它必须有一个字段里含用高程值,2.利用ITinEdit接口生成TIN
重要的是这两个方法:InitNew(int pExtent,,IEnvelope)它需要一个IEnvelope接口的对象
以及方法:SetSpatialReference(int pSpatialReference,ISPatialReference);因此我们首先创建一个ISpatialReference接口对象,利用get_SpatialReference属性得到要素类的空间引用并将空间引用赋给ISpatialReference接口对象,再创建一个IEnvelope接口对象,利用putref_ SpatialReference属性将前面创建的ISpatialReference接口对象的空间引用赋给IEnvelope接口对象,再创建一个TIN对象并得到其ITinEdit接口,利用此接口的InitNew方法创建TIN,而前面创建的IEnvelope接口对象作为该方法的输入对象,这样就可以生成TIN了.
3.上面已能生成TIN,但生成的TIN并没有高程信息,要获得高程字段,再生成三维TIN,这要用到IFields接口和IField的指针,用其get_Fields方法获取高程信息.
代码如下:
//创建TIN数据,同时根据以有的要素类来创建泰森多边形
Public Sub CreateTin()
Dim TinSurface As ITin
Set TinSurface = New Tin
Dim FeatClass As IFeatureClass
Dim Pdocment As IMxDocument
Set Pdocment = ThisDocument
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = Pdocment.ActiveView.FocusMap.Layer(0)
Dim pEnv As IEnvelope
Set pEnv = pFeatureLayer.AreaOfInterest.Envelope
Dim pTinEdit As ITinEdit
Set pTinEdit = New Tin
pTinEdit.InitNew pEnv
pTinEdit.SaveAs "H:\New Folder\tin\a3", False '创建放的路径
pTinEdit.StartEditing
Set FeatClass = pFeatureLayer.FeatureClass
Dim pTagFeild As IField
Set pTagFeild = New Field
Dim pHightFeild As IField
Set pHightFeild = FeatClass.Fields.Field(0)
'MsgBox pHightFeild.Name
'//其中FeatClass.Fields.Field(2), 是要素类中的某一字段 作为Tin的高程信息
pTinEdit.AddFromFeatureClass FeatClass, Nothing, FeatClass.Fields.Field(2), FeatClass.Fields.Field(0), esriTinMassPoint
pTinEdit.StopEditing (True)
pTinEdit.Refresh
Dim pTinNodeCollection As ITinNodeCollection
Set pTinNodeCollection = pTinEdit
MsgBox pTinNodeCollection.NodeCount
Dim ptin As ITin
Set ptin = pTinEdit
MsgBox ptin.DataNodeCount
Dim pNewfeatureclass As IFeatureClass
Set pNewfeatureclass = OpenFeatureClass_Example
MsgBox ptin.DataEdgeCount
///创建泰森多边形 OpenFeatureClass_Example是 找到的要素类图层
pTinNodeCollection.ConvertToVoronoiRegions OpenFeatureClass_Example, Nothing, Nothing, "NodeIndex", "asdf"
End Sub
//添加要素类的图层函数
Public Function OpenFeatureClass_Example() As IFeatureClass
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("H:\New Folder", 0)
' Dim pWorkspaceEdit As IWorkspaceEdit
' Set pWorkspaceEdit = pFeatureWorkspace
' pWorkspaceEdit.StartEditing True
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("Vor3")
Set OpenFeatureClass_Example = pFeatureClass
end function