If i have the extents for a layer how do i get ArcGIS to center the drawing

252
5
Jump to solution
01-08-2020 05:28 AM
RickHawkins
New Contributor II

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.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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.

View solution in original post

0 Kudos
5 Replies
HannesZiegler
Esri Contributor

Hi Rick,

This may work for you:

df.panToExtent(df.getSelectedExtent())

You can read more here: DataFrame—Help | ArcGIS for Desktop 

JoshuaBixby
MVP Esteemed Contributor

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?

0 Kudos
RickHawkins
New Contributor II

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

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

View solution in original post

0 Kudos
RickHawkins
New Contributor II

Thanks for the help. Appreciate it.

0 Kudos