新建shapefile,自定义字段,新加入记录

             // 生成新的shapefile
            IFields pFields  =   new  FieldsClass();
            IFieldsEdit pFieldsEdit 
=  pFields  as  IFieldsEdit;
            IField pField 
=   new  FieldClass();
            IFieldEdit pFieldEdit 
=  pField  as  IFieldEdit;

            pFieldEdit.Name_2 
=   " shape " ;
            pFieldEdit.Type_2 
=  esriFieldType.esriFieldTypeGeometry;

            IGeometryDef pGeometryDef 
=   new  GeometryDefClass();
            IGeometryDefEdit pGeoDefEdit 
=  pGeometryDef  as  IGeometryDefEdit;
            pGeoDefEdit.GeometryType_2 
=  esriGeometryType.esriGeometryPolygon;
            pGeoDefEdit.SpatialReference_2 
=  axMapControl1.SpatialReference;

            pFieldEdit.GeometryDef_2 
=  pGeometryDef;
            pFieldsEdit.AddField(pField);

            pField 
=   new  FieldClass();
            pFieldEdit 
=  pField  as  IFieldEdit;
            pFieldEdit.Length_2 
=   10 ;
            pFieldEdit.Name_2 
=   " CODE " ;
            pFieldEdit.AliasName_2 
=   " CODE " ;
            pFieldEdit.Type_2 
=  esriFieldType.esriFieldTypeString;

            pFieldsEdit.AddField(pField);

            String MapPath 
=   @" E:\data\result " ;
            String LayerName 
=   " AddFeature " ;

            
            IWorkspaceFactory pWsf 
=   new  ShapefileWorkspaceFactory();
            IFeatureWorkspace pFws
= pWsf.OpenFromFile(MapPath, 0 as  IFeatureWorkspace;
            IFeatureClass pFeatureClass
= pFws.CreateFeatureClass(LayerName  +   " .shp " , pFields,  null null , esriFeatureType.esriFTSimple,  " shape " "" );

新建shapefile,自定义字段,路径自定义。
在文件里加入POLYGON,自定义字段值


            IDataset pDataset 
=  (IDataset)pFeatureClass;
            pFws 
=  (IFeatureWorkspace)pDataset.Workspace;
            IWorkspaceEdit pWorkspaceEdit 
=  (IWorkspaceEdit)pFws;
            pWorkspaceEdit.StartEditing(
true );
            pWorkspaceEdit.StartEditOperation();

            IFeatureBuffer pFeatureBuffer 
=  pFeatureClass.CreateFeatureBuffer();
            
// Create insert Feature Cursor using buffering = true.
            IFeatureCursor pFeatCursor  =  pFeatureClass.Insert( true );

            
object  featureOID;
            
// 定义新加元素的值
            pFeatureBuffer.set_Value(pFeatureBuffer.Fields.FindField( " CODE " ), " CODE " );

            
// 生成Polygon**********************************************************
            IPolygon poly  =   new  PolygonClass();
            IPoint pPoint 
=   new  PointClass();


            
object  o  =  Type.Missing;
            IPointCollection pPc 
=   new  MultipointClass();
            pPc 
=  (IPointCollection)poly;

            
for  ( int  i  =   0 ; i  <=   0 ; i ++  )
            
{
                pPoint.PutCoords(
200400);
                pPc.AddPoint(pPoint, 
ref o, ref o);

                pPoint.PutCoords(
400400);
                pPc.AddPoint(pPoint, 
ref o, ref o);

                pPoint.PutCoords(
400200);
                pPc.AddPoint(pPoint, 
ref o, ref o);

                pPoint.PutCoords(
200200);
                pPc.AddPoint(pPoint, 
ref o, ref o);
            }


            
// 生成完毕  ************************************************************

            pFeatureBuffer.Shape 
=  poly;
            featureOID 
=  pFeatCursor.InsertFeature(pFeatureBuffer);

            
// Flush the feature cursor to the database       
            
// Calling flush allows you to handle any errors at a known time rather then on the cursor destruction.
            pFeatCursor.Flush();

            pWorkspaceEdit.StopEditOperation();
            pWorkspaceEdit.StopEditing(
true );

            
// 释放Cursor
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatCursor);
            axMapControl1.AddShapeFile(MapPath,LayerName
+ " .shp " );



转载于:https://www.cnblogs.com/secenes/archive/2008/07/17/1245165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值