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:

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;
case "Minimum":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MIN;
case "Mean":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MEAN;
case "Blend":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_BLEND;
case "First":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_FIRST;
case "Last":
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_LAST;
pMosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_MAX;

//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;


Anonymous said...

Nice raster clipping tool. I just used it with ArcGIS 9.3 and it worked fine. Thanks for sharing!

Patrick said...

This looks like a great tool but I can't seem to get it installed. The setup runs fine but nothing shows up in ArcMap and doing the Tools>Customize>Add From File routine doesn't seem to work either. Any thoughts?

Ben Slater said...

Patrick -
The installation doesn't add the command to any toolbars on its own - you have to do it manually:

1. In the Tools>Customize dialog, click on the "Commands" tab.

2. Under "Categories" scroll down and you should see "WSSI" listed.

3. Click on "WSSI" and you should see "WSSI Mosaic Wizard" listed in the commands section.

4. Drag the WSSI Mosaic Wizard icon to a toolbar of your choice.

5. Close the Customize dialog and click on the icon to start the wizard.

Anonymous said...

Please, can you PM me and tell me few more thinks about this, I am really fan of your blog...

Kurt and Julie said...

I don't know if you will see this comment because this post is 3 years old but I can't download your tool and was wondering if there was another way to download it. I see that you offer some through windows live but not this one. If you could help that would be great.

blogger said...

hey some one re upload the tool link is dead thanks.