arcpy create feature class from csv/xy event layer where one field has more than 255 characters

4954
4
02-10-2017 09:09 AM
MikeSteven
New Contributor III

I'm writing a Python script that will take a csv containing point data, create an xy event layer from the csv and then create a file geodatabase feature class from that xy event layer (and then will do some more things with the feature class). One of the text fields in the csv has entries that in some cases have more than 255 characters and I'm struggling to work out how to get the csv/xy event layer to convert to a file geodatabase feature class.

I had thought to use the arcpy.CopyFeatures_management tool but it won't work with the entries that have more than 255 characters.

I next looked into manually creating an empty feature class with all of the fields already in it (editing the length of that one field to contain 1,000 characters) and use the arcpy.Append_management tool to append the features into feature class but the append tool won't take an xy event layer as the input.

I have the empty feature class created with the field length edited to contain 1,000 characters so I've also tried to use cursor.insertRow to insert the features from the xy event layer in to the empty feature class but I'm getting an error I think when I define the cursor:

cursor = arcpy.da.InsertCursor("D:\\GISData\\AccidentAnalysis\\AccidentAnalysis.gdb\\aa13To16", "*")

Perhaps this is the way to go but I've not used the cursor much and I can't figure out why this isn't working.

I'm looking for other ways to get this csv converted to a feature class but haven't found one yet. Any suggestions? Or any advice on how to get the cursor.insertRow to work?

Cheers,

Mike

Edit: arcpy code:

import arcpy

sr = arcpy.SpatialReference(27700)

inputCsv = "D:\\GISData\\AccidentAnalysis\\TestData.csv"

aaLayer = "aaLayer"
arcpy.MakeXYEventLayer_management(inputCsv, "easting", "northing", aaLayer, sr)

cursor = arcpy.da.InsertCursor("D:\\GISData\\AccidentAnalysis\\AccidentAnalysis.gdb\\aa13To16", "*")

for feature in aaLayer:
   cursor.insertRow(feature)

del cursor

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

If you are going to be working with cursors, bypass the xy event layer and just iterate over the CSV file and insert records using the insert cursor.

If you post specific code and error messages, people can offer more specific feedback.  If you can upload a portion of your data, even better.

0 Kudos
MikeSteven
New Contributor III

Thanks for getting back to me Joshua. I've edited the original post with the code and some data

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't have time to dig into the data itself, but at first glance I do have a couple comments.  First and foremost, you can't just loop over a layer the way you are trying.  You need to create a search cursor against the layer and loop over it.  Second, I strongly discourage the use of "*" for specifying field names with cursors, especially insert cursors.  If, for some reason, the schema of the table changes, your code will likely bomb because there might be extra or missing columns or columns in different order than when you first wrote the code.

JoshuaBixby
MVP Esteemed Contributor

I had thought to use the arcpy.CopyFeatures_management tool but it won't work with the entries that have more than 255 characters.

I guess I don't see that limitation.  I took your CSV file, ran Make XY Event Layer followed by Copy Features and everything looks good.  I assume the field you are talking about is "story"?  On my machine, it is a text field of 8000 length.

I am running ArcGIS 10.5.