2

In the Code Sample from the ArcGIS Pro help for the ArcPy class named Geometry there is something that catches my attention:

# Run the Copy Features tool, setting the output to the geometry object.
# geometries is returned as a list of geometry objects.
geometries = arcpy.CopyFeatures_management("c:/data/streets.shp",
                                           arcpy.Geometry())

Here the Result object (geometries) returned by running arcpy.CopyFeatures_management() is a list of Geometry objects, which is very useful for subsequent processing in that sample code.

However, other than by coming across this sample code, is there any way that I could have learned what that Result object would be?

I have reviewed what I think are the two most obvious places to look in the ArcGIS Pro help:

I think it may be hinted at in the Result documentation where it says that the Return Value is:

The output, either as a recordset or a string.

If the output of the tool, such as MakeFeatureLayer is a layer, getOutput will return a Layer object.

Result outputs can also be accessed by index, so result.getOutput(0) and result[0] are equivalent.

but I was/am hoping to gain a clearer understanding.

  • What's the confusion? Are you trying to predict the geometry type (point, line, polygon...) or predict that the result object is a list of geometries? You can get the geometry type from the geometry pro.arcgis.com/en/pro-app/arcpy/classes/geometry.htm to decide if you have points/lines/polygons etc but what does type(geometries) return? stackoverflow.com/questions/2225038/… – Michael Stimson Apr 13 '18 at 2:44
  • @MichaelStimson I'm trying to "predict that the result object is a list of geometries" (probably by having some documentation to read). For example, I thought a search of the CopyFeatures page on 'result' or 'return' might jump to something that says it. – PolyGeo Apr 13 '18 at 3:31
6

The Result object returned from arcpy functions is a bit ambiguous as it is just a 'Geoprocessing Result' and the output data depends on the function ran. If you want to know what the output data is, you can do this a few ways:

# via type, result is output from an arcpy tool
print(type(result.getOutput(0)))

# via it's class 
print(result.getOutput(0).__class__)

# when the tool returns multiple outputs
print([result.getOutput(i) for i in range(result.outputCount)])

Type checking is useful when you need to do something based on a type. This could be especially useful with your example of getting geometries, which could be one of many types:

# do geometry checks
geometries = arcpy.CopyFeatures_management("c:/data/streets.shp",
                                           arcpy.Geometry())
for geometry in geometries:
    # check for point
    if isinstance(geometry, arcpy.Point):
        print('it is a point')
    elif isinstance(geometry, arcpy.Polygon):
        print('it is a polygon)

    # do checks for polygon and multipoint too

    # check if null geometry (will be None)
    elif not isinstance(geometry, arcpy.Geometry): #base class for all geometries
        raise TypeError('Null geometry detected')

Also useful for making sure you are getting a valid type that you expect from a tool (using arcpy.Describe could also be useful for the output):

# check if output is a layer
lyr = arcpy.management.MakeFeatureLayer(r'\\server\some_data\test.shp', 'test').getOutput(0)

if isisntance(lyr, arcpy.mapping.Layer):
    # do something with layer
else:
    raise TypeError('did not return a valid layer!')

Getting at Result objects is also useful in custom tools. Any output's returned from the tool via the arcpy.SetParameter(index, value) (or maybe even a function return statement?) can also be collected the same way.

  • 3
    I think "The Result object returned from arcpy functions is a bit ambiguous as it is just a 'Geoprocessing Result' and the output data depends on the function ran." sums up why I felt my comprehension was missing something. To me it seems as if the documentation for every tool needs another section to describe what will be returned if a Result object gets set to it. – PolyGeo Apr 13 '18 at 4:55
  • 2
    I agree, that would be really helpful. – crmackey Apr 13 '18 at 5:06
  • 2
    Yes, the documentation is a little scant on what a GPResult object is and how/if it can be used.. only functions like GetCount where the result object is directly used in the examples have any sort of documentation. Perhaps it's some sort of secret handshake for developers - there's a few in ArcObjects, if you don't know they're there you'd miss them altogether. – Michael Stimson Apr 13 '18 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.