Showing posts with label Raster. Show all posts
Showing posts with label Raster. Show all posts

Monday, May 21, 2007

A better raster clipping and mosaic tool for ArcMap



I've been frustrated with processing raster imagery in ArcMap ever since 8.0 came out way back when. I finally got around to making my own tool that makes clipping and mosaicking much easier and more intuitive. All the relevant info (mosaic type, extent, etc) is right there on the dialog, and most importantly, this tool works seamlessly with raster catalogs. Another nicety is that the output is a plain old TIF file with a TFW file, so that AutoCad puts the image into the right place (for some reason I could never get ArcMap to make a GeoTIFF file that AutoCad Map could place properly).
The only thing I wasn't able to work out satisfactorily is setting the cell size. I wasn't able to have ArcMap do this while performing the mosaic, it would have to be a totally seperate step, which ended up being too time consuming.


Here's the full Visual Studio solution, including an installer:
WSSI_ArcMapTools1.zip

Here's some of the code for anyone that's interested in the inner workings. This is the section that performs the actual mosaic:



private IDataset Mosaic(IRasterCollection pRasterCollection, string strPath, string strName, IEnvelope pEnvelope, double CellSize, string MosaicType)
{
IMosaicRaster pMosaicRaster;
ISaveAs2 pSaveAs;
IDataset pResultData;
IWorkspaceFactory pWkspFact;
IWorkspace pWorkspace;
IRasterProps pRasterProps;

//Set mosaic type
pMosaicRaster = (IMosaicRaster)pRasterCollection;
switch (MosaicType)
{
case "Maximum":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MAX;
break;
case "Minimum":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MIN;
break;
case "Mean":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MEAN;
break;
case "Blend":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_BLEND;
break;
case "First":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_FIRST;
break;
case "Last":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_LAST;
break;
default:
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MAX;
break;
}

//Set the extent
pRasterProps = (IRasterProps)pMosaicRaster;
pRasterProps.Extent = pEnvelope;
pRasterProps.SpatialReference = m_MxDocument.FocusMap.SpatialReference;

//Set Cell Size...
// ... nevermind, we can't :(

//Set the save as
pWkspFact = new RasterWorkspaceFactoryClass();
pWorkspace = pWkspFact.OpenFromFile(strPath, 0);
pSaveAs = (ISaveAs2)pMosaicRaster;
IRasterStorageDef pRasterDef = new RasterStorageDefClass();
IPnt pPnt = new PntClass();
pPnt.X = CellSize;
pPnt.Y = CellSize;
pRasterDef.CellSize = pPnt; //This actually only works when storing an SDE Raster

pResultData = (IDataset)pSaveAs.SaveAsRasterDataset(strName, pWorkspace, "TIFF", pRasterDef);
return pResultData;
}