Thursday, February 23, 2012

Select Distinct for ArcGIS 10


Writing tools in VB.net is very easy to do with the ArcGIS 10 Snap-In framework.  This is made even better by the fact that you can code with Visual Studio 2008 Express Edition, which is free, instead of paying for Visual Studio Professional as with ArcGIS 931.

The Select Distinct Tool that I will demonstrate is used to select unique values in your attributes.  Very useful for identifying duplicated attributes (such as IDS), or getting unique values based on attribute sorting (for example, give me the records having the longest segment length for each unique street name).  You can do this with SQL queries as well, but this tool is usually easier to do when your data is in file geodatabase or shapefile format.  If the data is in SQL server I would generally not recommend this tool. 

Once you have installed VB express, and then the DotNet SDK for ArcGIS (and the service packs), creating a command button toolbar is very straightforward:
1
    Create a new project:




























Choose Button as your Add-in type:








  










   Add the ArcGIS ArcObjects SDK references you will need for your logic:


  Modify the Config.esriaddinx file to contain a toolbar containing your button.  

   Auto Complete makes the toolbar configuration very simple!  You will have to add the bolded Toolbars section below manually, but the Commands section is created automatically using the wizard. 

<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>MyNewAddIn</Name>
  <AddInID>{2e9f459d-a591-4016-95b2-27ddc30ad802}</AddInID>
  <Description>Type in a description for this Add-in.</Description>
  <Version>1.0</Version>
  <Image>Images\MyNewAddIn.png</Image>
  <Author>roy.jackson</Author>
  <Company>GISPROBLOG</Company>
  <Date>2/23/2012</Date>
  <Targets>
    <Target name="Desktop" version="10.0" />
  </Targets>
  <AddIn language="CLR" library="MyNewAddIn.dll" namespace="MyNewAddIn">
    <ArcMap>
      <Commands>
<Button id="GISPROBLOG_MyNewAddIn_SelectDistinct" class="SelectDistinct" message="Add-in command generated by Visual Studio project wizard." caption="Select Distinct" tip="Add-in command tooltip." category="Add-In Controls" image="Images\SelectDistinct.png" />
      </Commands>
      <Toolbars>
<Toolbar id="GISPROBLOG_SELECTDISTINCT" caption="GISPROBLOG_TOOLS" showInitially="true">
          <Items>
            <Button refID="GISPROBLOG_MyNewAddIn_SelectDistinct" />
          </Items>
        </Toolbar>
      </Toolbars>
    </ArcMap>
  </AddIn>
</ESRI.Configuration>



      Configure the button class to launch the main form code.  

    Note the new My.ArcMap object!

Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.ADF
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS

Public Class SelectDistinct
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button
    Dim m_pMxDoc As IMxDocument
    Dim m_pApp As IMxApplication
    Public myForm As New frmSelectDistinct

    Public Sub New()
        m_pApp = My.ArcMap.Application
        m_pMxDoc = My.ArcMap.Application.Document
    End Sub

    Protected Overrides Sub OnClick()
        myForm = New frmSelectDistinct
        myForm.m_app = My.ArcMap.Application
        myForm.Show()
        myForm.TopMost = True
    End Sub

    Protected Overrides Sub OnUpdate()

    End Sub
End Class


       Implement the frmSelectDistinct.vb code (see the download link at the end of the article)

     Run the Project!  

     Your toolbar will be available from the customize menu:










Another great element of the snap-in framework is that it is very easy to distribute the actual file that makes the code available to the end user.   Simply provide the user the with the .addin file, and once they double-click it the code is installed!  No more windows installer requirements.  To remove the addin, you can look in ArcCatalog’s addin folder in the home geodatabase and delete from there by right clicking and choosing delete:
















I have seen some issues using ESRI SDK objects in your code when the end user doesn’t have the development environment installed, but these can be handled with various configuration settings. 

Also, custom icons were a pain to figure out, but just change the "Build Action" property of the icon to "AddInContent", and "Copy to Output Directory" to "Copy Option:.  

The source code, and .addin file for this project (look in the bin/debug folder), can be downloaded here:

The installer can be downloaded here:

http://dl.dropbox.com/u/63807183/SelectDistinct/SelectDistinctAddIn.esriAddIn

I hope this was helpful!



No comments: