现在的位置: 首页 > 综合 > 正文

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

2012年11月01日 ⁄ 综合 ⁄ 共 3519字 ⁄ 字号 评论关闭
            //生成新的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,0as IFeatureWorkspace;
            IFeatureClass pFeatureClass
=pFws.CreateFeatureClass(LayerName + ".shp", pFields, nullnull, 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");

抱歉!评论已关闭.