I am trying to zoom into a selected polygon as part of code for a custom print tool within AppBuilder. Below is my code. However, I am having trouble getting it to zoom into the selected polygon. I have ran into various errors, but the current error is below. Any ideas for resolving this?
Code:
# Part 3: Zoom into selected polygon
aprx = arcpy.mp.ArcGISProject("CURRENT")
# trying to zoom into sltPoly
mv = aprx.activeView # mapview
m = mv.map # map
lay = m.listLayers(sltPoly)
sltLyr = m.listLayers()[3]
for g in m.listLayers():
print("Layer: {0} Layers".format(g.name))
lyrExt = mv.getLayerExtent(sltLyr)
mv.camera.setExtent(lyrExt)
df.scale = 6000
arcpy.SelectLayerByAttribute_management(sltPoly, "CLEAR_SELECTION")
arcpy.Near_analysis(points_lyr, sltPoly)
Error (redacting was my own):
Error executing tool. Export Web Map : Traceback (most recent call last): File "**redacted**\dynamicprintservice\ExportWebMap.py", line 211, in <module> aprx = arcpy.mp.ArcGISProject("CURRENT") File "c:\program files\arcgis\server\framework\runtime\arcgis\Resources\arcpy\arcpy\_mp.py", line 337, in __init__ self._arc_object = arcgisscripting._mapping.ArcGISProject(*gp_fixargs((aprx_path,), True)) OSError: CURRENT Failed to execute (Export Web Map). Failed to execute (Export Web Map).
related
Solved: Why does arcpy.mp.ArcGISProject() give an OSError? - Esri Community
CURRENT can only be used from an open project and python being able to see the project (eg a custom or python tool in a toolbox, or the python IDE etc)
I did come across that thread, but it did not seem related because I am not referring to a toolbox as this user was in their code.
I actually am running the tool within an app with an open map via a print tool, so python "seeing" the project should not be a problem.
might be the case, but you could check by throwing in an arcpy.AddMessage and/or print statement after getting it... perhaps
aprx = arcpy.mp.ArcGISProject("CURRENT")
print(aprx.filePath)
related
I too am having problems with arcpy.mp.ArcGISProject("CURRENT")
As part of my code I use arcpy.mp.ArcGISProject("CURRENT") to detect whether a script is running within ArcGIS Pro or outside (i.e. from IDE or scheduled task). I notice that occasionally arcpy.mp.ArcGISProject("CURRENT") throws an OSError which is caught by a try ... except, despite running the script as a Python toolbox.
This will happen a couple of times, and then ArcGIS Pro will revert to its expected behaviour.