GPPointFileInfo\GPInputCoordinateSystem.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPInputCoordinateSystem.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry

Namespace GPPointFileInfo
  Public Class GPInputCoordinateSystem
        Implements GPPointFileInfo.IGPInputCoordinateSystem
    Private Shared _parameterIndex As Integer

    Private _spatialReference As ESRI.ArcGIS.Geometry.ISpatialReference

    Public Sub New(ByVal parameterValueArray As IParameterValueArray)
      Dim gpValue As IGPValue = parameterValueArray.GetGPValue(_parameterIndex)

      _spatialReference = GetSpatialReference(gpValue)
    End Sub

    Public Shared Function GetGPParameterEdit() As IGPParameterEdit
      Dim gpParameterEdit As IGPParameterEdit = New GPParameterClass()

      Dim gpDataType As IGPDataType = New GPSpatialReferenceTypeClass()

      Dim gpValue As IGPValue = New GPSpatialReference()

      gpParameterEdit.Name = "in_input_coordinate_system"
      gpParameterEdit.DisplayName = "Input Coordinate System"
      gpParameterEdit.DataType = gpDataType
      gpParameterEdit.Value = gpValue
      gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput
      gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeOptional

      Return gpParameterEdit
    End Function

    Public Shared Sub SetGPParameterIndex(ByVal index As Integer)
      _parameterIndex = index
    End Sub

        Public ReadOnly Property SpatialReference() As ESRI.ArcGIS.Geometry.ISpatialReference Implements IGPInputCoordinateSystem.SpatialReference
            Get
                Return _spatialReference
            End Get
        End Property

    Private Function GetSpatialReference(ByVal gpValue As IGPValue) As ESRI.ArcGIS.Geometry.ISpatialReference
      Dim spatialReference As ESRI.ArcGIS.Geometry.ISpatialReference

      Dim gpSpatialReference As IGPSpatialReference = TryCast(gpValue, IGPSpatialReference)

      If gpSpatialReference.SpatialReference Is Nothing Then
        spatialReference = GetUnknownSpatialReference()
      Else
        spatialReference = gpSpatialReference.SpatialReference
      End If

      Dim spatialReferenceResolution As ISpatialReferenceResolution = TryCast(spatialReference, ISpatialReferenceResolution)

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

      Dim spatialReferenceTolerance As ISpatialReferenceTolerance = TryCast(spatialReferenceResolution, ISpatialReferenceTolerance)

      spatialReferenceTolerance.SetDefaultXYTolerance()
      spatialReferenceTolerance.SetDefaultZTolerance()

      Return spatialReference
    End Function

    Private Function GetUnknownSpatialReference() As ESRI.ArcGIS.Geometry.ISpatialReference
      Dim spatialReference As ESRI.ArcGIS.Geometry.ISpatialReference

      Dim unknownCoordinateSystem As IUnknownCoordinateSystem = New UnknownCoordinateSystemClass()

      spatialReference = TryCast(unknownCoordinateSystem, ESRI.ArcGIS.Geometry.ISpatialReference)

      Return spatialReference
    End Function
  End Class
End Namespace