免费视频|新人指南|投诉删帖|广告合作|地信网APP下载

查看: 2333|回复: 1
收起左侧

如何创建Shape文件附代码

[复制链接]

1986

主题

10万

铜板

98

好友

技术员

Network change life, change t

积分
17879

斑竹勋章地信元老

QQ
发表于 2009-12-15 09:03 | 显示全部楼层 |阅读模式
如何创建Shape文件:
本例实现的是如何创建一个Shape文件。
l   要点
首先创建新IField接口实例,生成新字段,并获得该实例的IFieldEdit接口对象,用FieldsEdit的AddField方法将新字段加入到IFields接口对象中,最后用IFeatureWorkspace的CreateFeatureClass方法生成新的Shape文件
主要用到IFeatureWorkspace接口,IWorkspaceFactory接口,IFieldsEdit接口,IFieldEdit接口,IFeatureClass接口。
l   程序说明
函数CreatShapeFile根据输入的文件路径和文件名,创建Shape文件。
l   代码
Private Sub CreatShapeFile(ByVal sFilePath As String, ByVal sFileName As String)
    Dim pFeatureWorkspace           As IFeatureWorkspace
    Dim pWorkspaceFactory           As IWorkspaceFactory
    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
On Error GoTo ErrorHandler:
    sNewShapeFileName = Dir(sFilePath & sFileName & ".shp")
    If (sNewShapeFileName <> "") Then
        MsgBox ("文件已经存在")
        Exit Sub
    End If
    sShapeFieldName = "Shape"

    'Open the folder to contain the shapefile as a workspace
    Set pWorkspaceFactory = New ShapefileWorkspaceFactory
    Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0)
    'Set up a simple fields collection
    Set pFields = New esriCore.Fields
    Set pFieldsEdit = pFields
    'Make the shape field
    'it will need a geometry definition, with a spatial reference
    Set pField = New esriCore.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
    'Add others miscellaneous text field
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Name = "SmallInteger"
        .Type = esriFieldTypeSmallInteger
    End With

    pFieldsEdit.AddField pField
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Name = "Integer"
        .Type = esriFieldTypeInteger
    End With
    pFieldsEdit.AddField pField
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Name = "Single"
        .Type = esriFieldTypeSingle
    End With
    pFieldsEdit.AddField pField
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Precision = 5
        .Scale = 5
        .Name = "Double"
        .Type = esriFieldTypeDouble
    End With
    pFieldsEdit.AddField pField
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Length = 30
        .Name = "String"
        .Type = esriFieldTypeString
    End With
    pFieldsEdit.AddField pField
    Set pField = New esriCore.Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Name = "Date"
        .Type = esriFieldTypeDate
    End With
    pFieldsEdit.AddField pField     
    'Create the 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 & "\MyShapeFile.shp")
    If (sNewShapeFileName = "") Then
        MsgBox ("Build Success")
    Else
        MsgBox ("Build Fail")
    End If
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub
Private Sub UIButtonControl1_Click()
    Dim pVBProject              As VBProject
On Error GoTo ErrorHandler:
    Set pVBProject = ThisDocument.VBProject
    'Dont include .shp extension
    CreatShapeFile pVBProject.FileName & "\..\..\..\.." & "\data\", "MyShapeFile"
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub
Private Sub UIButtonControl1_Click()
    Dim pVBProject              As VBProject
On Error GoTo ErrorHandler:
    Set pVBProject = ThisDocument.VBProject
    'Dont include .shp extension
    CreatShapeFile pVBProject.FileName & "\..\..\..\.." & "\data\", "MyShapeFile"
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub
轻轻的我来签到了,想带走一堆铜板...

1145

主题

10万

铜板

2

好友

传奇会员

Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30

积分
21817

灌水勋章活跃勋章冰雪节勋章

QQ
发表于 2013-11-10 20:20 | 显示全部楼层
进来看看 学习学习

评分

参与人数 1铜板 +1 收起 理由
admin + 1 亲,你好快哦~~~

查看全部评分

加强科技支撑和引领  实现地质找矿新突破 。     
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线客服
快速回复 返回顶部 返回列表