默认头像
路人甲
路人甲
  • 注册日期2003-11-11
  • 发帖数73
  • QQ
  • 铜币377枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:1690回复:2

c#+mo2.3添加多个shp文件

楼主#
更多 发布于:2006-08-17 20:10

/*说明:C#中添加多个shp文件,默认对象为axMap1,如果名称改变只需修改最后一句即可
 时间:2006-08-17
 作者:gisgeoboy
 邮箱/MSN:gisskystar@126.com
 注:需要引用System.Windows.Forms和ESRI.MapObjects2.Core;
 */
 public void AddShpFile()
 {
  string FilePath ,FileNameNoExtend ;//定义文件路径,没有扩展的文件名称
  int Position ;                     //路径中最后一个“\”的位置
  DataConnection dc=new DataConnectionClass();//定义连接
  MapLayer layer = null;
  ESRI.MapObjects2.Core.GeoDataset gds = null;

  OpenFileDialog openShpFile = new OpenFileDialog();
  openShpFile.Multiselect=true;
  openShpFile.Filter="shp文件(*.shp)|*.shp";
  openShpFile.RestoreDirectory=true;

  if( openShpFile.ShowDialog() == DialogResult.OK )
  {
   if(( openShpFile.OpenFile()) != null )
   {
    string[] fileNames=openShpFile.FileNames;    

    Position=fileNames[0].LastIndexOf( "\\" );
    FilePath=fileNames[0].Substring( 0,Position );
    dc.Database=FilePath;
    dc.Connect();
    if (dc.Connected == true)
    {
     foreach( string shpFileWithPath in fileNames )
     {
      FileNameNoExtend=shpFileWithPath.Substring(Position+1,shpFileWithPath.Length-Position-5);
       gds = dc.FindGeoDataset(FileNameNoExtend);
      if ( gds != null )
      {
       layer = new MapLayerClass();
       layer.GeoDataset = gds;
       axMap1.Layers.Add ( layer );
      }  
     }            
    }    
   }
  }  
 }

喜欢0 评分0
默认头像
路人甲
路人甲
  • 注册日期2006-08-05
  • 发帖数146
  • QQ
  • 铜币591枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2006-08-18 09:38
感谢上面这位大哥~~~~~~~~~

呵呵 希望对我有帮助!!!!谢谢了

举报 回复(0) 喜欢(0)     评分
默认头像
路人甲
路人甲
  • 注册日期2003-11-11
  • 发帖数73
  • QQ
  • 铜币377枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2006-08-18 10:24

程序改进:

/*
 说明:C#中添加多个shp文件,默认对象为axMap1,如果名称改变,秩序修改最后一句即可
 时间:2006-08-18
 作者:gisgeoboy
 邮箱/MSN:gisskystar@126.com
 注:需要引用System.Windows.Forms、System.IO和ESRI.MapObjects2.Core;
 */
 public void AddShpFile()
 {
  string FilePath ;//定义文件路径  
  DataConnection dc=new DataConnectionClass();//定义连接
  MapLayer layer = null;
  ESRI.MapObjects2.Core.GeoDataset gds = null;

  OpenFileDialog openShpFile = new OpenFileDialog();
  openShpFile.Multiselect=true;
  openShpFile.Filter="shp文件(*.shp)|*.shp";
  openShpFile.RestoreDirectory=true;

  if( openShpFile.ShowDialog() == DialogResult.OK )
  {
   if(( openShpFile.OpenFile()) != null )
   {
    string[] fileNames=openShpFile.FileNames;    
    FilePath=Path.GetDirectoryName(fileNames[0]);
    dc.Database=FilePath;
    dc.Connect();
    if (dc.Connected == true)
    {
     foreach( string shpFileWithPath in fileNames )
     {      
      gds = dc.FindGeoDataset(Path.GetFileNameWithoutExtension(shpFileWithPath));
      if ( gds != null )
      {
       layer = new MapLayerClass();
       layer.GeoDataset = gds;
       axMap1.Layers.Add ( layer );
      }  
     }            
    }    
   }
  }  
 }

举报 回复(0) 喜欢(0)     评分
默认头像

返回顶部