Shp文件生成TIN

Imports System.IO 
Imports ESRI.ArcGIS.esriSystem 
Imports ESRI.ArcGIS.SystemUI 
Imports ESRI.ArcGIS.ADF 
Imports ESRI.ArcGIS.Geodatabase 
Imports ESRI.ArcGIS.Geometry 
Imports ESRI.ArcGIS.Controls 
Imports ESRI.ArcGIS.Carto 
Imports ESRI.ArcGIS.DataSourcesFile 
 
 
Public Class CreateTIN 
    Inherits Windows.Forms.Form 
 
     
 
    Private strFileName As String 
 
    Dim pWorkspaceFactory As IWorkspaceFactory 
    Dim pFeatureWorkspace As IFeatureWorkspace 
    Dim pFeatureClass As IFeatureClass 
    Dim pFeatureLayer As IFeatureLayer 
 
    Dim m_selHIndex As Integer = 0 
    Dim m_selTIndex As Integer = 0 
     
 
    Private Sub btnInput_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInput.Click 
 
        AddShapefile() 
 
        '将获得的SHP文件全名显示在txtInput中 
        txtInput.AppendText(pFeatureLayer.Name) 
 
        '将 pFeatureWorkspace 的所有字段添加到COMBOX控件列表中 
        cboHeightField.Refresh() 
 
        Dim lFieldCount As Long 
        Dim pFClassPointFields As IFields 
 
        If pFeatureClass Is Nothing Then 
            Exit Sub 
        End If 
 
        pFClassPointFields = pFeatureClass.Fields 
 
        lFieldCount = pFClassPointFields.FieldCount 
 
        Dim n As Integer 
        For n = 0 To lFieldCount - 1 
            Dim pname As String 
            Dim pField As IField 
            pField = pFClassPointFields.Field(n) 
            pname = pField.Name 
            Dim ppname As String = pname 
            cboHeightField.Items.Add(ppname) 
 
        Next 
 
        cboHeightField.Refresh() 
    End Sub 
 
    Private Sub cboHeightField_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboHeightField.SelectionChangeCommitted 
        m_selHIndex = cboHeightField.SelectedIndex 
    End Sub 
 
    Private Sub cboTriangu_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTriangu.SelectionChangeCommitted 
        m_selTIndex = cboTriangu.SelectedIndex 
    End Sub 
     
 
    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click 
        CreateTinFromFeature() 
    End Sub 
 
 
    Private Sub AddShapefile() 
        '获取当前路径和文件名 
        Dim dlg As OpenFileDialog 
        Dim fileName As String 
        Dim filepath As String 
 
        dlg = New OpenFileDialog 
        dlg.Filter = "Shape(*.shp)|*.shp|all files(*.*)|*.*" 
        dlg.InitialDirectory = "D:\" 
        dlg.Title = "打开Shapefile数据" 
        dlg.Multiselect = True 
        dlg.ShowDialog() 
 
        Dim strFullPath As String = dlg.FileName 
        If strFullPath Is Nothing Then 
            Exit Sub 
        End If 
 
        Dim Index As Integer = strFullPath.LastIndexOf("\") 
        If Index <= 0 Then 
            Exit Sub 
        Else 
            filepath = strFullPath.Substring(0, Index) 
            fileName = strFullPath.Substring(Index + 1) 
        End If 
 
 
        '打开工作空间并添加shp文件 
        pWorkspaceFactory = New ShapefileWorkspaceFactory() 
        pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filepath, 0) 
 
        pFeatureLayer = New FeatureLayerClass() 
        pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName) 
        pFeatureLayer.FeatureClass = pFeatureClass 
        pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName 
 
    End Sub 
 
    Private Sub CreateTinFromFeature() 
 
        Dim pGeoDataset As IGeoDataset 
        pGeoDataset = pFeatureClass 
 
        Dim pExtent As IEnvelope 
        pExtent = pGeoDataset.Extent() 
        'TIN将使用通过pExtent设置的空间引用 
        pExtent.SpatialReference = pGeoDataset.SpatialReference 
 
        '获得高程字段 
        Dim pfields As IFields 
        pfields = pFeatureClass.Fields 
        Dim pHeightField As IField 
        pHeightField = pfields.Field(m_selHIndex) 
 
        '创建TIN对象并且得到ITinEdit接口 
        Dim pTinEdit As ITinEdit = New Tin 
        '初始化TIN 
        pTinEdit.InitNew(pExtent) 
 
 
        '调用相应的函数将shape文件(含高程信息)增加到TIN中 
        If m_selTIndex = 0 Then 
            pTinEdit.AddFromFeatureClass(pFeatureClass, Nothing, pHeightField, Nothing, esriTinSurfaceType.esriTinMassPoint) 
        ElseIf m_selTIndex = 1 Then 
            pTinEdit.AddFromFeatureClass(pFeatureClass, Nothing, pHeightField, Nothing, esriTinSurfaceType.esriTinHardLine) 
        ElseIf m_selTIndex = 2 Then 
            pTinEdit.AddFromFeatureClass(pFeatureClass, Nothing, pHeightField, Nothing, esriTinSurfaceType.esriTinSoftLine) 
        Else 
            MessageBox.Show("创建TIN失败!") 
            Me.Close() 
        End If 
 
        '保存TIN 
        pTinEdit.SaveAs(SaveFileDialog1.FileName) 
        pTinEdit.StopEditing(False) 
 
        Me.Close() 
 
    End Sub 
 
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click 
        Me.Close() 
    End Sub 
 
    Private Sub btnSaveAs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click 
        '另存为 
        With SaveFileDialog1 
            .DefaultExt = "" 
            .FileName = strFileName 
            .Filter = "All files(*.*)|*.*" 
            .FilterIndex = 1 
            .InitialDirectory = "d:" 
            .OverwritePrompt = True 
            .Title = "Save File Dialog" 
        End With 
        SaveFileDialog1.ShowDialog() 
 
        '将生成的TIN文件全名显示在txtOutput中 
        txtOutput.AppendText(SaveFileDialog1.FileName) 
    End Sub 
 
 
    
End Class 
posted @ 2010-07-01 10:15  zhh  阅读(2061)  评论(0编辑  收藏  举报