本文主要是介绍AE+C#按掩膜提取,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
点击“空间分析”菜单标题按钮,其中包括:
点击按掩膜提取,会在主界面的MapControl控件里实现该功能。
click事件
private void 按掩膜提取ToolStripMenuItem_Click(object sender, EventArgs e){//设置栅格图层pRasterLayer = GetLayerByName(mainMapControl.Map, textBox2.Text.ToString()) as IRasterLayer;//raster = pRasterLayer as IRaster;raster = pRasterLayer.Raster;IGeoDataset maskRaster = null;maskRaster = raster as IGeoDataset;//获取矢量图层形状IQueryFilter queryFilter = new QueryFilterClass();queryFilter.WhereClause = "";IFeatureCursor featureCursor = pTocFeatureLayer.FeatureClass.Search(queryFilter, true);IFeature feature = featureCursor.NextFeature();IGeometry pGeometry = feature.Shape;IPolygon pPolygon = pGeometry as IPolygon;//执行裁剪操作ClipRaster(raster, pPolygon, "yanmo");}//根据图层名获取图层GetLayerByName/// <summary>/// </summary>/// <param name="pMap">地图文档</param>/// <param name="sLyrName">图层名</param>/// <returns></returns>public static ILayer GetLayerByName(IMap pMap, string sLyrName){ILayer pLyr = null;ILayer pLayer = null;try{for (int i = 0; i < pMap.LayerCount; i++){pLyr = pMap.get_Layer(i);if (pLyr.Name.ToUpper() == sLyrName.ToUpper()){pLayer = pLyr;break;}}}catch (Exception ex){}return pLayer;}//按掩膜提取,流程如下://采用IExtractionOp接口的方法//栅格裁剪(IExtractionOp)//输入栅格//矢量面//保存路径public static void ClipRaster(IRaster pRaster, IPolygon clipGeo, string saveFileName){try{var pProps = pRaster as IRasterProps;if (pProps != null){//设置环境参数object cellSizeProvider = pProps.MeanCellSize().X;var pInputDataset = pRaster as IGeoDataset;IExtractionOp pExtractionOp = new RasterExtractionOpClass();var pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);object extentProvider = clipGeo.Envelope;object snapRasterData = Type.Missing;pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);//裁切操作var pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo, true);//裁切后得到的IRasterIRaster clipRaster;if (pOutputDataset is IRasterLayer){var rasterLayer = pOutputDataset as IRasterLayer;clipRaster = rasterLayer.Raster;}else if (pOutputDataset is IRasterDataset){var rasterDataset = pOutputDataset as IRasterDataset;clipRaster = rasterDataset.CreateDefaultRaster();}else if (pOutputDataset is IRaster){clipRaster = pOutputDataset as IRaster;}else{return;}//保存裁切后得到的clipRasterShowRasterResult(pOutputDataset, saveFileName);}MessageBox.Show("成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception){MessageBox.Show("失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}}public static void ClipRaster(IRasterLayer pRasterLayer, IPolygon clipGeo, string saveFileName){var pRaster = pRasterLayer.Raster;ClipRaster(pRaster, clipGeo, saveFileName);}public static void ClipRaster(IRasterDataset pRasterDataset, IFeature pFeature, string saveFileName){var pRaster = pRasterDataset.CreateDefaultRaster();var clipGeo = (IPolygon)pFeature.Shape;ClipRaster(pRaster, clipGeo, saveFileName);}//显示输出栅格结果private static void ShowRasterResult(IGeoDataset geoDataset, string rasterName){IRasterLayer rasterLayer = new RasterLayerClass();//实例化IRaster raster = new Raster();raster = (IRaster)geoDataset;rasterLayer.CreateFromRaster(raster);rasterLayer.Name = rasterName;MainForm fm1 = new MainForm();fm1.mainMapControl.AddLayer((ILayer)rasterLayer, 0);fm1.mainMapControl.ActiveView.Refresh();}
这篇关于AE+C#按掩膜提取的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!