I have the following code:
import arcpy
#mxd = arcpy.mapping.MapDocument("CURRENT")
mxd = arcpy.mapping.MapDocument(r"C:/GISMAPS/LUS Map.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
OHLayer = arcpy.mapping.ListLayers(mxd, "Primary OH", df)[0]
df.extent = OHLayer.getSelectedExtent(False)
#df.scale = df.scale * 1.1
extent = ({df.extent.XMin}, {df.extent.YMin}, {df.extent.XMax}, {df.extent.YMax})
print('XMin: {}, YMin: {}'.format(df.extent.XMin, df.extent.YMin))
print('XMax: {}, YMax: {}'.format(df.extent.XMax, df.extent.YMax))
arcpy.RefreshActiveView()
del mxd
This is the output for lines 12 and 13
How do i center this drawing so that section of map is displayed on the screen? I have the extents but do not know how to use them to do this.
Thanks for any help.
Solved! Go to Solution.
The df.extent shows just that, the extent of the data frame itself regardless of what data layers are loaded and what data is selected. That said, df.extent is also writable, so you can set the property to change the extent of the dataframe. This is why I said Line 08 of your earlier code should both center and zoom the dataframe to the selected feature in the layer.
Hi Rick,
This may work for you:
df.panToExtent(df.getSelectedExtent())
You can read more here: DataFrame—Help | ArcGIS for Desktop
Line 08 of your code
df.extent = OHLayer.getSelectedExtent(False)
should both center and zoom the dataframe to the extent of the selected object. I just tested it again, and it works. Do you only want to center and not zoom?
yes i would like to zoom also. Another question that i had is what does the df.extent show. For example in the table of contents it shows the layer Primary OH with individual feeders shown below. The user will select 1 feeder to show on the display and i would want to use the extents for the feeder. So does df.extent show for the layer or can it show just the extents for the feeder selected?
Thanks for the help
The df.extent shows just that, the extent of the data frame itself regardless of what data layers are loaded and what data is selected. That said, df.extent is also writable, so you can set the property to change the extent of the dataframe. This is why I said Line 08 of your earlier code should both center and zoom the dataframe to the selected feature in the layer.
Thanks for the help. Appreciate it.