I am writing a python script for ArcGIS. I'm fairly new to this so I hope I'm not missing something very obvious. I am having trouble preserving the attributes of a polyline feature class when converting its vertices to points inside a search cursor loop. So far the workaround I've come up with is to run FeatureVerticesToPoints_management outside of the cursor, then join it to the cursor results inside the cursor:
arcpy.FeatureVerticesToPoints_management(selectFCP, tempPtName, "ALL") with arcpy.da.SearchCursor(selectFCP, "SHAPE@") as cursor: for row in cursor: arcpy.FeatureVerticesToPoints_management(row, tempPoint, "ALL") arcpy.SpatialJoin_analysis(tempPoint, tempPtName, tempPtJoin, "", "KEEP_ALL", "", "ARE_IDENTICAL_TO")
The FeatureVerticesToPoints_management only succeeds inside the cursor if I use the "SHAPE@" field by itself, but then none of the other attributes I'm interested in show up in the output. If I try to use all the fields ("*") or list other attributes with the SHAPE@ field (e.g. ["SHAPE@", "field1"]), it fails. Any ideas? I hope that made sense... I don't mind using the workaround but it is redundant and it seems like there should be a way to do this inside the cursor loop.
I am trying to work with one feature at a time for a series of operations inside the search cursor loop, which is why I can't just do the vertex->point conversion for all the lines outside of the loop-- just FYI.
I am able to get this to work in the python window in ArcGIS but not when running my script. I'm thinking that maybe the problem is that the name of the field I'm trying to preserve is a Field-type parameter I collect from the user prior to running the search cursor in the script. Here is the code that works in ArcMap. It works when I define the variable field as a simple string prior to running it:
with arcpy.da.SearchCursor("parcel_area_input", ["SHAPE@", field]) as cursor: for row in cursor: arcpy.FeatureVerticesToPoints_management(row, tempPoint, "ALL") arcpy.AddField_management(tempPoint, field, "TEXT", "", "", "240") arcpy.CalculateField_management(tempPoint, field, "row", "PYTHON") arcpy.Append_management("tempPoint", "fixPointsTEST", "NO_TEST")
This keeps all the line attributes, and keeps them together with the right points (I realized doing a spatial join didn't always join the attributes to the right point). Running this same code (not using feature layer names, of course) from my script, where field is a user-selected parameter derived from the input feature class, all the attributes are the same for all of the points instead of matching the row from which the points were created.
Do I need to define or use the parameter differently in my script, or set it up differently in the script options?
Right now it is set at arcpy.GetParameterAsText in my script, and the data type in the script properties is a Field that is derived from the input.