CopySpatialReference in ArcMap

Last Modified:01/09/2001

This sample demonstrates how to copy the spatial reference from a dataset in ArcMap to the raster datasets in a specified folder. All the raster datasets will be given the new spatial reference, so care must be taken that the target datasets are actually valid for this spatial reference.This will not reproject the coordinate values of the data, it only assigns a spatial reference. The sample code can easily be changed to work with other types of datasets.

Also see the CopySpatialReference in ArcCatalog sample.

How to use:
  1. Start ArcMap.

  2. Add a dataset that has a spatial reference that you wish to copy to your raster datasets.
  3. Update the macro with the folder name that contains the raster datsets to be updated.
  4. Run the Macro.
Public Sub copyspatialref()
'this routine will copy the spatial reference of the current
'map document to the raster datasets in the specified folder
  Dim pMxDoc As IMxDocument
  Dim pWorkspaceFactory As IWorkspaceFactory
  Dim pWorkspace As IWorkspace
  Dim pDataset As IDataset
  Dim pRDataset As IRasterDataset
  Dim RasterLayerName As String
  Dim strWorkspace As String
  Dim pSpatialReference As ISpatialReference
  Dim pEnumDataset As IEnumDataset
  Dim pRasterDataset As IRasterDataset
  Dim pRaster As IRaster
  Dim PGDSE As IGeoDatasetSchemaEdit
  Dim newworkspace As String

  newworkspace = "F:\sptest"
  ' Get Document and Map
  Set pMxDoc = ThisDocument
  ' Get Spatial Reference from Map
  Set pSpatialReference = pMxDoc.Maps.Item(0).SpatialReference
   ' Set Workspace and Raster names
   strWorkspace = newworkspace
   ' Set Raster Workspace Factory
   Set pWorkspaceFactory = New RasterWorkspaceFactory
   Set pWorkspace = pWorkspaceFactory.OpenFromFile(strWorkspace, 0)
   ' Get only RasterDatasets
   Set pEnumDataset = pWorkspace.Datasets(esriDTRasterDataset)
   ' Reset Enum to beginning
   ' Get first dataset
   Set pRasterDataset = pEnumDataset.Next
   ' Make sure there is at least 1 Raster Dataset
   If pRasterDataset Is Nothing Then
      MsgBox "No Raster Datasets were found!", vbCritical
      Exit Sub
   End If
   Do While Not pRasterDataset Is Nothing
      Set pRaster = pRasterDataset.CreateDefaultRaster
      Set PGDSE = pRasterDataset
      ' Write Spatial Reference to Raster Dataset
      PGDSE.AlterSpatialReference pSpatialReference
      Set pRasterDataset = pEnumDataset.Next

End Sub