0

This python code works in ArcGIS Pro but Not in ArcMap . I read some issues that mention a get around by making a feature layer. I tried that and I then get an error saying polytest doesn't exist. maybe since I am writing to a temp file?

    import arcpy, os, shutil, sys

# Path to store geoprocessing files
user_profile_path = os.environ['USERPROFILE']
gdb = r"{}/AppData/Local/Temp/PolyChecker/PolyCheck.gdb".format(user_profile_path)

# GDB already exists, delete it
if arcpy.Exists(gdb):
    shutil.rmtree(r"{}/AppData/Local/Temp/PolyChecker".format(user_profile_path))

# Create fresh GDB path
os.makedirs(os.path.dirname(gdb))

# Create GDB
arcpy.CreateFileGDB_management(os.path.dirname(gdb), os.path.basename(gdb))

# Script arguments
MapUnitPolys = arcpy.GetParameterAsText(0)
if MapUnitPolys == '#' or not MapUnitPolys:
    MapUnitPolys = "MapUnitPolys"  # provide a default value if unspecified

MapUnitPolys_CopyFeatures = arcpy.GetParameterAsText(1)

# Set Geoprocessing environments
MapUnitPolys = MapUnitPolys

# Validate that all Polygons have a map unit

invalid_polygon_found = False

with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
    for row in cursor:
        #arcpy.AddMessage(str(row[1]))
        # Does this Polygon have a map unit
        if row[1] == "" or row[1] == "<Null>" or row[1] is None or row[1] is 0:
            invalid_polygon_found = True
            arcpy.AddMessage('Polygon OBJECT ID:{} is missing map unit... exiting.'.format(row[2]))


# Invalid polygons were found, terminate
if (invalid_polygon_found):
    sys.exit(1)

Polygon_Neighbors = "{}/polytest".format(gdb)

PolygonNeighbor_TableSelect = "{}/PolygonNeighbor_TableSelect".format(gdb)

inFeatures_lyr = "{}/inFeatures_1yr".format(gdb)

# Process: Polygon Neighbors
arcpy.PolygonNeighbors_analysis(MapUnitPolys, Polygon_Neighbors, "OBJECTID;MapUnit", "NO_AREA_OVERLAP", "BOTH_SIDES",
                                "", "METERS", "SQUARE_METERS")


# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Polygon_Neighbors, "NEW_SELECTION", "src_MapUnit = nbr_MapUnit")

# Process: Table Select
arcpy.TableSelect_analysis(Polygon_Neighbors, PolygonNeighbor_TableSelect, "src_MapUnit = nbr_MapUnit")

arcpy.GetCount_management(PolygonNeighbor_TableSelect)

arcpy.AddMessage(arcpy.GetMessages())

if int(arcpy.GetCount_management(PolygonNeighbor_TableSelect)[0]) > 0:
    arcpy.MakeFeatureLayer_management(MapUnitPolys, inFeatures_lyr)
else:
    print ("done")

# Process: Add Join
arcpy.AddJoin_management(inFeatures_lyr, "OBJECTID", PolygonNeighbor_TableSelect, "src_OBJECTID", "KEEP_COMMON")

# Process: Copy Features
arcpy.CopyFeatures_management(inFeatures_lyr, MapUnitPolys_CopyFeatures, "", "0", "0", "0")

# Process: Remove Join
arcpy.RemoveJoin_management(inFeatures_lyr, "")

# Execute Delete
arcpy.Delete_management(PolygonNeighbor_TableSelect)
arcpy.Delete_management(Polygon_Neighbors)

arcpy.AddMessage('All done! Check Polygons')

error1

    Traceback (most recent call last):
  File "C:\Downloads\GeMS-ArcPro-Toolbox-master\GeMS-ArcPro-Toolbox-master\Scripts\PolyChecker2.py", line 56, in <module>
    arcpy.SelectLayerByAttribute_management(Polygon_Neighbors, "NEW_SELECTION", "src_MapUnit = nbr_MapUnit")
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 7726, in SelectLayerByAttribute
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a table
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).


Failed to execute (PolyChecker).
Failed at Mon Aug 12 13:28:48 2019 (Elapsed Time: 3.90 seconds)
1

In your code Polygon_Neighbors is a table. You'll need a table view to perform your selection.

Polygon_Neighbors_tv = arcpy.MakeTableView_management (Polygon_Neighbors, "Polygon_Neighbors_tv") [0]
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Polygon_Neighbors_tv, "NEW_SELECTION", "src_MapUnit = nbr_MapUnit")

Note that you can create a table view with with an sql query already assigned should you wish to skip the selection.

  • great! thank you. I get this error now: line 72, in <module> arcpy.AddJoin_management(inFeatures_lyr, "OBJECTID", PolygonNeighbor_TableSelect, "src_OBJECTID", "KEEP_COMMON") File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 6546, in AddJoin raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Layer Name or Table View: Dataset C:Temp/PolyChecker/PolyCheck.gdb/inFeatures_1yr does not exist or is not supported Failed to execute (AddJoin). – Lbook Aug 12 at 21:01
  • @Lbook That's a different question for a different post. If you feel I've answered your question please mark my answer as correct. – Emil Brundage Aug 12 at 21:19
  • @Lbook you might have only upvoted. Click the check mark to answer it correct. – Emil Brundage Aug 12 at 21:31
  • Okay! thank you – Lbook Aug 12 at 22:05
  • do you know why I am getting the second error? – Lbook Aug 13 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.