Alter SpatialReference

Created:01/03/2001
Description:

To enable a feature layer to be projected on-the-fly, ArcMap needs to know the source coordinate system (for the layer) and the target coordinate system (for the Map display).

This sample uses a shape file (representing the Eastern Europe) that uses the Two Point Equidistant projected coordinate system. It has an associated PRJ file that records this information.

If we delete or rename the PRJ file before the shape file has been loaded into ArcMap, then ArcMap will try and make an estimate as to what coordinate system is being used and will assign the feature layer an "Unknown" SpatialReference. It will not be able to reproject the feature layer on-the-fly, unless we reassign the feature layer with the correct coordinate system (or Spatial Reference).

ESRI recommend that ArcCatalog is used to maintain your data, including the Spatial References. However, this ArcMap sample shows how you can alter the Spatial Reference of a feature layer that has already been loaded whilst ArcMap is running.

This sample uses the IGeoDatasetSchemaEdit interface to alter the Spatial Reference for a GeoDataset.


How to use:
  1. This sample uses two shape files that, depending on your installation, can be found in the following folder:

    C:\ArcGIS\arcexe83\ArcObjects Developer Kit\Samples\Data\Projection

    File NameDescriptionShapeTypeProjected Coord System
    europeEquidistantEastern EuropePolygonTwo Point Equidistant
    westeuutm33Western Europe PolygonUTM Grid Zone 33N

  2. Using Windows Explorer, rename europeEquidistant.prj to europeEquidistant.prjxxx. This prevents ArcMap from automatically locating the PRJ file when it loads the europeEquidistant feature layer.

  3. Start ArcMap.

  4. Add a feature layer - browse and add westeuutm33. As this is the first layer to be added, the Map display will use the same Spatial Reference that is associated with this layer, i.e. UTM, Grid Zone 33N.

  5. Now browse to and add the europeEquidistant feature layer. Note that ArcMap recognises that this layer does not have Spatial Reference information.
  6. By default, you should not be able to see the europeEquidistant feature layer. If you click on the Full Extent tool the europeEquidistant feature layer can be now be seen somwhere to the south.
  7. If the europeEquidistant.prj file was available, ArcMap would have been able to perform an on-the-fly projection and automatically re-project the europeEquidistant feature layer to fit the westeuutm33 feature layer.
  8. To correct this, add a new ArcMap Macro called AlterSpatialReference and in the VBA editor, paste the following code into it.
  9. Run the Macro. You will see that the europeEquidistant feature layer is reprojected and is now aligned with westeuutm33. Both feature layers are being displayed using UTM Grid Zone 33N.
  10. If you go back to the Windows Explorer, you will also find that ArcMap has written out a new europeEquidistant.prj to disk.
Sub AlterSpatialReference()

  ' Start by getting a handle on the current FocusMap
  Dim pMxDoc As IMxDocument
  Set pMxDoc = Application.Document

  Dim pMap As IMap
  Set pMap = pMxDoc.FocusMap

  'Assume that europeEquidistant is added last and that it is at the top of the map.
  Dim pLayer As IFeatureLayer
  Set pLayer = pMap.Layer(0)

  'This is how we get the current spatial reference for a layer
  Dim pGeoDataset As IGeoDataset
  Dim pSpatialReference As ISpatialReference
  Set pGeoDataset = pLayer 'QI for the geodatset from the layer
  Set pSpatialReference = pGeoDataset.SpatialReference
  ' Note that ArcMap sets the SR as "Unknown"
  MsgBox pLayer.Name + " SpatialReference is " + pSpatialReference.Name

  If (pSpatialReference.Name = "Unknown") Then
    ' Get the FeatureClass from the Layer
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pLayer.FeatureClass
    ' QI for the Geodataset from the FeatureClass
    Set pGeoDataset = pFeatureClass
    ' QI for GeoDatasetSchemaEdit from the Geodataset
    Dim pGeoDatasetEdit As IGeoDatasetSchemaEdit
    Set pGeoDatasetEdit = pGeoDataset
    ' Test if we can alter the spatialreference, if we can 
    ' then we create a factory and use that to create
    ' a projected coordinate system.
    If (pGeoDatasetEdit.CanAlterSpatialReference = True) Then
      Dim pSpatRefFact As ISpatialReferenceFactory2
      Set pSpatRefFact = New SpatialReferenceEnvironment
      ' Use a SpatialReferenceFactory to create the Projected Coordinate System.
      ' Here we are using a Factory Code for the Two Point Equidistant coordinate system.
      Dim pProjCoordSys As IProjectedCoordinateSystem
      Set pProjCoordSys = pSpatRefFact.CreateProjectedCoordinateSystem(esriSRProjCS_World_TwoPointEquidistant)
      ' Now alter the layers spatial reference
      pGeoDatasetEdit.AlterSpatialReference pProjCoordSys

      ' Now get the updated SpatialReference and its name
      Set pSpatialReference = pGeoDataset.SpatialReference
      MsgBox pLayer.Name + " SpatialReference is " + pSpatialReference.Name
      ' and force a full refresh
      pMxDoc.ActiveView.Refresh
    End If
  End If
End Sub