3

We're migrating automated mapping scripts from ArcGIS Desktop to ArcGIS Pro — Python 2.7 to 3, arcpy.mapping module to arcpy.mp module. All these scripts iterate through a loop to select various features of interest, and then focus the dataframe extent on that selection. For example:

gas_main_layer = arcpy.mapping.ListLayers(mxd, "GasMain", data_frame)[0]
data_frame.extent = gas_main_layer.getSelectedExtent()
# adjust scale slightly so view is larger than polygon extent
data_frame.scale *= 1.2

I easily figured out how to transition ListLayers to the new mapping module's workflow, but I can't find a comparable method on the Layer class to get the selected extent and then reset the zoom/extent of its Map. What is the way to get the same functionality out of the new module?

2
  • 1
    The last bit in your code snippet of adjusting your scale a fraction is done at gis.stackexchange.com/a/268361/115 – PolyGeo Oct 17 '18 at 20:01
  • Thank you! Once I discovered what "controls" the extent/scale in Pro vs Desktop, it was much simpler to search for solutions and figure out how to control everything I needed to. – Erica Oct 17 '18 at 20:44
5

I found the solution in a blog on ArcGIS Pro for Iterative Map Exports which is about a very similar problem.

In the arcpy.mp module, querying the layer extent is now a Mapframe method (and Mapframe Camera method) rather than a Layer method.

project  = arcpy.mp.ArcGISProject(aprx_file_path)
layout   = project.listLayouts("project_layout")[0]
mapframe = layout.listElements("MAPFRAME_ELEMENT", "layout_mapframe")[0]

...

# query layer extent through mapframe
layer_extent = mapframe.getLayerExtent(gas_main_layer, True)
# apply extent to mapframe camera
mapframe.camera.setExtent(layer_extent)
1

If you don't have layouts or are more interested in getting a map (dataframe) extent:

aprx=arcpy.mp.ArcGISProject("CURRENT")
mapView = aprx.listMaps()[0]
mapExtents=mapView.defaultCamera.getExtent()

print(mapExtents.lowerLeft, mapExtents.upperRight)

Alter to something like .listMaps("Map Name X")[0] to find a particular one.

And it appears necessary to save the project first before the info becomes available. Hat tip to David Matthews and Joshua Bixby on Geonet.

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.