ArcEngine c # create a shp layer and write the elements to the layer to save

Source: Internet
Author: User

 


I read the post of a hero and wrote something similar to this one. So I copied it directly and made a class in. You can use it directly.

 


Using System;

Using System. Collections. Generic;
Using System. Text;
Using ESRI. ArcGIS. Geodatabase;
Using System. Windows. Forms;
Using ESRI. ArcGIS. DataSourcesFile;


Namespace topo.com
{
/// <Summary>
/// After the buffer analysis is exported, the point layer of JointCount> 1 appears.
/// </Summary>
Public class ExportToShp
{
Public string shpPath;
/// <Summary>
/// Save the point layer that outputs JointCount> 1
/// </Summary>
/// <Param name = "apFeatureClass"> </param>
Public void ExportFeatureClassToShp (IFeatureClass apFeatureClass)
{


If (apFeatureClass = null)
{


MessageBox. Show ("select", "system prompt ");
Return;


}
// Call the save file Function
SaveFileDialog sa = new SaveFileDialog ();


Sa. Filter = "SHP file (. shp) | *. shp ";


Sa. ShowDialog ();


Sa. CreatePrompt = true;


String ExportShapeFileName = sa. FileName;


// String StrFilter = "SHP file (. shp) | *. shp ";
// String ExportShapeFileName = SaveFileDialog (StrFilter );


If (ExportShapeFileName = "")
Return;


String ExportFileShortName = System. IO. Path. GetFileNameWithoutExtension (ExportShapeFileName );

String ExportFilePath = System. IO. Path. GetDirectoryName (ExportShapeFileName );


ShpPath = ExportFilePath + "\" + ExportFileShortName + "\" + ExportFileShortName + ". shp ";
// Set the parameters of the exported element class
IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass ();

IDataset pOutDataset = (IDataset) apFeatureClass;

POutFeatureClassName = (IFeatureClassName) pOutDataset. FullName;

// Create a workspace that outputs the shp File
IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass ();

IWorkspaceName pInWorkspaceName = new WorkspaceNameClass ();

PInWorkspaceName = pShpWorkspaceFactory. Create (ExportFilePath, ExportFileShortName, null, 0 );


// Create a collection of Elements
IFeatureDatasetName pInFeatureDatasetName = null;

// Create a element class
IFeatureClassName pInFeatureClassName = new FeatureClassNameClass ();

IDatasetName pInDatasetClassName;

PInDatasetClassName = (IDatasetName) pInFeatureClassName;

PInDatasetClassName. Name = ExportFileShortName; // as the output parameter

PInDatasetClassName. WorkspaceName = pInWorkspaceName;
// Use FIELDCHECKER to check the validity of the field and obtain the field set for the output SHP.

Long iCounter;

IFields pOutFields, pInFields;

IFieldChecker pFieldChecker;

IField pGeoField;

IEnumFieldError pEnumFieldError = null;

PInFields = apFeatureClass. Fields;

PFieldChecker = new FieldChecker ();

PFieldChecker. Validate (pInFields, out pEnumFieldError, out pOutFields );

// Search geometric fields cyclically

PGeoField = null;

For (iCounter = 0; iCounter <pOutFields. FieldCount; iCounter ++)
{
If (pOutFields. get_Field (int) iCounter). Type = esriFieldType. esriFieldTypeGeometry)
{
PGeoField = pOutFields. get_Field (int) iCounter );
Break;
}
}
// Obtain the geometric definition of the geometric field
IGeometryDef pOutGeometryDef;

IGeometryDefEdit pOutGeometryDefEdit;

POutGeometryDef = pGeoField. GeometryDef;

// Set the space reference and grid of geometric Fields
POutGeometryDefEdit = (IGeometryDefEdit) pOutGeometryDef;

POutGeometryDefEdit. GridCount_2 = 1;

POutGeometryDefEdit. set_GridSize (0, 1500000 );
Try
{
// Start Import
IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass ();

PShpToClsConverter. ConvertFeatureClass (pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0 );

MessageBox. Show ("exported successfully", "system prompt ");
}
Catch (Exception ex)
{
MessageBox. Show ("the following exception occurred:" + ex. ToString ());
}
}


}
}

Related Article

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.