using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.GeoAnalyst;
public class ArcGISRaster
{
//ArcGIS影像操作
/// <summary>
/// 影像切割by yl 2008.06.16 [email protected],参考http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28659&extra=&page=1修改
/// </summary>
/// <param name="pRasterLayer">//要裁切的影像图层</param>
/// <param name="FileName">文件名为.img</param>
public static 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 RasterExtractionOpClass();
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
//如果直接保存为img影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();
IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0);
ISaveAs pSaveAs = clipRaster as ISaveAs;
pSaveAs.SaveAs(System.IO.Path.GetFileName(FileName), pWorkspace, "IMAGINE Image");
}
}
调用: //by yl 2008.06.16 [email protected]
private void mainMap_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IGeometry clipGeo = this.mainMap.TrackPolygon(); //
ILayer layer = this.mainMap.get_Layer(0); //要裁切的影像图层
IRasterLayer pRasterLayer = layer as IRasterLayer;
ArcGISPub.ArcGISRaster.RasterClip(pRasterLayer,clipGeo as IPolygon,@"c:\temp\yl.img");
}
参考http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28659&extra=&page=1修改