Select to view content in your preferred language

Zoom map to the extent of a layer in Arcpy.mp (ArcGIS Pro Project)

27112
23
Jump to solution
05-02-2017 01:08 PM
JamesTaylor3
Regular Contributor

I'm writing code to create new ArcGIS Pro Projects using the Pro arcpy  (package arcpy.mp)

Doing something like this:

import arcpy.mp
aprx = arcpy.mp.ArcGISProject(r"C:\temp\Empty.aprx")
map = aprx.listMaps()[0]
map.map.addDataFromPath(r"C:\Users\xxxxxx\Documents\Test1\Orthomosaic.tif")
aprx.saveACopy(r"C:\temp\jNew.aprx")

But I can't figure out to zoom the map to the extent of Orthomosaic.tif (or any other extent for that matter).

From the docs at   Alphabetical list of arcpy.mp classes—ArcPy | ArcGIS Desktop 

I can see that a MapFrame has a zoomToAllLayers method, but a MapFrame is only available on a Layout element.

Anyone know how to do this?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
JamesTaylor3
Regular Contributor

I just talked to an arcpy.mp developer and he said zooming on a Map View is not supported in arcpy.mp yet and probably will not be for awhile.

View solution in original post

23 Replies
IanMurray
Honored Contributor

I don't use ArcGIS Pro yet, but looking at the various helps, I would guess you would need to get the extent object of your layer and then apply that extent a Camera Object, which you can use to overwrite the default Camera object in your Map or Mapframe.

The Example on the Camera Help my point you in the right direction.

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

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

http://pro.arcgis.com/en/pro-app/arcpy/mapping/map-class.htm

JamesTaylor3
Regular Contributor

There's this statement:

Extent is not a direct property of the Camera object because it is not an explicit property but rather a derived property. Camera positions do not store extent properties, but from an X, Y location and a scale, extent is derived. The getExtent method will return a derived extent. When an extent is returned from a 3D map view, it is based on the camera position looking straight down (pitch=-90). When the setExtent method is used on a 3D map, again, the result will appear as if the camera is looking straight down.

And I have tried setting X,Y and Scale but they don't seem to take.  They always remain nan.

I have also tried zooming a layout and copying the camera from that to the Map.defaultCamera.   The extent on the map seems correct on the Map when you open the project, however ArcGIS Pro doesn't zoom the map to that extent when you open the project.

0 Kudos
IanMurray
Honored Contributor

Your last sentence seems to contradict itself, could you be a little clearer?

The Camera is the only thing now that controls extents and scales, since there is no longer a dataframe, so it seems like changing the Camera would be to go about this. 

From the help:

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")

m = aprx.listMaps("Yose*")[0]
lyr = m.listLayers("Ranger Stations")[0]
lyt = aprx.listLayouts("Main Attr*")[0]
mf = lyt.listElements("mapframe_element", "Yosemite National Park")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))

aprx.saveACopy(r"C:\Projects\YosemiteNP\Yosemite_Updated.aprx")
del aprx‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Have you tried setting the camera of both the map frame and map?

Also would you mind posting your code ths far you've been trying?

bixb0012

0 Kudos
JamesTaylor3
Regular Contributor

The example you listed above only zooms the Layout, not the Map that the Layout contains.

I have tried setting both the MapFrame and the Map.  That's what I was trying to say in the last sentence of my last post.

My code is essentially this:

import arcpy.mp
aprx = arcpy.mp.ArcGISProject(r"C:\temp\Empty.aprx")
map = aprx.listMaps()[0]

layout = aprx.listLayouts()[0]
map.map.addDataFromPath(r"C:\Users\xxxxxx\Documents\Test1\Orthomosaic.tif")

mapFrame = layout.listElements("MAPFRAME_ELEMENT")[0]

mapFrame.zoomToAllLayers()

map.defaultCamera = mapFrame.camera
aprx.saveACopy(r"C:\temp\New.aprx")

And the Map is added to the Layout in Empty.aprx before I run this code.

Now when I open New.aprx the map is NOT zoomed to the extent of all layers (the extent of Orthomasaic.tif), however if you look at the Map Properties its extent is set to the extent of all layers (the extent of Orthomasaic.tif).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In order to modify the extent, scale, etc... using ArcPy; you need to retrieve the camera of a mapframe, not the defaultCamera of a map.  Insert a layout, insert a mapframe tied to a map, and then you can set the extent, scale, etc...

(Example code will come later)

0 Kudos
JamesTaylor3
Regular Contributor

I uploaded my code, project and image for testing to here:

https://community.esri.com/docs/DOC-10001-zoom-to-a-layer-in-arcgis-pro-project-using-arcpymp

Here's my code:

import arcpy.mp

try:
 aprx = arcpy.mp.ArcGISProject(r"C:\Temp\ArcPro\Empty.aprx")
 
 map = aprx.listMaps()[0]
 print(map.name)
 
 layout = aprx.listLayouts()[0]
 mapframe = layout.listElements("MAPFRAME_ELEMENT")[0]
 print (mapframe.name)
 print (mapframe.map.name)

 ext = mapframe.camera.getExtent()
 print ("MapFrame Extent: " + ext.JSON)

 mapframe.map.addDataFromPath(r"C:\Temp\ArcPro\Orthomosaic.tif")
 mapframe.zoomToAllLayers()

 ext = mapframe.camera.getExtent()
 print ("MapFrame Extent: " + ext.JSON)

 mapext = map.defaultCamera.getExtent()
 print ("Map Extent: " + mapext.JSON)

 aprx.saveACopy(r"C:\Temp\ArcPro\New.aprx")

except Exception as err:
 print("Error Aprx. %s" % str(err))

Here's the output:

MyMap
My Map Frame
MyMap
MapFrame Extent: {"xmin":-13520478.935883986,"ymin":2728123.2144682845,"xmax":-10568557.934067367,"ymax":6815398.4477528343,"spatialReference":{"wkid":32632,"latestWkid":32632}}
MapFrame Extent: {"xmin":424855.68683000002,"ymin":5132283.3477261541,"xmax":425127.21863000002,"ymax":5132659.3148338469,"spatialReference":{"wkid":32632,"latestWkid":32632}}
Map Extent: {"xmin":424842.11024000001,"ymin":5132306.757046001,"xmax":425140.79522000003,"ymax":5132635.9055140009,"spatialReference":{"wkid":32632,"latestWkid":32632}}
Press any key to continue . . .

When you open New.aprx in ArcGIS Pro the Layout is zoomed to Orthomosaic.tif, but MyMap is not, even though the Map Extent looks correct and is the same as the MapFrame Extent.

by Anonymous User
Not applicable

{"xmin":-13520478.935883986,"ymin":2728123.2144682845,"xmax":-10568557.934067367,"ymax":6815398.4477528343,"spatialReference":{"wkid":32632,"latestWkid":32632}}


Do you know what units the map frame extent coordinates are in? I am having a similar issue. thank you 

0 Kudos
JamesTaylor3
Regular Contributor

I just talked to an arcpy.mp developer and he said zooming on a Map View is not supported in arcpy.mp yet and probably will not be for awhile.

MarkHammond
New Contributor

I was able to get this working on ArcGIS Pro 2.1.2 (but previous versions may work as well).  It seems to only work if I run it from the command line, not from within the Pro python window.  Here is the code in case anyone is trying to do something similar.

This code queries a layer for a feature with a specific ID, then zooms the mapframe to the extent of the feature and outputs a PDF.

import arcpy


project = arcpy.mp.ArcGISProject("//mapserver/blah/Pro-workforce2.aprx")
map = project.listMaps()[0]

whereZoom = "BlahID = '20181520528'"

layer = map.listLayers("Zoom")[0]
arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION",whereZoom)

lyt = project.listLayouts()[0]

mf = lyt.listElements('MAPFRAME_ELEMENT','*')[0]
mf.camera.setExtent(mf.getLayerExtent(layer,True,True))
mf.zoomToAllLayers()


lyt.exportToPDF("//mapserver/blah/test-%s.pdf" % whereZoom)