1

I am trying to create an ArcPy script that will read a raster cell value at the start point and end point of a polyline, and flip the line based on those values.

I do not want to create another feature class, I just want to edit a feature class in an existing file geodatabase.

I am able to get the cell values at the nodes, however, the lines are not flipping direction, so I think the problem lies within the second for loop.

Is there a way to do this without writing the geometries to a list and reversing the order because I have had more trouble trying to do just that?

The code is as follows:

import arcpy

inFeatures = "Lines"
shapeName = arcpy.Describe(inFeatures).shapeFieldName
rows = arcpy.SearchCursor(inFeatures)

Raster = 'srtm2f.tif'

for row in rows:
    feat = row.getValue(shapeName)
    xy1 = feat.firstPoint
    xy2 = feat.lastPoint
    resultSTART = arcpy.GetCellValue_management(Raster, str((xy1)), "1")
    CellValueSTART = float(resultSTART.getOutput(0))
    resultEND = arcpy.GetCellValue_management(Raster, str((xy2)), "1")
    CellValueEND = float(resultEND.getOutput(0))

for features in inFeatures:
    if CellValueSTART < CellValueEND:
         arcpy.FlipLine_edit(inFeatures)
    elif CellValueSTART > CellValueEND:
        print 'Feature was correctly digitized'
    elif CellValueSTART == CellValueEND:
        print 'Feature had same elevation'
1
  • it seems flipline_edit accepts a feature layer, not sure if it accepts a single line geometry? - anyway, the second loop appears to flip all lines in 'inFeatures' multiple times, through using arcpy.FlipLine_edit(inFeatures) rather than arcpy.FlipLine_edit(features) Commented Jun 8, 2015 at 22:43

1 Answer 1

2

There are some serious problems with your script..

Read up on Variable Scope, you are accessing values from your first loop in your second loop, at this stage the values are consistent with the last feature processed... so the flipping of the lines depends only on the last line not for each line.

It is important to dismiss a cursor, in this case with arcpy.SearchCursor you need to del it when you're done or the locks will stay on the data.

FlipLine_edit accepts a whole feature class, not a single geometry object, to flip a geometry use the instructions How to edit (particularly flip direction of) polyline obtained through UpdateCursor?. Of course you can use FlipLine_edit tool but you need to give it a whole feature class.. you could try splitting into OK and Flip features then appending.

Using the link (simplest way):

import arcpy

inFeatures = "Lines"
shapeName = arcpy.Describe(inFeatures).shapeFieldName
rows = arcpy.UpdateCursor(inFeatures) # got to be an update cursor to change rows
Raster = 'srtm2f.tif'

def FlipLine(InPolyline):
    Part = InPolyline.getPart(0) # only the first part
    rPnts=arcpy.Array()
    for i in range(len(Part)): rPnts.append(Part[len(Part)-i-1]) # flip the points in the array
    OutShape = arcpy.Polyline(rPnts)
    return OutShape

for row in rows:
    feat = row.getValue(shapeName)
    xy1 = feat.firstPoint
    xy2 = feat.lastPoint
    resultSTART = arcpy.GetCellValue_management(Raster, str((xy1)), "1")
    CellValueSTART = float(resultSTART.getOutput(0))
    resultEND = arcpy.GetCellValue_management(Raster, str((xy2)), "1")
    CellValueEND = float(resultEND.getOutput(0))
    if CellValueSTART < CellValueEND:
      arcpy.AddMessage("Updating")
      row.setValue( shapeName,FlipLine(feat))
      rows.updateRow(row) # update it
    else:
      arcpy.AddMessage("Line was either flat or correct")
# release locks
del row
del rows    
# This bit doesn't relate...
##for features in inFeatures:
##    if CellValueSTART < CellValueEND:
##         arcpy.FlipLine_edit(inFeatures)
##    elif CellValueSTART > CellValueEND:
##        print 'Feature was correctly digitized'
##    elif CellValueSTART == CellValueEND:
##        print 'Feature had same elevation'

Disclaimer: I have not run this code so there might be some minor bugs in it.

4
  • This worked perfectly. The only bug is the double equal sign in Line 12. Thanks for the help, I am new to arcpy. Commented Jun 9, 2015 at 1:50
  • DOH... equivalence vs assignment. I'll fix that. Your code wasn't bad for a beginner just a few basics you need to cover. Commented Jun 9, 2015 at 2:12
  • The only issue I'm having (and this may be another novice question) is that the script also changes the flipped inFeatures shape length to zero. Commented Jun 9, 2015 at 17:34
  • Including the spatial reference fixed the problem. Commented Jun 9, 2015 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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