Select to view content in your preferred language

Using the Intersect Geoprocessor with IFeatureLayers in C#

2051
3
04-12-2010 09:12 AM
WadeDrane
Emerging Contributor
I am trying to get the following code to work but am having no luck. I am developing in C# using the geoprocessor. I want to intersect two featurelayers within the ArcMap TOC but I can not find an example to do this. The only example is code that uses pathnames to geodatabases for the multiple inputs and that doesnt show me how to use the featurelayers in the TOC of ArcMap for the inputs. This is the code I have but it seems that the intersect doesnt like the featurelayer names I am using for the input. Any help is appreciated. Thanks

The Code:

using ESRI.ArcGIS.Geoprocessor;

// LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName is a function to get the featurelayer
IFeatureLayer pParcelFLayer = LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName(farmassessment.m_mxDocument.ActiveView, "Parcel Lines");
IFeatureLayer pSoilsFLayer = LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName(farmassessment.m_mxDocument.ActiveView, "Soils");
// String for the output path
String stroutput = "J:\\pva\\Appdata\\Farm\\fa" + strParcelID;
//Setting up the Geoprocessor
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
Intersect intersectLayers = new Intersect();
intersectLayers.in_features = "'" + pParcelFLayer.Name + "' '';'" + pSoilsFLayer.Name + "' ''";
intersectLayers.out_feature_class = stroutput;
intersectLayers.join_attributes = "ALL";
intersectLayers.output_type = "INPUT";
GP.Execute(intersectLayers, null);
0 Kudos
3 Replies
DevdattaTengshe
Deactivated User
I think it might not be getting the correct inputs. As far as I am aware, not all tools cannot use ArcObjects as Inputs.

To confirm, please get the messages from the Geoprocessor. You can use the following code:

if (GP.MessageCount > 0)
    {

        for (int Count = 0; Count <= GP.MessageCount - 1; Count++)
        {

            Console.WriteLine(GP.GetMessage(Count));

        }

    }
0 Kudos
WadeDrane
Emerging Contributor
Here is the code I used. I hope this saves someone Time:

// LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName is a function to get the featurelayer
IFeatureLayer pParcelFLayer = LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName(fa rmassessment.m_mxDocument.ActiveView, "Parcel Lines");
IFeatureLayer pSoilsFLayer = LOJIC.ArcGIS.Carto.Layers.GetFeatureLayerByName(fa rmassessment.m_mxDocument.ActiveView, "Soils");
// String for the output path
String stroutput = "J:\\pva\\Appdata\\Farm\\fa" + strParcelID;
//Setting up the Geoprocessor
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
ESRI.ArcGIS.AnalysisTools.Intersect intersectLayers = new Intersect();
//using the IGpValueTableObject to add multiple featurelayer from the TOC to the Input of the Intersect Geopros.
IGpValueTableObject valTbl = new GpValueTableObjectClass();
valTbl.SetColumns(2);
object row = "";
object rank = 1;

row = pParcelFLayer;
valTbl.SetRow(0, ref row);
valTbl.SetValue(0, 1, ref rank);

row = pSoilsFLayer;
valTbl.SetRow(1, ref row);
rank = 2;
valTbl.SetValue(1, 1, ref rank);

intersectLayers.in_features = valTbl;
intersectLayers.out_feature_class = stroutput;
intersectLayers.join_attributes = "ALL";
intersectLayers.output_type = "INPUT";
           
GP.Execute(intersectLayers, null);
0 Kudos
MikeTischler
Emerging Contributor
Mine's in VB, but it might give you some help.  I had to create a string variable that held a semicolon delimited list of paths and give that to the geoprocessing tool.  Took me a while to figure out how to format it correctly...looks like you're having the same issue.

        Dim gps As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor
        Dim intersectme As ESRI.ArcGIS.AnalysisTools.Intersect
        Dim errmsg As String
        

        intersectme = New ESRI.ArcGIS.AnalysisTools.Intersect
        gps.SetEnvironmentValue("Extent", "MAXOF")
        
        Dim fcnames As String
        Dim pDataset As IDataset

        Dim pWorkspace As IWorkspace


   'infcs() is an array of feature classes that I want to intersect
        'the geoprocessing tool expects a semicolon delimited list of paths
        For i = 0 To UBound(infcs) - 2
            pDataset = infcs(i)
            pWorkspace = pDataset.Workspace
            fcnames = fcnames & "'" & pWorkspace.PathName & "\" & pDataset.Name & ".shp'" & ";"


        Next
        pDataset = infcs(UBound(infcs) - 1)
        pWorkspace = pDataset.Workspace
        fcnames = fcnames & "'" & pWorkspace.PathName & "\" & pDataset.Name & ".shp'"

        
        intersectme.in_features = fcnames
        intersectme.out_feature_class = outfolder & "\" & prefix & "_" & "output"
        gps.Execute(intersectme, Nothing)
        errmsg = ""
        If gps.MessageCount > 0 Then

            For j = 0 To gps.MessageCount - 1

                errmsg = errmsg & gps.GetMessage(j) & vbCrLf

            Next
            
        End If
0 Kudos