1

Recently I've ramped up my exposure to Python coding and I'm currently working on a workflow which utilizes a layer class to export images retrieved from extents derived from selected features. The workflow currently loops through each of the selected features within the layer class and then exports that view/extent as a .JPEG. This is all great! But I'm trying to configure a dynamic naming convention which will utilize the values within the attribute table to name files and having some trouble.

The simple naming convention for the "exportToJPEG" step currently just adds a value of x to the end of the file name prefix. Instead I would like this step to grab text values from a "Bounding_Area" column within the "Bounding Box" attribute table so that the file name convention looks something more like:

lyt.exportToJPEG(r"P:\testOutBound" + "_" + "Bounding_Area")

Instead of:

lyt.exportToJPEG(r"P:\testOutBound" + str(x))

My current code looks like this:

import arcpy
aprx = arcpy.mp.ArcGISProject(r"P:\test.aprx")

arcpy.env.workspace = "P:\testing.gdb"
arcpy.env.overwriteOutput = True

arcpy.MakeFeatureLayer_management("P:\TestShapes.shp","BoundingBoxes")

map = aprx.listMaps()[0]
layer = map.listLayers("BoundingBoxes")[0]

for x in range (1,20):
    obj = "ID=%s" % (x)
    arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", obj)

    lyt = aprx.listLayouts()[0]

    mf = lyt.listElements('MAPFRAME_ELEMENT','*')[0]
    mf.camera.setExtent(mf.getLayerExtent(layer,True,True))
    mf.zoomToAllLayers()

    arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")

    lyt.exportToJPEG(r"P:\testOutBound" + str(x))

For similar dynamic path naming/path referencing Python tasks on other (non-arcpy) projects I've used the glob module or data frame column references but I'm not quite sure if either of those courses take the right approach.

  • I prefer to use the definitionQuery property of a layer rather than performing selections upon a layer in code like this. To look up a field value I would use a SearchCursor. Anywhere that I used to think about glob with ArcPy I use arcpy.da.Walk. – PolyGeo Jun 20 '18 at 19:55
  • Thank you for very much for your comment. I'll look into definitionQuery properties and the SearchCursor (as well as potentially arcpy.da.Walk). I'll post again if I can get everything working the way I want – forest_codes Jun 20 '18 at 21:54
-1

I prefer to use the definitionQuery property of a layer rather than performing selections upon a layer in code like this.

To look up a field value I would use a SearchCursor.

Anywhere that I used to think about glob with ArcPy I use arcpy.da.Walk.

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.