I need to call arcpy.ListTransformations function in C#. I have to pass 2 spatial References (ISpatialReference objects, I guess) and an envelope(IEnvelope object) to this function. This function returns the transformations list. I need to get these strings in C# code. Can anybody tell me how to accomplish this in ArcGIS Engine application.
Thanks
Solved! Go to Solution.
@Domenico. Thanks again for your reply. However if you note that the Execute function is not setting the output parameter, so we are getting nothing. I have found the solution for this problem. The following code works.
Thank you for your help.
IGeoProcessorResult2 geoProcessorResult = null;
IGPValue output = null;
try
{
geoProcessorResult = geoprocessor.Execute("Transformations", parameters, null) as IGeoProcessorResult2;//Transformations is name of script
output = geoProcessorResult.GetOutput(0) as IGPValue;
IGPMultiValue GPMultiValue = output as IGPMultiValue;
for (int i = 0; i < GPMultiValue.Count; i++)
{
list.Add((GPMultiValue.get_Value(i).GetAsText()));
}
}
catch (Exception)
{
returnMessages(geoprocessor);
}
Check out the answer to this post for the arcpy code:
You use arcpy functions in C#:
Leveraging ArcPy in .NET
The following are the ways you can leverage ArcPy:
Mapping module—For more information see, Geoprocessing scripts for map document management and output in the ArcGIS Desktop User Help system.
Geostatistical Analyst module—For more information, see A quick tour of the Geostatistical Analyst ArcPy classes in the ArcGIS Desktop User Help system.
Spatial Analyst module—For more information, see An overview of Spatial Analyst classes in the ArcGIS Desktop User Help system.
There are many classes in an ArcPy package that you can integrate in your code.
I would probably go with the last option of using the geoprocessor object.
Also, there are ways to access transformations directly in ArcObjects using C# - see the ISpatialReferenceFactory2 Interface
@Owen
Thanks for the reply, however I am getting HRESULT E_FAIL while I try to call IGeoprocessor.Execute(....) method. Please help in this regard.
Secondly I could not find any other way to find the transformations methods between that are best suited for a given extent in Arcobjects. this script however runs perfect when I debug using ArcMap. If you know any way please share.
I have only problems in running this from C#.
Thanks.
The simplest method is write a python script (arcpy) and expose it how toolbox in arcobjects. Then you use arcobjects and call this tool.
In arcpy you have ListTransformations (http://resources.arcgis.com/en/help/main/10.1/index.html#/ListTransformations/018v0000001p000000/)
toolbox tool exposed with a toolbox:
import arcpy try: layer = arcpy.GetParameter(0) layerDescribe = arcpy.Describe(layer) from_sr = layerDescribe.featureClass.spatialReference to_sr = arcpy.GetParameter(1) extent = layerDescribe.extent transformations = arcpy.ListTransformations(from_sr, to_sr, extent) arcpy.SetParameter(2, transformations) except StandardError, ErrDesc: arcpy.AddMessage("Error: " + str(ErrDesc)) except: arcpy.AddMessage("Error: get list transformations")
In arcobjects:
IVariantArray parameters = new VarArrayClass(); parameters.Add(this.pathLayer); //path and file lyr example c:\temp\test.lyr parameters.Add(this.spatialReferenceOut); IGPValue output = null; Geoprocessor geoprocessor = new Geoprocessor(); geoprocessor.AddToolbox(@"c:\Temp\myToolbox.tbx"); ExecuteTask(this.geoprocessor, parameters, "ListTransformations", 0 ,out output); //ListTransformations is the name of your toolbox tool IGPMultiValue GPMultiValue = output as IGPMultiValue; for (int i = 0; i < GPMultiValue.Count;i++ ) { cboDatumTransformation.Items.Add((GPMultiValue.get_Value(i) as IGPString).Value); }
So you have only datum transformations available for extent of input
@Domenica, Thanks for your reply. I have alrady tried this method. I have 2 problems in this.
1. Please provide the implementation of Execute method.I tried to implement Execute but I got HRESULT E_FAIL while I call geoprocessor.Execute(....).
2. I have situation where I don't have feature layer. I have already inSR, outSR and extent. So i am going to pass 2 ISpatialReference objects and extent. I have written python script with these required parameters, tool and I am getting values when running from Arcmap. However I am getting HRESULT E_FAIL as I describe above. Please help in this regard.
In my method ExecuteTask I have also manage error but the method that I call is:
IGeoProcessorResult2 geoProcessorResult2 = gp.Execute(nameTask, parameters, null) as IGeoProcessorResult2;
Have you set parameters in tool with type correct?
@Domenico. Thanks again for your reply. However if you note that the Execute function is not setting the output parameter, so we are getting nothing. I have found the solution for this problem. The following code works.
Thank you for your help.
IGeoProcessorResult2 geoProcessorResult = null;
IGPValue output = null;
try
{
geoProcessorResult = geoprocessor.Execute("Transformations", parameters, null) as IGeoProcessorResult2;//Transformations is name of script
output = geoProcessorResult.GetOutput(0) as IGPValue;
IGPMultiValue GPMultiValue = output as IGPMultiValue;
for (int i = 0; i < GPMultiValue.Count; i++)
{
list.Add((GPMultiValue.get_Value(i).GetAsText()));
}
}
catch (Exception)
{
returnMessages(geoprocessor);
}
Yes, in my code I have output = geoProcessorResult.GetOutput(0) as IGPValue; and return output from method using out because my function ExecuteTask return bool (true=success).
However important that you have resolved your question.
Yeah right. The confusion occurs as probably you forget to provide the implementation of ExecuteTask function in your earlier reply.