Hi Folks, need help to sort my first script I have written in python (arcpy, sqlite3). Basically I am trying to update x y fields of a non-spatial sqlite3 database table (contains about 10 million records) with point geometries (SHAP@X and SHAPE@Y) values from one of the point feature layer (about 20000-30000 records), where IDs (Station_Value) match in both point feature layer and target sqlite3 database table.
The script I have written will run but take about 40 minutes to update about 20000 records. I can't really figure it out why? hope someone can help to improve it, your help in this regard will be much appreciated. Below is a sample of my script:
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">customErr</SPAN><SPAN class="punctuation token">(</SPAN>Exception<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">pass</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> sqlite3<SPAN class="punctuation token">,</SPAN> traceback
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Get parameters</SPAN>
inputFC <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>
dbName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
tblName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
fldStn <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN>
fldX <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN>
fldY <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Check FC is point type, has ID field.</SPAN>
inDesc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>inputFC<SPAN class="punctuation token">)</SPAN><SPAN class="comment token"># Get description of FC</SPAN>
<SPAN class="keyword token">if</SPAN> inDesc<SPAN class="punctuation token">.</SPAN>shapeType<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="string token">"point"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">raise</SPAN> customErr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Data must be point type"</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> inDesc<SPAN class="punctuation token">.</SPAN>hasOID<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">raise</SPAN> customErr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Data must have an ID field"</SPAN>
<SPAN class="comment token"># Get reference to active data frame</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation 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>
df <SPAN class="operator token">=</SPAN> mxd<SPAN class="punctuation token">.</SPAN>activeDataFrame
arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Updating Database..."</SPAN><SPAN class="punctuation token">)</SPAN>
cnGP <SPAN class="operator token">=</SPAN> sqlite3<SPAN class="punctuation token">.</SPAN>connect<SPAN class="punctuation token">(</SPAN>dbName<SPAN class="punctuation token">)</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Station_Value'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SHAPE@X'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SHAPE@Y'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>inputFC<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN>cursor<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
Station_Value <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
coordX <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
coordY <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
strSQL <SPAN class="operator token">=</SPAN> <SPAN class="string token">'UPDATE {0} SET {3} = {5}, {4} = {6} WHERE {1} in ({2})'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>tblName<SPAN class="punctuation token">,</SPAN> fldStn<SPAN class="punctuation token">,</SPAN>Station_Value<SPAN class="punctuation token">,</SPAN>fldX<SPAN class="punctuation token">,</SPAN>fldY<SPAN class="punctuation token">,</SPAN>coordX<SPAN class="punctuation token">,</SPAN>coordY<SPAN class="punctuation token">)</SPAN>
cnGP<SPAN class="punctuation token">.</SPAN>execute<SPAN class="punctuation token">(</SPAN>strSQL<SPAN class="punctuation token">)</SPAN>
cnGP<SPAN class="punctuation token">.</SPAN>commit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
cnGP<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> customErr<SPAN class="punctuation token">,</SPAN> msg<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>msg<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> ErrorDesc<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>ErrorDesc<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN>traceback<SPAN class="punctuation token">.</SPAN>format_exc<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">finally</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">del</SPAN> inputFC<SPAN class="punctuation token">,</SPAN> DbName<SPAN class="punctuation token">,</SPAN> tblName<SPAN class="punctuation token">,</SPAN> fldStn <SPAN class="punctuation token">,</SPAN> fldX<SPAN class="punctuation token">,</SPAN> fldY<SPAN class="punctuation token">,</SPAN> mxd<SPAN class="punctuation token">,</SPAN> df
<SPAN class="keyword token">del</SPAN> planList<SPAN class="punctuation token">,</SPAN> inDesc<SPAN class="punctuation token">,</SPAN> inputFC
<SPAN class="keyword token">del</SPAN> conn<SPAN class="punctuation token">,</SPAN> strSQL
arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"in_memory"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">pass</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>Note: I am working in ArcGIS/Arcview Basic version 10.6.