GPPointFileInfo\SpatialReferenceClass.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\SpatialReferenceClass.cs
// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
// 

using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geometry;

namespace GPPointFileInfo
{
    public class SpatialReferenceClass : GPPointFileInfo.ISpatialReference
    {
        private ESRI.ArcGIS.Geometry.ISpatialReference _spatialReference;
        private bool _isUnknown;

        public SpatialReferenceClass(ICoordinateSystem coordinateSystem)
        {
            _spatialReference = GetSpatialReference(coordinateSystem);

            _isUnknown = GetIsUnknown(_spatialReference);
        }

        public ESRI.ArcGIS.Geometry.ISpatialReference SpatialReference
        {
            get
            {
                return _spatialReference;
            }
        }

        private ESRI.ArcGIS.Geometry.ISpatialReference GetSpatialReference(ICoordinateSystem coordinateSystem)
        {
            ESRI.ArcGIS.Geometry.ISpatialReference spatialReference;

            if ((coordinateSystem is IESOutputCoordinateSystem) && (coordinateSystem.SpatialReference == null))
            {
                spatialReference = GetUnknownSpatialReference();
            }
            else
            {
                spatialReference = coordinateSystem.SpatialReference;
            }

            ISpatialReferenceResolution spatialReferenceResolution = spatialReference as ISpatialReferenceResolution;

            spatialReferenceResolution.ConstructFromHorizon();
            spatialReferenceResolution.SetDefaultXYResolution();
            spatialReferenceResolution.SetDefaultZResolution();

            ISpatialReferenceTolerance spatialReferenceTolerance = spatialReference as ISpatialReferenceTolerance;

            spatialReferenceTolerance.SetDefaultXYTolerance();
            spatialReferenceTolerance.SetDefaultZTolerance();

            return spatialReference;
        }

        private ESRI.ArcGIS.Geometry.ISpatialReference GetUnknownSpatialReference()
        {
            return new UnknownCoordinateSystemClass();
        }

        public bool IsUnknown
        {
            get
            {
                return _isUnknown;
            }
        }

        private bool GetIsUnknown(ESRI.ArcGIS.Geometry.ISpatialReference spatialReference)
        {
            return (spatialReference.Name == "Unknown");
        }
    }
}