基于C#的AE二次开发之影像数据的裁切(掩膜)
我的开发环境为ArcGIS Engine 10.2与Visual studio2010,主地图名称为axMapControl1,如果变动则需要修改(注意相关事件的添加与动态链接库的引入)。
- 效果预览
实现代码
- 引用类库
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.GeoAnalyst;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.Geometry;
- 代码实现
private void 影像裁剪ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (axMapControl1.get_Layer(0) is IFeatureLayer && axMapControl1.get_Layer(1) is IRasterLayer)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;//输入裁剪矢量图层
IRasterLayer pRasterLayer = axMapControl1.get_Layer(1) as IRasterLayer;//输入裁剪栅格图层
IPolygon pPoly = pFeatureLayer.FeatureClass.GetFeature(0).Shape as IPolygon;//设置裁剪要素
rasterClip(pRasterLayer, pPoly, "rasterClip0");//调用裁剪方法
//强提醒,完成裁剪
MessageBox.Show("裁剪完成!");
}
}
/// <summary>
/// 掩膜处理
/// </summary>
/// <param name="pRasterLayer">输入裁剪栅格图层</param>
/// <param name="clipGeo">输入裁剪矢量要素</param>
/// <param name="FileName">输出文件名称</param>
public void rasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo, string FileName)
{
IRaster pRaster = pRasterLayer.Raster;//获取栅格文件
IRasterProps pProps = pRaster as IRasterProps;
object cellSizeProvider = pProps.MeanCellSize().X;
IGeoDataset pInputDataset = pRaster as IGeoDataset;
IExtractionOp pExtractionOp = new RasterExtractionOp() as IExtractionOp;
IRasterAnalysisEnvironment 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);
IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true);
IRaster clipRaster; //裁切后得到的IRaster
if (pOutputDataset is IRasterLayer)
{
IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
clipRaster = rasterLayer.Raster;
}
else if (pOutputDataset is IRasterDataset)
{
IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
clipRaster = rasterDataset.CreateDefaultRaster();
}
else if (pOutputDataset is IRaster)
{
clipRaster = pOutputDataset as IRaster;
}
else
{
return;
}
//保存裁切后得到的clipRaster,直接保存为tif影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactory();
//保存裁剪后的影像到本地路径下
string filepath = @"c:\dat\";
//判断文件夹是否存在,如果不存在新建文件夹
if (false == System.IO.Directory.Exists(filepath))
{
//创建文件夹
Directory.CreateDirectory(filepath);
}
IWorkspace pWorkspace = pWKSF.OpenFromFile(filepath, 0);
ISaveAs pSaveAs = clipRaster as ISaveAs;
//输出要素类的名称,文件后缀可以设置
pSaveAs.SaveAs(FileName+".tif", pWorkspace, "TIFF");
}
- 导出图像格式
格式名称 | 字符串引用 |
---|---|
Imagine | "IMAGINE Image" |
TIFF | "TIFF" |
GRID | "GRID" |
JPEG | "JPG" |
JP2000 | "JP2" |
BMP | "BMP" |
PNG | "PNG" |
GIF | "GIF" |
PCI Raster | "PIX" |
X11 Pixmap | "XPM" |
PCRaster | "MAP" |
Memory Raster | "MEM" |
HDF4 | "HDF4" |
BIL | "BIL" |
BIP | "BIP" |
BSQ | "BSQ" |
Idrisi Raster Format | "RST" |
ENVI Raster Format | "ENVI" |
Geodatabase Raster | "GDB" |