+ Reply to Thread
Results 1 to 6 of 6

Thread: arcpy Array methods

  1. #1
    William Watson
    Join Date
    Feb 2012
    Posts
    3
    Points
    0
    Answers Provided
    0


    0

    Default arcpy Array methods

    Has anyone else had problems with the Array class? I'm trying to do a sort on line segments that I have stored as arrays of points. All the Array methods seem to work fine except for insert and clone. Every time I use the insert method it appends.

    Also does anybody have a working example of how to use the clone method?

  2. #2
    Dan Patterson

    Join Date
    Apr 2010
    Posts
    1,753
    Points
    442
    Answers Provided
    41


    0

    Default Re: arcpy Array methods

    do you have a code example?
    Geomatics, Carleton University, Ottawa, Canada
    http://obidangis.blogspot.ca/

  3. #3
    William Watson
    Join Date
    Feb 2012
    Posts
    3
    Points
    0
    Answers Provided
    0


    0

    Default Re: arcpy Array methods

    Code:
    import arcpy
    
    infc = "C:/Pipeline"
    
    # Identify the geometry field
    #
    desc = arcpy.Describe(infc)
    shapefieldname = desc.ShapeFieldName
    
    # Create search cursor
    #
    rows = arcpy.SearchCursor(infc)
    featureList=[]
    array = arcpy.Array()
    point = arcpy.Point()
    
    # Build polyline list
    #
    for row in rows:
        
        feat = row.getValue(shapefieldname)
        partnum = 0
        partcount = feat.partCount
    
        while partnum < partcount:
            part = feat.getPart(partnum)
            pnt = part.next()
            pntcount = 0
            pntArray = arcpy.Array()
             
            while pnt:
                
                if pnt:
                    point = arcpy.Point()
                    point.X = pnt.X
                    point.Y = pnt.Y
                    point.ID = pnt.ID
                    point.Z = pnt.Z
                    point.M = pnt.M
                    
                    #Create an array of points representing a line
                    pntArray.add(point)
                    
                    pnt = part.next()
                    pntcount += 1
    
                if not pnt: 
                    pnt = part.next()
                        
            partnum += 1
    
            #Create a master array of point arrays in order to sort them
            array.add(pntArray)
            
    
    # Sort polyline array
    #
    
    pLineMax = array.count
    dummyPoint = arcpy.Point()
    dummyArray = arcpy.Array()
    
    #Index for point arrays that needs comparison
    pLineIndex = 0
    
    while pLineIndex < pLineMax - 1:
    
        #Index for point arrays that is a test comparison
        pLineIndex2 = pLineIndex + 2
    
        while pLineIndex2 < pLineMax - 1:
    
            # Compare endpoint of line to endpoints of other lines
            tempPoint1 = array[pLineIndex].getObject(array[pLineIndex].count-1)
            tempPoint2 = array[pLineIndex2].getObject(0)
            tempPoint3 = array[pLineIndex2].getObject(array[pLineIndex2].count-1)
    
            # if endpoint matches another segment
            if str(tempPoint1) == str(tempPoint2):
                
                # Assign pointer to point array that needs to be moved
                pntArray = array.getObject(pLineIndex2)
    
                # Remove point array from master array
                array.remove(pLineIndex2)
    
                # Insert point array in the appropriate place
                array.insert(pLineIndex + 1, pntArray)
    
                # Exit the while loop
                pLineIndex2 = pLineMax
                              
            pLineIndex2 += 1
    
        pLineIndex +=1
    The first part is copied from the help files and modified. I wrote this in PythonWin 2.6. I only have the sort for one end of the line segment so far.

    I'm new to Python but I have some experience as a programmer.

    Thank you very much

  4. #4
    William Watson
    Join Date
    Feb 2012
    Posts
    3
    Points
    0
    Answers Provided
    0


    0

    Default Re: arcpy Array methods

    The only workaround I've found is to redraw the array of point arrays in the order I want. I know that the Array insert and replace methods expect a point or array object, and the clone method expects a point object. I can only think that the problem is that these methods aren't accepting the parameters as array or point objects, even though I built the arrays using arcpy.Array(), array.add(), and pntArray.add(), and I've built the points using arcpy.Point(). I'm passing the objects based as addresses retrieved by using the getObject method. It almost seems like the methods will only accept static objects and not dynamic objects, but isn't the array just a set of pointers?

  5. #5
    Rob Flemming
    Join Date
    Mar 2011
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

    Default Re: arcpy Array methods

    Another bug, I think:

    import arcpy
    p = arcpy.Point()
    a = arcpy.Array()
    a.add(p)
    a.add(p)
    a.add(p)
    p.X=11
    a.replace(0,p) #so far so good
    p.X=12
    a.replace(2,p) #but, they are both replaced
    p2 = arcpy.Point() #however, if you make a new point...
    p2.X=42
    a.replace(2,p2)#...it goes only where expected

    If you change an attribute of an existing point object, even the id, and then replace it somewhere, it will replace all the old instances in the array with the new attribute.
    (So maybe it's not a bug?)
    (just a little annoying)

  6. #6
    Kim Ollivier
    Join Date
    Oct 2009
    Posts
    589
    Points
    130
    Answers Provided
    10


    0

    Default Re: arcpy Array methods

    Maybe it is because we are all too casual with the difference between immutable and mutable objects in Python. The arrays are mutable, so assigning a new value does not work as expected.
    Code:
    # classic example of mutabilty in action
    a = ['spam', 'eggs', 100, 1234]
    b = a
    b.append("abc")
    
    print a
    # ['spam','eggs',100,1234,'abc'] # << note that a is altered when we only appended to b
    print b
    # ['spam','eggs',100,1234,'abc']
    Last edited by kimo; 07-19-2012 at 04:00 AM.
    Kim Ollivier (EADP101)
    www.ollivier.co.nz kimo@ollivier.co.nz
    "Everywhere is within walking distance
    if you have the time", Steven Wright

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts