昨天帮一位朋友实现一个根据属性提取要素的功能,现记录如下,以备后用:
先看看效果图吧!
要素提取前
要素提取后
以下实现这个功能的关键代码:
//主程序的路径(这个是全局变量)
private string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
//设置环境
Geoprocessor gp = new Geoprocessor();
gp.SetEnvironmentValue("workspace",appPath+@"\data\");
gp.OverwriteOutput = true;
//获取要素图层
IFeatureLayer pLayer = this.axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass inputFeatures = pLayer.FeatureClass;
//使用Select工具
ESRI.ArcGIS.AnalysisTools.Select selectFeature = new Select();
selectFeature.in_features = inputFeatures;
selectFeature.out_feature_class = @"output\fgselect.shp";
selectFeature.where_clause = "GRIDCODE>=50";
//执行
gp.Execute(selectFeature,null);
// 将结果加载到当前地图
string pFolder = Path.GetDirectoryName(appPath+@"\data\output\fgselect.shp");
string pFileName = Path.GetFileName(appPath + @"\data\output\fgselect.shp");
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace shpWorksapce = pWorkspaceFactory.OpenFromFile(pFolder, 0);
IFeatureWorkspace pFeatureWorkspace=shpWorksapce as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFileName);
IFeatureLayer outputLayer = new FeatureLayerClass();
outputLayer.FeatureClass = pFeatureClass;
outputLayer.Name = pFeatureClass.AliasName;
this.axMapControl1.Map.AddLayer((ILayer)outputLayer);
this.axMapControl1.ActiveView.Refresh();
ArcGIS里面的GP真的是功能强大呀,借助它可以省不少事,看来以后还得多研究一下GP这东东。