ArcEngine c# 创建shp图层,并将要素写入图层保存

之前看了一位大侠的帖子,写的是跟这个差不多的,所以,我就直接copy下来再vs中做了一个类。大家可以直接拿过去用。

 

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>
/// 导出缓冲分析之后出现JointCount>1的点位图层
/// </summary>
public  class ExportToShp
{
public string shpPath;
/// <summary>
/// 保存输出JointCount>1的点位图层
/// </summary>
/// <param name=”apFeatureClass”></param>
public void ExportFeatureClassToShp(IFeatureClass apFeatureClass)
{

if (apFeatureClass == null)
{

MessageBox.Show(“请选择”, “系统提示”);
return;

}
//调用保存文件函数
SaveFileDialog sa = new SaveFileDialog();

sa.Filter = “SHP文件(.shp)|*.shp”;

sa.ShowDialog();

sa.CreatePrompt = true;

string ExportShapeFileName = sa.FileName;

// string StrFilter = “SHP文件(.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”;
//设置导出要素类的参数
IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();

IDataset pOutDataset = (IDataset)apFeatureClass;

pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;

//创建一个输出shp文件的工作空间
IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();

IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();

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

//创建一个要素集合
IFeatureDatasetName pInFeatureDatasetName = null;

//创建一个要素类
IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();

IDatasetName pInDatasetClassName;

pInDatasetClassName = (IDatasetName)pInFeatureClassName;

pInDatasetClassName.Name = ExportFileShortName;//作为输出参数

pInDatasetClassName.WorkspaceName = pInWorkspaceName;
//通过FIELDCHECKER检查字段的合法性,为输出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);

//通过循环查找几何字段

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;
}
}
//得到几何字段的几何定义
IGeometryDef pOutGeometryDef;

IGeometryDefEdit pOutGeometryDefEdit;

pOutGeometryDef = pGeoField.GeometryDef;

//设置几何字段的空间参考和网格
pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;

pOutGeometryDefEdit.GridCount_2 = 1;

pOutGeometryDefEdit.set_GridSize(0, 1500000);
try
{
//开始导入
IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();

pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, “”, 1000, 0);

MessageBox.Show(“导出成功”, “系统提示”);
}
catch (Exception ex)
{
MessageBox.Show(“the following exception occurred:” + ex.ToString());
}
}

}
}

标签