arcpy.polyline not creating line

631
4
Jump to solution
07-27-2020 10:12 AM
BrookeHodge
New Contributor III

I'm going crazy and hoping someone can help.  I'm trying to bring in a point feature class, go row by row (using a .da.searchcursor), add two points into an array, and create a polyline from them. Then use an da.update cursor to add that line segment into a line feature class.  Then go to the next row and do the same until I have made a number of line segments from the point features.  I have tried this a ton of different ways, all with the same result, an updated line feature class that has the added rows, but NO lines draw and the length field for all segments are 0.  I've altered this to just run the points and add the points to a new point feature and that works just fine.  This code has also been updated to incorporate the .da.cursors (I have similar code running off the old cursors that work, i'm just trying to update it and the .polyline is not working right).  Does anyone have any ideas or things to try?  I'm going crazy. TIA!

import arcpy

# Set environments / workspace. Workspace will be a parameter in tool.  This parameter should point to a gdb that has all the effort point feature classes
arcpy.env.workspace = r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb"
arcpy.env.overwriteOutput = True
# input point fc
fc = r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb\SurveyPointsUTM19_TestSet_Short_6"

arcpy.MakeFeatureLayer_management(fc, "fc_lyr")

# Sort the table by fileid and eventno and create a new fc called fc_sort
sort_fields = [["FILEID", "ASCENDING"], ["EVENTNO", "ASCENDING"]]
fc_Sorted = arcpy.Sort_management("fc_lyr", "fc_sort", sort_fields)

# Create the search cursor
fields = ["SHAPE@"]
with arcpy.da.SearchCursor(fc_Sorted, fields) as cur:
    for row in cur:
        geom1 = row[0].getPart()
        cur.next()
        geom2 = row[0].getPart()
        array = arcpy.Array()
        array.add(geom1)
        array.add(geom2)
        polyline = arcpy.Polyline(array, arcpy.SpatialReference(26919))
        with arcpy.da.InsertCursor(r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb\OnEffortLinesTEST", 'SHAPE@') as insertCursor:
            insertCursor.insertRow([polyline])
        array.removeAll()
        del insertCursor
del cur

The result is all the points used draw and are correct (the fc_Sorted /fc_sort), and the line feature class seems to updated in that rows are added, but no lines draw and the Length field is 0.

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

You're using the same point for the start and end of the line, i.e. geom1 == geom2 because you haven't updated the row.  This is probably why you get a zero length line.

Try:

    for row in cur:
        geom1 = row[0].getPart()
        next_row = cur.next()
        geom2 = next_row[0].getPart()
‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

/blogs/dan_patterson/2016/08/14/script-formatting  is you want line numbers

You are adding a list ( ie ...  [polyline]  )in your 4th last line.  Have you tried taking polyline out of the list (ie dump [ ] )?


... sort of retired...
0 Kudos
BrookeHodge
New Contributor III

Hi Dan, thanks so much for your reply and thanks for pointing me to the code formatting info.  This was my first time posting code in a forum and I simply copied and pasted not knowing there was another way.  I think I understand what you are suggesting, and it was the original way I tried to complete this task.  Below, I tried to create a polyline from an array, shoot it into an empty list (below called featureList), then use the CopyFeatures_management to create an output feature class from the featureList list, however, that did not work either.  Below is that attempt. You may notice this is slightly different than my original post (there is an additional 'if' statement testing variables and determine if it should draw a polyline), however, I took all that out trying to simplify things until I figured out how to get any line to actually draw.  Is this what you were thinking?  Or did I misinterpret your suggestion?

import arcpy

# Set environments / workspace. Workspace will be a parameter in tool.  This parameter should point to a gdb that has all the effort point feature classes
arcpy.env.workspace = r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb"
arcpy.env.overwriteOutput = True
featureList = []
# Set where the output featureclass will go
fc = r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb\SurveyPointsUTM19_TestSet_Short_6"

# Make a feature Layer to work off
arcpy.MakeFeatureLayer_management(fc, "fc_lyr")

# Sort the table by fileid and eventno and create a new fc called fc_sort
sort_fields = [["FILEID", "ASCENDING"], ["EVENTNO", "ASCENDING"]]
fc_Sorted = arcpy.Sort_management("fc_lyr", "fc_sort", sort_fields)

# Create the search cursor
fields = ['FILEID', 'BEAUFORT', 'LEGTYPE', 'LEGSTAGE', 'VISIBLTY', 'MONTH', 'YEAR', "SHAPE@"]
with arcpy.da.SearchCursor(fc_Sorted, fields) as cur:
    for row in cur:
        # Name variables and assign values starting on first record of table
        fileid1 = row[0]
        beaufort = row[1]
        legtype = row[2]
        legstage = row[3]
        visiblty = row[4]
        month = row[5]
        year = row[6]
        # Get the geometry of the point for that record
        geom1 = row[7].getPart()
        # Go to next record and name variables and assign values
        next(cur)
        fileid2 = row[0]
        geom2 = row[7].getPart()
        array = arcpy.Array()
        if ((fileid1 == fileid2) and (beaufort != None and beaufort <= 4) and (visiblty != None and visiblty >= 2)) and \
                (not legtype == '0') and \
                (not (legtype == '7' and legstage == ' ')) and \
                (not (legtype == '9' and legstage == ' ')) and \
                (not (legtype == '5' and legstage == ' ')):
            array.add(geom1)
            array.add(geom2)
            polyline = arcpy.Polyline(array, arcpy.SpatialReference(26919))
            featureList.append(polyline)
            array.removeAll()
        else:
            pass
del cur
outputLines = arcpy.CopyFeatures_management(featureList, r"C:\Users\bhodge\Dropbox (New England Aquarium)\MonumentsWork\MonumentSpeciesDiversity\DataProcessing\MichelleCData.gdb\OnEffortLines6")
0 Kudos
Luke_Pinner
MVP Regular Contributor

You're using the same point for the start and end of the line, i.e. geom1 == geom2 because you haven't updated the row.  This is probably why you get a zero length line.

Try:

    for row in cur:
        geom1 = row[0].getPart()
        next_row = cur.next()
        geom2 = next_row[0].getPart()
‍‍‍‍‍‍‍‍‍
BrookeHodge
New Contributor III

My goodneess you've saved my sanity!  Yes, this worked, thank you so much!!!

0 Kudos