5

I was provided an Excel Spreadsheet which had the definitions of fields for a feature class. There were several worksheets in the file, each containing field definitions for different feature classes. After a bit of cleanup, I converted each worksheet to a CSV with the following columns:

Field_Name
Field_Type
Field_Length
Field_Alias

Each of the CSV files had between 300 and 400 rows, which means there are that many fields (I know it's a lot of fields, but it's not my schema design).

I put together a bit of code to process the CSV files and create fields for the feature classes. Note, the feature classes already exist in a geodatabase and the CSV files have the same name as the feature classes.

The following code works, but it takes over 20 minutes to create 3 feature classes with 300-400 fields each.

count = 0
for csv_file in CSVList:
 print csv_file
 print count
 with open(csv_file, 'rb') as csvfile:
 reader = csv.reader(csvfile, delimiter = ',')
 for line in reader:
 field_name = line[0]
 field_type = line[1]
 field_length = line[2]
 field_alias = line[3]
 arcpy.AddField_management(FCList[count],field_name, field_type, "","",field_length,field_alias)
 count+=1

Is there a more efficient way to create a feature class from a CSV file with field definitions? Or, is arcpy.AddField_management as good as it gets for this sort of thing?

asked Jun 16, 2014 at 4:06
3
  • It depends on how many times you want to do this. You can write an XML workspace and import the whole XML schema; you could write it in C# or VB.net, creating a fields object to create a feature class. I remember something previously about Visio and schemas but not details, it wasn't me who was doing it but perhaps that will jog someones' memory. Commented Jun 16, 2014 at 4:13
  • 1
    Check out gis.stackexchange.com/questions/99792/… - adding 300-400 fields, assuming they are of allowed types, to an empty feature class should be much faster using arcpy.da.ExtendTable() than using arcpy.AddField_management(). Commented Jun 16, 2014 at 4:25
  • 1
    I'll have a look at arcpy.da.ExtendTable. Looks promising. I'm not about to start learning C# or VB.net. I'll stick with python solutions for now. Commented Jun 16, 2014 at 4:41

2 Answers 2

1

Since the geodatabase already exist, you could have used X-Ray for ArcCatalog add-in which was Developper by Esri, in conjunction with Vertex3. It allows you, amongst other things, to export the schema to spreadsheet, perform changes in the spreadsheet and import it back into a geodatabse.

X-Ray for ArcCatalog is available for 9.2 and 9.3 in ArcScript, and 10.x in ArcGIS Online.

answered Jun 16, 2014 at 12:56
2
  • Good idea. I thought about using X-Ray, but didn't use it for some reason. Is it markedly faster? Commented Jun 16, 2014 at 20:10
  • I didn't benchmark it but I am assuming it would since to my knowledge it was developed using either C# or .NET. Commented Jun 17, 2014 at 9:46
0

You can convert your feature class to a layer with Make Feature Layer before adding the fields. I've made the test with a table and 20 fields to add: On my computer it takes normally 2 min 04 secs to create the table and add the fields. It only takes 31 secs with the Make Feature Layer added. It would still take about 5 mins for you, but it's easy to implement...

answered Jun 16, 2014 at 6:36
3
  • Interesting. Why is it faster that way? Does it create the layer in memory? Commented Jun 16, 2014 at 20:20
  • Yes by defintion a feature layer is created in memory. It's not the most efficient form of in memory data (compared to numpy arrays) but it provides more options regarding field formats. I see also that you need to define an alias, aliases can't be set with numpy arrays. Commented Jun 17, 2014 at 6:13
  • 2
    Just be clear, the layer's geodata are not in memory, just validated informatino about the layer. If you create a feature layer or table view and pass that to a tool, the tool does not have to validate the input (ie make sure it exists, get the field list and other properties - the layer has all this "metadata" in memory already). Commented Jul 7, 2014 at 19:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.