GIS二次开发实验报告 下载本文

5 实验五:GIS分析

5.1 实验目的

? 了解GIS空间分析的内容 ? 掌握空间关系查询方法 ? 掌握空间拓扑分析方法 ? 掌握空间数据统计方法

5.2 实验内容

? 实现空间关系查询程序 ? 实现缓冲区分析程序 ? 实现要素统计程序

5.3 实验步骤与结

1.在主窗体菜单栏添加一个控件文本“GIS分析”,添加下拉菜单,文本“空间查询”,控件名“miSpatilFilter”

2.添加新的类“Analysis.cs” 3.添加类库引用

4.添加“空间查询”函数,代码如下

public bool QueryIntersect(string srcLayerName, string tgtLayerName, IMap iMap, esriSpatialRelationEnum spatialRel) {

DataOperator dataOperator = new DataOperator(iMap); //定义并根据图层名称获取图层对象 IFeatureLayer iSrcLayer = (IFeatureLayer)dataOperator.GetLayerByName(srcLayerName); IFeatureLayer iTgtLayer = (IFeatureLayer)dataOperator.GetLayerByName(tgtLayerName); //通过查询过滤获取Continents层中亚洲的几何 IGeometry geom; IFeature feature;

IFeatureCursor featCursor; IFeatureClass srcFeatClass;

IQueryFilter queryFilter = new QueryFilter();

20

queryFilter.WhereClause = \设置查询条件 featCursor = iTgtLayer.FeatureClass.Search(queryFilter, false); feature = featCursor.NextFeature();

geom = feature.Shape;//获取亚洲图形几何

//根据所选择的几何对城市图层进行属性与空间过滤 srcFeatClass = iSrcLayer.FeatureClass;

ISpatialFilter spatialFilter = new SpatialFilter(); spatialFilter.Geometry = geom;

spatialFilter.WhereClause = \人口等级等于5的城市 spatialFilter.SpatialRel = (ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum)spatialRel; //定义要素选择对象,以要素搜索图层进行实例化

IFeatureSelection featSelect = (IFeatureSelection)iSrcLayer; //以空间过滤器对要素进行选择,并建立新选择集 featSelect.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); return true; }

5.添加“空间查询”点击事件响应函数,代码如下

private void miSpatilFilter_Click(object sender, EventArgs e) {

MapAnalysis mapAnalysis = new MapAnalysis(); mapAnalysis.QueryIntersect(\Cities\\axMapControl1.Map, esriSpatialRelationEnum.esriSpatialRelationIntersection); IActiveView activeView;

activeView = axMapControl1.ActiveView;

activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, 0, axMapControl1.Extent); }

6.实验运行结果如下

21

7.添加“GIS分析”的下拉控件,文本“缓冲区分析”,控件名“miBuffer” 8.添加“缓冲区分析”函数,代码如下

public bool Buffer(string layerName, string sWhere, int iSize, IMap iMap) {

//根据过滤条件获取城市名称为背景的城市要素几何 IFeatureClass featClass; IFeature feature; IGeometry iGeom;

DataOperator dataOperator = new DataOperator(iMap); IFeatureLayer featLayer (IFeatureLayer)dataOperator.GetLayerByName(layerName); featClass = featLayer.FeatureClass;

IQueryFilter queryFilter = new QueryFilter(); queryFilter.WhereClause = sWhere; IFeatureCursor featCursor;

featCursor = (IFeatureCursor)featClass.Search(queryFilter, false); int count = featClass.FeatureCount(queryFilter); feature = featCursor.NextFeature(); iGeom = feature.Shape; /////////////////

ITopologicalOperator ipTO = (ITopologicalOperator)iGeom; IGeometry iGeomBuffer = ipTO.Buffer(iSize); ///////////////////

ISpatialFilter spatialFilter = new SpatialFilter(); spatialFilter.Geometry = iGeomBuffer;

spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIndexIntersects; //////////////////////////////////

IFeatureSelection featSelect = (IFeatureSelection)featLayer; featSelect.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); return true; }

9.添加“缓冲区分析”点击事件响应函数,代码如下 private void miBuffer_Click(object sender, EventArgs e) {

MapAnalysis mapAnalysis = new MapAnalysis(); mapAnalysis.Buffer(\Cities\\axMapControl1.Map);

IActiveView activeView;

activeView = axMapControl1.ActiveView;

activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, axMapControl1.Extent); }

10.实验运行结果如下

22

=

1,

0,

11.添加“GIS分析”的下拉控件,文本“要素统计”,控件名“miStatistic” 12.添加“要素统计”函数,代码如下 ////////////要素统计

public string Statistic(string layerName, string fieldName, IMap iMap) {

DataOperator dataOperator = new DataOperator(iMap); IFeatureLayer featLayer = (IFeatureLayer)dataOperator.GetLayerByName(layerName); ///

IFeatureClass featClass = featLayer.FeatureClass; IDataStatistics dataStatistic = new DataStatistics(); IFeatureCursor featCursor;

featCursor = featClass.Search(null, false); ICursor cursor = (ICursor)featCursor; dataStatistic.Cursor = cursor;

///////指定统计字段为面积字段,统计出最小面积、最大面积及平均面积 dataStatistic.Field = fieldName; IStatisticsResults statResult;

statResult = dataStatistic.Statistics; double dMax; double dMin; double dMean;

dMax = statResult.Maximum; dMin = statResult.Minimum; dMean = statResult.Mean; string sResult;

sResult = \最大面积\最小面积\平均面积\ return sResult; }

13.添加“要素统计啊”点击事件响应函数,代码如下

23