ArcPro script doesn't run all processes

503
3
11-04-2020 11:21 AM
2Quiker
Occasional Contributor II

I have a script that I am trying to move over to Pro but some process in the script don't run. The script has about 7 process, the script does not fail but it doesn't finish the last process witch applies smbologyfromlayer. For example the last process doesn't run when I run the script but if copy it into Pro's python window and hit enter it runs fine, it updates the symbology. I have tried reload(arcpy) and arcpy.ClearWorkspaceCache_management() before the arcpy.ApplySymbologyFromLayer_management process but it still doesn't work. Not sure what I am missing or not doing...

import arcpy, string, os
from importlib import reload


arcpy.env.overwriteOutput = True

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("TAXLOTS")[0]
lyt = aprx.listLayouts("Layout1")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT")[0]

arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

#selects PinID from TAXLOTS
values = arcpy.GetParameterAsText(0)
fieldName = "PinID"
values = values.split(";")  # split values into list
values = ["'{0}'".format(v) for v in values] # add single quotes
whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)

#Udpates Text Element Template from layout
Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT","Text"):
        if elm.text == "Template":
            elm.text = (Name)


#Zooms to selected parcel in layout
ext = mf.getLayerExtent(lyr)
mf.camera.setExtent(ext)
mf.camera.scale *= 15 

#Exports the selected parcels for SUBJECT_PROPERTY.
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
   arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")


#Remove layer
for m in aprx.listMaps():
    print("Map: {0} Layers".format(m.name))
    for lyr in m.listLayers("SUBJECT_PROPERTY"):
        m.removeLayer(lyr)

#Add layer Subject_property
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)
shp_path = wp + '\\' + "SUBJECT_PROPERTY.shp"
map = aprx.listMaps()[0]  # assumes data to be added to first map listed
map.addDataFromPath(shp_path)

reload(arcpy)
arcpy.ClearWorkspaceCache_management()

#Update Subject Property symbology
#arcpy.env.workspace = os.path.dirname(aprx.filePath)
#wp = os.path.dirname(aprx.filePath)
#This arcpy.ApplySymbologyFromLayer_management doesn't update the layer symbology in the script
arcpy.ApplySymbologyFromLayer_management("SUBJECT_PROPERTY", wp + '\\'+ "Symbology_SUBJECT_PROPERTY.lyrx")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If I copy the last process into Pro python window in the same project and run it, it does update the symbology.

#Update Subject Property symbology
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

arcpy.ApplySymbologyFromLayer_management("SUBJECT_PROPERTY", wp + '\\'+ "Symbology_SUBJECT_PROPERTY.lyrx")
0 Kudos
3 Replies
2Quiker
Occasional Contributor II

It appears that the toolbox script has issues in Pro python, this same script runs fine in ArcMap.

I got past the Symobology by deleting  m, lyr, lyt, mt after the first process but I had to re-add them before the Symobology process. Not sure why it's holding on to these, is there a way to clear the processes in Pro?

 

Changed the last process of ApplySymobologyFromLayer_Managment to Add field

#Add field to lyr
SP = "SUBJECT_PROPERTY"
arcpy.AddField_management(SP,"BUFF_DIST","DOUBLE")

 SP is a layer that is created by the first process and is in my layout in Pro, it's not being used by any process other than it's in the current layout . I do see 4 "SUBJECT_PROPERTY.shp.*****.11336.8984.sr.lock" in the explorer.

Error after trying to use Add Field with the same script;

ERROR 000464: Cannot get exclusive schema lock. Either being edited or in use by another application or service.
Failed to execute (AddField).

 

0 Kudos
RandyBurton
MVP Regular Contributor

Its a little difficult to understand your code since it isn't formatted.

It does appear that you are trying to run the tool inside Pro using the "CURRENT" project (in what looks like it might be the 4th line).  But I'm not sure that you are using the current project when attempting to update the symbology.

0 Kudos
2Quiker
Occasional Contributor II

Sorry about that, I didn't see the option to address the syntax. Even after select Python as the syntax there is not numbers for each row.

The following is given me an error and it seems no what I do the script always stops at a certain process no matter if I change the process.

but I just run just the #Creat SelParsA1 section in the projects python window it will run but not in the script so I am not sure what I am doing wrong.

 

import arcpy, string, os
from importlib import reload

#Zooms to selected parcel in layout
#arcpy.env.addOutputsToMap = True
arcpy.env.overwriteOutput = True

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyr = map.listLayers("TAXLOTS")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("MAPFRAME_ELEMENT")[0]

arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

values = arcpy.GetParameterAsText(0)
fieldName = "DXF_TEXT"
values = values.split(";")  # split values into list
values = ["'{0}'".format(v) for v in values] # add single quotes
whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)

Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT","Text"):
        if elm.text == "Template":
            elm.text = (Name)

#Zooms to selected parcel
ext = mf.getLayerExtent(lyr)
mf.camera.setExtent(ext)
mf.camera.scale *= 16 

#Exports the selected parcels for SUBJECT_PROPERTY.
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
   arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")

#Update Subject Property symbology
#layer = m.listLayers('SUBJECT_PROPERTY')[0]
#arcpy.ApplySymbologyFromLayer_management(str(layer), "Symbology_SUBJECT_PROPERTY.lyrx")

del aprx, map, lyr, mf, lyt
'''
#Remove layer Subject property
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]# assumes data to be added to first map listed
lyr = m.listLayers()[0]
m.removeLayer(lyr)

del aprx, m,lyr,
'''
#Udpate Subject Property datasource
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = map.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]

try:
    cp = lyr.connectionProperties
    cp['connection_info']['database'] = wp
    cp['dataset'] = 'SUBJECT_PROPERTY.shp'
    lyr.updateConnectionProperties(lyr.connectionProperties, cp)
except:
    pass

del aprx, map, lyt, lyr, mf

reload(arcpy)
arcpy.ClearWorkspaceCache_management()

#Creat SelParsA1
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = map.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]

arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

TAX = "TAXLOTS"
SP2 = "SUBJECT_PROPERTY" 
arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")

try:
    if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
        arcpy.SelectLayerByAttribute_management("TAXLOTS", "REMOVE_FROM_SELECTION", "DXF_TEXT = ' ' OR DXF_TEXT IS NULL") #"\"DXF_TEXT\" = ' '"

        arcpy.FeatureClassToFeatureClass_conversion("TAXLOTS", wp, "SelParsA1")

except:
    pass
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")

del lyr

lyr = map.listLayers("SelParsA1")[0]

try:
    cp = lyr.connectionProperties
    cp['connection_info']['database'] = wp
    cp['dataset'] = 'SelParsA1.shp'
    lyr.updateConnectionProperties(lyr.connectionProperties, cp)
except:
    pass

 

 

Error:

Traceback (most recent call last):
  File "D:\GIS Folder\Python_ArcGISPro\NotificationMapScripts\NotificationMapScriptTest_E.py", line 86, in <module>
    arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8909, in SelectLayerByLocation
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8906, in SelectLayerByLocation
    retval = convertArcObjectToPythonObject(gp.SelectLayerByLocation_management(*gp_fixargs((in_layer, overlap_type, select_features, search_distance, selection_type, invert_spatial_relationship), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError:  ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
 The base table definition string "SUBJECT_PROPERTY" is invalid.
The database was not found.
Failed to execute (SelectLayerByLocation).
 Failed to execute (NotificatonMapScript).

 

 

Runs in projects python window but not not included in the script posted above

import arcpy, string, os
from importlib import reload

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = m.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]

arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)

TAX = "TAXLOTS"
SP2 = "SUBJECT_PROPERTY" 
arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")

try:
    if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
        arcpy.SelectLayerByAttribute_management("TAXLOTS", "REMOVE_FROM_SELECTION", "DXF_TEXT = ' ' OR DXF_TEXT IS NULL") #"\"DXF_TEXT\" = ' '"

        arcpy.FeatureClassToFeatureClass_conversion("TAXLOTS", wp, "SelParsA1")

except:
    pass
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")

del lyr

 

0 Kudos