wavvylia
路人甲
路人甲
  • 注册日期2003-07-28
  • 发帖数384
  • QQ
  • 铜币555枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:3104回复:7

如何将所画多边形保存为Shape文件

楼主#
更多 发布于:2004-10-11 14:36
<P>我用鼠标画出一些多边形,想将之存为Shape文件,但不知怎么去做,那个兄弟给个思路?多谢了!</P>
<P>所画多边形如图所示:</P>

喜欢0 评分0
wavvylia
路人甲
路人甲
  • 注册日期2003-07-28
  • 发帖数384
  • QQ
  • 铜币555枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2004-10-14 14:11
<P>我的本意是生成Polygon Shape后,用它进行裁切的。现在做出来了,我将代码贴上,希望对大家能所帮助:</P><P>1、创建Shape文件</P><P>Public Function CreateShapefile(sPath As String, sName As String, sSpatial As ISpatialReference) As IFeatureClass  ' Dont include .shp extension
  
   ' Open the folder to contain the shapefile as a workspace
   Dim pFWS As IFeatureWorkspace
   Dim pWorkspaceFactory As IWorkspaceFactory
   Set pWorkspaceFactory = New ShapefileWorkspaceFactory
   Set pFWS = pWorkspaceFactory.OpenFromFile(sPath, 0)
  
   ' Set up a simple fields collection
   Dim pFields As IFields
   Dim pFieldsEdit As IFieldsEdit
   Set pFields = New 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 Field
   Set pFieldEdit = pField
   pFieldEdit.Name = "Shape"
   pFieldEdit.Type = esriFieldTypeGeometry
  
   Dim pGeomDef As IGeometryDef
   Dim pGeomDefEdit As IGeometryDefEdit
   Set pGeomDef = New GeometryDef
   Set pGeomDefEdit = pGeomDef
   With pGeomDefEdit
     .GeometryType = esriGeometryPolygon
     Set .SpatialReference = sSpatial ' New UnknownCoordinateSystem
   End With
   Set pFieldEdit.GeometryDef = pGeomDef
   pFieldsEdit.AddField pField
  
   ' Add another miscellaneous text field
   Set pField = New 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(sName, pFields, Nothing, _
                                            Nothing, esriFTSimple, "Shape", "")
                                            
   Set CreateShapefile = pFeatClass
End Function
2、然后用得到的IpointCollection写入到Shape文件中</P><P>Public Sub InsertPolygonFeatures(pFeatureClass As IFeatureClass, sPointColl As IPointCollection) ', numberToCreate As Long)
  
   Dim pFeatureBuffer As IFeatureBuffer
   Dim pPoint As IPoint
   Dim pPointCollection As IPointCollection
   Dim pTopo As IPolygon 'ITopologicalOperator
   Dim pFeatureCursor As IFeatureCursor
   Set pFeatureCursor = pFeatureClass.Insert(True)
  
   Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer
   Set pTopo = sPointColl
   pTopo.Close
  
   Set pFeatureBuffer.Shape = pTopo
   pFeatureCursor.InsertFeature pFeatureBuffer</P><P>   pFeatureCursor.Flush
End Sub
</P>
举报 回复(0) 喜欢(0)     评分
游客

返回顶部