5

I am wondering what the purpose of arcpy.Array() is. It seems an intermediate step between creating a list of objects and performing some sort of conversion, for example, such as in the sample code for the 10.1 help.

    features.append(
            arcpy.Polyline(
                arcpy.Array([arcpy.Point(*coords) for coords in feature])))

What purpose does the arcpy array serve? Why can't a list suffice? What's the logic behind this intermediate step?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Emil Brundage
  • 13,859
  • 3
  • 26
  • 62
  • It adds support for iteration via some of its methods. Seems similar to an iterator that can be created for lists with the built-in iter() function. – Paul Aug 18 '15 at 21:02
  • 1
    I could be wrong here, but I believe that under the hood arcpy.Array is creating a Python object equivalent of the ArcObjects IPointArray interface, which is a required input for constructing line/polygon geometries. http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//002m0000026w000000 – crmackey Aug 18 '15 at 21:05
  • +1. Agree. Very annoying. Used to be a list of lists – FelixIP Aug 18 '15 at 21:31
  • I think @crmackey is right. You can see this in the source of the Array() class in C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py and the ArrayMixin() class in C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py (replacing 10.1 with whatever version you have). – dmahr Aug 19 '15 at 00:43
  • There's a lot of speculation here, but from what I think I understand, arcpy is a wrapper on the arcgisscripting.pyd (essentially a DLL) which I think is a course grained wrapper of a lot of ArcObjects functionality written in C++. I do know for a fact that ArcObjects is not directly exposed in arcpy/arcgisscripting but you can get at them using the comtypes module. My main point is I think that using arcpy.Array packages up a point collection in a C/C++ like object for easier translation into arcgisscritping, hence as @dmahr suggested may happen in mixins.py. – crmackey Aug 19 '15 at 12:35
  • @crmackey I think you should create an answer from your comments here. – PolyGeo Sep 05 '16 at 05:51
  • @PolyGeo thanks for the suggestion. I have turned my comment into an answer. – crmackey Sep 06 '16 at 16:28

3 Answers3

2

This is all my own speculation, so take it with a grain of salt:

From what I think I understand, arcpy is a wrapper/bridge to the arcgisscripting.pyd (essentially a DLL) which I think is a course grained wrapper of a lot of ArcObjects functionality written in C++. I do know for a fact that ArcObjects is not directly exposed in arcpy/arcgisscripting but you can get at them using the comtypes module.

My main point is I think that using arcpy.Array() is the arcpy version of the ArcObjects IPointArray or probably more closely the IPointCollection interface. This data structure is probably enforced so that some internal data validation can happen under the hood to make sure there are valid inputs. Just using lists of lists could cause some problems (explicit is better than implicit :) ) without validation. That way, any arcpy.Geometry object knows it is getting valid input if it contains an arcpy.Array (even if it is empty). The arcpy.Array also understands things like donut holes.

crmackey
  • 8,484
  • 18
  • 33
1

ArcGIS provides its internal Functions through a python interface. Because some of them operate with a list of objects and they use the type Array as a function parameter, python lists do not work in this context. It isn't very 'pythonic' but there are examples in other modules too, numpy for example, where python standard types are substituted with specialized ones. The reason behind is, that there are tools like swig for 'converting' C++ Code to python, by putting a python caller to every c++ function (or class). This is an interesting (at least old) alternative approach to access arcobjects from python: Accessing ArcObjects from Python?

Andreas Müller
  • 2,622
  • 13
  • 20
-2

One example for using an array would be creating a polyline from an array of points. I am by no means a python expert (yet) but from my understanding is that it can provide a way of automating creating features from a list (array) of other features. Here is an article I found that may help a little.

ESRI Desktop 10.1 Help Article

AReaGIS
  • 57
  • 9
  • That is the exact same link OP posted. – Paul Aug 18 '15 at 20:57
  • I just realized that I posted a link to the same article/link you posted. Sorry! LOL

    Did that help you any? I know you could get much more complicated tasks completed by using arrays.

    – AReaGIS Aug 18 '15 at 21:00
  • @Paul, yes I realized that after I posted. I do believe though that this article answers the question. I do not think that it is an all inclusive article in regard to all the different scenarios in which to use it though. – AReaGIS Aug 18 '15 at 22:01