I'm trying to set the following coordinates to a dataframe:
custom_extent = {1:-87.678956, 2: -87.611652, 3:41.867129, 4:41.954932}
where `1` is key for `XMin`, `2` is key for `XMax`, `3` is key for `YMin`, and `4` is key for `YMax`
However, when I try to set the new extent up, the coordinates are not the results I expect. Here's a code snippet:
print data_frame.extent.XMin # prints 480718.855003
original_spatial_reference = data_frame.spatialReference
newExtent = data_frame.extent
data_frame.spatialReference = arcpy.SpatialReference("WGS 1984")
print data_frame.extent.XMin # prints -87.6986700746
newExtent.XMin, newExtent.XMax = custom_extent[1], custom_extent[2]
newExtent.YMin, newExtent.YMax = custom_extent[3], custom_extent[4]
data_frame.extent = newExtent
print data_frame.extent.XMin # prints -93.500018925
data_frame.spatialReference = original_spatial_reference
print data_frame.extent.XMin # prints -2.38749449402
Every printout does not match the desired `XMin`: `-87.678956`. Any thoughts?
Try building the Extent object from scratch instead of reconstructing one you get from the DataFrame object. If I understand what you are expecting for results, the following code works for me in the interactive Python window in ArcMap:
custom_extent = {'XMin':-87.678956, 'XMax':-87.611652, 'YMin':41.867129, 'YMax':41.954932} newExtent = arcpy.Extent(**custom_extent) print data_frame.extent.XMin data_frame.spatialReference = arcpy.SpatialReference("WGS 1984") print data_frame.extent.XMin data_frame.extent = newExtent print data_frame.extent.XMin
If you want to use a dictionary to store an extent's bounds, may I suggest using key names that correspond to function parameters. That way, you can just pass the dictionary as keyword arguments to an Extent function.
Update: It dawned on me after posting the code above why your original code might not be working. I believe it fails because you change the spatial reference of the data frame after getting the extent object from the data frame. If you change the spatial reference first, it seems to work.
custom_extent = {1:-87.678956, 2: -87.611652, 3:41.867129, 4:41.954932} print data_frame.extent.XMin data_frame.spatialReference = arcpy.SpatialReference("WGS 1984") print data_frame.extent.XMin newExtent = data_frame.extent newExtent.XMin, newExtent.XMax = custom_extent[1], custom_extent[2] newExtent.YMin, newExtent.YMax = custom_extent[3], custom_extent[4] data_frame.extent = newExtent print data_frame.extent.XMin