Summary
Images can be projected from one coordinate system to another and saved to one of the three output formats using the following sample script. Note: this script is provided as is and is not supported by ESRI Technical Support.
Procedure
- Start ArcMap.
- Open the Visual Basic Editor.
In ArcMap, select Tools > Macros > Visual Basic Editor.
- In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and click View Code.
![[O-Image] Visual Basic Project Explorer](https://webapps-cdn.esri.com/CDN/support-site/technical-articles-images/000004509/00N39000003LL2C-0EM39000000wccP.png)
Note:
Code in the ThisDocument code module will only run in the current map document. To store the code in all your map documents, open the Normal.mxt ThisDocument code module.
- Copy the following code into the code module:
Public Sub ProjectThisImage()
' Instantiate a WorkspaceFactory object.
Dim pWrkFac As IWorkspaceFactory
Set pWrkFac = New RasterWorkspaceFactory
' Initialize and set the path to the Raster Workspace.
Dim strPath As String
strPath = "c:\temp"
' Open the raster workspace.
Dim pRasterWorkspace As IRasterWorkspace
Set pRasterWorkspace = pWrkFac.OpenFromFile(strPath, 0)
' Initialize and set the name of the input image.
Dim strInName As String
strInName = "myimage.tif"
' Open the raster dataset.
Dim pRasterDataset As IRasterDataset
Set pRasterDataset = pRasterWorkspace.OpenRasterDataset(strInName)
' Change from RasterDataset to RasterLayer.
Dim pRasterLayer As IRasterLayer
Set pRasterLayer = New RasterLayer
pRasterLayer.CreateFromDataset pRasterDataset
' Instantiate the raster from the raster layer.
Dim pRaster As IRaster
Set pRaster = pRasterLayer.Raster
Dim pRasterProps As IRasterProps
Set pRasterProps = pRaster
' QI from IRaster to IRasterProps
Set pRasterProps = pRaster
' Get the current cell size.
Dim pCell As IPoint
Set pCell = New Point
pCell.X = pRasterProps.MeanCellSize.X
pCell.Y = pRasterProps.MeanCellSize.Y
' Create a new raster geometry process.
Dim pRasGeoProc As IRasterGeometryProc
Set pRasGeoProc = New RasterGeometryProc
' Set the spatial reference to UTM, Zone 18N, NAD83.
Dim pSpatRefFac As ISpatialReferenceFactory2
Set pSpatRefFac = New SpatialReferenceEnvironment
Dim pSpatRef As ISpatialReference
Set pSpatRef = pSpatRefFac.CreateProjectedCoordinateSystem(esriSRProjCS_NAD1983UTM_18N)
' Project the image to the new spatial reference.
pRasGeoProc.ProjectFast pSpatRef, RSP_NearestNeighbor, , pRaster
'pRasGeoProc.ProjectFast pSpatRef, RSP_NearestNeighbor, pCell, pRaster
' Instantiate a new Raster Band Collection.
Dim pRasterBandCollection As IRasterBandCollection
Set pRasterBandCollection = pRaster
' Set the new image name.
Dim strOutName As String
strOutName = "mynewimage.tif"
' Set the output raster type.
Dim strOutType As String
strOutType = "TIFF"
'NOTE: This can be one of three types: "TIFF", "GRID", or "IMAGINE Image";
'the extension on the file should be changed accordingly.
' Save the projected raster to the new name.
pRasterBandCollection.SaveAs strOutName, pRasterWorkspace, strOutType
'Clean the objects out of memory.
Set pWrkFac = Nothing
Set pRasterWorkspace = Nothing
Set pRasterDataset = Nothing
Set pRaster = Nothing
Set pRasterProps = Nothing
Set pCell = Nothing
Set pRasGeoProc = Nothing
Set pSpatRefFac = Nothing
Set pSpatRef = Nothing
Set pRasterBandCollection = Nothing
End Sub
- Close the Visual Basic Editor.
- Run the code.
A. Click Tools > Macros > Macros to display the Macros dialog box.
B. Select a macro and click Run.