How to import or export a spatial reference


Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

Additional Requirements
  • This article assumes your project includes references to the ESRI Geometry assembly.

Importing or exporting a spatial reference

This article shows how to create a predefined projected coordinate system (PCS), define its coordinate grid and tolerance values, then export and import it two different ways. First, it exports the coordinate system to a .prj file, then uses the contents of the .prj file to create a second PCS.
 
At the end of this process, you might expect that both PCSs will be identical, but they are not. PRJ files do not store coordinate grid information, so re-creating a spatial reference from a PRJ file will lose any coordinate grid and tolerance information that might have been defined for it.
 
Complete equality of spatial references (equal coordinate systems and equal coordinate grids) cannot be checked with one method. IClone.IsEqual compares coordinate systems but not coordinate grids. You need to use other methods to do the latter. See the following:
 

[C#]
private void ImportExportSR_Example()
{
//Instantiate a predefined spatial reference and set its coordinate grid information.
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_10N);
ISpatialReferenceResolution spatialReferenceResolution = projectedCoordinateSystem as ISpatialReferenceResolution;
ISpatialReferenceTolerance spatialReferenceTolerance = projectedCoordinateSystem as ISpatialReferenceTolerance;

spatialReferenceResolution.ConstructFromHorizon();
spatialReferenceTolerance.SetDefaultXYTolerance();

//Export the PCS to a .prj file.
String fileName = "c:\\temp\\utm10.prj";
spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName, projectedCoordinateSystem);

//Rehydrate it as a new spatial reference object.
ISpatialReference projectedCoordinateSystem2 = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(fileName);

//See if they are equal.
IClone comparison = projectedCoordinateSystem as IClone;

//Should be true, but coordinate grid information has not been checked.
System.Windows.Forms.MessageBox.Show((comparison.IsEqual(projectedCoordinateSystem2 as IClone)).ToString());

ISpatialReference2 comparePrecisions = projectedCoordinateSystem as ISpatialReference2;

//Should be false, PRJ files do not persist coordinate grid information.
System.Windows.Forms.MessageBox.Show((comparePrecisions.IsXYPrecisionEqual(projectedCoordinateSystem2)).ToString());
}

[VB.NET]
Private Sub ImportExportSR_Example()

  'Instantiate a predefined spatial reference and set its coordinate grid information.
  Dim spatialReferenceFactory As ISpatialReferenceFactory = New SpatialReferenceEnvironmentClass()
  Dim projectedCoordinateSystem As IProjectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_WGS1984UTM_10N) '32610
  Dim spatialReferenceResolution As ISpatialReferenceResolution = CType(projectedCoordinateSystem, ISpatialReferenceResolution)
  Dim spatialReferenceTolerance As ISpatialReferenceTolerance = CType(projectedCoordinateSystem, ISpatialReferenceTolerance)
  spatialReferenceResolution.ConstructFromHorizon()
  spatialReferenceTolerance.SetDefaultXYTolerance()

  'Export the PCS to a .prj file.
  Dim fileName As String = "c:\\temp\\utm10a.prj" 
  spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName, projectedCoordinateSystem)

  'Rehydrate it as a new spatial reference object.
  Dim projectedCoordinateSystem2 As ISpatialReference = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(fileName)

  'See if they are equal.
  Dim comparison As IClone = CType(projectedCoordinateSystem, IClone)

  'Should be true, but coordinate grid information has not been checked.
  System.Windows.Forms.MessageBox.Show((comparison.IsEqual(projectedCoordinateSystem2)).ToString())
  Dim comparePrecisions As ISpatialReference2 = CType(projectedCoordinateSystem, ISpatialReference2)

  'Should be false, PRJ files do not persist coordinate grid information.
  System.Windows.Forms.MessageBox.Show((comparePrecisions.IsXYPrecisionEqual(projectedCoordinateSystem2)).ToString())
End Sub