导出为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);
}

转载于:https://www.cnblogs.com/AEkaifa/p/8315245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值