public static IRaster ClipRaster(IRaster2 sourceRaster, IEnvelope extent, IWorkspace workspace, string rasterName, string rasterFormat) { //map algebra method for clipping //NoData value is not set properly (always ends up as largest positive integer supported by pixel type) //clip SetRasterAnalysisEnvCellSize(sourceRaster, true); SetRasterAnalysisEnvExtent(extent, sourceRaster, true); IMapAlgebraOp mapAlgebraOp = new RasterMapAlgebraOpClass(); string rasterSymbol = "source"; mapAlgebraOp.BindRaster((IGeoDataset)sourceRaster, rasterSymbol); IGeoDataset tempClipRaster = mapAlgebraOp.Execute("[" + rasterSymbol + "]"); //set pixel type and nodata value to match input raster and save IRasterProps propsClip = tempClipRaster as IRasterProps; IRasterProps propsSource = sourceRaster as IRasterProps; propsClip.PixelType = propsSource.PixelType; propsClip.NoDataValue = propsSource.NoDataValue; // this doesn't stick IRasterDataset3 clipOutput = ((ISaveAs2)tempClipRaster).SaveAs(rasterName, workspace, rasterFormat) as IRasterDataset3; //delete the temporary dataset created by the map algebra op if (((IDataset)((IRaster2)tempClipRaster).RasterDataset).CanDelete()) ((IDataset)((IRaster2)tempClipRaster).RasterDataset).Delete(); IRaster outRaster = clipOutput.CreateDefaultRaster(); //IRaster outRaster = clipOutput.CreateFullRaster(); //this method has same result return outRaster; }
Eric,
Just a thought, if you have used the geoprocessing clip tool could you not then use the SetNull tool after the clip, if the ISaveAs is not working?
Duncan
Try using the IRasterWorkspaceEx.SaveAsRasterDataset() method.
e.g.
var rasterWorkspace = (IRasterWorkspaceEx) workspace; IRasterProps propsSource = sourceRaster as IRasterProps; IRasterDef rasterDef = new RasterDef(); rasterDef.SpatialReference = propsSource.SpatialReference; var raster = ((IRasterDataset3)tempClipRaster).CreateFullRaster(); ((IRasterProps)raster).NoDataValue = propsSource.NoDataValue // Ensure that this is the value you expect. IRasterDataset3 clipOutput = rasterWorkspace.SaveAsRasterDataset(rasterName, raster, null, null, rasterDef, null);