slicing error in python in ArgGIS 10.4.1 for desktop

477
5
Jump to solution
01-01-2019 02:18 PM
EvanD
by
New Contributor II

I am attempting to run a script that throws an error in a seemingly simple statement in bold below:

for i in range(1,len(part)):
cut=part[i-1:i+1]
sides.append(arcpy.Polyline(cut,SR))

I ran the script up to where part is defined then entered:

>>> part
<Array [<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>, <Point (2524290.3774, 2562712.6347, #, #)>, <Point (2524290.0113, 2562562.6347, #, #)>, <Point (2524240.5113, 2562562.7555, #, #)>]>
>>> c = part[0:2]
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 161, in __getitem__
return convertArcObjectToPythonObject(self._arc_object.GetObject(index))
AttributeError: Array: Error in parsing arguments for GetObject

I have tried on two computers, one with python 3 also installed and one without. The python directories returned by sys.path are all there (although the path also includes the non-existent 'C:\\WINDOWS\\SYSTEM32\\python27.zip').

Any ideas on how to troubleshoot this? Thanks.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

The array needs getObject to access the individual points. The supported properties and methods are listed in the help

Array—ArcPy classes | ArcGIS Desktop 

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

The array needs getObject to access the individual points. The supported properties and methods are listed in the help

Array—ArcPy classes | ArcGIS Desktop 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you running 10.4.1 or 10.4?  Prior to 10.4.1, ArcPy geometry objects did not support indexing and slicing.  I believe that included ArcPy array objects as well, but I don't have access to a 10.4 machine to test.

Your code works fine in 10.6.x, and I believe it will also work fine in 10.5.x:

>>> import arcpy
>>> coords = [
...   (2524240.5113, 2562562.7555),
...   (2524240.8774, 2562712.7555),
...   (2524290.3774, 2562712.6347),
...   (2524290.0113, 2562562.6347),
...   (2524240.5113, 2562562.7555)
... ]
>>> 
>>> part = arcpy.Array(arcpy.Point(*coord) for coord in coords)
>>> part
<Array [<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>, <Point (2524290.3774, 2562712.6347, #, #)>, <Point (2524290.0113, 2562562.6347, #, #)>, <Point (2524240.5113, 2562562.7555, #, #)>]>
>>> part[0:2]
<Array [<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>]>
>>> 
>>> c = part[0:2]
>>> 
0 Kudos
EvanD
by
New Contributor II

It says 10.4.2.5686.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Build 5686 is 10.4.1 final.  Although My Esri states my enhancement requests for geometry slicing was implemented in 10.4.1, it could be wrong and 10.5 instead. I know it works in 10.6.1, so the question remains what version exactly does it start working.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It has worked in Pro since I can remember... the arcpy's differ though

import arcpy
coords = [(2524240.5113, 2562562.7555), (2524240.8774, 2562712.7555), (2524290.3774, 2562712.6347), (2524290.0113, 2562562.6347), (2524240.5113, 2562562.7555) ]
part = arcpy.Array(arcpy.Point(*coord) for coord in coords)

part
<Array [<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>, <Point (2524290.3774, 2562712.6347, #, #)>, <Point (2524290.0113, 2562562.6347, #, #)>, <Point (2524240.5113, 2562562.7555, #, #)>]>

part[:2]
<Array [<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>]>

pnts = [p for p in part]

pnts
[<Point (2524240.5113, 2562562.7555, #, #)>, <Point (2524240.8774, 2562712.7555, #, #)>, <Point (2524290.3774, 2562712.6347, #, #)>, <Point (2524290.0113, 2562562.6347, #, #)>, <Point (2524240.5113, 2562562.7555, #, #)>]

arr = np.asarray([(p.X, p.Y) for p in part])
arr
array([[2524240.5113, 2562562.7555],
       [2524240.8774, 2562712.7555],
       [2524290.3774, 2562712.6347],
       [2524290.0113, 2562562.6347],
       [2524240.5113, 2562562.7555]])

np.mean(arr, axis=0)
array([2524260.45774, 2562622.70718])
0 Kudos