How to get the last element of a polyline array?

713
3
Jump to solution
07-01-2013 08:40 AM
MennoNijhuis1
New Contributor III
To my surprise, it is impossible to get the last element of an array from a polyline (which is a arcpy.arcobjects.arcobjects.Array, so not a normal Python array for which it would definitely work!).

my_array[-1]

throws this error:
Runtime error <type 'exceptions.RuntimeError'>: Array: Error in getting object from array


EDIT:

OK a rather stupid workaround I thought of myself:
my_array.getObject(len(my_array)-1)

gives:
<Point (280349.568, 311663.817, #, 162.393)>

I still wonder: why is it not possible to just use my_array[-1]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
markdenil
Occasional Contributor III
As you point out, an arcpy.arcobjects.arcobjects.Array is not a normal Python array,
so it doesn't index nicely.
As a geometry object, however, it has a lastPoint property, which should be simple to access.

View solution in original post

0 Kudos
3 Replies
markdenil
Occasional Contributor III
As you point out, an arcpy.arcobjects.arcobjects.Array is not a normal Python array,
so it doesn't index nicely.
As a geometry object, however, it has a lastPoint property, which should be simple to access.
0 Kudos
ShaunWalbridge
Esri Regular Contributor
You could subclass arcpy.Array to include a __getitem__ method, and then handle slicing and indexing there, if you want to be Pythonic about accessing the elements. However, Mark's solution is probably the simplest answer.

cheers,
Shaun
0 Kudos
MennoNijhuis1
New Contributor III
I hadn't even thought of accessing the geometry object, focussed as I was with accessing the array. This will work nicely, thanks both!
0 Kudos