加载矢量数据的代码优化
深入来看,依据ESRI公司数据浏览架构,数据打开需要经历以下流程:
首先利用新建工作空间工厂的方法打开工作空间;然后强制转换为要素工作空间或者栅格工作空间;最后使用要素工作空间或者栅格工作空间的方法打开(要素/矢量或栅格)数据。
对于ShapeFile来说,工作空间就是它所在的文件夹,打开工作空间需要使用对应的工作空间工厂,即ShapeFileWorkspaceFactoryClass,然后调用IWorkspaceFactory的OpenFromFile方法,就可以得到一个工作空间,这也是设计模式中工厂方法的体现。工作空间工厂的打开方法返回的是一般意义的工作空间,根据具体数据还需要进行接口转换。因为ShapeFile是矢量数据,所以把工作空间接口跳转到IFeatureWorkspace,从而读取其中的要素类。
这一点对于多种格式的数据(如Coverage、GeoDatabase、ArcSDE数据格式)都是一样。
对比代码:
private void menuAddShp_Click(object sender, EventArgs e) { IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); IWorkspace pWorkspace1 = pWorkspaceFactory.OpenFromFile(@"D:\data", 0); IFeatureWorkspace pFeatureWorkspce = pWorkspace1 as IFeatureWorkspace; IFeatureClass pFC = pFeatureWorkspce.OpenFeatureClass("华东地区.shp"); IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = pFC; pFLayer.Name = pFC.AliasName; ILayer pLayer = pFLayer as ILayer; IMap pMap = axMapControl1.Map; pMap.AddLayer(pLayer); axMapControl1.ActiveView.Refresh(); }
private void addShapeFile2() { ... //省略 IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath,0) as IFeatureWorkspace; IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName); pFLayer.Name = pFLayer.FeatureClass.AliasName; this.axMapControl1.AddLayer(pFLayer as ILayer); this.axMapControl1.Refresh(); }
第二处的代码明显要少于第一处的代码,但是在理解上第一处的代码更加容易让人读。(所以需要结合一下)
经过测试
1、
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
可以写为(推荐使用):
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
2、
(推荐使用)
IWorkspace pWorkspace1 = pWorkspaceFactory.OpenFromFile(@"D:\data", 0); IFeatureWorkspace pFeatureWorkspce = pWorkspace1 as IFeatureWorkspace;
可以写为:
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(@"D:\data", 0) as IFeatureWorkspace;
3、
相同
IFeatureLayer pFLayer = new FeatureLayerClass();
4、
(推荐使用)
IFeatureClass pFC = pFeatureWorkspce.OpenFeatureClass("华东地区.shp"); pFLayer.FeatureClass = pFC;
可以写为:
pFLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
5、
(推荐使用)
pFLayer.Name = pFC.AliasName;
可以写为:
pFLayer.Name = pFLayer.FeatureClass.AliasName;
5、
ILayer pLayer = pFLayer as ILayer; IMap pMap = axMapControl1.Map; pMap.AddLayer(pLayer);
可以写为:
this.axMapControl1.AddLayer(pFLayer as ILayer);
或者可以写为(推荐使用):
this.axMapControl1.AddLayer(pFLayer);
7、
axMapControl1.ActiveView.Refresh();
可以写为(推荐使用):
this.axMapControl1.Refresh();
像其他数据的加载基本上都是大同小异的,以下列出的是加载矢量数据的代码。
个人推荐使用的总结代码:
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkspace1 = pWorkspaceFactory.OpenFromFile(@"D:\data", 0); IFeatureWorkspace pFeatureWorkspce = pWorkspace1 as IFeatureWorkspace; IFeatureClass pFC = pFeatureWorkspce.OpenFeatureClass("华东地区.shp"); IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = pFC; pFLayer.Name = pFC.AliasName; this.axMapControl1.AddLayer(pFLayer); this.axMapControl1.Refresh();
图解代码:
谢谢观看!本人初学GIS二次开发,如果有不对的地方,请多多包涵!