导出为shape文件

private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//导出为shape文件。
{
string serror = "";
for (int i = 0; i < sFeature.Count; i++)
{
IFeature pFeature = sFeature[i];
IFeatureClass pSourceFeaCls = pFeature.Class as IFeatureClass;
if (pSourceFeaCls == null)
return;
IWorkspace pSourceWks = (pSourceFeaCls as IDataset).Workspace;
ISpatialReference pSourceSpr = ((pSourceFeaCls as IDataset) as IGeoDataset).SpatialReference;

//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "Shape(*.shp)|*.shp|All Files(*.*)|*.*";
sfd.CreatePrompt = false;
if (sfd.ShowDialog() != DialogResult.OK)
return;

//得到文件名和路径
string strShppath = System.IO.Path.GetDirectoryName(sfd.FileName);
string strShpFile = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName);

//创建shp工作空间
IWorkspaceFactory pWksFac = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeaWks = (IFeatureWorkspace)pWksFac.OpenFromFile(strShppath, 0);

//创建字段集
IFields pFlds = new FieldsClass();
IFieldsEdit pFldsEdt = (IFieldsEdit)pFlds;
//创建基础字段
IField pFld = new FieldClass();
IFieldEdit pFldEdt = (IFieldEdit)pFld;
string strShapeFieldName = "shape";
pFldEdt.Name_2 = strShapeFieldName;
pFldEdt.Type_2 = esriFieldType.esriFieldTypeGeometry;
//为esriFieldTypeGeometry类型的字段创建几何定义
IGeometryDef pGeoDef = new GeometryDefClass();
IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
pGeoDefEdit.GeometryType_2 = pSourceFeaCls.Fields.get_Field(1).GeometryDef.GeometryType;//设置shp文件的几何类型
pGeoDefEdit.SpatialReference_2 = pSourceSpr;//设置shp文件的空间参考
pFldEdt.GeometryDef_2 = pGeoDef;
pFldsEdt.AddField(pFld);
//创建其他字段
for (int j = 2; j < pSourceFeaCls.Fields.FieldCount; j++)
{
pFld = new FieldClass();
pFldEdt = (IFieldEdit)pFld;
pFldEdt.Name_2 = pSourceFeaCls.Fields.get_Field(j).Name;
pFldEdt.Type_2 = pSourceFeaCls.Fields.get_Field(j).Type;
pFldsEdt.AddField(pFld);
}
//创建空白shp文件
pFeaWks.CreateFeatureClass(strShpFile, pFlds, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, "");
//打开Shapefile,获取FeaCls
IWorkspace pTargetWks = pWksFac.OpenFromFile(strShppath, 0);
if (pTargetWks == null)
return;
IFeatureClass pTargetFeaCls = (pTargetWks as IFeatureWorkspace).OpenFeatureClass(strShpFile);

int ifeaCount = pSourceFeaCls.FeatureCount(null);
IFeatureCursor pFeaCur = pSourceFeaCls.Search(null, false);
IFeature pSourceFea = pFeaCur.NextFeature();
if (pSourceFea == null)
return;
bool blok = false;

IWorkspaceEdit pWksEdt = pTargetWks as IWorkspaceEdit;
pWksEdt.StartEditing(false);
pWksEdt.RedoEditOperation();
try
{
while (pSourceFea != null)
{
IFeature pTargetFea = pTargetFeaCls.CreateFeature();
if (pTargetFea != null)
{
pTargetFea.Shape = pSourceFea.Shape;
CopyAttribute(pSourceFea, pTargetFea);
pTargetFea.Store();
}
pSourceFea = pFeaCur.NextFeature();
}
blok = true;
}
catch (Exception exc)
{
serror = exc.Message;
}
finally
{
if (blok)
pWksEdt.StopEditOperation();
else
pWksEdt.AbortEditOperation();
pWksEdt.StopEditing(blok);
if (blok)
MessageBox.Show("导出成功,共计导出" + ifeaCount + "条记录");
else
MessageBox.Show("导出失败" + serror);
}

posted @ 2018-01-19 08:47  漠语~  阅读(887)  评论(0编辑  收藏  举报