Select to view content in your preferred language

How to get IFeatureLayer from using Geoprocessor and SelectLayerByLocation

2185
2
06-26-2013 07:40 PM
MarkMindlin
Frequent Contributor
Hi there!
I would appreciate if anybody can tell please
if it possible (and how(please provide a code))
to get IFeatureLayer from using Geoprocessor and SelectLayerByLocation
///////////////////////
some pseudo code here

Geoprocessor gp = new Geoprocessor();
IFeatureClass pCurrentFeatureClass = // some code to OpenFeatureClass
MakeFeatureLayer makeFeatureLayer = new MakeFeatureLayer();
makeFeatureLayer.in_features = pCurrentFeatureClass;
makeFeatureLayer.out_layer = "CurrentFeatureLayer";
SelectLayerByLocation selectLayerByLocation = new SelectLayerByLocation();
selectLayerByLocation.in_layer = "CurrentFeatureLayer";
gp.Execute(selectLayerByLocation,null);
//////////////////////

HERE: HOW TO GET IFeatureLayer? // in order to get ISelectionSet in the "pCurrentFeatureClass" after SelectLayerByLocation
//////////////////////////////////////
Please do not provide solutions with GP.CopyFeatures, mainly because I need to save ObjectID from the pCurrentFeatureClass.
Please do not provide ArcObjects solutions with IQueryFilter.
0 Kudos
2 Replies
YairSegal
Deactivated User
Hello,
Try the following solution:

Geoprocessor GP = new Geoprocessor();

MakeFeatureLayer makeFeatureLayer = new MakeFeatureLayer();
makeFeatureLayer.in_features = @"C:\temp.gdb\in_feature_class";
makeFeatureLayer.out_layer = "out_feature_layer_name";

GP.Execute(makeFeatureLayer, null);
  
SelectLayerByLocation selectLayerByLocation = new SelectLayerByLocation();
selectLayerByLocation.in_layer = "out_feature_layer_name";

IGeoProcessorResult resultSelection = GP.Execute(selectLayerByLocation, null) as IGeoProcessorResult;
IGPUtilities gpUtils = new GPUtilitiesClass();
IFeatureLayer myFeatureLayer = gpUtils.DecodeLayer(resultSelection.GetOutput(0)) as IFeatureLayer;
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's how I return a feature class from a geoprocessing process. This example uses the geoprocessing tool "CreateFeatureClass". You can also return a feature layer from the function ReturnObjectfromResult.

    Friend Function CreateFeatureClass(ByVal FCLocation As String, ByVal FCName As String, ByVal pSR As ESRI.ArcGIS.Geometry.ISpatialReference3, ByVal GeometryType As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim CreateFClass As New ESRI.ArcGIS.DataManagementTools.CreateFeatureclass
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(CreateFClass)

                CreateFClass.out_path = FCLocation
                CreateFClass.out_name = FCName
                CreateFClass.spatial_reference = pSR
                CreateFClass.geometry_type = GeometryType

                Result = RunTool(CreateFClass, Nothing, True)
                If Result Is Nothing Then Return Nothing

                Return ReturnObjectfromResult(Result, "Feature Class")

            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Create Feature Class")
            Return Nothing
        End Try

    End Function

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
            Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

            GP.AddOutputsToMap = AddOutput
            GP.OverwriteOutput = True

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

        Return Result

    End Function

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

        If pResult Is Nothing Then Exit Sub

        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)
                Case "Feature Layer"
                    Return GPUtil.OpenFeatureLayerFromString(InMemFC)
                Case Else
                    Return Nothing
            End Select

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

    End Function
0 Kudos