How to convert from Map (mxd) to KML files (C#)

2041
2
01-03-2012 10:39 AM
yangzishe
New Contributor
I need to add a function of conversion from Map (mxd) to KML to my project. It seems there is not much reference on the internet. I have gotten some sample codes here:  http://forums.esri.com/Thread.asp?c=159&f=1707&t=232374
I converted the codes to C#. I did not have any problems when I compiled it. But I received an error when I ran the application:

Source: ESRI.ArcGIS.Geoprocessing

StackTrace: at ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.Execute(String Name, IVariantArray ipValues, ITrackCancel pTrackCancel)
   at ESRI.ArcGIS.Geoprocessor.Geoprocessor.ExecuteInner(IGPProcess process, ITrackCancel trackCancel, IGeoProcessor igp, IVariantArray iva)
   at ESRI.ArcGIS.Geoprocessor.Geoprocessor.Execute(IGPProcess process, ITrackCancel trackCancel)

Message: Error HRESULT E_FAIL has been returned from a call to a COM component.

Please advise!
Reply
0 Kudos
2 Replies
AnthonyMikelson
New Contributor III
We too are encountering this same issue, however it is random.  I know this may be a loaded question, but can someone from esri give some possible scenarios of what could cause this error to be thrown?

Thank you,
Tony
Reply
0 Kudos
JohnNelson3
New Contributor III
I could never get the Append Tool with the NO_TEST option to work so I called a Python script.


Test Failed
     
          try
            {
                ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
                ESRI.ArcGIS.ConversionTools.MapToKML mk = new ESRI.ArcGIS.ConversionTools.MapToKML();
                mk.in_map_document = @"U:\kmltest.mxd";
                mk.data_frame = "Layers";
                mk.map_output_scale = 24000;
                mk.out_kmz_file = @"U:\junk5.kml";
                /*mk.is_composite = "NO_COMPOSITE";
                mk.is_vector_to_raster = "VECTOR_TO_VECTOR";
                mk.extent_to_export ="";
                mk.image_size = 2048;
                mk.dpi_of_client = 192;*/
                gp.Execute(mk, null);
            }
            catch
            {
                MessageBox.Show("Failed");
            }
            this.Close();

Python Fix


appendStatus = RunPythonScript.ExecuteCommand("cmd", "/c " + @"C:\python26\ArcGIS10.0\python.exe " + @"U:\loc\GIS\PROJECTS\CustomTools\PythonScripts\append.py" + " " + input + "," + output);




    class RunPythonScript
    {
        public static Boolean ExecuteCommand(string command, string arug)
        {
            Boolean appendComplete = true;
            try
            {
              
                string result;
                Process pProcess = new Process();
                pProcess.StartInfo.UseShellExecute = false;
                pProcess.StartInfo.Arguments = arug;
                pProcess.StartInfo.CreateNoWindow = true;
                pProcess.StartInfo.RedirectStandardOutput = true;
                pProcess.StartInfo.RedirectStandardError = true;
                pProcess.StartInfo.FileName= command;               
                pProcess.Start();
                result = pProcess.StandardOutput.ReadToEnd();
                if (result.Substring(0, 4) == "true")
                {
                    appendComplete = true;
                }
                else
                {
                    appendComplete = false;
                }
            }
            catch
            {
                appendComplete = false;
            }
           
            return appendComplete;
        }
    }
Reply
0 Kudos