Insert Features using Ifeatureclass.createFeaturebuffer method

本文提供了一个使用 ArcObjects SDK 在不依赖默认内置函数的情况下手动复制要素的示例。该示例详细展示了如何从输入要素类读取数据并将其正确地插入到输出要素类中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Insert new Feature not using default built-in function..ESRI放出的例子

Completely show how to correctly insert features from InputFeatureclass to OutputFeatureClass 

 


ExpandedBlockStart.gifContractedBlock.gif
Public Sub LoadFeatures()Sub LoadFeatures()
  
Dim pInFeatureClass As IFeatureClass
  
Dim pOutFeatureClass As IFeatureClass
  
Dim pSearchFeatureCursor As IFeatureCursor
  
Dim pFeature As IFeature
  
Dim pInsertFeatureBuffer As IFeatureBuffer
  
Dim pInsertFeatureCursor As IFeatureCursor
  
Dim NewFeatureCount As Integer
  
  
On Error GoTo ErrorHandler
  
  
'Open shapefile where new features will be written to
  'For simplicity, sample does not contain code to create a new shapefile
  Set pOutFeatureClass = OpenFeatureClass("d:\data\usa""test")
  
If pOutFeatureClass Is Nothing Then Exit Sub
  
Set pInsertFeatureCursor = pOutFeatureClass.Insert(True)
  
Set pInsertFeatureBuffer = pOutFeatureClass.CreateFeatureBuffer
  
  
'Open shapefile containing the features that will be copied
  Set pInFeatureClass = OpenFeatureClass("d:\data\usa""counties")
  
If pInFeatureClass Is Nothing Then Exit Sub

  
'Loop through all the features in InFeatureClass
  Set pSearchFeatureCursor = pInFeatureClass.Search(NothingTrue)
  
Set pFeature = pSearchFeatureCursor.NextFeature
  
Do While Not pFeature Is Nothing
    
'Add the original feature's geometry to the feature buffer
    Set pInsertFeatureBuffer.Shape = pFeature.Shape
    
'Add all the original feature's fields to the feature buffer
    AddFields pInsertFeatureBuffer, pFeature
    
'Insert the feature into the cursor
    pInsertFeatureCursor.InsertFeature pInsertFeatureBuffer
    NewFeatureCount 
= NewFeatureCount + 1
    
'Flush the feature cursor every 100 features
    'This is safer because you can write code to handle a flush error
    'If you don't flush the feature cursor it will automatically flush but
    'after all of your code executes at which time you have no control
    If NewFeatureCount = 100 Then
      pInsertFeatureCursor.Flush
      NewFeatureCount 
= 0
    
End If
    
Set pFeature = pSearchFeatureCursor.NextFeature
  
Loop
  pInsertFeatureCursor.Flush 
'Flush the cursor one last time
  
  
Exit Sub 'Exit to avoid error handler
  
ErrorHandler:
  
MsgBox Err.Description
  
Resume Next
End Sub


ExpandedBlockStart.gifContractedBlock.gif
Private Sub AddFields()Sub AddFields(pFeatureBuffer As IFeatureBuffer, pFeature As IFeature)
  
Dim pRowBuffer As IRowBuffer
  
Dim pNewFields As IFields 'fields on target feature class
  Dim pNewField As IField
  
Dim pFields As IFields 'fields on original feature class
  Dim pField As IField
  
Dim FieldCount As Integer
  
Dim NewFieldIndex As Long
  
  
'Copy the attributes of the orig feature the new feature
  Set pRowBuffer = pFeatureBuffer
  
Set pNewFields = pRowBuffer.Fields
  
  
Set pFields = pFeature.Fields
  
For FieldCount = 0 To pFields.FieldCount - 1
    
Set pField = pFields.Field(FieldCount)
    
If Not pField.Type = esriFieldTypeGeometry And Not pField.Type = esriFieldTypeOID _
      
And pField.Editable Then
        NewFieldIndex 
= pNewFields.FindField(pField.Name)
        
If Not NewFieldIndex = -1 Then
          pFeatureBuffer.Value(NewFieldIndex) 
= pFeature.Value(FieldCount)
        
End If
    
End If
  
Next FieldCount
End Sub


ExpandedBlockStart.gifContractedBlock.gif
Public Function OpenFeatureClass()Function OpenFeatureClass(strWorkspace As String, strFeatureClass As StringAs IFeatureClass
  
On Error GoTo ErrorHandler
  
Dim pShpWorkspaceName As IWorkspaceName
  
Dim pDatasetName As IDatasetName
  
Dim pName As IName
  
  
'Create the workspace name object
  Set pShpWorkspaceName = New WorkspaceName
  pShpWorkspaceName.PathName 
= strWorkspace
  pShpWorkspaceName.WorkspaceFactoryProgID 
= "esriCore.shapefileworkspacefactory.1"
  
  
'Create the feature class name object
  Set pDatasetName = New FeatureClassName
  pDatasetName.Name 
= strFeatureClass
  
Set pDatasetName.WorkspaceName = pShpWorkspaceName
  
  
'Open the feature class
  Set pName = pDatasetName
  
Set OpenFeatureClass = pName.Open
  
  
Exit Function
  
ErrorHandler:
  
Set OpenFeatureClass = Nothing
End Function


转载于:https://www.cnblogs.com/iswszheng/archive/2009/03/18/1415478.html

using System; using System.Drawing; using System.Linq; using System.Text; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Runtime.InteropServices; using MapControlApplication1; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.ADF; using ESRI.ArcGIS.SystemUI; using AE_test; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Desktop.AddIns; namespace MapControlApplication1 { public delegate void AfterDrawGeometry(IGeometry geometry); public class DrawPolygoncs : ITool { private IGeometry _polygon=null;//定义一个几何对象,作为绘制结果 private INewPolygonFeedback _polyFeedback=null;//定义一个多边形反馈对象 private IPoint _startPoint=null;//多边形起始结点 private IPoint _endPoint=null;//多边形终止结点 private bool _drawStart=false;//多边形绘制开始标记 public event AfterDrawGeometry eventAfterDrawGeometry; protected AxMapControl myMapControl=null; protected ESRI.ArcGIS.Controls.IHookHelper myHook; //返回结果多边形 public IGeometry Polygon { get{ return _polygon;} } public override void OnCreate(object hook) { myHook.Hook= hook; if(myHook == null) myHook=new ESRI.ArcGIS.Controls.HookHelperClass(); if(_drawStart) { (myHook.Hook as IMapControl3).CurrentTool=this; _polyFeedback = new NewPolygonFeedbackClass(); _polyFeedback.Display=myHook.ActiveView.ScreenDisplay; } } public override void OnClick()//单击鼠标开始绘图或添加结点 { _polygon=null;//每次重设多边形为空值 _drawStart=true;//开始绘制标记置为true (myHook.Hook as IMapControl).CurrentTool=this; _polyFeedback=new NewPolygonFeedbackClass(); _polyFeedback.Display=myHook. ActiveView. ScreenDisplay; } public override void OnMouseDown(int Button, int Shift, int X, int Y) { if(Button ==1) { if(_startPoint == null)//如果是多边形第一个结点 { _startPoint =(myHook.FocusMap as IActiveView).ScreenDisplay. DisplayTransformation.ToMapPoint(X,Y); _polyFeedback.Start(_startPoint);//开始多边形绘制 } else { _endPoint =(myHook.FocusMap as IActiveView).ScreenDisplay. DisplayTransformation.ToMapPoint(X,Y); _polyFeedback.AddPoint(_endPoint);//添加多边形绘制结点 } } } public override void OnMouseMove(int Button, int Shift, int X, int Y) { if(_startPoint !=null) { IPoint movePoint=(myHook.FocusMap as IActiveView).ScreenDisplay. DisplayTransformation.ToMapPoint(X,Y); _polyFeedback.MoveTo(movePoint);//鼠标移动过程中实时显示反馈效果 } } public override void Refresh(int hDC) { base.Refresh(hDC); if (_polyFeedback !=null) { (_polyFeedback as IDisplayFeedback).Refresh(hDC);//实时显示反馈效果 } } public override void OnDblClick()//双击鼠标结束绘图 { _polygon=_polyFeedback.Stop(); _startPoint=null; _drawStart=false; } int ITool.Cursor { get { return 0; } // 返回默认光标 } bool ITool.Deactivate() { return true; // 允许工具取消激活 } bool ITool.OnContextMenu(int x, int y) { return false; // 不处理右键菜单 } void ITool.OnDblClick() { OnDblClick(); // 调用自定义的双击事件 } public void OnKeyDown(int keyCode, int shift) { throw new NotImplementedException(); } public void OnKeyUp(int keyCode, int shift) { throw new NotImplementedException(); } public void OnMouseUp(int button, int shift, int x, int y) { throw new NotImplementedException(); } } }
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值