ArcEngine整个Map选择集的闪烁以及跨图层选择集导出为Shp

对于在单个Shp图层上进行要素的选择,并进行闪烁,我想做过AE的同学并不陌生了,这里也不再赘述,

但是当我们想在几个图层间进行选择要素的对比的时候(就看一下空间位置就OK),那么怎么保存这些选择集呢,很简单在每个图层进行选择后,不清空上次的查询结果就OK了,只要不运行IMap::ClearSelection()就OK了,在这同时每个矢量图层离得IselectionSet也都存储了各个图层的被选择要素。

所以这里要是进行整个Map的选择集闪烁的话,就有两种方式了:(1)遍历Map的选择集,(2)遍历每个矢量图层的选择集

我做一个第一种方式的例子:

string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
string filename = @"\SpatialAnalyst\小鹿.bmp";
filename = ThePath + filename;
IPictureMarkerSymbol pPicsymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, filename);
pPicsymbol.Size = 16;
ISelection pselection = map_control.Map.FeatureSelection;
IEnumFeature pEnumfeature = pselection as IEnumFeature;
pEnumfeature.Reset();
IFeature pfeature = pEnumfeature.Next();
while (pfeature != null)
  {
    map_control.FlashShape(pfeature.Shape, 3, 300, pPicsymbol as ISymbol);
    pfeature = pEnumfeature.Next();
  }

好的,选择集闪烁后,我们要导出所有要素了,一般情况下,我们可能就按照上面同样的方法进行遍历,然后把每个IMap里的选中要素的属性和几何信息进行导出,然后呢,我也这么做到,可是我发现错了,IMap::ClearSelection里的要素是不包含属性的,也就是说他只负责选中与不选中,里面并不包含属性,那怎么办呢,只能遍历每个矢量图层的选择集喽。

代码如下

public void ExportFeature()

{  

SaveFileDialog saveDia = new SaveFileDialog();  

saveDia.Title = "Export Shp.......";  

saveDia.CreatePrompt = false;  

saveDia.OverwritePrompt = false;//提示用户是否覆盖  

saveDia.Filter = "ShapeFile  (*.shp)|*.shp";

 saveDia.RestoreDirectory = true;  

saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加

 if (saveDia.ShowDialog() == DialogResult.OK)

 {   

strFullPath = saveDia.FileName;  

 int Index = strFullPath.LastIndexOf("\\");  

 filePath = strFullPath.Substring(0, Index);  

 fileName = strFullPath.Substring(Index + 1);   

shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);

  foreach (FileInfo file in new DirectoryInfo(filePath).GetFiles())

  {   

 if (file.Name == fileName)

   {    

 file.Delete();//如果该Shp文件已经存在,则删除它以及与它相关的文件    

 FileInfo fileshx = new FileInfo(filePath + "\\" + shapeName + ".shx");    

 if (fileshx.Exists)      fileshx.Delete();    

 FileInfo fileprj = new FileInfo(filePath + "\\" + shapeName + ".prj");     

if (fileprj.Exists)      fileprj.Delete();    

 FileInfo filedbf = new FileInfo(filePath + "\\" + shapeName + ".dbf");     if (filedbf.Exists)      filedbf.Delete();

   }   

}  

 try

  {   

 IFeatureClass pfeatureClass = ShapeFileClass.CreatNewShapeFile(filePath, fileName);   

 ITable ptable = pfeatureClass as ITable;    

custumeQuetySource custumeQuety = (custumeQuetySource)mapcontrol.CustomProperty;  

  List<IFeatureLayer> plistLy = custumeQuety.listLyName;    //这是我自己写的一个记录曾经参与查询的图层的类

for (int p = 0; p < plistLy.Count; p++)   

 {     

IFeatureLayer pfeatureLy = (IFeatureLayer)plistLy[p];    

 ISelectionSet pselectionset = ((IFeatureSelection)pfeatureLy).SelectionSet;    

 if (pselectionset != null)  

   {    

  ICursor pCursor = null;    

  pselectionset.Search(null, false, out pCursor);    

  IFeatureCursor pfeaturecursor = pCursor as IFeatureCursor;  

    IFeature pfeature = pfeaturecursor.NextFeature();     

 while (pfeature != null)    

  {     

  IFeatureBuffer pfeaturebuffer = pfeatureClass.CreateFeatureBuffer();  

     IFeatureCursor pfeatureCursor = pfeatureClass.Insert(true);

      pfeaturebuffer.Shape = pfeature.Shape;    

   for (int i = 2; i < pfeature.Fields.FieldCount; i++)    

   {        try       

 {         object shuxing = pfeature.get_Value(i);      

   pfeaturebuffer.set_Value(i, shuxing);        

 object shuxin2g = pfeaturebuffer.get_Value(i);     

   }       

 catch      

  {

       }     

  }     

  pfeatureCursor.InsertFeature(pfeaturebuffer);     

  pfeatureCursor.Flush();      

 pfeature = pfeaturecursor.NextFeature();    

  }   

  }  

  }   

 MessageBox.Show("ShapeFile导出完成");  

 }

  catch (Exception ex)  

 {  

  MessageBox.Show("ShapeFile导出失败\n" + ex.Message);

  }

 }

}

作者: 风云

出处: http://www.cnblogs.com/fengyunlishi/

本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

作者: 风云 出处: http://www.cnblogs.com/fengyunlishi/ 本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/fengyunlishi/p/2722595.html