The code below seems to function correctly most of the time except when running through a situation where line 43 is False (no extent updates based on this layer) and Line 51 is True (1+ feature in this layer), meaning Line 54 is also True (extent set at Line 22 should still match extent at Line 52).
What I'm not understanding is 2 things:
1. Why is it running through the elif at Line 61 when the condition should be False (as per Line 54 being True)
2. Why when it does run through the elif at Line 61 I get the below Trackback error about the list Index being out of range when it works elsewhere in the same manner?
Hope that all makes sense!
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env<SPAN class="punctuation token">,</SPAN> mapping
<SPAN class="keyword token">from</SPAN> bisect <SPAN class="keyword token">import</SPAN> bisect
<SPAN class="comment token">#input parameters;</SPAN>
Plantation <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
ForestID <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
CommonName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
PYear <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN>
OpCode <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#variables;</SPAN>
title1 <SPAN class="operator token">=</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">" PLANTATION"</SPAN>
title2 <SPAN class="operator token">=</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\r\nPLANTATION"</SPAN>
title3 <SPAN class="operator token">=</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">" ("</SPAN> <SPAN class="operator token">+</SPAN> CommonName <SPAN class="operator token">+</SPAN> <SPAN class="string token">")"</SPAN>
title4 <SPAN class="operator token">=</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">" P"</SPAN> <SPAN class="operator token">+</SPAN> PYear <SPAN class="operator token">+</SPAN> <SPAN class="string token">" AREA STATEMENT"</SPAN>
mxd <SPAN class="operator token">=</SPAN> mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
elements <SPAN class="operator token">=</SPAN> mapping<SPAN class="punctuation token">.</SPAN>ListLayoutElements<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN>
layers <SPAN class="operator token">=</SPAN> mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN>
HTL_layer_list <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Entered In GeoMaster"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Entered In GeoMaster Archive"</SPAN><SPAN class="punctuation token">]</SPAN>
dflist <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
orig_ext <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent
new_exp <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PLANTATION = '"</SPAN> <SPAN class="operator token">+</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
new_sur_exp <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PLANTATION <> '"</SPAN> <SPAN class="operator token">+</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
as_new_exp <SPAN class="operator token">=</SPAN> <SPAN class="string token">"EnteredInGeoMaster IS NOT NULL AND Plantation = '"</SPAN> <SPAN class="operator token">+</SPAN> Plantation <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
opcode_exp1 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Entered_In_GeoMaster IS NOT NULL AND Ops_Code = '"</SPAN> <SPAN class="operator token">+</SPAN> OpCode <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
opcode_exp2 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Ops_Code = '"</SPAN> <SPAN class="operator token">+</SPAN> OpCode <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
scalelist <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">5000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7500</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">10000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">12500</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">20000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">25000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">40000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">50000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">60000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">70000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">75000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">80000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">90000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">100000</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> mxd<SPAN class="punctuation token">.</SPAN>title <SPAN class="operator token"><></SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Cutting Returns Entered in GeoMaster"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">###other code here###</SPAN>
<SPAN class="keyword token">elif</SPAN> mxd<SPAN class="punctuation token">.</SPAN>title <SPAN class="operator token">==</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Cutting Returns Entered in GeoMaster"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>activeView <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Page_Layout"</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> layers<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Entered In GeoMaster"</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> opcode_exp1
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" definition query updated"</SPAN><SPAN class="punctuation token">)</SPAN>
EIGfeature_count <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> EIGfeature_count <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
ext <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>getExtent<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#gets the new extent of the Entered In GeoMaster layer</SPAN>
dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="operator token">=</SPAN> ext <SPAN class="comment token">#applies the Entered In GeoMaster layer extent to the dataframe</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Entered In GeoMaster Archive"</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> opcode_exp2
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" definition query updated"</SPAN><SPAN class="punctuation token">)</SPAN>
EIGAfeature_count <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> EIGAfeature_count <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
current_ext <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent
<SPAN class="keyword token">if</SPAN> current_ext <SPAN class="operator token">==</SPAN> orig_ext<SPAN class="punctuation token">:</SPAN>
ext <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>getExtent<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#gets the new extent of the Entered In GeoMaster layer</SPAN>
dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="operator token">=</SPAN> ext <SPAN class="comment token">#applies the Entered In GeoMaster layer extent to the dataframe</SPAN>
oldscale <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>scale <SPAN class="comment token"># Get scale of dataframe</SPAN>
newscale <SPAN class="operator token">=</SPAN> scalelist<SPAN class="punctuation token">[</SPAN>bisect<SPAN class="punctuation token">(</SPAN>scalelist<SPAN class="punctuation token">,</SPAN> oldscale<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># bisect old scale to get new scale</SPAN>
dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>scale <SPAN class="operator token">=</SPAN> newscale <SPAN class="comment token"># Apply new scale to dataframe</SPAN>
<SPAN class="keyword token">elif</SPAN> current_ext <SPAN class="operator token"><></SPAN> orig_ext<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># get current map extent</SPAN>
xmin<SPAN class="punctuation token">,</SPAN> xmax <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMax
ymin<SPAN class="punctuation token">,</SPAN> ymax <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">,</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMax
<SPAN class="comment token"># loop through def query layer extents and create one extent to fit them all</SPAN>
lyr_ext <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>getExtent<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>XMin <SPAN class="operator token"><</SPAN> xmin<SPAN class="punctuation token">:</SPAN>
xmin <SPAN class="operator token">=</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>XMin
<SPAN class="keyword token">if</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>YMin <SPAN class="operator token"><</SPAN> ymin<SPAN class="punctuation token">:</SPAN>
ymin <SPAN class="operator token">=</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>YMin
<SPAN class="keyword token">if</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>XMax <SPAN class="operator token">></SPAN> xmax<SPAN class="punctuation token">:</SPAN>
xmax <SPAN class="operator token">=</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>XMax
<SPAN class="keyword token">if</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>YMax <SPAN class="operator token">></SPAN> ymax<SPAN class="punctuation token">:</SPAN>
ymax <SPAN class="operator token">=</SPAN> lyr_ext<SPAN class="punctuation token">.</SPAN>YMax
<SPAN class="comment token"># set df extent to new extent</SPAN>
dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Extent<SPAN class="punctuation token">(</SPAN>xmin<SPAN class="punctuation token">,</SPAN> ymin<SPAN class="punctuation token">,</SPAN> xmax<SPAN class="punctuation token">,</SPAN> ymax<SPAN class="punctuation token">)</SPAN>
oldscale <SPAN class="operator token">=</SPAN> dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>scale <SPAN class="comment token"># Get scale of dataframe</SPAN>
newscale <SPAN class="operator token">=</SPAN> scalelist<SPAN class="punctuation token">[</SPAN>bisect<SPAN class="punctuation token">(</SPAN>scalelist<SPAN class="punctuation token">,</SPAN> oldscale<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># bisect old scale to get new scale</SPAN>
dflist<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>scale <SPAN class="operator token">=</SPAN> newscale <SPAN class="comment token"># Apply new scale to dataframe</SPAN>
<SPAN class="keyword token">elif</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"PlantationsHarvestPlan_OperationsPlanner_WA"</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> new_exp
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" definition query updated"</SPAN><SPAN class="punctuation token">)</SPAN><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></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><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></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>Traceback (most recent call last):
File "W:\Mapping\ToolboxPythonCodes\SCRIPTS\Update Definition Queries.py", line 78, in <module>
newscale = scalelist[bisect(scalelist, oldscale)] # bisect old scale to get new scale
IndexError: list index out of range
For reference, the aim here is to updated the definition queries on 2 layers in an open MXD, check for features in each layer and zoom the extent to view all the features in both layers before stepping the scale up (using bisect) to the next round scale in the list.