[GIS] Checkout Spatial Analyst extention, ArcObjects code

arcmaparcobjectsspatial-analyst

Update

After investigating more closely the CODE 2 sample below (at the end of this post), it does check out spatial analyst (sa) extension, however it does not reflect it in the Tools>Extensions menu option as being checked. However you can still use the sa tools.

————–Original question—————

I would like to checkout spatial analyst extension for ArcMap 9.3.1 via this ArcObjects code below, but when I run the code (CODE1) I get these two message box statements and the extension does not checkout under the Tools>Extensions dialog.

"You license has already been initialized please check you implementation"

AND

"SpatialAnalyst license has been checked out for use"

I'm not sure what exactly the first message box means? Any suggestions would be greatly appreciated.

I also found this code (CODE2) to turn on spatial analyst extension, but it does not work either. Maybe I'm missing something obvious with this one.?
Thank you!

CODE 1


Private Sub UIButtonControl6_Click()

  Dim licenseStatus As esriLicenseStatus

  licenseStatus = CheckOutLicense(esriLicenseProductCodeEngine)

  If (licenseStatus = esriLicenseNotLicensed) Then

    licenseStatus = CheckOutLicense(esriLicenseProductCodeArcView)

  If (licenseStatus = esriLicenseNotLicensed) Then

    licenseStatus = CheckOutLicense(esriLicenseProductCodeArcEditor)

      If (licenseStatus = esriLicenseNotLicensed) Then
        licenseStatus = CheckOutLicense(esriLicenseProductCodeArcInfo)
      End If
    End If
  End If

  'Take a look at the licenseStatus to see if it failed
  'Not licensed

  If (licenseStatus = esriLicenseNotLicensed) Then

    MsgBox "You are not licensed to run this product"

  'The licenses needed are currently in use

 ElseIf (licenseStatus = esriLicenseUnavailable) Then

    MsgBox "There are insufient licenses to run"

 'The licenses unexpected license failure

  ElseIf (licenseStatus = esriLicenseFailure) Then

    MsgBox "Unexpected license failure please contact you administrator'"

  'Already initialized (Initialization can only occur once)

  ElseIf (licenseStatus = esriLicenseAlreadyInitialized) Then

    MsgBox "You license has already been initialized please check you implementation"

  'Everything was checkedout successfully

  ElseIf (licenseStatus = esriLicenseCheckedOut) Then

    MsgBox "Licenses checked out successfully"

  End If

    Dim bAlreadyCheckedOut As Boolean

  bAlreadyCheckedOut = m_pAoInitialize.IsExtensionCheckedOut(esriLicenseExtensionCodeSpatialAnalyst)

  If (Not bAlreadyCheckedOut) Then

    licenseStatus = m_pAoInitialize.CheckOutExtension (esriLicenseExtensionCodeSpatialAnalyst)

    If (licenseStatus = esriLicenseUnavailable) Then

      MsgBox "All SpatialAnalyst licenses are currently in use"

    ElseIf (licenseStatus = esriLicenseCheckedOut) Then

      licenseStatus = esriLicenseCheckedOut

      MsgBox "SpatialAnalyst license has been checked out for use"

    Else
      MsgBox "Unexpected licensing failure contact you administor"
    End If
  End If

  End Sub

Private Function CheckOutLicense(productCode As esriLicenseProductCode) As esriLicenseStatus

  Dim licenseStatus As esriLicenseStatus
  Set m_pAoInitialize = New AoInitialize
  CheckOutLicense = esriLicenseUnavailable

  'Check the productCode

  licenseStatus = m_pAoInitialize.IsProductCodeAvailable(productCode)
  If (licenseStatus = esriLicenseAvailable) Then

    'Check the extensionCode

    licenseStatus = m_pAoInitialize.IsExtensionCodeAvailable(productCode, esriLicenseExtensionCodeSpatialAnalyst)
    If (licenseStatus = esriLicenseAvailable) Then
      'Initialize the license
      licenseStatus = m_pAoInitialize.Initialize(productCode)
    End If
  End If

  CheckOutLicense = licenseStatus

End Function

CODE 2


  Dim gp As Object

            gp = CreateObject("esriGeoprocessing.GPDispatch.1")

            If gp.CheckExtension("spatial") = "NotLicensed" Then

                MessageBox.Show("NotLicensed")

            ElseIf gp.CheckExtension("spatial") = "Unavailable" Then

                MessageBox.Show("Unavailable")

            ElseIf gp.CheckExtension("spatial") = "NotInitialized" Then

                MessageBox.Show("NotInitialized")

            ElseIf gp.CheckExtension("spatial") = "Available" Then

                gp.CheckOutExtension("spatial")

                MessageBox.Show("Checkout")

            End If

Best Answer

You have to distinguish between product licenses and extension licenses. ArcGIS Desktop as a whole is licensed at three product levels: ArcView, ArcEditor, and ArcInfo.

In addition to the product itself there are various extensions that can be licensed. Spatial Analyst is one such extension.

  • If your code is running from within ArcMap there will already be a product license in use of some kind, otherwise ArcMap would not be running. That is why you are getting the first message. Typically you will only run explicit code to check out a product license in an ArcEngine application.
  • The second piece of code you have is going through the geoprocessing framework to check out an SA license. Unless you're in a Python GP script, this is a poor way of doing things. In ArcObjects your first code segment is the way to handle licenses.

UPDATE

To programmatically enable an extension in a Desktop application follow the instructions in the How to use extensions help topic. VB.NET code copied directly from the help:

Dim factoryType As Type = Type.GetTypeFromProgID("esriSystem.ExtensionManager")
Dim extensionManager As IExtensionManager = CType(Activator.CreateInstance(factoryType), IExtensionManager)

Dim uid As IUID = New UIDClass()
uid.Value = "esriSpatialAnalystUI.SAExtension"
Dim extension As IExtension = extensionManager.FindExtension(uid)
Dim extensionConfig As IExtensionConfig = CType(extension, IExtensionConfig)

Dim wasEnabled As Boolean = (extensionConfig.State = esriExtensionState.esriESEnabled)

If Not wasEnabled Then
    If Not(extensionConfig.State = esriExtensionState.esriESUnavailable) Then
        extensionConfig.State = esriExtensionState.esriESEnabled
    Else 
        ' Handle the case when the license is not available.
        ' Provide an error message or exit to avoid running unavailable functionality.
    End If
End If