Select to view content in your preferred language

How do I set the map in a layout mapframe with python in Arc Pro?

2138
12
08-03-2022 07:08 AM
by Anonymous User
Not applicable
 
0 Kudos
12 Replies
jcarlson
MVP Esteemed Contributor

Even if it's the same text, it's a good practice to put your question in both the title and body of your posts here.

What you're looking for is Camera.setExtent(), which you can read up on here: https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/camera-class.htm

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Hi Josh

 

Thanks for pointer around etiquette.

I have an empty map frame in a layout that I want to set the map view in and then zoom to the extent of one of the layers. I can't seem to get off the ground with this one or find much help on the web. Any assistance would be appreciated. Seems like it a fairly simple standard thing to want to do?

 

Matt

0 Kudos
jcarlson
MVP Esteemed Contributor

I'd start with the Introduction to arcpy.mp article. There are some linked tutorials that can walk you through some of the simpler procedures.

Basically, you'd use something like this:

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')

# get your layout
lyt = aprx.listLayouts('Your Layout Name')[0]

# get the map frame
mf = lyt.listElements('MAPFRAME_ELEMENT', 'your map frame name')[0] # note that the name of the frame is NOT the name of the map

# get specific layer you want
lyr = mf.map.listLayers('Your Layer Name')[0]

# set frame extent to match layer
mf.camera.setExtent(mf.getLayerExtent(lyr))

 

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Hi Josh

That is what I ran and I receive the following error:

RuntimeError Traceback (most recent call last)
In [74]:
Line 7: mf.camera.setExtent(mf.getLayerExtent(lyr))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))

RuntimeError:

0 Kudos
jcarlson
MVP Esteemed Contributor

Try running mf.getLayerExtent(lyr) on its own line? Maybe explicitly state the optional parameters, too.

mf.getLayerExtent(lyr, False, False)
- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

RuntimeError Traceback (most recent call last)
In [88]:
Line 7: mf.getLayerExtent(lyr, False, False)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))

RuntimeError:

0 Kudos
jcarlson
MVP Esteemed Contributor

Double check the value of the variables, make sure the lyr, lyt, and mf are the objects they're supposed to be.

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

aprx = arcpy.mp.ArcGISProject("CURRENT")

m = aprx.listMaps("Map")[0]
lyt = aprx.listLayouts("Layout1")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame 1")[0]
lyr = m.listLayers("Osterley")[0]
mf.getLayerExtent(lyr,False, False)

 

RuntimeError Traceback (most recent call last)
In [105]:
Line 7: mf.getLayerExtent(lyr,False, False)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))

RuntimeError:
---------------

0 Kudos
jcarlson
MVP Esteemed Contributor

I mean try running a cell that's just "m" and see what is returned, make sure they're actually layouts, layers, etc.

- Josh Carlson
Kendall County GIS
0 Kudos