GIS二次开发之不同类型的数据的打开显示

 
 

1.打开VS,新建C#窗体应用程序

 
 

 
  
 
 
 

2.拖拉容器控件:SplitContainer

 
 

 

 
 

3.向SplitContainer中添加Arcgis Windows Forms MapControl控件,并且设置其dock属性为fill

 
 

 

 
 

4.向MapControl中添加LicenseControl控件,该控件是用来为其它控件进行授权许可,运行时不会显示,因此不需设置其位置形状

 
 

 

 
 

5.此时若直接点击运行,会提示错误,原因是没有在程序运行之前添加:ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

 
  
 
 
 

 

 
 

6.解决方案就是通过双击Program.cs进入代码段,在Application.Run(new Form1());语句之前输入ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

 
 

 

 
  
 
 
 

7.此时程序可以正常运行

 
  
 
 
 

8.根据自己的数据使用要求,添加引用,将自己需要的数据类型都添加进去

 
 

 

 
 

9.在Form1.cs代码段前添加using引用

 
 

 

 
 

10.向容器中添加button按钮,该按钮需要实现的功能是打开矢量数据

11.在button中添加click响应事件

 
  
 
 
  
 

1
namespace WindowsFormsApplication1 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 } 9 OpenFileDialog open = new OpenFileDialog(); 10 string allname; 11 string location; 12 string filename; 13 string ext; 14 15 private void button1_Click(object sender, EventArgs e) 16 { 17 try 18 { 19 20 open.ShowDialog(); 21 allname = open.FileName; 22 location = System.IO.Path.GetDirectoryName(allname); 23 filename = System.IO.Path.GetFileName(allname); 24 ext = System.IO.Path.GetExtension(allname).ToLower();/*获得打开文件的路径,文件名,后缀名*/ 25 axMapControl1.ClearLayers(); 26 if (ext == ".tif") 27 { 28 IWorkspaceFactory workspcFac = new RasterWorkspaceFactory(); 29 IRasterWorkspace rasterworkspc; 30 IRasterDataset rasterDatst = new RasterDatasetClass(); 31 IRasterLayer layer = new RasterLayer();/*创建raster的相关展示控件*/ 32 rasterworkspc = workspcFac.OpenFromFile(location, 0) as IRasterWorkspace; 33 rasterDatst = rasterworkspc.OpenRasterDataset(filename); 34 layer.CreateFromDataset(rasterDatst); 35 36 axMapControl1.AddLayer(layer); 37 axMapControl1.Refresh(); 38 39 } 40 if (ext == ".shp") 41 { 42 IWorkspaceFactory pworkspacefactory; 43 IFeatureWorkspace pfeatureworkspace; 44 IFeatureClass pfeatureclass; 45 IFeatureLayer layer;/*创建shapfile的相关展示控件*/ 46 pworkspacefactory = new ShapefileWorkspaceFactoryClass(); 47 pfeatureworkspace = pworkspacefactory.OpenFromFile(location, 0) as IFeatureWorkspace; 48 pfeatureclass = pfeatureworkspace.OpenFeatureClass(filename); 49 layer = new FeatureLayer(); 50 layer.FeatureClass = pfeatureclass; 51 axMapControl1.AddLayer(layer); 52 axMapControl1.Refresh(); 53 } 54 if (ext == ".dwg" || ext == "dxf") 55 { 56 CadWorkspaceFactoryClass fac = new CadWorkspaceFactoryClass(); 57 IFeatureWorkspace space = fac.OpenFromFile(location, 0) as IFeatureWorkspace;/*创建cad的相关展示控件*/ 58 IFeatureClass polyline = space.OpenFeatureClass(filename + ":polyline"); 59 IFeatureLayer layer1 = (IFeatureLayer)new CadFeatureLayer(); 60 layer1.FeatureClass = polyline; 61 62 63 IFeatureClass point = space.OpenFeatureClass(filename + ":point"); 64 IFeatureLayer layer2 = (IFeatureLayer)new CadFeatureLayer(); 65 layer2.FeatureClass = point; 66 67 IFeatureClass polygon = space.OpenFeatureClass(filename + ":polygon"); 68 IFeatureLayer layer3 = (IFeatureLayer)new CadFeatureLayer(); 69 layer3.FeatureClass = polygon; 70 71 IFeatureClass anno = space.OpenFeatureClass(filename + ":Annotation"); 72 IFeatureLayer layer = (IFeatureLayer)new CadAnnotationLayer(); 73 layer.FeatureClass = anno; 74 75 axMapControl1.AddLayer(layer1); 76 axMapControl1.AddLayer(layer2); 77 axMapControl1.AddLayer(layer3); 78 79 axMapControl1.AddLayer(layer); 80 axMapControl1.Refresh(); 81 82 } 83 else 84 { 85 MessageBox.Show("该文件类型不支持打开!"); 86 } 87 88 } 89 catch 90 { 91 MessageBox.Show("打开文件失败"); 92 } 93 94 95 }

 

猜你喜欢

转载自www.cnblogs.com/qin5429/p/10647024.html
今日推荐