Tool for generating Feature Class including field names/type...

3802
14
07-22-2015 10:26 AM
TravisLathrop1
New Contributor III


I am trying to build a tool that will generate a feature class and then populate the fields with the correct column name and format based off a Excel file.  Has anyone had any luck doing this before?  I'm looking for model or code examples if anyone happens to have anything similar.  Has anyone seen any tutoria documents online that do this type of request?  That could help also.

Thanks in advance!

0 Kudos
14 Replies
DarrenWiens2
MVP Honored Contributor

This is certainly possible using Python. If you want to go in that direction, please post your starting script, or at least your Excel file as an example.

TravisLathrop1
New Contributor III

Thanks for the follow up Darren.

I have no code to share as I've been working in model builder.  If you think the python route is the way to go I'm all ears.  Here is what my Excel file looks like.  I want column A to represent the fields in the new feature class and the values in column B should the the field type.  Is this doable?

fields.jpg

0 Kudos
DarrenWiens2
MVP Honored Contributor

Step 1: convert Excel to CSV file. If you don't want to do that, you can work with Excel directly, but it's more complicated.

Step 2: run Python script. This will probably take some time to digest. Comments follow '#' (leave in or delete. They will be ignored).

import arcpy, csv, os

fc = r'C:/junk/FILE_GDB.gdb/newEmptyFC' # point to where you want your FC
if arcpy.Exists(fc):
    arcpy.Delete_management(fc) # delete FC if it exists
arcpy.CreateFeatureclass_management(os.path.dirname(fc),
                                    os.path.basename(fc)) # create FC

fieldtypes = {  'char':'TEXT',
                'inte':'LONG'} # dictionary to translate from your listed data types to ArcGIS data types. You'll have to add more.

with open(r'C:/junk/excel_test.csv', 'rb') as csvfile: # open CSV file
    reader = csv.reader(csvfile, dialect='excel') # make a CSV reader
    headers = reader.next() # skip header row
    for row in reader: # read through rows
        if row[0] <> 'OBJECTID': # don't create OID field. Will be created automatically
            print fieldtypes[row[1][:4]] # convince yourself that it is using the dictionary properly
            arcpy.AddField_management(fc, row[0], fieldtypes[row[1][:4]]) # create field of appropriate data type, based on first 4 characters in CSV file
TravisLathrop1
New Contributor III

Darren-I've had more time to digest this and I had some simply syntax errors on the front end.  After I got those resolved it crashed out on me at the second to last line.  Any ideas on what i might change?

codeimage.png

0 Kudos
WesMiller
Regular Contributor III

Looks like an indentation error you would need to set your print statements to be in your if statement

TravisLathrop1
New Contributor III

It runs but I get this error now....any ideas?

Traceback (most recent call last):

  File "C:/Users/tlathrop/Desktop/TL GIS/EnterpriseGeoDatabase/TestScript.py", line 16, in <module>

    print fieldtypes[row[1][:4]]

KeyError: 'inte

0 Kudos
DarrenWiens2
MVP Honored Contributor

I was being kind of lazy making the dictionary because I wasn't quite sure how your data looked.

fieldtypes[row[1][:4]]

...means: take the first four letters of the value in row[1], find the dictionary key that matches that value, and return the value at that key in the dictionary. Since there is no such value in your dictionary, it returns an error. When I made the example dictionary, I shortened the dictionary keys to four letters ("integer" became "inte") so there would be matches.

0 Kudos
TravisLathrop1
New Contributor III

I understand the dictionary (I think o_O). I think it loos correct it makes sense but I'm still getting errors.

Here is my code:

finalcode.png

Here is the error I get:errorcode.png

Any idea what I need to change?  I appreciate your help too btw!!!

0 Kudos
DarrenWiens2
MVP Honored Contributor

Ah, this one's easy. Python is case sensitive - it should be AddField, not Addfield.