Saturday, 9 June 2018

arcobjects - Using Merge GeoProcess task in ArcGIS Engine with C#?


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();

}

Answer




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 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(gg_ProgressChanged);
gg.ToolExecuted += new EventHandler(gg_ToolExecuted);
gg.MessagesCreated += new EventHandler(gg_MessagesCreated);

gg.ExecuteAsync(inter);
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...