Help with MapFrame panToExtent in ArcGIS Pro 2.9

647
4
Jump to solution
07-28-2023 01:39 PM
Shauna-RaeBrown
Occasional Contributor

I've written an arcpy script that pulls coordinate data from a saved email/text file.  I've also added the polygon created into a feature class, and used a definition query on the layer so that the new feature is the only one visible in the map.

What I'm trying to do is pan the layout's map frame to the new extent of the layer.  The following script works (i.e. retains the current scale of the map frame, and pans to the layer), but it throws the included error.  I'm running my script in ArcGIS Pro 2.9.

I've already read the MapFrame and Camera classes

 https://pro.arcgis.com/en/pro-app/2.9/arcpy/mapping/mapframe-class.htm 

https://pro.arcgis.com/en/pro-app/2.9/arcpy/mapping/camera-class.htm 

I can't find ANY examples of how to use the MapFrame panToExtent method.  Please help.

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.panToExtent(mf.getLayerExtent(lyr, False, True)))
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 389, in setExtent
    return convertArcObjectToPythonObject(self._arc_object.setExtent(*gp_fixargs((extent,), True)))
TypeError: Camera.setExtent() missing required argument 'extent' (pos 1)

 

0 Kudos
1 Solution

Accepted Solutions
Shauna-RaeBrown
Occasional Contributor

@Anonymous User  Thanks for your suggestions.  I FINALLY figured it out.  I set the mf.camera.scale.

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
mf.camera.scale = 25200.0

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable

I don't think the

mf.panToExtent(lyrExt)

returns anything so nesting it into the setExtent method is passing it None.

try separating them and passing in the extent.

lyrExt = mf.getLayerExtent(lyr, False, True)
mf.panToExtent(lyrExt)
mf.camera.setExtent(lyrExt)

 

0 Kudos
Shauna-RaeBrown
Occasional Contributor

Thanks for your help @Anonymous User .  Unfortunately, when I tried your suggestion...

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
lyrExt = mf.getLayerExtent(lyr, False, True)
mf.panToExtent(lyrExt)
mf.camera.setExtent(lyrExt)

 I found that it works the same as...

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))

I'm trying to stay at a scale of 1:25,200, but the both of these scripts zoom to the feature's/layer's extent.

If I figure it out, I'll post my corrected script.

0 Kudos
by Anonymous User
Not applicable

Try setting the symbolized_extent to False so it works off the geometry?

mf.getLayerExtent(lyr, False, False)

 

0 Kudos
Shauna-RaeBrown
Occasional Contributor

@Anonymous User  Thanks for your suggestions.  I FINALLY figured it out.  I set the mf.camera.scale.

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
mf.camera.scale = 25200.0
0 Kudos