肥猫shp编程.docx
- 文档编号:10516952
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:8
- 大小:16.04KB
肥猫shp编程.docx
《肥猫shp编程.docx》由会员分享,可在线阅读,更多相关《肥猫shp编程.docx(8页珍藏版)》请在冰点文库上搜索。
肥猫shp编程
1.3.GeoDataBase
1.3.1.如何加载Shape文件
本例实现的是在ArcMap中连接指定的Shape文件,并将其加载到当前激活
的Map中。
要点
通过FeatureLayer类实现IFeatureLayer接口对象,设置
IFeatureLayer.FeatureClass属性和Name属性,使用IMap.AddLayer方法将新
层添加到当前地图。
利用IWorkspaceFacktory接口、IFeatureWorkspace接口
和IFeatureLayer接口实现连接Shape文件
程序说明
函数OpenShapeFile根据输入的Shape文件路径sFilePath,将文件名为
sFileName的Shape文件连接到当前激活的Map中去。
代码
PrivateSubOpenShapeFile(ByValsFilePathAsString,ByValsFileNameAsString)
DimpWorkspaceFactoryAsIWorkspaceFactory
DimpFeatureWorkspaceAsIFeatureWorkspace
DimpFeatureLayerAsIFeatureLayer
DimpMxDocumentAsIMxDocument
DimpMapAsIMap
DimsDirAsString
OnErrorGoToErrorHandler:
sDir=Dir(sFilePath&"\"&sFileName&".shp")
If(sDir="")Then
sDir=Dir(sFilePath&"\"&sFileName)
If(sDir="")Then
MsgBox("文件不存在")
-30-
ExitSub
EndIf
EndIf
'CreateanewShapefileWorkspaceFactoryobjectandopenashapefilefolder
SetpWorkspaceFactory=NewShapefileWorkspaceFactory
SetpFeatureWorkspace=pWorkspaceFactory.OpenFromFile(sFilePath,0)
'CreateanewFeatureLayerandassignashapefiletoit
SetpFeatureLayer=NewFeatureLayer
SetpFeatureLayer.FeatureClass=pFeatureWorkspace.OpenFeatureClass(sFileName)
pFeatureLayer.Name=pFeatureLayer.FeatureClass.AliasName
'AddtheFeatureLayertothefocusmap
SetpMxDocument=Application.Document
SetpMap=pMxDocument.FocusMap
pMap.AddLayerpFeatureLayer
ExitSub
ErrorHandler:
MsgBoxErr.Description
EndSub
PrivateSubUIButtonControl1_Click()
DimpVBProjectAsVBProject
OnErrorGoToErrorHandler:
SetpVBProject=ThisDocument.VBProject
OpenShapeFilepVBProject.FileName&"\..\..\..\.."&"\data\","Continents"
ExitSub
ErrorHandler:
MsgBoxErr.Description
EndSub
1.3.6.如何创建Shape文件
本例实现的是如何创建一个Shape文件。
要点
首先创建新IField接口实例,生成新字段,并获得该实例的IFieldEdit接
口对象,用FieldsEdit的AddField方法将新字段加入到IFields接口对象中,
最后用IFeatureWorkspace的CreateFeatureClass方法生成新的Shape文件
主要用到IFeatureWorkspace接口,IWorkspaceFactory接口,IFieldsEdit
接口,IFieldEdit接口,IFeatureClass接口。
程序说明
函数CreatShapeFile根据输入的文件路径和文件名,创建Shape文件。
代码
PrivateSubCreatShapeFile(ByValsFilePathAsString,ByValsFileNameAsString)
DimpFeatureWorkspaceAsIFeatureWorkspace
DimpWorkspaceFactoryAsIWorkspaceFactory
DimpFieldsAsIFields
DimpFieldsEditAsIFieldsEdit
DimpFieldAsIField
DimpFieldEditAsIFieldEdit
DimpGeometryDefAsIGeometryDef
DimpGeometryDefEditAsIGeometryDefEdit
DimpFeatClassAsIFeatureClass
DimsShapeFieldNameAsString
DimsNewShapeFileNameAsString
OnErrorGoToErrorHandler:
sNewShapeFileName=Dir(sFilePath&sFileName&".shp")
If(sNewShapeFileName<>"")Then
MsgBox("文件已经存在")
ExitSub
EndIf
sShapeFieldName="Shape"
'Openthefoldertocontaintheshapefileasaworkspace
SetpWorkspaceFactory=NewShapefileWorkspaceFactory
SetpFeatureWorkspace=pWorkspaceFactory.OpenFromFile(sFilePath,0)
-38-
'Setupasimplefieldscollection
SetpFields=NewesriCore.Fields
SetpFieldsEdit=pFields
'Maketheshapefield
'itwillneedageometrydefinition,withaspatialreference
SetpField=NewesriCore.Field
SetpFieldEdit=pField
pFieldEdit.Name=sShapeFieldName
pFieldEdit.Type=esriFieldTypeGeometry
SetpGeometryDef=NewGeometryDef
SetpGeometryDefEdit=pGeometryDef
WithpGeometryDefEdit
.GeometryType=esriGeometryPolygon
Set.SpatialReference=NewUnknownCoordinateSystem
EndWith
SetpFieldEdit.GeometryDef=pGeometryDef
pFieldsEdit.AddFieldpField
'Addothersmiscellaneoustextfield
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Name="SmallInteger"
.Type=esriFieldTypeSmallInteger
EndWith
pFieldsEdit.AddFieldpField
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Name="Integer"
.Type=esriFieldTypeInteger
EndWith
pFieldsEdit.AddFieldpField
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Name="Single"
.Type=esriFieldTypeSingle
EndWith
pFieldsEdit.AddFieldpField
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Precision=5
.Scale=5
.Name="Double"
.Type=esriFieldTypeDouble
EndWith
pFieldsEdit.AddFieldpField
-39-
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Length=30
.Name="String"
.Type=esriFieldTypeString
EndWith
pFieldsEdit.AddFieldpField
SetpField=NewesriCore.Field
SetpFieldEdit=pField
WithpFieldEdit
.Name="Date"
.Type=esriFieldTypeDate
EndWith
pFieldsEdit.AddFieldpField
'Createtheshapefile
'(someparametersapplytogeodatabaseoptionsandcanbedefaultedasNothing)
SetpFeatClass=pFeatureWorkspace.CreateFeatureClass_
(sFileName,pFields,Nothing,Nothing,_
esriFTSimple,sShapeFieldName,"")
sNewShapeFileName=Dir(sFilePath&"\MyShapeFile.shp")
If(sNewShapeFileName="")Then
MsgBox("BuildSuccess")
Else
MsgBox("BuildFail")
EndIf
ExitSub
ErrorHandler:
MsgBoxErr.Description
EndSub
PrivateSubUIButtonControl1_Click()
DimpVBProjectAsVBProject
OnErrorGoToErrorHandler:
SetpVBProject=ThisDocument.VBProject
'Dontinclude.shpextension
CreatShapeFilepVBProject.FileName&"\..\..\..\.."&"\data\","MyShapeFile"
ExitSub
ErrorHandler:
MsgBoxErr.Description
EndSub
PrivateSubUIButtonControl1_Click()
DimpVBProjectAsVBProject
OnErrorGoToErrorHandler:
SetpVBProject=ThisDocument.VBProject
'Dontinclude.shpextension
CreatShapeFilepVBProject.FileName&"\..\..\..\.."&"\data\","MyShapeFile"
ExitSub
ErrorHandler:
-40-
MsgBoxErr.Description
EndSub
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- shp 编程
![提示](https://static.bingdoc.com/images/bang_tan.gif)