.NET ArcObjects Intersect

4081
6
02-02-2011 03:40 PM
CaleBerkey
New Contributor
All,

I'm automating a process for some workmates involving 3 geoprocessing commands.  I'm using ArcObjects C#.  I'm having some difficulties getting the Intersect object to like what I'm passing it for in_features.  Documentation is scarce and I can't find any examples.  The input is of type object, which is horribly unhelpful, since if you pass it the apparently wrong type of object the thing just fails without any explanation. 

If someone has a .NET example of using ESRI.ArcGIS.AnalysisTools.Intersect please post your code.  This repeated poke and hope approach is draining my time and patience.

Many thanks.
0 Kudos
6 Replies
Venkata_RaoTammineni
Occasional Contributor
All,

I'm automating a process for some workmates involving 3 geoprocessing commands.  I'm using ArcObjects C#.  I'm having some difficulties getting the Intersect object to like what I'm passing it for in_features.  Documentation is scarce and I can't find any examples.  The input is of type object, which is horribly unhelpful, since if you pass it the apparently wrong type of object the thing just fails without any explanation. 

If someone has a .NET example of using ESRI.ArcGIS.AnalysisTools.Intersect please post your code.  This repeated poke and hope approach is draining my time and patience.

Many thanks.


use ITopologicalOperator
0 Kudos
RuchiraWelikala
Occasional Contributor
The information in the following link should be helpful:

http://edndoc.esri.com/arcobjects/9.2/NET/c4ff8b68-0410-435f-b8e5-682d5cea47cf.htm
0 Kudos
KenBuja
MVP Esteemed Contributor
This is written in VB.NET and is for another tool, but you should be able to use its logic.

    Public Function Clip(ByVal pInFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal pClipFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal OutputName As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim Clipper As New ESRI.ArcGIS.AnalysisTools.Clip
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(Clipper)
                Clipper.in_features = pInFClass
                Clipper.clip_features = pClipFClass
                Clipper.out_feature_class = OutputName

                Result = RunTool(Clipper, Nothing)
                If Result Is Nothing Then
                    System.Windows.Forms.MessageBox.Show("Could not clip dataset")
                    Return Nothing
                End If

                Return ReturnObjectfromResult(Result, "Feature Class")
            End Using

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Clip error")
            Return Nothing
        End Try

    End Function

    Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String)

        Dim ErrorMessage As String

        If pResult.MessageCount > 0 Then
            For Count As Integer = 0 To pResult.MessageCount - 1
                ErrorMessage += pResult.GetMessage(Count)
            Next
        End If

        System.Windows.Forms.MessageBox.Show(ErrorMessage, Title)

    End Sub

    Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object

        Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
        Dim InMemFC As String
        Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities

        Try
            GPVal = result.GetOutput(0)
            InMemFC = GPVal.GetAsText()

            Select Case Type
                Case "Feature Class"
                    Return GPUtil.OpenFeatureClassFromString(InMemFC)
                Case "Table"
                    Return GPUtil.OpenTableFromString(InMemFC)
            End Select

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")
            Return Nothing
        End Try

    End Function

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
'I am using a global variable for the Geoprocessor
            Result = CType(Globals.GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
            Globals.GP.ClearMessages()
        Catch ex As Exception
            ReturnMessages(Result, "Fail")
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
        End Try

        Return Result

    End Function
0 Kudos
CaleBerkey
New Contributor
Thanks all. 

The problem I'm having compared to the samples and documentation you suggest is that I want to intersect two feature classes, not just one.  The examples demonstrate how to perform a given geoprocessing task against only one feature class, in which case the pathname or the feat class interface is passed into the routine.  I can't find any examples of how to pass in multiple feature classes for the intersect, and my various attempts are failing.  I've tried variations on semicolon-delimited pathnames, pathnames wrapped in single quotes, etc., and the routine simply fails when executing.

I'd really love some example code of someone passing multiple feature classes into a geoprocessing routine because I'm obviously not doing what the object wants me to do, and unfortunately, an input type of 'object' is of absolutely no help in determining what to pass in.
0 Kudos
KenBuja
MVP Esteemed Contributor
What I often do when I write a geoprocessing routine is to first run the tool in ArcMap and examine the results message. This shows the syntax of the parameters that expected by the tool.

When you run the Intersect tool, it's expecting several different input feature layers (test1.shp and test2.shp, for example) and their rankings (nothing, in this case), and an output (C:\ArcGIS\Default.gdb\test_Intersect). Here's what the Executing message reports for a test run:

"test1 #; test2 #" C:\ArcGIS\Default.gdb\test_Intersect All # INPUT

So for the code, you would set in_features to "test1 #; test2 #". All you have to do is supply the names of the dataset and a number for the ranking (or a # if ranking doesn't matter), separated by a semicolon.

dim Inter as New ESRI.ArcGIS.AnalysisTools.Intersect

Inter.in_features =  "test1 #; test2 #"
Inter.out_feature_class = "C:\ArcGIS\Default.gdb\test_Intersect"
0 Kudos
CaleBerkey
New Contributor
Thanks Ken, much appreciated.

While I haven't figured out exactly why my program was failing, I discovered what was causing it to fail.  For whatever reason, ArcObjects wouldn't recognize the intersect output filesystem location.  It's a network drive and just couldn't get there.  When repointing the program to a local geodatabase, the intersect worked as advertised, and in that case I used a simple semicolon delimited path string.

Unfortunately, since the routine was not reporting a useful error, it took many attempts of trial and error, altering little bits as I went, to discover this.  Why the dissolve and buffer could be output to the network but not the intersects is beyond me, but at least I have a suitable workaround for my coworkers for now.
0 Kudos