it's weird that the arcpy.Append_management function does not have any arguments for SQL Expressions.
Does anyone know a way around this ?
Below is my code. Basically I want to take a feature layer with a sql statement and put it in a sde.
Would I have to use arcpy.MakeFeatureLayer_management() then append?
And I want to ensure that the expression only grabs 1 feature, if there are more than 1 features trying to append it will fail. Would I do this in the validation or could I do this in the script ?
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> sys
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
scratchLoc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchFolder
cs<SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="number token">102003</SPAN><SPAN class="punctuation token">)</SPAN>
xmlLoc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"\\......_Scripts\KMZtoInvBdy\INV_BDY.XML"</SPAN>
gdbname <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"prjbdy_scratch.gdb"</SPAN>
fcLoc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"\prjbdy_scratch.gdb\inv_project_boundary"</SPAN>
cntylyr <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"......\Default.gdb\AA_counties_SpatialJoin"</SPAN>
mxd <SPAN class="operator token">=</SPAN> <SPAN class="string token">"CURRENT"</SPAN>
date <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
dateprj<SPAN class="operator token">=</SPAN> date<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%d/%m/%Y"</SPAN><SPAN class="punctuation token">)</SPAN>
fl <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
Expression <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>
<SPAN class="keyword token">if</SPAN> Expression <SPAN class="operator token">==</SPAN> '<SPAN class="comment token">#' or not Expression:</SPAN>
Expression <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># provide a default value if unspecified</SPAN>
Project_Name <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>
Project_Status <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>
<SPAN class="keyword token">if</SPAN> Project_Status <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Operating"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Status <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OPE"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Status <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Prospecting"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Status <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PRO"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Status <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Development"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Status <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DEV"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Status <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Interconnection Area"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Status <SPAN class="operator token">=</SPAN> <SPAN class="string token">"INT"</SPAN>
Project_Tech <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>
<SPAN class="keyword token">if</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Wind Farm"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"WND"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Solar Plant"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SOL"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Thermal Plant"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"THM"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Battery Storage"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"BAT"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Co-Generation"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"COG"</SPAN>
<SPAN class="keyword token">elif</SPAN> Project_Tech <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Desalinization Plant"</SPAN><SPAN class="punctuation token">:</SPAN>
Project_Tech <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DES"</SPAN>
Primary_Expasion <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="keyword token">if</SPAN> Primary_Expasion <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Primary"</SPAN><SPAN class="punctuation token">:</SPAN>
Primary_Expasion <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">elif</SPAN> Primary_Expasion <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Expansion"</SPAN><SPAN class="punctuation token">:</SPAN>
Primary_Expasion <SPAN class="operator token">=</SPAN> <SPAN class="number token">2</SPAN>
MapFeature <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> MapFeature <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Display"</SPAN><SPAN class="punctuation token">:</SPAN>
MapFeature <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">elif</SPAN> MapFeature <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Do Not Display"</SPAN><SPAN class="punctuation token">:</SPAN>
MapFeature <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CreateFileGDB_management<SPAN class="punctuation token">(</SPAN>scratchLoc<SPAN class="punctuation token">,</SPAN>gdbname<SPAN class="punctuation token">)</SPAN>
gdbloc <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>scratchLoc<SPAN class="punctuation token">,</SPAN>gdbname<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ImportXMLWorkspaceDocument_management<SPAN class="punctuation token">(</SPAN>gdbloc<SPAN class="punctuation token">,</SPAN>xmlLoc<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Append_management<SPAN class="punctuation token">(</SPAN>fl<SPAN class="punctuation token">,</SPAN>scratchLoc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> gdbname <SPAN class="operator token">+</SPAN> r<SPAN class="string token">"\inv_project_boundary"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NO_TEST"</SPAN><SPAN class="punctuation token">,</SPAN>Expression<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#be able to import any type of feature eg.shapefile,fc,geojson </SPAN>
<SPAN class="comment token">#different conversions</SPAN>
<SPAN class="comment token">#Calculates Area</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddGeometryAttributes_management<SPAN class="punctuation token">(</SPAN>scratchLoc <SPAN class="operator token">+</SPAN> fcLoc<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"AREA_GEODESIC"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"ACRES"</SPAN><SPAN class="punctuation token">,</SPAN>cs<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>scratchLoc <SPAN class="operator token">+</SPAN> fcLoc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"prjbdy_Layer"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>cntylyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"cntlyr_Layer"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"cntlyr_Layer"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"INTERSECT"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"prjbdy_Layer"</SPAN><SPAN class="punctuation token">)</SPAN>
scur <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"cntlyr_Layer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> scur<SPAN class="punctuation token">:</SPAN>
cnty_name <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">.</SPAN>getValue<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"NAME"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
statename <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">.</SPAN>getValue<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"STATE_ABBR"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
scur2 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"prjbdy_Layer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> scur2<SPAN class="punctuation token">:</SPAN>
Acres <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">.</SPAN>getValue<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"AREA_GEO"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> scur
<SPAN class="keyword token">del</SPAN> scur2
<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">"***Can't find County***"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#arcpy.Append_management(scratchLoc + fcLoc, scratchLoc + "\\" + gdbname + r"\inv_project_boundary","NO_TEST")</SPAN>
ucur <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>scratchLoc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> gdbname <SPAN class="operator token">+</SPAN> r<SPAN class="string token">"\inv_project_boundary"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"PROJECT"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"PROJ_STATUS"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"TECH"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"MAP"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"PRIMARY_"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"DATE_REVISED"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"COUNTY"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"STATE"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"ACRES"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> ucur<SPAN class="punctuation token">:</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">=</SPAN> Project_Name
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> Project_Status
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> Project_Tech
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> MapFeature
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> Primary_Expasion
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> dateprj
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> cnty_name
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> statename
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> Acres
ucur<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> ucur
<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">"***Can't Add County***"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"***RESULTS***"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>Acres<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Revised Dated: "</SPAN> <SPAN class="operator token">+</SPAN> dateprj<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"County, State: "</SPAN> <SPAN class="operator token">+</SPAN> cnty_name <SPAN class="operator token">+</SPAN> <SPAN class="string token">", "</SPAN> <SPAN class="operator token">+</SPAN> statename<SPAN class="punctuation token">)</SPAN>
<SPAN class="string token">''</SPAN>'
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Adding to IS_MAIN.DBO.inv_project_boundary"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Append_management<SPAN class="punctuation token">(</SPAN>scratchLoc <SPAN class="operator token">+</SPAN>r<SPAN class="string token">"\kml_scratch.gdb"</SPAN> <SPAN class="operator token">+</SPAN> r<SPAN class="string token">"\inv_project_boundary"</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"..........._project_boundary"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Finished adding to"</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>