Script not adding output feature class to mxd

1039
5
09-30-2016 09:44 AM
CCWeedcontrol
Regular Contributor

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.

0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor

Whether this is your main problem or not, you can't create a geodatabase feature class (regardless of being in-memory or not) that starts with a number. I also don't believe it can contain a space.

0 Kudos
CCWeedcontrol
Regular Contributor

Sorry, not sure what you mean by you "you can't create a geodatabase feature class"  I mean i do know what you mean but  i am not putting or exporting to a geodatabase. The buffer analysis is just exporting them to the workspace folder.

I mention that my problem is that after the buffer analysis runs it doesn't add the output to the TOC. I would still like to know why it doesn't add the output to the TOC but...

I guess i don't need it to add it the TOC i just need Merge the  "1 Miles" & "2 Mile" shapefiels together and update the Buff_Dist field.

I tried the following but no luck

error
arcpy.Merge_management(shplist, os.path.join(out, "B1_2"))

Parameters are not valid.
ERROR 000468: Input shape types are not equal
Failed to execute (Merge).


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")

out = arcpy.env.workspace
shplist =  arcpy.ListFeatureClasses(["1 Mile", "2 Mile"])

arcpy.Merge_management(shplist, os.path.join(out, "B1_2"))

 
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

ListFeatureClasses—Help | ArcGIS for Desktop 

the second parameter doesn't want the names of the featureclasses, it wants the feature type.   And if you want to omit parameters you have to name the parameter you are using, So basically it has no clue what you are trying to list since the wildcard for the first parameter is being specified as your list and the remaining parameters get ignored.  Check the syntax in the help.  If you want to use two specific feature parameters, just use them and call them by name.

Merge—Help | ArcGIS for Desktop use the featureclasses in here, skip the listFC stuff

CCWeedcontrol
Regular Contributor

I tried just calling them out by name but i ge the Error 000732 Dataset 1Mile;2Mile does not exist or is not supported
Failed to execute (Merge). I am currently trying to do this out of scrip tool to run inside ArcMap. The "1 Mile" and "2 Mile" shp get created, i can see them inside the workspace folder. I guess i need to somehow merge the "1 Mile" and "2 Mile" in side the workspace folder.

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


distances = ["1 Mile", "2 Mile"]
for distance in distances:
    outfile = distance  
    arcpy.Buffer_analysis(SP, outfile, distance, "FULL", "ROUND", "", "ToBufDist")
    

#out = arcpy.env.workspace
lyr = ["1 Mile", "2 Mile"]

arcpy.Merge_management(lyr, os.path.join(wp, "1_2"))
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

could be those file names ... '1 Mile'  .... begins with a number, contains a space... going to a shapefile would probably cause a hiccup, maybe a featureclass? but you say the script says it can't be found or is not supported, but you can add it manually (?) ... if you can add it manually and are intent on using a filename like that, at least you have a workaround.

0 Kudos