两部分:

第一部分Shape 数据加载(同时打开多个文件)

第二部分 ao 中一些打开数据的代码--AO学习资料笔记

第一部分Shape 数据加载(同时打开多个文件)

  /// <summary>
  /// Shape 数据加载
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
    private void mnuData_1_1_Click(object sender, System.EventArgs e)
  {
   OpenFileDialog dlg=new OpenFileDialog();
   dlg.Filter="Shape file(*.shp)|*.shp";
   dlg.Title="打开 Shape数据文档";
   dlg.Multiselect=true;
   //定义存放打开 IFeatureClass 的字符串数组
   string[] FilePath;
   if(dlg.ShowDialog()==DialogResult.OK)
   {    
    FilePath=new string[dlg.FileNames.Length];
    FilePath=dlg.FileNames;
    if(FilePath.Length > 0)
    {
     string WorkSpacePath =System.IO.Path.GetDirectoryName(FilePath[0]);
     string[] ShapeFilePath=new string[FilePath.Length] ;
     //获得打开 IFeatureClass 的字符串数组
     for(int i=0;i<FilePath.Length;i++)
     {
      ShapeFilePath[i]=System.IO.Path.GetFileName(FilePath[i]);
     }
     IWorkspaceFactory pWorkspaceFactory=new ShapefileWorkspaceFactoryClass();
     IWorkspace pWorkspace=pWorkspaceFactory.OpenFromFile(WorkSpacePath,0);
     IFeatureWorkspace pFeatureWorkspace=pWorkspace as IFeatureWorkspace;
     for(int i=0;i<ShapeFilePath.Length;i++)
     {
      IFeatureClass pFeatureClass=pFeatureWorkspace.OpenFeatureClass(ShapeFilePath[i]);
      IDataset pDataset=pFeatureClass as IDataset;

      IFeatureLayer pFeatureLayer=new FeatureLayerClass();
      pFeatureLayer.FeatureClass=pFeatureClass;
      pFeatureLayer.Name=pDataset.Name;
      ILayer pLayer=pFeatureLayer as ILayer;
      this.axMapControl1.Map.AddLayer(pLayer);
     }
    }
   }
   
  }

第二部分 ao 中一些打开数据的代码--AO学习资料笔记

'----------------------------------------------------------------
'函数功能:从本地文件打开一个 FeatureClass
'----------------------------------------------------------------
Public Function OpenFC(sPath As String, sName As String) As IFeatureClass
  Dim pWSF As IWorkspaceFactory
  Dim pFCWS As IFeatureWorkspace
  Dim pFC As IFeatureClass
    
  Set pWSF = New ShapefileWorkspaceFactory
  Set pFCWS = pWSF.OpenFromFile(sPath, 0)
 
  Set pFC = pFCWS.OpenFeatureClass(sName)

Set OpenFC = pFC
  Set pFC = Nothing
End Function
'-----------------------------------
'函数功能:删除制定字段
'-----------------------------------
Sub DeleteField(pFC As IFeatureClass, sFieldName As String)
          
        Dim i As Integer
        Dim pFields As IFields
        Set pFields = pFC.Fields
 
        Dim pField As iField
    
    
           i = pFields.FindField(sFieldName)
           If i >= 0 Then
               Set pField = pFields.Field(i)
                pFC.DeleteField pField
          End If
 
End Sub
'--------------------------------------------------------
'函数功能:从本地文件打开一个 dbase 表
'--------------------------------------------------------
Public Function OpenTable(sFilePath As String, sTableName As String) As ITable
  Dim pWorkspace As IWorkspace
  Dim pFact As IWorkspaceFactory
  Set pFact = New ShapefileWorkspaceFactory
    
  Set pWorkspace = pFact.OpenFromFile(sFilePath, 0)
  Dim pFWorkspace As IFeatureWorkspace
  Set pFWorkspace = pWorkspace
  
  Dim pTable As ITable
  Set pTable = pFWorkspace.OpenTable(sTableName)
 
  Set OpenTable = pTable
  Set pTable = Nothing
  
End Function

'---------------------------------------------
'函数功能:打开本地的栅格影像
'---------------------------------------------
Public  Function  OpenRasterDataset(sDir  As  String,  sFile  As  String)  As
IRasterDataset
 
    'Open the raster dataset with the given name.
    'sDir is the directory the file resides
    'sFile is the filename
    
    Dim pWsFact As IWorkspaceFactory
    Dim pWS As IRasterWorkspace
    Dim pRasterDataset As IRasterDataset
 
 
    'Open the workspace
    Set pWsFact = New RasterWorkspaceFactory
    Set pWS = pWsFact.OpenFromFile(sDir, 0)
 
    
    'Open the raster dataset
    Set pRasterDataset = pWS.OpenRasterDataset(sFile)
 
 
    'Return
    Set OpenRasterDataset = pRasterDataset
 
    Set pWsFact = Nothing
    Set pWS = Nothing
    Set pRasterDataset = Nothing
 
End Function
'-------------------------
'创建一个 dbf 表
'-------------------------
Public Function createDBF(strName As String, strFolder As String, Optional
pFields As IFields) As ITable
' createDBF: simple function to create a DBASE file.
' note: the name of the DBASE file should not contain the .dbf extension
'
  On Error GoTo EH
  
  ' Open the Workspace
  Dim pFWS As IFeatureWorkspace

Dim pWorkspaceFactory As IWorkspaceFactory
  Dim fs As Object
  Dim pFieldsEdit As IFieldsEdit
  Dim pFieldEdit As IFieldEdit
  Dim pField As iField
 
  Set pWorkspaceFactory = New ShapefileWorkspaceFactory
  Set fs = CreateObject("Scripting.FileSystemObject")
  If Not fs.FolderExists(strFolder) Then
    MsgBox "Folder does not exist: " & vbCr & strFolder
    Exit Function
  End If
 
  Set pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)
  
  ' if a fields collection is not passed in then create one
  If pFields Is Nothing Then
    ' create the fields used by our object
    Set pFields = New Fields
    Set pFieldsEdit = pFields
    pFieldsEdit.FieldCount = 1
    
    'Create text Field
    Set pField = New Field
    Set pFieldEdit = pField
    With pFieldEdit
        .length = 30
        .Name = "TextField"
        .Type = esriFieldTypeString
    End With
    Set pFieldsEdit.Field(0) = pField
  End If
  
  Set createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")
  
  Exit Function
EH:
    MsgBox Err.Description, vbInformation, "createDBF"
 
End Function
'----------------------------------------------------
'判断一个属性表中是否存在指定字段
'----------------------------------------------------
Public Function FieldIsExist(pFC As IFeatureClass, sFieldName As String) AsBoolean
        FieldIsExist = False
        Dim i As Integer
        Dim pFields As IFields
        Set pFields = pFC.Fields
 
        Dim pField As iField
 
        i = pFields.FindField(sFieldName)
         If i >= 0 Then
             FieldIsExist = True
         End If
 
End Function
'---------------------------
' 得到文档的路径
'---------------------------
Public Function GetDocPath() As String
      Dim pTemplates As ITemplates
      Dim lTempCount As Long
      Dim strNormalPath As String
      Dim strBasePath As String
      Dim strDocPath As String
      
      Set pTemplates = Application.Templates
      lTempCount = pTemplates.Count
      
      ' Normal is always the first item
      strNormalPath = pTemplates.Item(0)
      
      ' The document is always the last item
      strDocPath = pTemplates.Item(lTempCount - 1)
      
      ' If present, the base template is the middle item
      If lTempCount = 3 Then
        strBasePath = pTemplates.Item(1)
      Else
        strBasePath = "NO BASE TEMPLATE LOADED"
      End If
      
      strDocPath = StrReverse(strDocPath)
      Dim inPos As Integer
      inPos = InStr(strDocPath, "\")
      strDocPath = VBA.Right(strDocPath, Len(strDocPath) - inPos)

GetDocPath = StrReverse(strDocPath)
End Function
'----------------------------
'打开一个 workspace
'----------------------------
Public Function OpenWS(sPathName As String) As IWorkspace
        Dim pWSF As IWorkspaceFactory
        Dim pWS As IWorkspace
        
        Set pWSF = New ShapefileWorkspaceFactory
        Set pWS = pWSF.OpenFromFile(sPathName, 0)
         
        Set OpenWS = pWS
        Set pWS = Nothing
        
End Function
 
'--------------------------------------------------
'将本地 shape 文件添加到图层中显示
'--------------------------------------------------
 
Public Sub AddShapeFile(sPath As String, sName As String)
    
    Dim pWorkspaceFactory As IWorkspaceFactory
    Dim pFeatureWorkspace As IFeatureWorkspace
    Dim pFeatureLayer As IFeatureLayer
    Dim pMxDocument As IMxDocument
    Dim pMap As IMap
    
    'Create a new ShapefileWorkspaceFactory object and open a shapefile folder
    Set pWorkspaceFactory = New ShapefileWorkspaceFactory
    Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sPath, 0)
    'Create a new FeatureLayer and assign a shapefile to it
    Set pFeatureLayer = New FeatureLayer
    Set  pFeatureLayer.FeatureClass  =
pFeatureWorkspace.OpenFeatureClass(sName)
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
    'Add the FeatureLayer to the focus map
    Set pMxDocument = Application.Document
    Set pMap = pMxDocument.FocusMap
    pMap.AddLayer pFeatureLayer
 
End Sub

posted on 2010-10-08 11:38  hl3292  阅读(2788)  评论(0编辑  收藏  举报