da.InsertCursor to write geometry from csv file

1841
7
Jump to solution
07-10-2017 09:16 PM
TravisPeterson
New Contributor III

Are there any good examples showing how to create point geometry using arcpy.da.InsertCursor from a CSV File.  All the examples I find start with a list and not a file. 

I attached the script I wrote using the old InsertCursor method.  I read da.InsertCursor is the better way to write geometry and I can't figure it out.

Thanks for the suggestions.  I figured it out. I posted response.  I just needed make a list out of the csv rows to pass to my insert cursor.  

0 Kudos
1 Solution

Accepted Solutions
AlexanderBrown5
Occasional Contributor II

Travis,

You should be able to utilize the regular help to figure it out, especially if you have been all the way through Geog 485 Programming & Automation!  Is Jim Detwiler still teaching this class?

Accessing data using cursors—Help | ArcGIS Desktop 

InsertCursor—Help | ArcGIS Desktop 

Python arcpy.da.InsertCursor how to create a newRow() 

~Alex

View solution in original post

7 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

I'm on an iPad, so not looking at the script, but wondering if there is a reason you are needing to use the cursor option?  

You may want to look at Excel To Table—Help | ArcGIS Desktop  or other import table options, then

and then Make XY Event Layer—Help | ArcGIS Desktop and then make sure to save the output.  There are more details on how to do that in the help files.    These should be faster than trying to look at every record to create the geometry.

If if its a one time thing, you can also do something similar in ArcMap.

0 Kudos
BlakeTerhune
MVP Regular Contributor

This is obviously classwork so giving you a direct solution would be unfair. Creating point geometry is pretty straightforward because it's typically just an X and a Y coordinate. Check out the documentation for more information.

Point—Help | ArcGIS Desktop 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To give the OP the benefit of the doubt, just because the attached script was created for a class final project, it doesn't mean this question is for a class project.

travisjpeterson‌, is this question for another class project?  The script you attached already does the job of loading CSV points into a feature class, all you need to do is modify lines 108 to 164 to use the DA cursor instead of the original cursor.  Since you already know how to open a CSV and loop over it, the real issue you are having appears to be understanding how the DA cursor works.  Do you have a specific issue that is confusing you?

TravisPeterson
New Contributor III

Thanks Josh.  I didn't understand how to make a list out of the rows in my csv.  Small gap in understanding on my end and I figured it out by experimenting.   I spent some extra time working through examples using the DA cursor and was able to get everything working.  Thanks. 

VinceAngelo
Esri Esteemed Contributor

You're not helping yourself by providing a zip file. The script should by small enough to fit in the question page, so your participation will be hurt by a requirement to download, unzip, and open.

- V

0 Kudos
AlexanderBrown5
Occasional Contributor II

Travis,

You should be able to utilize the regular help to figure it out, especially if you have been all the way through Geog 485 Programming & Automation!  Is Jim Detwiler still teaching this class?

Accessing data using cursors—Help | ArcGIS Desktop 

InsertCursor—Help | ArcGIS Desktop 

Python arcpy.da.InsertCursor how to create a newRow() 

~Alex

TravisPeterson
New Contributor III

Hey everyone.  Sorry to spark controversy.    Just looking for a few examples not a complete script rewrite.  I figured out the answer myself this morning around 1:00AM.   I could have maybe asked a different way and been more clear.

 I ended up creating a tuple of values in a list, made a cursor with the matching fields in an existing feature class. I iterated through the list I made and inserted the row.  Ran into an issue with the SHAPE@XY, I needed to use SHAPE@X, SHAPE@Y.   I didn't understand that I could just make a list out of the row values in the csv.  Once I realized this I was able to make progress.

This is basically what I came up with to get the job done.   Thanks to those that were constructive.

rowValues = row[pointNum], row[elevation], row[featureType],  csvFileName, float(row[lat]), float(row[lon])

list.append(rowValues)

cursor = arcpy.da.InsertCursor(fc,["PointNumber","Elevation","FeatureType", "FileName","SHAPE@Y", "SHAPE@X"])

for row in list:
   cursor.insertRow(row)