Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Help! Having the hardest time getting the correct syntax when adding layers as gp parameters to call the Merge_management GeoProcessing Task. Below is just one of MANY, MANY ways I have tried with no luck. Can anyone help?

I've successfully called many GeoProcessing tasks via code in ArcEngine, but this is the first one to take an array ( ie. [layer1,layer2,etc]) as one of the parameters.

Intial Attemp:

    public void RunMergeGeoProcess(ILayer SourceLayer1, ILayer SourceLayer2, string ResultFeatureClassName, string GeoDBAbsFileName)
    {

        IFeatureLayer featureLayerSource1 = (IFeatureLayer)SourceLayer1;
        IFeatureLayer featureLayerSource2 = (IFeatureLayer)SourceLayer2;


        _geoProcessor.SetEnvironmentValue("Workspace", GeoDBAbsFileName);
        //_gpParams.Add(featureLayerSource1);
        ////_gpParams.Add(featureLayerSource2);

        IWorkspace refWS = ((IDataset)featureLayerSource1.FeatureClass).Workspace;
        string dbPath = refWS.PathName;

        IDataset myDS1 = featureLayerSource1.FeatureClass as IDataset;
        string mysDS1 = dbPath + "\\" + myDS1.Name;
        IDataset myDS2 = featureLayerSource2.FeatureClass as IDataset;
        string mysDS2 = dbPath + "\\"+ myDS2.Name;

        string myResult = dbPath + "\\" + ResultFeatureClassName;

        string param1 = "[" + mysDS1 + ", " + mysDS2 + "]";

        _gpParams.Add(param1);
        _gpParams.Add(myResult);


        try
        {
            _geoProcessor.OverwriteOutput = true;
            _geoProcessor.Execute("Merge_management", _gpParams, null);
            //returnMessages(_geoProcessor);
        }
        catch (Exception ex)
        {
            MessageBox.Show("There was a GeoProcessing Error." + ex.ToString());
            returnMessages(_geoProcessor);
        }

        _gpParams.RemoveAll();

    }

Working Version:

    public void RunMergeGeoProcess(ILayer SourceLayer1, ILayer SourceLayer2, string ResultFeatureClassName, string GeoDBAbsFileName)
    {

        IFeatureLayer featureLayerSource1 = (IFeatureLayer)SourceLayer1;
        IFeatureLayer featureLayerSource2 = (IFeatureLayer)SourceLayer2;

        _geoProcessor.SetEnvironmentValue("Workspace", GeoDBAbsFileName);

        IWorkspace refWS = ((IDataset)featureLayerSource1.FeatureClass).Workspace;
        string dbPath = refWS.PathName;

        IDataset myDS1 = featureLayerSource1.FeatureClass as IDataset;
        string mysDS1 = dbPath + "\\" + myDS1.Name;
        IDataset myDS2 = featureLayerSource2.FeatureClass as IDataset;
        string mysDS2 = dbPath + "\\" + myDS2.Name;

        string myResult = dbPath + "\\" + ResultFeatureClassName;
        string param1 =  mysDS2 + ";" + mysDS1  ;
        _gpParams.Add(param1);
        _gpParams.Add(myResult);

        try
        {
            _geoProcessor.OverwriteOutput = true;
            _geoProcessor.Execute("Merge_management", _gpParams, null);
            //returnMessages(_geoProcessor);
        }
        catch (Exception ex)
        {
            MessageBox.Show("There was a GeoProcessing Error during the Merge Operation." + ex.ToString());
            //returnMessages(_geoProcessor);
        }

        _gpParams.RemoveAll();

    }
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You should be able to use a semicolon-delimited list for the input parameter. An example using the Intersect tool is below:

    private void Intersect(List<string> LayerNames, string OutputLayerLocation)
    {
        Intersect inter;
        StringBuilder sb;
        Geoprocessor gg;

        sb = new StringBuilder();

        foreach (string LayerName in LayerNames)
        {
            sb.Append(LayerName);
            sb.Append(";");
        }

        sb.Length--;

        inter = new Intersect();
        inter.in_features = sb.ToString();
        inter.out_feature_class = OutputLayerLocation;

        gg = new Geoprocessor();
        gg.OverwriteOutput = true;
        gg.ProgressChanged += new EventHandler<ESRI.ArcGIS.Geoprocessor.ProgressChangedEventArgs>(gg_ProgressChanged);
        gg.ToolExecuted += new EventHandler<ToolExecutedEventArgs>(gg_ToolExecuted);
        gg.MessagesCreated += new EventHandler<MessagesCreatedEventArgs>(gg_MessagesCreated);

        gg.ExecuteAsync(inter);
    }
share|improve this answer
    
To put it simply, the tool does not want an array, but a list of paths split on semi-colons: "C:\\PathToShape1;C:\\PathToShape2;C:\\PathToShape3" –  Nathanus Dec 14 '11 at 16:07
    
@Nathanus That's the odd part; all the documentation I've seen mentions an "array", so I struggled with this myself. (And thanks for making my message a bit clearer.) –  Michael Todd Dec 14 '11 at 16:12
    
Thanks a TON!!!!! I could not find that syntax ANYWHERE. Now it is working, I just need to work on the field mappings (when they are unequal). Thanks SO much!!! I'll post my version of final code, when done –  user3013 Dec 14 '11 at 17:03
    
I came from Arcpy scripting, so I simply assumed it wanted the same kind of input it used in Python. Blind luck that I was right. How I came to the conclusion that I should have used a semicolon string instead of the list it shows in the documentation I do not remember... :) –  Nathanus Dec 14 '11 at 17:04
    
@user3013 Don't forget to mark Michael's answer as correct (there should be a check mark under the vote count on the side). Glad to be of help. –  Nathanus Dec 14 '11 at 17:05
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.