1

I'm trying to add a text file of points to a feature class using insert-cursor and I can't seem to figure it out. I'm new to python and arcpy so this is a little confusing for me. the error message I get is below. if anything else is needed I will try and provide it.

the error message reads:

Traceback (most recent call last): File "L:\400\lab9data\InsertCursor_GPSPts.py", line 61, in theInsertCur.insertRow([newRow]) File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\arcobjects.py", line 106, in insertRow return

convertArcObjectToPythonObject(self._arc_object.InsertRow(*gp_fixargs(args)))

RuntimeError: Error in InsertRow

#import arcpy
import arcpy

#set up an insert cursor to write to the feature class you created in part 1. Variable name should be theInsertCur. 

#1 ----------Add one line of code below-----------------------------
theInsertCur = arcpy.InsertCursor("l://400//lab9data1.1//lab9data//cursor.gdb//GPS_Pts//myyGPSpoints")

#a counter used to skip first heading line
theCnt = 1

#open the file and read each line using a loop
#2 ---------------Change file path below------------------ 
for theLine in open("l://400//lab9data1.1//lab9data//GPS_collection.txt", 'r'):

    #We are skipping the heading line and then reading all other lines in the text file
    if theCnt > 1:

        #split the line into a list of three values 
        threeValues = theLine.split(',')

        #print threeValues list to get an idea of what is happening
        print(threeValues)

        #get the x coordinate, y coordinate, and speed values from the threeValues list
        theX = float(threeValues[0])
        theY = float(threeValues[1])
        theSpeed = float(threeValues[2])

        #create a new point object with theX and theY as its coordinates. Variable name should be newPoint.
        #see Professional Library - Geoprocessing - The ArcPy site package - Classes - Point
        #3 ----------Add one line of code below-----------------------------
        newPoint = arcpy.Point(theX, theY)

        #add a row to InsertCursor. Variable name should be newRow. 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
        #4 ----------Add one line of code below-----------------------------
        newRow = arcpy.InsertCursor("l://400//lab9data1.1//lab9data//cursor.gdb//GPS_Pts//myyGPSpoints")

        #set the shape field value to the newPoint. The shape field name is 'Shape' 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Classes - Row 
        #5 ----------Add one line of code below-----------------------------
        newPoint = theInsertCur.newRow()


        #set the speed field value to theSpeed. The speed field name should be 'Speed' 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Classes - Row 
        #6 ----------Add one line of code below-----------------------------
        theSpeed = theInsertCur.newRow()

        #insert newRow using theInsertCur
        ##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
        #7 ----------Add one line of code below-----------------------------
        theInsertCur.insertRow(newRow)

    #advance counter
    theCnt = theCnt + 1

#delete references to cursor and row to clean up
#uncomment the code below if you are confident you have set the variables above
#to match what is in the code below.
##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
#8 ----------Uncomment below-----------------------
del theInsertCur
del newRow
  • 3
    Please paste code not a screen shot of your code. Is there a reason why you're using arcpy.InsertCursor and not arcpy.da.InsertCursor? It seems from your code you're trying to insert your cursor and not your new row which is being created twice but not assigned any values and theInsertCur is not being defined. – Michael Stimson Apr 17 '18 at 23:50
  • it's a homework assignment and I'm not looking for an answer just trying to figure out what I'm doing wrong, thanks. – user118954 Apr 17 '18 at 23:57
  • 1
    arcpy.cursors work, I personally find arcpy.da.cursors easier to work with. Start by changing the name of the cursor to theInsertCur = arcpy.InsertCursor(... then newRow = theInsertCur.newRow() then set newRow.setValue("SHAPE",newPoint), newRow.setValue("XField", theX), change the name of the fields to match your feature class.. then theInsertCursor.insertRow(newRow). – Michael Stimson Apr 18 '18 at 0:04
1

There's a few problems with your code that show you don't quite understand about cursor and row objects, I'll see if I can explain.

A cursor is an object that operates like a shuttle between the feature class and python so you can see/set/insert one row at a time, a row is one feature in a spatial table. You create a cursor first then from that cursor you create a row then you need to set the values on that row then use the cursor to insert that new feature into the feature class.

Using your code:

import arcpy

#set up an insert cursor to write to the feature class you created in part 1. Variable name should be theInsertCur. 

#1 ----------Add one line of code below-----------------------------
theInsertCur = arcpy.InsertCursor("l://400//lab9data1.1//lab9data//cursor.gdb//GPS_Pts//myyGPSpoints")

#a counter used to skip first heading line
theCnt = 1

#open the file and read each line using a loop
#2 ---------------Change file path below------------------

for theLine in open("l://400//lab9data1.1//lab9data//GPS_collection.txt", 'r'):

    #We are skipping the heading line and then reading all other lines in the text file
    if theCnt > 1:

        #split the line into a list of three values 
        threeValues = theLine.split(',')

        #print threeValues list to get an idea of what is happening
        print(threeValues)

        #get the x coordinate, y coordinate, and speed values from the threeValues list
        theX = float(threeValues[0])
        theY = float(threeValues[1])
        theSpeed = float(threeValues[2])

        #create a new point object with theX and theY as its coordinates. Variable name should be newPoint.
        #see Professional Library - Geoprocessing - The ArcPy site package - Classes - Point
        #3 ----------Add one line of code below-----------------------------
        newPoint = arcpy.Point(theX, theY)

        #add a row to InsertCursor. Variable name should be newRow. 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
        #4 ----------Add one line of code below-----------------------------
        newRow = theInsertCur.newRow()

        #set the shape field value to the newPoint. The shape field name is 'Shape' 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Classes - Row 
        #5 ----------Add one line of code below-----------------------------
        newRow.setValue("SHAPE",newPoint) # set the shape

        #set the speed field value to theSpeed. The speed field name should be 'Speed' 
        ##see Professional Library - Geoprocessing - The ArcPy site package - Classes - Row 
        #6 ----------Add one line of code below-----------------------------
        newRow.setValue("Speed",theSpeed) # set the speed, assuming your field name is 'speed'

        #insert newRow using theInsertCur
        ##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
        #7 ----------Add one line of code below-----------------------------
        theInsertCur.insertRow(newRow)

    #advance counter
    theCnt = theCnt + 1

#delete references to cursor and row to clean up
#uncomment the code below if you are confident you have set the variables above
#to match what is in the code below.
##see Professional Library - Geoprocessing - The ArcPy site package - Functions - Cursors - InsertCursor
#8 ----------Uncomment below-----------------------
del theInsertCur
del newRow

Your Answer

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