2

I want to flip the direction of a polyline feature obtained as a result of an Update query; e.g.

lines= arcpy.UpdateCursor(myFeatureClass,myQuery)
dsc= arcpy.Describe(myFeatureClass)
try:
    for ln in lines:
        # ? Check number of parts
        # ? Flip direction of each, or some of the parts
        lines.updateRow(ln)
except:
    print "Error:", sys.exc_info()[0]
finally:
    # releasing locks
    if lines: del lines
    if ln: del ln

BTW, any better way to know how many rows are returned than counting them?

  • I figured that ln.shape is the key to the geometry class I am interested in, e.g. if I want to check if it is multi-part, I can check ln.shape.isMultipart – Mahdi Jul 30 '13 at 18:13
7

Just for the record, and to help the folks who might be looking:

def flipLine(myFeatureClass, myQuery):
    try:
        lines=arcpy.UpdateCursor(myFeatureClass, myQuery)
        dsc=arcpy.Describe(myFeatureClass)
        lc=0
        for ln in lines:
            if ln.shape.partCount > 1: 
                print "Warning: multiple parts! extra parts are automatically trimmed!"
            lp= ln.shape.getPart(0)
            rPnts=arcpy.Array()
            for i in range(len(lp)): rPnts.append(lp[len(lp)-i-1])
            rPoly=arcpy.Polyline(rPnts)
            ln.shape= rPoly
            lines.updateRow(ln)
    except:
        print "Error:", sys.exc_info()[0]
    finally:
        if lines: del lines
        if ln: del ln
1

You can use Get Count to get the number of rows.

#Need to use .getOutput(0) to extract the value from the result object.
#Value is a string, so cast to int.
result = int(arcpy.GetCount_management(lyrfile).getOutput(0)) 

There is a tool, Flip Line that modifies the input. But if you want to use UpdateCursor, one way to do it would be to write the coordinates of all the vertices to a list of lists and then write them back to the lines in reverse.

If you are using ArcMap 10.1, definitely make the swap to the data access module. It's faster and has better support for accessing the geometry of features.

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.