0

I need to add data to an existing .aprx layout and zoom to the newly added data extent. Then, I need to export layout with new data as a JPEG.

This workflow is not clear online. My data is not in a .gdb, they are shapefile (or can be .kml files).

Currently, I am at:

# Read in project
aprx = arcpy.mp.ArcGISProject("path/to/project.aprx")
m = aprx.listMaps()[0]
fc = "path/to/poly.shp"
# add layer
# lyr = arcpy.management.MakeFeatureLayer(fc, os.path.basename(fc)[:-4])
lyr = m.addDataFromPath(fc)
lyt = aprx.listLayouts("Layout")[0]
# Set extent to layer, then save!
m.defaultCamera.setExtent(arcpy.Describe(lyr).extent)
lyt.exportToJPEG("./temp.jpg")

However, when saving the figure, it does not change extent to the newly added layer. I get the original extent of the map.

1
  • 1
    You forgot to say what goes wrong with your code. What is the difference between the actual behaviour and the expected behaviour. Please edit your question to update it with this information. Sep 27, 2023 at 0:07

1 Answer 1

2

It's unclear from your question what your actual problem is, but I can see one issue in the code...

In ArcGIS Pro, updating the map's camera extent, doesn't update the camera extent of the map frame in the layout.

When I'm writing a script tool that should update the map or map frame, I try to cater for which ever of the two is the active view at the time like this:

        activeView = aprx.activeView
        try:
            activeView.camera.getExtent()
            activeMapView = activeView
        except:
            try:
                activeLayoutMapFrames = activeView.listElements("mapframe_element")
                if len(activeLayoutMapFrames) == 1:
                    activeMapView = activeLayoutMapFrames[0]
            except:
                activeMapView = None

Let's step through what this does:

  • The activeView is which ever view is currently active in ArcGIS Pro. This could be a map view or a layout view. We don't know yet.
  • activeView.camera.getExtent() will fail if this is not a map view (and therefore it is probably a layout view, and not a map view). If it succeeds, then we have found an active map view in activeView.
  • If the above fails, then activeView.listElements("mapframe_element") will fail if this is not a layout view. If it succeeds, then we have found an active layout view, and we check it for the number of map frames. If there is only one map frame, use this as the active map view (it's not really a map view as such, but you can still still run the activeMapView.camera.setExtent() on it later on).

In your case, you are not using a CURRENT aprx, so the idea of an activeView is not relevant. But the principle is still the same. You could change your code to something like the following (UNTESTED!) in order to change the camera extent on your map frame, instead of on your map.

# Read in project
aprx = arcpy.mp.ArcGISProject("path/to/project.aprx")
m = aprx.listMaps()[0]
fc = "path/to/poly.shp"
# add layer
# lyr = arcpy.management.MakeFeatureLayer(fc, os.path.basename(fc)[:-4])
lyr = m.addDataFromPath(fc)
lyt = aprx.listLayouts("Layout")[0]
# This assumes that the layout has exactly one map frame (or that the map frame you want to change is the first map frame)
mapFrame = lyt.listElements("mapframe_element")[0]
# Set extent to layer, then save!
# using 'camera' not 'defaultCamera' for the map frame
mapFrame.camera.setExtent(arcpy.Describe(lyr).extent)
lyt.exportToJPEG("./temp.jpg")

If this does not resolve the issue, then you need to be a lot more specific regarding what the problem actually is.

1
  • Thanks alot for your answer, it works with no errors! I really appreciate it.
    – Clouseau
    Sep 28, 2023 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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