Create shapefile, Custom field, and add new record

Source: Internet
Author: User
// Generate a new 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 ","");

Create shapefile, Custom field, and path customization.
Add POLYGON to the file to customize the field value


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;
// Define the value of the newly added element
PFeatureBuffer. set_Value (pFeatureBuffer. Fields. FindField ("CODE"), "CODE ");

// Generate a Polygon ************************************ **********************
Ipolympus Gon 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 );
}

// After the generation is completed ************************************ ************************

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 );

// Release Cursor
System. Runtime. InteropServices. Marshal. ReleaseComObject (pFeatCursor );
AxMapControl1.AddShapeFile (MapPath, LayerName + ". shp ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.