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!
Solved! Go to Solution.
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.
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
The answer depends on what you want to do with the data. The reply by
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.
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.