Hello! I have a series of tools that I have written for a python toolbox. The idea is that we can take a variety of types of sources of point data and load it into a single geodatabase. I have been using this pretty effectively up until recently and I'm not sure what is happening. The only change I have made recently is to add an attribute index to the geodatabase for quicker searching.
The basics of the script is that it takes the most recent data and gets its source date and it's extents. The extents are either provided or calculated using Bounding Geometry. Once I have those two layers, it then queries the database first to find all data within the bounding polygon and then to find any of that selected data that is older. It then deletes the older data within the bounding polygon. It is these two steps where it is not working as expected. Even when I know there is data within the bounding polygon, it is saying it didn't find any data in the Select By Location.
The script is written so that it can take a number of input files and iterate through them. It seems to work for the first one but then returns no selection on following iterations. The only thing that I can think is that when I have loaded in the previous data it has somehow messed up the index and destroyed future searches.
Any ideas on what to try would be helpful!
Here's some of the code:
In the execute portion:
<SPAN class="comment token"># Get the SurveyJob file</SPAN>
fcSurveyJob <SPAN class="operator token">=</SPAN> gdb <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\SurveyJob"</SPAN> <SPAN class="comment token">#this has the extents of the point data</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Exists<SPAN class="punctuation token">(</SPAN>fcSurveyJob<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"No SurveyJob feature class was found...skipping file..."</SPAN><SPAN class="punctuation token">)</SPAN>
intCurJob <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">continue</SPAN> <SPAN class="comment token"># go to the next gdb</SPAN>
<SPAN class="comment token"># Get the SurveyPoints file</SPAN>
fcSurveyPoint <SPAN class="operator token">=</SPAN> gdb <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\SurveyPoint"</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Exists<SPAN class="punctuation token">(</SPAN>fcSurveyPoint<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"No SurveyPoint feature class was found...skipping file..."</SPAN><SPAN class="punctuation token">)</SPAN>
intCurJob <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">continue</SPAN> <SPAN class="comment token"># go to the next gdb</SPAN>
<SPAN class="comment token"># Delete Points within SurveyJob that are older than current data </SPAN>
sordat <SPAN class="operator token">=</SPAN> helper<SPAN class="punctuation token">.</SPAN>GetSORDATfromEhydro<SPAN class="punctuation token">(</SPAN>fcSurveyJob<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#SORDAT = Source Date as a Date type</SPAN>
<SPAN class="keyword token">if</SPAN> overallSORDAT <SPAN class="operator token"><</SPAN> sordat<SPAN class="punctuation token">:</SPAN> overallSORDAT <SPAN class="operator token">=</SPAN> sordat
helper<SPAN class="punctuation token">.</SPAN>DeletePointsWithinJob<SPAN class="punctuation token">(</SPAN>fcSurveyJob<SPAN class="punctuation token">,</SPAN> lyrUSACE<SPAN class="punctuation token">,</SPAN> long<SPAN class="punctuation token">(</SPAN>sordat<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%Y%m%d"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># data is stored in the geodatabase as a long</SPAN>
<SPAN class="comment token"># Import points</SPAN>
helper<SPAN class="punctuation token">.</SPAN>ImportPoints<SPAN class="punctuation token">(</SPAN>fileTemp<SPAN class="punctuation token">,</SPAN> lyrUSACE<SPAN class="punctuation token">,</SPAN> sorind<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>and my helper class:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">DeletePointsWithinJob</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> TempBoundingPolygon<SPAN class="punctuation token">,</SPAN> USACELyr<SPAN class="punctuation token">,</SPAN> sordat<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Select OutputFC within current area</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n -Deleting older points within area of new data..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Selecting Features within bounding polygon "</SPAN> <SPAN class="operator token">+</SPAN> TempBoundingPolygon <SPAN class="operator token">+</SPAN> <SPAN class="string token">"..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -SORDAT="</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>sordat<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
originalLyr <SPAN class="operator token">=</SPAN> USACELyr
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
USACELyr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>USACELyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"OutputLayer_lyr"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># arcpy.SelectLayerByLocation_management("OutputLayer_lyr", "WITHIN", eHydroSurveyJob)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error"</SPAN><SPAN class="punctuation token">)</SPAN>
USACELyr <SPAN class="operator token">=</SPAN> originalLyr
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN>USACELyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WITHIN"</SPAN><SPAN class="punctuation token">,</SPAN> TempBoundingPolygon<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Found "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>USACELyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" points within Bounding Polygon"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Select from that selection</SPAN>
strSelection <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SORDAT < "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>sordat<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>USACELyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SUBSET_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> strSelection<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Selection string= "</SPAN> <SPAN class="operator token">+</SPAN> strSelection<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Found "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>USACELyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" older points"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#... more code</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">ImportPoints</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> eHydroFC<SPAN class="punctuation token">,</SPAN> OutputFC<SPAN class="punctuation token">,</SPAN> SORIND<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n -Copying data to "</SPAN> <SPAN class="operator token">+</SPAN> OutputFC<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Setting SORIND to: "</SPAN> <SPAN class="operator token">+</SPAN> SORIND<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># open a search cursor on the file</SPAN>
inputFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"SHAPE@XY"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Z_depth"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SurveyDateStamp"</SPAN><SPAN class="punctuation token">]</SPAN>
outputSR <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>OutputFC<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
inputRows <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>eHydroFC<SPAN class="punctuation token">,</SPAN> inputFields<SPAN class="punctuation token">,</SPAN> spatial_reference<SPAN class="operator token">=</SPAN>outputSR<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># get input cursor</SPAN>
outputFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"SHAPE@XY"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DEPTH_M"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SORDAT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SORIND"</SPAN><SPAN class="punctuation token">]</SPAN>
outputRows <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>OutputFC<SPAN class="punctuation token">,</SPAN> outputFields<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set Progressor</SPAN>
intCount <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>eHydroFC<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" -Copying "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>intCount<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" features."</SPAN><SPAN class="punctuation token">)</SPAN>
newProgressor <SPAN class="operator token">=</SPAN> MyProgressor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"step"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Copying input features..."</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> intCount<SPAN class="punctuation token">)</SPAN>
curCount <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="comment token"># Start copying</SPAN>
<SPAN class="keyword token">for</SPAN> inputRow <SPAN class="keyword token">in</SPAN> inputRows<SPAN class="punctuation token">:</SPAN>
inputDate <SPAN class="operator token">=</SPAN> inputRow<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
SORDAT <SPAN class="operator token">=</SPAN> long<SPAN class="punctuation token">(</SPAN>inputDate<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%Y%m%d"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
outputRows<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>inputRow<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>inputRow<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">3.28084</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> SORDAT<SPAN class="punctuation token">,</SPAN> SORIND<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
newProgressor<SPAN class="punctuation token">.</SPAN>StepProgressor<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
curCount <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">if</SPAN> curCount <SPAN class="operator token">%</SPAN> <SPAN class="number token">1000</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
newProgressor<SPAN class="punctuation token">.</SPAN>SetProgressorLabel<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Copying input features...est. time remaining: "</SPAN> <SPAN class="operator token">+</SPAN> newProgressor<SPAN class="punctuation token">.</SPAN>EstimatedTimeRemaining<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n -Importing complete."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> inputRows
<SPAN class="keyword token">del</SPAN> outputRows
<SPAN class="keyword token">return</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>I'm deleting the insertcursors properly and this was working perfectly right up until I added the attribute index on SORDAT. Do I need to rebuild the indices? If so, there wouldn't be much point in having them as that would take longer each time than the search would without it.
Thanks!
python toolboxes