Get transformation methods in Arcgis engine application using C#

3705
8
Jump to solution
10-05-2014 01:02 PM
sidd
by
New Contributor III

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

0 Kudos
1 Solution

Accepted Solutions
sidd
by
New Contributor III

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

            }

View solution in original post

0 Kudos
8 Replies
OwenEarley
Occasional Contributor III

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:

  • You can call any Python script (*.py) from .NET. This includes code that uses Python packages designed for analytical and mathematical purposes, such as R statistics, complex matrix algebra, SciPy, or any other script.
  • Similarly, you can use simplified ArcPy functions as well. The following are several modules that you can take advantage of:

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.

  • You can use the .NET geoprocessor to execute a script tool that is implemented with Python. This is the way GIS analysts use it.

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

sidd
by
New Contributor III

@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.

0 Kudos
nicogis
MVP Frequent Contributor

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

sidd
by
New Contributor III

@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.

0 Kudos
nicogis
MVP Frequent Contributor

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?

sidd
by
New Contributor III

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

            }

0 Kudos
nicogis
MVP Frequent Contributor

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.

0 Kudos
sidd
by
New Contributor III

Yeah right. The confusion occurs as probably you forget to provide the implementation of ExecuteTask function in your earlier reply.

0 Kudos