flipping lines with ArcView (Basic)

2691
2
05-28-2014 08:28 AM
MattTrebesch
New Contributor
Hi there.

So I used to have a VB script that could flip multiple selected lines, using an ArcView licence.  

Of course, now we can do this with an ArcEditor (Standard) licence via Python but, alas, I have users that do this kind of work that do NOT have that level of licencing.

I did find this code snippet that claims to flip lines for a Basic licence, but I'm having difficulty running it. 

Does anyone have a better way to flip multiple selected lines with a Basic licence?  or can anyone see the errors in the the below script?

import math,sys
import arcgisscripting
gp = arcgisscripting.create(9.3)


gp.overwriteoutput=1

try:


    filename = gp.getparameterastext(0)

    cur = gp.UpdateCursor(filename)

    npnt=gp.createobject("Point")
   
    row=cur.next()

    while row:
       
        feat=row.shape
        PointCount = feat.PointCount
        i=0
        xlist=[]
        ylist=[]
        print 'do'
        while i < feat.PartCount:
            ptArray = feat.GetPart(i)
            ptArray.Reset
            pnt=ptArray.Next()
            while pnt:
                xlist.append(pnt.x)
                ylist.append(pnt.y)
                pnt=ptArray.next()
           
            xlist.reverse()
            ylist.reverse()

            ptArray.removeall()
            for j in range(len(xlist)):
                npnt.x=xlist
                npnt.y=ylist
                ptArray.add(npnt)

            row.shape=ptArray
            cur.updateRow(row)
                       
            i=i+1   
        print 'ok'       
        row=cur.next()
       
    del row,cur,pnt,npnt

except:
    gp.adderror('error')
Tags (2)
0 Kudos
2 Replies
markdenil
Occasional Contributor III
It is more or less what I would expect is required,
but (because of arcgisscripting?) it devolves each point to its x and y components.
It seems to me that it would be simpler to just re-order the points in the line array
rather than to re-build them.

Replacing arcgisscripting with arcpy is the first upgrade I would suggest.

getPart ({index}) on the line object will
return an array of point objects for a particular part of geometry
(or an array containing a number of arrays, one for each part)

step through the array, and build a new, reversed array

get the point from the existing line object array using  array.getObject (index)
then add it to the new array.

You can use newArray.insert (0, thePnt)  
(to take the existing points in order and adding each to the beginning of the new array)
or
take the points in the reverse of the existing order (decrimenting the index to 0)
and adding them each to the end of new array with  newArray.append (value).

Now use the new array to build a new polyline object
and update the feature in the table row.

Sorry, no code, but I think re-doing the script in arcpy is the first step:
there are some better tools in the newer module.
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Matt,

The code below might work for you:

import arcpy

fc = r'C:\Users\Xander\Documents\ArcGIS\Default.gdb\polyline_flipped'

with arcpy.da.UpdateCursor(fc, ("SHAPE@")) as curs:
    for row in curs:
        polyline = row[0]
        arr_pol = arcpy.Array()
        for part in polyline:
            lst_part = list(part)
            lst_part.reverse()
            arr = arcpy.Array()
            for pnt in lst_part:
                arr.add(pnt)
        arr_pol.add(arr)
        polyline_new = arcpy.Polyline(arr_pol)
        row[0] = polyline_new
        curs.updateRow(row)


Kind regards,

Xander