Hi,
How do I get the extents that are visible in the viewing pane. In old arcpy it would've been:
Any help greatly appreciated.
James
#data frame extent #graphics extent knowni_coop
Something along the lines of:
map_name <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># name of map to get extent object</SPAN> aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> mp <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN>map_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> ext <SPAN class="operator token">=</SPAN> mp<SPAN class="punctuation token">.</SPAN>defaultCamera<SPAN class="punctuation token">.</SPAN>getExtent<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
If you are interested in layouts instead of maps, there is a listLayouts() method to call.
Cheers Joshua,
I've found that saving the .aprx before the rest of the above code is essential as it seems to define where the "camera's" location.
I will have to take a closer look at that behavior. There either might be different code that doesn't require the APRX to be saved first, or possibly it is a bug.
Joshua,
This issue of having to save the project in order to get the current map view extents is almost three years old. Are there any plans to fix this?
aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> aprx<SPAN class="punctuation token">.</SPAN>save<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This does not work for this either, you have to physically save the map first.
Joshua Bixby
Also if I move around in the Map View and then switch to the Layout View and I physically click to save the project the script to get the Map View extents does not work. If I physically click save in the Map View the script will work.
Here is the entire script.
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="comment token"># Use the current arcgis pro project</SPAN> aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># List the map</SPAN> m <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> aprx<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># this is not saving the project????</SPAN> layout <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listLayouts<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Template_8_5x11_Layout"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> frame <SPAN class="operator token">=</SPAN> layout<SPAN class="punctuation token">.</SPAN>listElements<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"MAPFRAME_ELEMENT"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> ext <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>defaultCamera<SPAN class="punctuation token">.</SPAN>getExtent<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> frame<SPAN class="punctuation token">.</SPAN>camera<SPAN class="punctuation token">.</SPAN>setExtent<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">)</SPAN> increment <SPAN class="operator token">=</SPAN> <SPAN class="number token">300</SPAN> currenscale <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>frame<SPAN class="punctuation token">.</SPAN>camera<SPAN class="punctuation token">.</SPAN>scale<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="number token">300</SPAN> <SPAN class="comment token">#arcpy.AddMessage("current " + str(currenscale))</SPAN> newscale <SPAN class="operator token">=</SPAN> currenscale <SPAN class="operator token">-</SPAN> <SPAN class="punctuation token">(</SPAN>currenscale <SPAN class="operator token">%</SPAN> increment<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#arcpy.AddMessage("NEW " + str(newscale))</SPAN> frame<SPAN class="punctuation token">.</SPAN>camera<SPAN class="punctuation token">.</SPAN>scale <SPAN class="operator token">=</SPAN> newscale <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
# zoom to selected features import arcpy p = arcpy.mp.ArcGISProject('CURRENT') m = p.listMaps()[0] mv = p.activeView # part of trail selected lay = m.listLayers('trail')[0] extent = mv.getLayerExtent(lay, True, True) mv.zoomToAllLayers(True,True)
Maybe this function was not available in 2017, but it is now in 2021 in version 2.8 or earlier.
The trick is to realise that the extents are available in the MapView of the active Map. While looking for this I tried out using Bookmarks which are also available, maybe a nice way to store the extent of selections.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.