Estoy llamando a un script de python desde un script personalizado de lenguaje de macros de muy alto nivel. Dentro del script de python hay un arcpy.SpatialJoin_analysis()
de mando. Por alguna razón, si la salida de esta operación no está ya en la base de datos geográfica del espacio de trabajo (lo que significa que he eliminado la salida anterior antes de ejecutar el script), la unión espacial se ejecuta pero sólo devuelve 20 registros de 259. Si lo ejecuto por segunda vez, no se ejecuta debido a un bloqueo del esquema (error 000464). No puedo obtener el mensaje de error porque la ventana de python desaparece demasiado rápido. Si lo ejecuto desde la ventana de comandos de ArcMap python no aparece ningún mensaje de error. Tengo el script configurado para sobrescribir la salida y mis opciones de geoprocesamiento permiten sobrescribir geoprocesos anteriores.
import arcpy
print" Running Python Script"
# Set up workspace and mxd
workSpace = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb"
arcpy.env.workspace = workSpace
arcpy.env.overwriteOutput = True
try:
# Perform a spatial join so the new routes can be assigned to the service location layer
target = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb\Service_Location"
join = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb\Suburban_Rec"
outFC = "XX_Recycle_SPJ"
arcpy.SpatialJoin_analysis(target, join, outFC)
arcpy.MakeFeatureLayer_management(outFC,"layerFC")
# Select all records in layerFC
arcpy.SelectLayerByAttribute_management("layerFC","NEW_SELECTION")
# Remove relevant commodity codes so they do not get deleted
QF1 = '\"Product_Code\"'
RV1 = '( \'R10R\', \'R10RBP\', \'R20R\', \'R20RBP\', \'R32R\', \'R32RBP\', \'R64R\', \'R64RBP\', \'R96R\', \'R96RBP\', \'R10RBY\', \'R20RBY\', \'R32RBY\', \'R64RBY\', \'R96RBY\', \'R10REC\', \'R20REC\', \'R32REC\', \'R64REC\', \'R96REC\')'
WC1 = QF1 + ' IN ' + RV1
arcpy.SelectLayerByAttribute_management("layerFC","REMOVE_FROM_SELECTION", WC1)
# Add unmatched records back to selection
QF2 = "Status"
RV2 = "M"
WC2 = """ {} <> '{}'""".format(QF2, RV2)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC2)
# Add records with incorrect service day back to selection
QF3 = "ServDay"
QF4 = "Service_Day"
WC3 = """ {} <> {} """.format(QF3, QF4)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC3)
QF5 = "Week"
QF6 = "Wk"
RV3 = ""
WC4 = """{0} <> {1} AND {1} <> '' """.format(QF5,QF6)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC4)
arcpy.DeleteFeatures_management("layerFC")
# Bring over route values rom spatial join
with arcpy.da.UpdateCursor("layerFC",["OfficRT","Route"]) as cursor:
for row in cursor:
row[1] = row[0]
cursor.updateRow(row)
# Bring over sequence values from Tower ODBC table
SeqTable = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\From_Tower\\CLS_XX_Last28Days.xlsx'
arcpy.ExcelToTable_conversion(SeqTable,"Join_Table")
QF7 = "SVCCODE"
QV3 = "R%R%"
WC5 = """ {} LIKE '{}' """.format(QF7, QV3)
arcpy.MakeTableView_management("Join_Table", "RecSeq", WC5)
with arcpy.da.UpdateCursor("RecSeq", "CUSTID") as cursor:
for row in cursor:
row[0] = str(row[0]) + "001"
cursor.updateRow(row)
print "UPDATED ROWS"
joinFc = "RecSeq"
joinIdFld = "CUSTID"
joinValFld = "OLDSEQUENCE"
valueDi = {x[0]: x[1] for x in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])}
with arcpy.da.UpdateCursor("layerFC", ["Sequence", "Address_ID"]) as cursor:
for update, key in cursor:
if not key in valueDi:
continue
row = (valueDi[key], key)
cursor.updateRow(row)
del cursor
# Export final feature class to output workspace
outputWS = r"C:\RS_Data\Workspace\BATCH_CO_161_Recycle_export\gisdb\layers.gdb"
finalFC = r"C:\RS_Data\Workspace\BATCH_CO_161_Recycle_export\gisdb\layers.gdb\XX_Recycle"
arcpy.FeatureClassToFeatureClass_conversion("layerFC", outputWS, "XX_Recycle")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
# Delete temp files
finally:
arcpy.Delete_management("layerFC")
arcpy.Delete_management("RecSeq")