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?

  • 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. – Michael Stimson Jun 16 '14 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(). – PolyGeo Jun 16 '14 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. – Fezter Jun 16 '14 at 4:41
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.

  • Good idea. I thought about using X-Ray, but didn't use it for some reason. Is it markedly faster? – Fezter Jun 16 '14 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. – ericchiasson Jun 17 '14 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...

  • Interesting. Why is it faster that way? Does it create the layer in memory? – Fezter Jun 16 '14 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. – GISGe Jun 17 '14 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). – Curtis Price Jul 7 '14 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.