2

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[0], tempPoint, "ALL")
        arcpy.AddField_management(tempPoint, field, "TEXT", "", "", "240")
        arcpy.CalculateField_management(tempPoint, field, "row[1]", "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.

  • 1
    feature vertices to point works with an input geometry , a feature class or a feature layer. So you could use row[0] in your workaround and you can use the other values (row[1]etc) further in your loop when you need them. Anyway, I don't see the point of your workaround and I can't give you a better advice without more details about your workflow. – radouxju Oct 21 '14 at 19:36
  • Can you run MakeFeatureLayer on the row, then run FeatureVerticesToPoints? (I honestly don't know if that will work and don't have ArcPy on my laptop to check.) – Erica Oct 21 '14 at 21:41
  • Hi Erica-- I tried your suggestion and it wouldn't work unless I fed it the SHAPE@ field directly, but this still resulted in a loss of all the other attributes. Thank you, though! Radouxju-- I am trying to convert my feature vertices to points one row at a time while keeping all of the attributes. This is my first attempt at using a search cursor so I appreciate your help! It seems like anything that works on the geometry of a feature requires only the SHAPE@ field as input, but then the other fields are lost in the output. I'm having trouble reattaching those attributes, so to speak. – Squinney Oct 23 '14 at 20:48
1

Okay, so I figured out what I was missing: I needed to define an expression using the row[1] value first thing in the loop:

with arcpy.da.SearchCursor("parcel_area_input", ["SHAPE@", field]) as cursor:
    for row in cursor:
        exp = "'%s'" % (row[1])
        arcpy.FeatureVerticesToPoints_management(row[0], tempPoint, "ALL")
        arcpy.AddField_management(tempPoint, field, "TEXT", "", "", "240")
        arcpy.CalculateField_management(tempPoint, field, exp, "PYTHON")
        arcpy.Append_management("tempPoint", "fixPointsTEST", "NO_TEST")

I think I had tried this earlier but had put the variable definition too far down in the loop, right before CalculateField.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.