面朝大海,春暖华开

focus on scientific computue, 3dgis, spatial database
专注于科学计算、GIS空间分析

 

arcEngine classic code(2)

1 新建shp文件
Public Sub CreatShapeFile(ByVal sFilePath As String, ByVal sFileName As String)     '创建shapefile层文件
On Error GoTo Errhandle:
    Dim pWorkspaceFactory As IWorkspaceFactory
    Dim pFeatureWorkspace As IFeatureWorkspace
    Dim pFields As IFields
    Dim pFieldsEdit As IFieldsEdit
    Dim pField As IField
    Dim pFieldEdit As IFieldEdit
    Dim pGeometryDef As IGeometryDef
    Dim pGeometryDefEdit As IGeometryDefEdit
    Dim pFeatClass As IFeatureClass
    Dim sShapeFieldName As String
    Dim sNewShapeFileName As String

    sNewShapeFileName = Dir(sFilePath & "\" & sFileName & ".shp")
    If (sNewShapeFileName <> "") Then
             MsgBox ("文件已经存在")
        Exit Sub
    End If
    sShapeFieldName = "Shape"                                '先创建一个字段名字
    '创建一个文件夹来存放shapefile文件
    Set pWorkspaceFactory = New ShapefileWorkspaceFactory
    Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0)
   
    'Set up a simple fields collection
    Set pFields = New esriGeoDatabase.Fields
    Set pFieldsEdit = pFields
    'Make the shape field
    'it will need a geometry definition, with a spatial reference
    Set pField = New esriGeoDatabase.Field
    Set pFieldEdit = pField
    '创建第一个字段
    pFieldEdit.Name = sShapeFieldName
    pFieldEdit.Type = esriFieldTypeGeometry
    Set pGeometryDef = New GeometryDef
    Set pGeometryDefEdit = pGeometryDef
    With pGeometryDefEdit
    .GeometryType = esriGeometryPolygon
    Set .SpatialReference = New UnknownCoordinateSystem
    End With
    Set pFieldEdit.GeometryDef = pGeometryDef
    pFieldsEdit.AddField pField               '添加字段到字段集中
    '再添加一个字段
    Set pField = New esriGeoDatabase.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Name = "type"
        .Type = esriFieldTypeString
    End With
    pFieldsEdit.AddField pField               '添加字段到字段集中
    '开始创建shapefile层文件
    '(some parameters apply to geodatabase options and can be defaulted as Nothing)
    Set pFeatClass = pFeatureWorkspace.CreateFeatureClass _
    (sFileName, pFields, Nothing, Nothing, _
    esriFTSimple, sShapeFieldName, "")
'    sNewShapeFileName = Dir(sFilePath & "\" & sFileName & ".shp")
'      If (sNewShapeFileName = "") Then
'        MsgBox ("Build Fail")
'    Else
'        MsgBox ("Build Success")
'    End If
Errhandle:
    Set pFeatClass = Nothing
    Set pGeometryDefEdit = Nothing
    Set pGeometryDef = Nothing
    Set pFieldEdit = Nothing
    Set pField = Nothing
    Set pFieldsEdit = Nothing
    Set pFields = Nothing
    Set pFeatureWorkspace = Nothing
    Set pWorkspaceFactory = Nothing


    If Err.Description <> "" Then
    MsgBox Err.Description & ":创建shapefile失败!", vbInformation, "提示信息"
    End If
End Sub

posted on   风过 无痕  阅读(472)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

统计

向日葵支付宝收钱码
点击右上角即可分享
微信分享提示