Select to view content in your preferred language

Help loading csv table

889
4
Jump to solution
09-28-2023 05:23 PM
Kevin_Andras
Occasional Contributor

Hi, 

I am in the early stages of converting a very large ArcGIS Desktop Add-In  to the Pro SDK.   My  add-in requires loading of .csv files that are generated by external executables.  These tables have values that ultimately get appended to feature class tables.  I can't figure out how to load .csv files.  Any help would be greatly appreciated. 

Thank you!

0 Kudos
1 Solution

Accepted Solutions
Kevin_Andras
Occasional Contributor
public async Task<Table> CSVtoTable()   //Task<Table>
        {
            Table tbl = null;
            try
            {
                var container = MapView.Active.Map;
                await QueuedTask.Run(() =>
                {
                    string csvPath = "c:\\temp\\test.csv";
                    string tblName = System.IO.Path.GetFileName(csvPath);
                    var StTable = StandaloneTableFactory.Instance.CreateStandaloneTable(new Uri(csvPath, UriKind.Absolute), container);
                    tbl = StTable.GetTable();

                    var tables = container.GetStandaloneTablesAsFlattenedList();
                    if (tables.Count() > 0)  //don't need this
                    {
                        var table = tables.First();
                        if (table.Name == tblName)
                            container.RemoveStandaloneTable(table);
                    }
                    using (RowCursor cur = tbl.Search(null, false))
                    {
                        while (cur.MoveNext())
                        {
                            using (Row row = cur.Current)
                            {
                                string id = Convert.ToString(row["ID"]);
                                Debug.Print(id);
                            }
                        }
                    }
                });
                
            }

            catch (Exception ex)
            {
                ErrorLog(ex.ToString());
                GC.Collect();
                return null;
            }
            return tbl;

        }

I figured out how to do what I needed.  This returns a Table object.   

View solution in original post

4 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

You can load your csv data to featureclass using geoprocessing. Code from ArcGIS Pro SDK Community sample:

      //args for GP Tool
      string input_table = @"C:\Users\uma2526\Documents\ArcGIS\Projects\CustomItemTest\AlaskaCitiesXY.csv";
      var gbd = Project.Current.GetItems<GDBProjectItem>().FirstOrDefault().Path;
      string outputFC = $@"{gbd}\AlaskaCitiesPointConv";
      string XField = "POINT_X";
      string YField = "POINT_Y";
      var sr = MapView.Active.Map.SpatialReference;
      
      var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
      var cts = new CancellationTokenSource();

      //Make the GP Tool arg array
      var args = Geoprocessing.MakeValueArray(input_table, outputFC, XField, YField, "", sr);
      //Execute
      _ = await Geoprocessing.ExecuteToolAsync("XYTableToPoint_management", args, environments, cts.Token,
                        (eventName, o) =>
                        {
                          System.Diagnostics.Debug.WriteLine($@"GP event: {eventName}");
                        }, GPExecuteToolFlags.None); //No action is taken, so the new Feature class doesn't get added to the map

Sample is here

0 Kudos
RichardDaniels
Frequent Contributor

The answer depends on what you want to do with the data. The reply by 

GintautasKmieliauskas creates a data source for immedate use with a GP processing. However, if you are just reading a text file into an array to concatenate data, as you mention in your post, you can do it all with standard C# coding. This could be done with the basic old readline, writeline of C or in a modern way using the DataTable data type. If reading from a SQL database table vs. ascii flie suggest using System.Data.SqlClient methods. Simple example at .net - How to read SQL Table data into a C# DataTable - Stack Overflow.
0 Kudos
Kevin_Andras
Occasional Contributor

Thanks!  The standard C# method should work...not sure why I never thought of that.    

In the old ArcMap SDK it was easy to get an ITable object from a .csv or .dbf file using TextFileWorkspaceFactory or IWorkspaceFactory.  Then I could just step through the table using a cursor.   I was hoping to be able to replicate that.  

https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic76800.html describes how to create a StandAloneTable, but that's probably more than I need, since I don't need to put it in a container.

0 Kudos
Kevin_Andras
Occasional Contributor
public async Task<Table> CSVtoTable()   //Task<Table>
        {
            Table tbl = null;
            try
            {
                var container = MapView.Active.Map;
                await QueuedTask.Run(() =>
                {
                    string csvPath = "c:\\temp\\test.csv";
                    string tblName = System.IO.Path.GetFileName(csvPath);
                    var StTable = StandaloneTableFactory.Instance.CreateStandaloneTable(new Uri(csvPath, UriKind.Absolute), container);
                    tbl = StTable.GetTable();

                    var tables = container.GetStandaloneTablesAsFlattenedList();
                    if (tables.Count() > 0)  //don't need this
                    {
                        var table = tables.First();
                        if (table.Name == tblName)
                            container.RemoveStandaloneTable(table);
                    }
                    using (RowCursor cur = tbl.Search(null, false))
                    {
                        while (cur.MoveNext())
                        {
                            using (Row row = cur.Current)
                            {
                                string id = Convert.ToString(row["ID"]);
                                Debug.Print(id);
                            }
                        }
                    }
                });
                
            }

            catch (Exception ex)
            {
                ErrorLog(ex.ToString());
                GC.Collect();
                return null;
            }
            return tbl;

        }

I figured out how to do what I needed.  This returns a Table object.