Geographic Information Systems Asked on June 10, 2021
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?
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)
Answered by Erica on June 10, 2021
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.
Answered by matt wilkie on June 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP