This is an offshoot question related to a question I posted earlier. I have the following arcpy code that inputs a Python list of polygon geometry objects to the Union_analysis tool, which then writes the result to the in_memory workspace. The result is then input to the Dissolve_management tool, which in turn writes the result to memory. This all occurs near the end of a function, and the final goal here is to return the single geometry from the Dissolve to the calling function, where it can be passed along. Hence my question: How can (can I?) I set the Dissolve output to a variable so that I can return it as a geometry object? The first thing that will be done with it is to read its geometry into a point array to use as the inner ring for another polygon.

    if len(lstHSGeoms) > 1:
        #Union geometries into one geometry
        arcpy.Union_analysis(lstHSGeoms, "in_memory\\lstHSUnionGeom", "ONLY_FID")
        arcpy.Dissolve_management("in_memory\\lstHSUnionGeom","in_memory\\lstHSOneGeom", None, None, "SINGLE_PART")
        hsOneGeom = lstHSOneGeom

The last line is the problem here. No surprise, arcpy complains that the global name lstHSOneGeom is not defined. Putting in the whole in_memory path as the assignment value, i.e. "in_memory\\lstHSOneGeom" just assigns the simple string to the hsOneGeom variable. There is no reference to the in_memory object.

I originally coded this block as the ArcGIS help files would have you do, which is to create empty arcpy Geometry objects and to set them as the outputs of the tools. When then used as inputs, they were rejected as invalid. At least I could keep a reference to the geometry objects in a variable, but the tools just wouldn't recognize them as valid input. So, I went with in_memory workspace update that you see in the snippet above. (Thanks to @dmahr for this solution in the previous question referenced above.) I now don't get errors from the tool, but I do get an error from the last line. Any suggestions?

I'm using ArcGIS Desktop (ArcView) 10.0, Windows 7, 8 GB RAM.


I am not sure I follow your methodology, but try this:

return arcpy.Dissolve_management("in_memory\\lstHSUnionGeom",
    "in_memory\\lstHSOneGeom", None, None, "SINGLE_PART").getOutput(0)

The result of a geoprocessing function is a Result object with methods such as getInput and getOutput.

  • Ah, the old result object trick. I should have thought of that. Thanks, blah238. I have dealt with the GetCount tool often enough that it should have come to mind immediately. I actually have already coded and tested a workaround using features in temp feature classes and deleting them later, but that has left a bad taste in my mouth, knowing that the in_memory option is out there. If I can find the time, I will test out this option again with your suggested solution to the object access problem. – celticflute Sep 12 '12 at 1:59

Your Answer

