modsdeop.bas

来自「FloodEvaluation-程序是gis方面的程序」· BAS 代码 · 共 1,068 行 · 第 1/3 页

BAS
1,068 行
字号
     Dim pOutSDEPropset As IPropertySet
     Set pOutSDEPropset = New PropertySet
     With pOutSDEPropset
        .SetProperty "Server", "stout"
        .SetProperty "Instance", "sde4_ora"
        .SetProperty "user", "gdb"
        .SetProperty "password", "gdb"
        .SetProperty "version", "SDE.DEFAULT"
     End With

     Dim pInSDEWorkspaceName As IWorkspaceName
     Set pInSDEWorkspaceName = New WorkspaceName
     pInSDEWorkspaceName.connectionProperties = pOutSDEPropset
     pInSDEWorkspaceName.WorkspaceFactoryProgID = "esriCore.SdeWorkspaceFactory.1"

     ' +++ get the name object for the input SDE feature dataset
     Dim pFeatureDatasetName As IFeatureDatasetName
     Set pFeatureDatasetName = New FeatureDatasetName
     Dim pSDEDatasetName As IDatasetName
     Set pSDEDatasetName = pFeatureDatasetName
     pSDEDatasetName.name = "USA"
     Set pSDEDatasetName.WorkspaceName = pInSDEWorkspaceName

     ' +++ create a new Access database to copy the feature dataset into
     Dim pOutAcFact As IWorkspaceFactory
     Set pOutAcFact = New AccessWorkspaceFactory

     Dim pOutAcWorkspaceName As IWorkspaceName
     Set pOutAcWorkspaceName = pOutAcFact.Create("C:\", "Usa", Nothing, 0)

     ' +++ create a new feature datset name object for the output Access
     ' +++ feature dataset, call it "USA"
     Dim pOutAcFeatDSName As IFeatureDatasetName
     Set pOutAcFeatDSName = New FeatureDatasetName

     Dim pOutAcDSName As IDatasetName
     Set pOutAcDSName = pOutAcFeatDSName

     Set pOutAcDSName.WorkspaceName = pOutAcWorkspaceName
     pOutAcDSName.name = "USA"

     ' +++ now do the conversion
     Dim pFdtoFd As IFeatureDataConverter
     Set pFdtoFd = New FeatureDataConverter

     pFdtoFd.ConvertFeatureDataset pFeatureDatasetName, pOutAcFeatDSName, Nothing, "", 1000, 0

     MsgBox "Conversion complete!", vbInformation + vbOKOnly, "提示信息"
     Exit Sub
EH:
    MsgBox "打开SDE库失败" & Chr(13) & ERR.Description, vbInformation, "打开SDE"
End Sub

'''*******************************************************************************************************
'''LoadShps
'''      use the data conversion functions to convert data from a Shapefile to an ArcSDE Geodatabase.
'''*******************************************************************************************************
Sub LoadShps()
     ' +++ Set connection properties. Change the properties to match your
     ' +++ server name, instance, user name and password for your SDE database
       
     On Error GoTo EH
     
     Dim pOutSDEPropset As IPropertySet
     Set pOutSDEPropset = New PropertySet
     With pOutSDEPropset
        .SetProperty "Server", "foundway"
        .SetProperty "Instance", "port:5150"
        .SetProperty "Database", "mydb"              ' Ignored with ArcSDE for Oracle
        .SetProperty "user", "sde"
        .SetProperty "password", "lan811"
        .SetProperty "version", "sde.DEFAULT"
''        .SetProperty "Server", "stout"
''        .SetProperty "Instance", "sde4_ora"
''        .SetProperty "user", "gdb"
''        .SetProperty "password", "gdb"
''        .SetProperty "version", "SDE.DEFAULT"
     End With
     
     ' +++ Create a new feature datset name object for the output SDE feature dataset, call
     ' +++ it "USA"
     Dim pOutSDEWorkspaceName As IWorkspaceName
     Set pOutSDEWorkspaceName = New WorkspaceName
     pOutSDEWorkspaceName.connectionProperties = pOutSDEPropset
     pOutSDEWorkspaceName.WorkspaceFactoryProgID = "esriCore.SdeWorkspaceFactory.1"
   
     Dim pOutSDEFeatDSName As IFeatureDatasetName
     Set pOutSDEFeatDSName = New FeatureDatasetName
     
     Dim pSDEDSname As IDatasetName
     Set pSDEDSname = pOutSDEFeatDSName
     Set pSDEDSname.WorkspaceName = pOutSDEWorkspaceName
     pSDEDSname.name = "ushigh"

     ' +++ Get the name object for the input shapefile workspace
     Dim pInShpWorkspaceName As IWorkspaceName
     Set pInShpWorkspaceName = New WorkspaceName
     pInShpWorkspaceName.pathName = "C:\Program Files\arcgis\arcexe83\ArcObjects Developer Kit\Samples\Data\Usa"
     pInShpWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"

     Dim pInShpFeatCLSNm As IFeatureClassName
     Set pInShpFeatCLSNm = New FeatureClassName
     Dim pShpDatasetName As IDatasetName
     Set pShpDatasetName = pInShpFeatCLSNm
     pShpDatasetName.name = "ushigh"
     Set pShpDatasetName.WorkspaceName = pInShpWorkspaceName

     ' +++ create the new output FeatureClass name object that will be passed
     ' +++ into the conversion function
     Dim pOutputDSName As IDatasetName
     Dim pOutputFCName As IFeatureClassName
     Set pOutputFCName = New FeatureClassName
     Set pOutputDSName = pOutputFCName
     Dim pInDSNAme As IDatasetName
      
     ' +++ Set the new FeatureClass name to be the same as the input FeatureClass name
     Set pInDSNAme = pInShpFeatCLSNm
     pOutputDSName.name = "ushigh"
     ' +++ Open the input Shapefile FeatureClass object, so that we can get its fields
     Dim pName As IName
     Dim pInShpFeatCls As IFeatureClass
     Set pName = pInShpFeatCLSNm
     Set pInShpFeatCls = pName.Open
      
     ' +++ Get the fields for the input feature class and run them through field checker to make sure there are no illegal or duplicate field names
     Dim pOutSDEFlds As IFields
     Dim pInShpFlds As IFields
     Dim pFldChk As IFieldChecker
     Dim i As Long
     Dim pGeoField As IField
     Dim pOutSDEGeoDef As IGeometryDef
     Dim pOutSDEGeoDefEdit As IGeometryDefEdit
     Set pInShpFlds = pInShpFeatCls.Fields
     Set pFldChk = New FieldChecker
     pFldChk.Validate pInShpFlds, Nothing, pOutSDEFlds

     ' +++ Loop through the output fields to find the geometry field
     For i = 0 To pOutSDEFlds.FieldCount
       If pOutSDEFlds.Field(i).Type = esriFieldTypeGeometry Then
         Set pGeoField = pOutSDEFlds.Field(i)
         Exit For
       End If
     Next i

     ' +++ Get the geometry field's geometry definition
     Set pOutSDEGeoDef = pGeoField.GeometryDef

     ' +++ Give the geometry definition a spatial index grid count and grid size
     Set pOutSDEGeoDefEdit = pOutSDEGeoDef
     pOutSDEGeoDefEdit.GridCount = 1
     pOutSDEGeoDefEdit.GridSize(0) = 1500000
      
     ' +++ Now use IFeatureDataConverter::Convert to create the output FeatureDataset and
     ' +++ FeatureClass.
     Dim pShpToFc As IFeatureDataConverter
     Set pShpToFc = New FeatureDataConverter
     pShpToFc.ConvertFeatureClass pInShpFeatCLSNm, Nothing, pOutSDEFeatDSName, _
                        pOutputFCName, Nothing, pOutSDEFlds, "", 1000, 0
          
     MsgBox "完成向SDE库写矢量图!", vbInformation, "信息提示"
     
    Exit Sub
EH:
    MsgBox "向SDE库写矢量图层失败" & Chr(13) & ERR.Description, vbInformation, "向SDE写矢量图层"
End Sub

'''*******************************************************************************************************
'''CreateShapefile
'''*******************************************************************************************************
Public Sub CreateShpfileToSDE()
On Error GoTo ER
  Const strFolder As String = "D:\Data"
  Const strName As String = "MyShapeFile" ' Dont include .shp extension
  Const strShapeFieldName As String = "Shape"
  
  ' Open the folder to contain the shapefile as a workspace
  Dim pFWS As IFeatureWorkspace
  Set pFWS = pWorkspace
  
  ' Set up a simple fields collection
  Dim pFields As IFields
  Dim pFieldsEdit As IFieldsEdit
  Set pFields = New esriCore.Fields
  Set pFieldsEdit = pFields
  
  Dim pField As IField
  Dim pFieldEdit As IFieldEdit
  
  ' Make the shape field
  ' it will need a geometry definition, with a spatial reference
  Set pField = New esriCore.Field
  Set pFieldEdit = pField
  pFieldEdit.name = strShapeFieldName
  pFieldEdit.Type = esriFieldTypeGeometry
  
  Dim pGeomDef As IGeometryDef
  Dim pGeomDefEdit As IGeometryDefEdit
  Set pGeomDef = New GeometryDef
  Set pGeomDefEdit = pGeomDef
  With pGeomDefEdit
    .GeometryType = esriGeometryPolygon
    Set .SpatialReference = New UnknownCoordinateSystem
  End With
  Set pFieldEdit.GeometryDef = pGeomDef
  pFieldsEdit.AddField pField

  ' Add another miscellaneous text field
  Set pField = New esriCore.Field
  Set pFieldEdit = pField
  With pFieldEdit
      .Length = 30
      .name = "MiscText"
      .Type = esriFieldTypeString
  End With
  pFieldsEdit.AddField pField
  
  ' Create the shapefile
  ' (some parameters apply to geodatabase options and can be defaulted as Nothing)
  Dim pFeatClass As IFeatureClass
  Set pFeatClass = pFWS.CreateFeatureClass(strName, pFields, Nothing, _
                                           Nothing, esriFTSimple, strShapeFieldName, "")
  MsgBox "完成矢量文件创建!", vbInformation + vbOKOnly, "提示信息"
  Exit Sub
ER:
  MsgBox "在SDE中创建文件失败" & Chr(13) & ERR.Description, vbInformation + vbOKOnly, "提示信息"
End Sub


'************************************************************************************************'''
''''检测并上载FeatureClass
'************************************************************************************************'''
Public Sub LoadShpfileToSDE(strFile As String, strDir As String, strSdeName As String, _
                            sdeProperSet As IPropertySet, ByRef pSdeFWS As IWorkspace)
On Error GoTo ERH
   
    Dim strFCName As String, bExist As Boolean
    strFCName = Left(strFile, Len(strFile) - 4)
    
    Dim inProperSet As IPropertySet
    Set inProperSet = New PropertySet
    inProperSet.SetProperty "database", strDir
    
    Dim sdeName As String
    sdeName = strSdeName
    
    bExist = SDEFeatureExist(sdeName, pSdeFWS)
    If bExist Then
        MsgBox "指定的SDE图层已经存在,请换名", vbInformation + vbOKOnly, "提示信息"
        
        frmSdeName.Left = (Screen.Width - frmSdeName.Width) / 2
        frmSdeName.Top = (Screen.Height - frmSdeName.Height) / 2
        frmSdeName.strName = strSdeName + "1"

        frmSdeName.Show vbModal
        If frmSdeName.bFlag Then
           sdeName = frmSdeName.strName
        Else
           MsgBox "命名失败", vbInformation + vbOKOnly, "提示信息"
           Exit Sub
        End If
    End If
    
    Call FCLoader(strFCName, sdeName, inProperSet, sdeProperSet)
    Exit Sub
    
ERH:
   MsgBox "上载矢量图层失败", vbInformation + vbOKOnly, "提示信息"
End Sub

'''*******************************************************************************************************
'''检测并向SDE上传栅格数据文件
'''*******************************************************************************************************
Public Sub LoadRasterToSDE(strFile As String, strDir As String, strSdeName As String, _
                           sdeProperSet As IPropertySet, _
                           ByRef pSdeRWS As IWorkspace, _
                           strPswd As String)
On Error GoTo ERH
   
    Dim bExist As Boolean
'    strName As String,strName = Left(strFile, Len(strFile) - 4)
    
'    Dim sdeName As String
'    sdeName = strSdeName
    
    bExist = SDERasterExist(strSdeName, pSdeRWS)
    If bExist Then
        MsgBox "指定的SDE图层已经存在,请换名", vbInformation + vbOKOnly, "提示信息"
        
        frmSdeName.Left = (Screen.Width - frmSdeName.Width) / 2
        frmSdeName.Top = (Screen.Height - frmSdeName.Height) / 2
        frmSdeName.strName = strSdeName + "1"
        frmSdeName.Show vbModal
        If frmSdeName.bFlag Then
           sdeName = frmSdeName.strName
        Else
           MsgBox "命名失败", vbInformation + vbOKOnly, "提示信息"
           Exit Sub
        End If
    End If

    Dim sServer As String, sInstance As String, sDB As String, sUser As String, sPasswd As String
    
    With sdeProperSet
        sServer = .GetProperty("Server")
        sInstance = .GetProperty("Instance")
        sDB = .GetProperty("Database")               ' Ignored with ArcSDE for Oracle
        sUser = .GetProperty("user")
        sPasswd = strPswd '.GetProperty("Password")
    End With
    Call AddRasterToSDE(strDir, strFile, sServer, sInstance, sDB, sUser, sPasswd, strSdeName)

    Exit Sub
ERH:
    MsgBox "SDE栅格图层写入失败" & Chr(13) & ERR.Description, vbInformation, "向SDE写入栅格"
End Sub

'************************************************************************************************'''
''''检测FeatureClass是否存在
'************************************************************************************************'''
Public Function SDEFeatureExist(ByVal strFCName As String, _
                                ByRef pFWS As IWorkspace) As Boolean

On Error GoTo Error_h
    
    Dim pDsName As IDatasetName
    Dim pEnumDSName As IEnumDatasetName
    Set pEnumDSName = pFWS.DatasetNames(esriDTFeatureClass)
    Set pDsName = pEnumDSName.Next

    While Not pDsName Is Nothing
        If pDsName.name = "SDE." & strFCName Then
            SDEFeatureExist = True
            Exit Function
        End If
        Set pDsName = pEnumDSName.Next
    Wend
    SDEFeatureExist = False
    Exit Function
Error_h:
   MsgBox strFCName & "不存在", vbInformation + vbOKOnly, "提示信息" 'MsgLogOut MeName, "SDEFeatureExist", False
End Function

'************************************************************************************************'''
''''检测SDERaster是否存在
'************************************************************************************************'''
Public Function SDERasterExist(ByVal strName As String, _
                                ByRef pRWS As IWorkspace) As Boolean

On Error GoTo Error_h
    
    Dim pDsName As IDatasetName
    Dim pEnumDSName As IEnumDatasetName
    Set pEnumDSName = pRWS.DatasetNames(esriDTRasterDataset)
    Set pDsName = pEnumDSName.Next

    While Not pDsName Is Nothing

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?