3

I am having trouble adding rows to a shapefile after it has been created. I'm not sure if I need to create the shapefile in memory and add the data or creating then re-open the file. While I've attempted to re-open the file, it will exit code 1 that file doesn't yet exist. Any and all suggestion would be useful. (except setting it on fire). I'm getting a "AttributeError: Row: Error in parsing arguments for SetValue" .... Please help.

for row in arcpy.da.SearchCursor(featureClass, ['FID','SHAPE@JSON','STREAM_ID','REACH_ID','SR_ID','STATION','INTERP','XS_ID','WSP001','WSP002','WSP003','WSP004','WSP005','WSP006','WSP007']):
featureClassData.append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14]])
outPutPath = "C:\\projectTemp\\FloodAnalysis\\output"
for i in range(0, len(featureClassData) -1):
FirstRowData = featureClassData[i]
SecondRowData = featureClassData[i+1]
FirstRowData[1] = eval(FirstRowData[1])
for cord in FirstRowData[1]['paths']:
 for c in cord:
 point = arcpy.Point(c[0],c[1])
 dataSetCreate = arcpy.CreateFeatureclass_management(outPutPath, 'test_section_' + `i` ,"", featureClass)
 dataSet = arcpy.InsertCursor(dataSetCreate)
 row1 = dataSet.newRow()
 row1.Shape = arcpy.geometries.Polyline(array1,spatialRef)
 row1.setValue(FirstRowData[2], FirstRowData[3], FirstRowData[4],
 FirstRowData[5], FirstRowData[6], FirstRowData[7],
 float(FirstRowData[8]), float(FirstRowData[9]), float(FirstRowData[10]),
 float(FirstRowData[11]), float(FirstRowData[12]),
 float(FirstRowData[13]), float(FirstRowData[14]))
 dataSet.insertRow(row1)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 23, 2014 at 15:53

2 Answers 2

6

The dataSetCreate variable is a Result object, not a feature class. You need to pass the path to the feature class to the insert cursor which you can get from the Result object using the getOutput method. Try:

dataSetCreate = arcpy.CreateFeatureclass_management(outPutPath, 'test_section_' + `i` ,"", featureClass)
dataSet = arcpy.InsertCursor(dataSetCreate.getOutput(0))

The second issue I can see is that you're using an arcpy.InsertCursor but trying to use arcpy.da.InsertCursor style syntax. They have quite different syntax for inserting rows.

This is what you're currently doing which will result in the AttributeError: Row: Error in parsing arguments for SetValue exception:

dataSet = arcpy.InsertCursor(dataSetCreate)
...
row1.setValue([list, of, field, values]) <--- this is the problem
dataSet.insertRow(row1)

Either use:

dataSet = arcpy.InsertCursor(dataSetCreate.getOutput(0))
...
row1.setValue(column_name, column_value)
row1.setValue(another_column_name, another_column_value)
etc...
dataSet.insertRow(row1)

Or:

dataSet = arcpy.da.InsertCursor(dataSetCreate.getOutput(0), [list, of, field, names])
...
dataSet.insertRow ([list, of, field, values])
answered Jan 23, 2014 at 20:30
3

If I understand correctly, I think the problem is here:

dataSetCreate = arcpy.CreateFeatureclass_management(outPutPath, 'test_section_' + `i` ,"", featureClass)
dataSet = arcpy.InsertCursor(dataSetCreate)

I believe when you assign arcpy.CreateFeatureclass_management to a variable, it will not be 'run' until it is called upon in the next line, at which point the Insert Cursor will be run first and will call a FC that doesn't exist yet as the create method has not been run.

Additionally I am pretty sure that the create FC method does not return a string of the output path to the created FC. Not sure on that, just know that I have never done it that way. I will check that out in the docs though.

Maybe try something like this:

arcpy.CreateFeatureclass_management(outPutPath, 'test_section_' + str(i) + '.shp' ,"", featureClass)
dataSet = arcpy.InsertCursor(outPutPath + '\\test_section_' + str(i) + '.shp')

This way you run the create FC method first and then just use the path to the output FC as an arg in the InsertCursor creation.

answered Jan 23, 2014 at 16:23
11
  • 1
    I think you're right, so the question is how to I get the fc created before it goes the cursor attempts to add rows of data? Commented Jan 23, 2014 at 16:44
  • 1
    You'll need to use outPutPath + '\\test_section_{0}'.format(i) to reference the actual feature class. Also, I'd use 'test_section_{0}'.format(i) instead of 'test_section_' + `i` Commented Jan 23, 2014 at 16:58
  • 1
    From that error, it looks like you omitted the first underscore in 'test_section_' Commented Jan 23, 2014 at 17:26
  • 1
    Are you working with shapefiles? If so you will also need to append '.shp' as such: outPutPath, 'test_section_' + str(i) + '.shp' Commented Jan 23, 2014 at 17:42
  • 2
    @Chaz This is partly correct in that you need to use the path to the feature class when creating the insert cursor, but your reasoning is wrong. The feature class is created as soon the the CreateFeatureclass tool is run.The variable dataSetCreate is an arcpy Result object. You can get the feature class path using the Result object getOutput method - dataSetCreate.getOutput(0) Commented Jan 23, 2014 at 20:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.