0

I am writing a function called getVertices. It uses arcpy to take a line feature class and turn it into an array of vertex lat/longs. It is a part of a larger script.

Here is the function:

def getVertices(line_feature):

    vertices =[]
    desc = arcpy.Describe(line_feature)
    shape_field = desc.shapeFieldName
    cursor = arcpy.UpdateCursor(line_feature)

    for row in cursor:
        shape = row.getValue(shape_field)
        part = shape.getPart(0)

        for point in part:
            coordinates = (point.X, point.Y)
            vertices.append(coordinates)

    return vertices

It does not work with all line feature classes, however. After processing many different line feature classes, I am seeing that in some of them only a chunk of the line is being processed--maybe the first 30 vertices, for example, or 3 vertices in the middle of the line.

I can't pin down if this is an issue in the function or in the line feature. To be clear, these are simple lines. I've double checked that the line features have no gaps in them.

What could the issue be?

EDIT: Here is the solution, which accounts for the multipart features lurking in my data.

def getVertices(line_feature):

    vertices =[]
    desc = arcpy.Describe(line_feature)
    shape_field = desc.shapeFieldName
    cursor = arcpy.UpdateCursor(line_feature)

    for row in cursor:
        shape = row.getValue(shape_field)
        part = shape.getPart()
        for p in part:

            for point in p:
                coordinates = (point.X, point.Y)
                vertices.append(coordinates)

    return vertices
  • 2
    You may have multiple Parts in a Line. Try adding a For loop, for part in parts. – klewis May 9 '17 at 22:19
  • @klewis when I add an additional loop, I get the error "Point object is not iterable" – user2094378 May 10 '17 at 5:03
  • 1
    If you have access to da cursors, there's an option explode_to_points that will give you all the vertices. – Paul May 10 '17 at 19:48
3

In your code, shape.getPart(0) equals to shape.getPart()[0] which just lists the first part's array/points. Therefore, you need to iterate parts not the points of the first part. For example the code below is the correct iteration for any shape row:

part = shape.getPart()
for p in part:
    for point in p:
        coordinates = (point.X, point.Y)
        vertices.append(coordinates)
  • I get the error "Point object is not iterable" – user2094378 May 10 '17 at 5:00
  • Sorry I forgot the part related to your real problem. you should revise part as part = shape.getPart() – fatih_dur May 10 '17 at 5:47
0

Iterate through all parts. This requires using .partCount and range.

def getVertices(line_feature):

    vertices =[]
    desc = arcpy.Describe(line_feature)
    shape_field = desc.shapeFieldName
    cursor = arcpy.UpdateCursor(line_feature)

    for row in cursor:
        shape = row.getValue(shape_field)
        for i in range (shape.partCount):
            part = shape.getPart(i)

            for point in part:
                coordinates = (point.X, point.Y)
                vertices.append(coordinates)

    return vertices

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.