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

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

楼主#
更多 发布于:2006-08-17 20:10
<P>/*说明:C#中添加多个shp文件,默认对象为axMap1,如果名称改变只需修改最后一句即可<BR>  时间:2006-08-17<BR>  作者:gisgeoboy<BR>  邮箱/MSN:gisskystar@126.com<BR>  注:需要引用System.Windows.Forms和ESRI.MapObjects2.Core;<BR>  */<BR>  public void AddShpFile()<BR>  {<BR>   string FilePath ,FileNameNoExtend ;//定义文件路径,没有扩展的文件名称<BR>   int Position ;                     //路径中最后一个“\”的位置<BR>   DataConnection dc=new DataConnectionClass();//定义连接<BR>   MapLayer layer = null;<BR>   ESRI.MapObjects2.Core.GeoDataset gds = null;</P>
<P>   OpenFileDialog openShpFile = new OpenFileDialog();<BR>   openShpFile.Multiselect=true;<BR>   openShpFile.Filter="shp文件(*.shp)|*.shp";<BR>   openShpFile.RestoreDirectory=true;</P>
<P>   if( openShpFile.ShowDialog() == DialogResult.OK )<BR>   {<BR>    if(( openShpFile.OpenFile()) != null )<BR>    {<BR>     string[] fileNames=openShpFile.FileNames;     </P>
<P>     Position=fileNames[0].LastIndexOf( "\\" );<BR>     FilePath=fileNames[0].Substring( 0,Position );<BR>     dc.Database=FilePath;<BR>     dc.Connect();<BR>     if (dc.Connected == true)<BR>     {<BR>      foreach( string shpFileWithPath in fileNames )<BR>      {<BR>       FileNameNoExtend=shpFileWithPath.Substring(Position+1,shpFileWithPath.Length-Position-5);<BR>        gds = dc.FindGeoDataset(FileNameNoExtend);<BR>       if ( gds != null )<BR>       {<BR>        layer = new MapLayerClass();<BR>        layer.GeoDataset = gds;<BR>        axMap1.Layers.Add ( layer );<BR>       }  <BR>      }            <BR>     }     <BR>    }<BR>   }  <BR>  }</P>
喜欢0 评分0
hahaaluo
路人甲
路人甲
  • 注册日期2006-08-05
  • 发帖数146
  • QQ
  • 铜币591枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2006-08-18 09:38
感谢上面这位大哥~~~~~~~~~
<P>呵呵 希望对我有帮助!!!!谢谢了 </P>
举报 回复(0) 喜欢(0)     评分
gisgeoboy
路人甲
路人甲
  • 注册日期2003-11-11
  • 发帖数73
  • QQ
  • 铜币377枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2006-08-18 10:24
<P>程序改进:</P>
<P>/*<BR>  说明:C#中添加多个shp文件,默认对象为axMap1,如果名称改变,秩序修改最后一句即可<BR>  时间:2006-08-18<BR>  作者:gisgeoboy<BR>  邮箱/MSN:gisskystar@126.com<BR>  注:需要引用System.Windows.Forms、System.IO和ESRI.MapObjects2.Core;<BR>  */<BR>  public void AddShpFile()<BR>  {<BR>   string FilePath ;//定义文件路径   <BR>   DataConnection dc=new DataConnectionClass();//定义连接<BR>   MapLayer layer = null;<BR>   ESRI.MapObjects2.Core.GeoDataset gds = null;</P>
<P>   OpenFileDialog openShpFile = new OpenFileDialog();<BR>   openShpFile.Multiselect=true;<BR>   openShpFile.Filter="shp文件(*.shp)|*.shp";<BR>   openShpFile.RestoreDirectory=true;</P>
<P>   if( openShpFile.ShowDialog() == DialogResult.OK )<BR>   {<BR>    if(( openShpFile.OpenFile()) != null )<BR>    {<BR>     string[] fileNames=openShpFile.FileNames;     <BR>     FilePath=Path.GetDirectoryName(fileNames[0]);<BR>     dc.Database=FilePath;<BR>     dc.Connect();<BR>     if (dc.Connected == true)<BR>     {<BR>      foreach( string shpFileWithPath in fileNames )<BR>      {       <BR>       gds = dc.FindGeoDataset(Path.GetFileNameWithoutExtension(shpFileWithPath));<BR>       if ( gds != null )<BR>       {<BR>        layer = new MapLayerClass();<BR>        layer.GeoDataset = gds;<BR>        axMap1.Layers.Add ( layer );<BR>       }  <BR>      }            <BR>     }     <BR>    }<BR>   }  <BR>  }<BR></P>
举报 回复(0) 喜欢(0)     评分
游客

返回顶部