ArcGIS二次开发基础教程(09):叠加分析
缓冲区分析的概念及原理请查看帮助文档 http://desktop.arcgis.com/zh-cn/arcmap/latest/tools/analysis-toolbox/how-buffer-analysis-works.htm
缓冲区分析
//实现对图层中所有点要素进行缓冲分析
IGraphicsContainer graphicsContainer = axMapcontrol1.Map as IGraphicsContainer;
IFeatureLayer featureLayer = GetLayerByName("图层名称");
//IQueryFilter filter = new QueryFilterClass();
//ISpactialFilter filter = new SpatialFilterClass();
//此处不设置查询条件,也可以设置如上面两行的属性或空间查询条件,只对符合特定条件的要素进行缓冲区分析
IFeatureCursor cursor = featureLayer.FeatureClass.Search(null,true);
IFeature feature = cursor.NextFeature();
//定义一个ITopologicalOperator变量 有关ITopologicalOperator的成员说明请务必查看帮助文档
ITopologicalOperator topo;
while(feature != null)
{
topo = feature.ShapeCopy as ITopologicalOperator;
//参数为缓冲区半径 半径大小需特别注意 根据实际地图的大小来确定
IGeometry buffer = topo.Buffer(0.001);
IElement ele = new PolygonClass();
ele.Shape = buffer;
graphicsContainer.AddElement(ele,0);
feature = cursor.NextFeature();
}
axMapControl1.Refresh();
裁剪
有关概念查看帮助文档 http://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/analysis-toolbox/clip.htm
//实现 从屏幕划取矩形(也可以是圆或者多边形)作为裁剪区域对指定图层进行裁剪并把裁剪结果作为新的图层添加到地图中
private void clip(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//以下代码实现在文件打开路径处创建一个新的FeatureClass
//首先在文件路径处打开要素类工作空间
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
//path为路径全局变量,打开文件时赋值
IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectory(path));
//裁剪图层
IFeatureLayer featureLayer = GetLayerByName("图层名称");
//copy目标图层的要素字段用来创建新的要素类
IFields fields = featureLayer.FeatureClass.Fields;
IObjectCopy copy = new ObjectCopyClass();
IFields fieldsCopy = copy.Copy(fields) as IFields;
//创建一个新的要素类 参数一为要素类名称,二为字段,其它请务必参考帮助文档
IFeatureClass newClass = featureWorkspace.CreateFeatureClass("Clip", fieldsCopy, null, null, esriFeatureType.esriFTSimple, "Shape", "");
//从地图获取裁剪区域
IEnvelop env = axMapControl1.TrackRectangle;
//用获得区域与图层进行空间相交查询获得目标要素
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
spatialFilter.Geometry = env as IEnvelop;
IFeatureCursor cursor = featureLayer.FeatureClass.Search(spatialFilter,true);
IFeature feature = cursor.NextFeature();
ITopologicalOperator topo;
//设置允许插入新要素
IFeatureCursor insterCursor = newClass.Inster(true);
//创建 缓冲要素 要素暂存在内存中
IFeatureBuffer buffer = newClass.CreateFeatureBuffer();
//遍历查询到的要素 进行裁剪获取空间信息
while(faeture != null)
{
topo = feature.ShapeCopy as ITopologicalOperator;
IGeometry geometry = new PolygonClass();
//裁剪只能获取要素信息,查询获得属性信息 两者结合才能得到完整的要素类
topo.QueryClipped(env,geometry);
//结合查询属性和空间信息
for(int i=0;i<feature.Fields.FieldCount;i++)
{
if(feature.get_Fields(i).Name.Equals("Shape"))
{
buffer.Shape = geometry;
continue;
}
buffer.set_Value(i,feature.get_Value(i));
}
//插入要素
insterCursor.InsterFeature(buffer);
feature = cursor.NextFeature();
}
//创建新图层 设置要素类
IFeatureLayer layer = new FeatureLayerClass();
layer.FeatureClass = newClass;
layer.Name = newClass.AliceName;
//添加到地图控件中
axMapControl1.AddLayer(layer);
axMapControl1.Refresh();
axTOCControl1.Update();
}
栅格叠加
查看帮助文档 http://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/commonly-used-tools/overlay-analysis.htm
//获取栅格数据集
private IGeoDataset GetDatasetByPath(string path)
{
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IRasterWorkspace rasterWorkspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(path)) as IRasterWorkspace;
IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(path));
return rasterDataset as IGeoDataset;
}
//从路径中获取栅格数据转换为栅格波段对象
IRasterBand rasterBand_1 = GetDatasetByPath(path1);
IRasterBand rasterBand_2 = GetDatasetByName(path2);
//创建一个IRasterBandCollection接口
IRasterBandCollection rasterBandCollection = new RasterClass();
//添加数据
rasterBandCollection.AppendBand(rasterBand_1);
rasterBandCollection.AppendBand(rasterBand_2);
//转换为IGeodataset
IGeoDataset inputDataset = rasterBandCollection as IGeoDataset;
//创建栅格波段集合对象
ILocalOp localOp = new RasterLocalOpClass();
//调用Conbine方法
IGeoDataset output = localOp.Combine(inputDataset);
//用叠加生成的地理数据集创建栅格图层
IRasterLayer layer = new RasterLayerClass();
layer.CreateFromRaster(output as IRaster);
layer.Name = "Combine";
axMapControl1.AddLayer(layer as ILayer);
axMapControl1.Refresh();
axTOCControl1.Update();