This document is archived and information here might be outdated.  Recommended version.


How to import or export a spatial reference (ArcObjects .NET 10.4 SDK)

How to import or export a spatial reference


Importing or exporting a spatial reference

This following code example 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 it 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 causes any coordinate grid and tolerance information that might have been defined for it to be lost.
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.
[C#]
private void ImportExportSR_Example()
{
    // Set up the SpatialReferenceEnvironment.
    // SpatialReferenceEnvironment is a singleton object and needs to use the Activator class.
    Type factoryType=Type.GetTypeFromProgID(
        "esriGeometry.SpatialReferenceEnvironment");
    System.Object obj=Activator.CreateInstance(factoryType);
    ISpatialReferenceFactory3 spatialReferenceFactory=obj as
        ISpatialReferenceFactory3;

    // Create a predefined PCS and set its coordinate grid information.
    // The default XY tolerance value is 0.001 meters or its equivalent in the coordinate 
    // system units.
    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()
    
    ' Set up the SpatialReferenceEnvironment.
    ' SpatialReferenceEnvironment is a singleton object and needs to use the Activator class.
    Dim factoryType As Type=Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment")
    Dim spatialReferenceFactory As ISpatialReferenceFactory3=CType(Activator.CreateInstance(factoryType), ISpatialReferenceFactory3)
    
    ' Instantiate a predefined coordinate system and set its coordinate grid information.
    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


See Also:

Using the SpatialReferenceEnvironment




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine