C# CreateFeaturesFromTextFile

477
4
08-05-2010 08:29 AM
JimHenry
New Contributor
I am new to the ESRI SDK and I am looking to automate my ArcView GUI based task.  I am not sure if I am even on the right track?

using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.SampleTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;

namespace EsriTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CreateFeaturesFromTextFile a = new CreateFeaturesFromTextFile();
            a.Input_Text_File = @"C:\in.txt";
//in combile stops here with error  80040111         
            Geoprocessor g = new Geoprocessor ();
            g.AddToolbox(a.Alias);
//I knew the below was a problem but I am stuck
            //g.Execute(new IGPProcess(), new ITrackCancel());
        }

    }
}

-------
in.txt
-------
CreateFeaturesFromTextFile 'C:\dataIn.txt' . 'C:\dataOut.shp' 'C:\Program Files\ArcGIS\Coordinate Systems\Geographic Coordinate Systems\World\wgs 1984.prj'

-------
dataIn.txt
-------
polyline
0 0
0 -99.3176813565276 40.0798595384306 0.0 0.0
0 -96.3237815000927 38.0960181079531 0.0 0.0
1 0
0 -99.1184311878643 35.411993948899 0.0 0.0
0 -96.3237815000927 38.0960181079531 0.0 0.0
2 0
0 -93.1532478820156 35.2528371349043 0.0 0.0
0 -96.3237815000927 38.0960181079531 0.0 0.0
3 0
0 -93.0996253349636 40.0025957282976 0.0 0.0
0 -96.3237815000927 38.0960181079531 0.0 0.0
END
0 Kudos
4 Replies
JimHenry
New Contributor
Found this link for my first error:
http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2009/12/01/Migrating-to-Engine-9.4-and-using-t...

Summary:
At 10, ESRI has introduced the new RuntimeManager Class.  This class can be used to interrogate which products are on the machine and then bind to a product.

reference ESRI.ArcGIS.Version
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);
0 Kudos
JimHenry
New Contributor
For anyone the cares here is the working solution:

using System;
using System.Collections;
using System.Text;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SampleTools;

namespace EsriTest
{
    class EsriTest
    {
        static void Main(string[] args)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);

            Geoprocessor GP = new Geoprocessor();
            CreateFeaturesFromTextFile a = new CreateFeaturesFromTextFile();
            a.Input_Text_File = @"C:\in.txt";
            a.Output_Feature_Class = @"dataOut.shp";
            a.Output_Feature_Class_Spatial_Reference = @"C:\Program Files\ArcGIS\Coordinate Systems\Geographic Coordinate Systems\World\wgs 1984.prj";
            a.Input_Decimal_Separator = ".";
            RunTool(GP, a, null);
        }

        private static void RunTool(Geoprocessor geoprocessor, IGPProcess process, ITrackCancel TC)
        {
   
            // Set the overwrite output option to true
            geoprocessor.OverwriteOutput = true;

            // Execute the tool           
            try
            {
                geoprocessor.Execute(process, null);
                ReturnMessages(geoprocessor);

            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                ReturnMessages(geoprocessor);
            }
        }

        // Function for returning the tool messages.
        private static void ReturnMessages(Geoprocessor gp)
        {
            if (gp.MessageCount > 0)
            {
                for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
                {
                    Console.WriteLine(gp.GetMessage(Count));
                }
            }

        }
    }
}
0 Kudos
AkashJain
New Contributor
I cannot locate/add ESRI.ArcGIS.Version assembly.

I am using ArcGIS Desktop 10 with VS 2008 on Windows 7.

Please help.
0 Kudos
LukeBadgerow
New Contributor
I'm just struggling to find the Execute Method that takes IGPProcess as an input.

I'm stuck with the one that takes a string input, and when I use that, my application just kind of fades away.  the code I'm working with currently:

    class ExportGeoprocessing
    {
        GeoProcessor gp = new GeoProcessor();
        
        IGPUtilities gputils = new GPUtilitiesClass();
        public void ExecuteGeoprocessingTask(String infeature, String outfeature,
            IWorkspace outworkspace, IFeatureWorkspace inputworkspace, IFields fields, Hashtable fieldpairings)
        {
            
            String extension = outfeature.Split('.')[1];
            IVariantArray parameters = null;
            object severity = 2;
            switch (extension)
            {
                case("dbf"):
                    parameters = BuildParameterObjectForTable(infeature, outfeature, outworkspace, inputworkspace, fields, fieldpairings);
                    break;
                case("shp"):
                    parameters = BuildParameterObjectForShapefile(infeature, outfeature, outworkspace, inputworkspace, fields, fieldpairings);
                    try
                    {
                        gp.Execute("FeatureclassToFeatureclass_conversion", parameters, null);
                    }
                    catch
                    {
                        Console.WriteLine(gp.GetMessages(ref severity).ToString());
                    }
                    break;
            }
        }

        private IVariantArray BuildParameterObjectForShapefile(String infeature, String outfeature,
            IWorkspace outworkspace, IFeatureWorkspace inputworkspace,IFields fields, Hashtable fieldpairings)
        {
            IVariantArray parameters = new VarArrayClass();
            IName fcname = GetFeatureName(infeature, outfeature, (IWorkspace)inputworkspace);
            IFeatureClass infc = inputworkspace.OpenFeatureClass(infeature);

            IGPFieldMapping fieldmapping = BuildOutputFieldMapping(fcname, fields, fieldpairings);

            parameters.Add(infc);
            parameters.Add(outworkspace); //make a string object
            parameters.Add(outfeature);
            parameters.Add(" ");
            parameters.Add(fieldmapping);

            return parameters;  
        }

        private IVariantArray BuildParameterObjectForTable(String infeature, String outfeature,
            IWorkspace outworkspace, IFeatureWorkspace inputworkspace, IFields fields, Hashtable fieldpairings)
        {
            IVariantArray parameters = new VarArrayClass();
            IName fcname = GetFeatureName(infeature, outfeature, (IWorkspace)inputworkspace);
            ITable intable = inputworkspace.OpenTable(infeature);
            return parameters; 
        }
        private IName GetFeatureName(String infeature, String outfeature, IWorkspace workspace)
        {
            IName infeaturename = null;
            String extension = outfeature.Split('.')[1];
            String infcname = infeature.Split('.')[1];
            switch (extension)
            {
                case ("dbf"):
                    IEnumDatasetName tablenames = workspace.get_DatasetNames(esriDatasetType.esriDTTable);
                    infeaturename = AssignNameVariable(infcname, tablenames);
                    break;
                case ("shp"):
                    IEnumDatasetName dsnames = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
                    if (infeature.Contains("\\"))
                    {
                        IDatasetName ds = dsnames.Next();
                        while (ds != null)
                        {
                            IEnumDatasetName fcs = ds.SubsetNames;
                            infeaturename = AssignNameVariable(infcname, fcs);
                            ds = dsnames.Next();
                        }
                    }
                    else
                    {
                        infeaturename = AssignNameVariable(infcname, dsnames);
                    }
                    break;
            }
            return infeaturename;
        }
        private IName AssignNameVariable(String infeaturename, IEnumDatasetName dsnames)
        {
            IName infcname = null;
            IDatasetName fc = dsnames.Next();
            while (fc != null)
            {
                if (fc.Name.ToUpper().Contains(infeaturename.ToUpper()))
                {
                    if (infcname == null)
                    {
                        infcname = (IName)fc;
                    }
                    break;
                }
                fc = dsnames.Next();
            }

            return infcname;
        }

        private IGPFieldMapping BuildOutputFieldMapping(IName fcname, IFields fields, Hashtable fieldpairings)
        {
            IGPFieldMapping fieldmapping = new GPFieldMappingClass();
            IDETable intable = (IDETable)gputils.MakeDataElementFromNameObject(fcname);
            IArray intables = new ArrayClass();
            intables.Add(intable);
            fieldmapping.Initialize(intables, null);
            IFields infields = intable.Fields;

            foreach (String key in fieldpairings.Keys)
            {
                int fieldindex = fieldmapping.FindFieldMap(fieldpairings[key].ToString());
                IGPFieldMap tempfieldmap = fieldmapping.GetFieldMap(fieldindex);

                int tempmapindex = tempfieldmap.FindInputField(intable, fieldpairings[key].ToString());
                IField inputfield = tempfieldmap.GetField(tempmapindex);

                int inindex = infields.FindField(fieldpairings[key].ToString());
                IField fieldin = infields.get_Field(inindex);

                IGPFieldMap fieldmap = new GPFieldMapClass();
                fieldmap.OutputField = fields.get_Field(fields.FindField(key.ToString()));
                fieldmap.AddInputField(intable, fieldin, 0, fieldin.Length);


                fieldmapping.AddFieldMap(fieldmap);
                fieldmap.RemoveAll();
            }
            return fieldmapping;
        } 
    }
0 Kudos