新建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", "");
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(200, 400);
pPc.AddPoint(pPoint, ref o, ref o);
pPoint.PutCoords(400, 400);
pPc.AddPoint(pPoint, ref o, ref o);
pPoint.PutCoords(400, 200);
pPc.AddPoint(pPoint, ref o, ref o);
pPoint.PutCoords(200, 200);
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");
posted on 2008-07-17 14:19 duckweeds 阅读(1797) 评论(1) 编辑 收藏 举报