AnsweredAssumed Answered

Script not adding output feature class to mxd

Question asked by CCweedcontrol on Sep 30, 2016
Latest reply on Oct 7, 2016 by Dan_Patterson

I am trying to apply a python script to a script tool so i can use in ArcMap. This portion of the script when put into ArcMap's python window runs fine, it adds the out put of the output's of "1 Miles" and "2 Miles" to TOC of the MXD and then proceeds to do the Merge, updatecursor and remove the outputs fine.

 

Runs fine in ArcMaps python window

import arcpy, os

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#lyrpath = lyr.workspacePath
arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)

try:
    SP = "SUBJECT_PROPERTY"
    lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )
except:
    pass

df.extent = lyr.getSelectedExtent()
df.scale = 24000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

try:
    SP = "SUBJECT_PROPERTY"
    lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )
except:
    pass

try:
    if arcpy.ListFields(SP, "BUFF_DIST"): 
         print "Field exists" 
    else: 
        arcpy.AddField_management("SUBJECT_PROPERTY","BUFF_DIST","Double")
except:
    pass

arcpy.RefreshActiveView()
arcpy.RefreshTOC()


distances = ["1 Mile", "2 Mile"]
for distance in distances:
    outfile = distance  #"wp%s" % distance 
    arcpy.Buffer_analysis(SP, outfile, distance, "FULL", "ROUND", "", "BUFF_DIST")

arcpy.Merge_management(["1 Mile","2 Mile"], "1_2")

with arcpy.da.UpdateCursor("1_2" , "BUFF_DIST") as cursor:
            for row in cursor:
                if row[0] == 5280:
                    row[0] = 1
                elif row[0] == 10560:
                    row[0] = 2
                cursor.updateRow(row)
            del row


wp1 = "1 Mile"

if arcpy.Exists(wp1):
    arcpy.Delete_management("1 Mile")

wp2 = "2 Mile"

if arcpy.Exists(wp2):
    arcpy.Delete_management("2 Mile")

A12 = "1_2"

if arcpy.Exists(A12):
    arcpy.Delete_management("1_2")

 

 

When i create a script tool and it runs but then i get an error at line 57, ExecuteError: Failed to execute. Parameters are not valid.ERROR 000732: Input Datasets: Dataset '1 Mile';'2 Mile' does not exist or is not supportedFailed to execute (Merge).

I can see that the the "1 Miles" and "2 Miles" get created but not added to the TOC of the MXD. which i think is why it error's out at the Merge process.

 

Error's out and does not add the "1 Miles" and "2 Miles" to the TOC.

 

import arcpy, string, os


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.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 elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.text == "Template":
        elm.text = Name

df.extent = lyr.getSelectedExtent()
df.scale = 24000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:

   arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")

del  lyr

lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]

SP = "SUBJECT_PROPERTY"
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

try:
    if arcpy.ListFields(SP, "BUFF_DIST"): 
         print "Field exists" 
    else: 
        arcpy.AddField_management("SUBJECT_PROPERTY","BUFF_DIST","Double")
except:
    pass

distances = ["1 Mile", "2 Mile"]
for distance in distances:
    outfile = distance #('In_memory/% distance')  #"wp%s" % distance 
    arcpy.Buffer_analysis(SP, outfile, distance, "FULL", "ROUND", "", "BUFF_DIST")

arcpy.Merge_management(["1 Mile","2 Mile"], "1_2")

with arcpy.da.UpdateCursor("1_2" , "BUFF_DIST") as cursor:
            for row in cursor:
                if row[0] == 5280:
                    row[0] = 1
                elif row[0] == 10560:
                    row[0] = 2
                cursor.updateRow(row)
            del row

del mxd, lyr

 

why doesn't the script add "1 Miles" and "2 Miles" to the TOC of the MXD?

Can the Buffer Analysis and Merge be done in "in_memory\ ". if so how? i have tried to set the buffer analysis in memory but i am not sure how to set two distances in_memory.

 

Thanks.

Outcomes