· 6 years ago · Aug 13, 2019, 06:16 PM
1# Path to store geoprocessing files
2user_profile_path = os.environ['USERPROFILE']
3gdb = r"{}/AppData/Local/Temp/PolyChecker/PolyCheck.gdb".format(user_profile_path)
4
5# GDB already exists, delete it
6if arcpy.Exists(gdb):
7 shutil.rmtree(r"{}/AppData/Local/Temp/PolyChecker".format(user_profile_path))
8
9# Create fresh GDB path
10os.makedirs(os.path.dirname(gdb))
11
12# Create GDB
13arcpy.CreateFileGDB_management(os.path.dirname(gdb), os.path.basename(gdb))
14
15# Script arguments
16MapUnitPolys = arcpy.GetParameterAsText(0)
17if MapUnitPolys == '#' or not MapUnitPolys:
18 MapUnitPolys = "MapUnitPolys" # provide a default value if unspecified
19
20MapUnitPolys_CopyFeatures = arcpy.GetParameterAsText(1)
21
22# Set Geoprocessing environments
23MapUnitPolys = MapUnitPolys
24
25# Validate that all Polygons have a map unit
26
27invalid_polygon_found = False
28
29with arcpy.da.SearchCursor(MapUnitPolys, ['SHAPE@', 'MapUnit', 'OBJECTID']) as cursor:
30 for row in cursor:
31 #arcpy.AddMessage(str(row[1]))
32 # Does this Polygon have a map unit
33 if row[1] == "" or row[1] == "<Null>" or row[1] is None or row[1] is 0:
34 invalid_polygon_found = True
35 arcpy.AddMessage('Polygon OBJECT ID:{} is missing map unit... exiting.'.format(row[2]))
36
37
38# Invalid polygons were found, terminate
39if (invalid_polygon_found):
40 sys.exit(1)
41
42Polygon_Neighbors = "{}/polytest".format(gdb)
43
44PolygonNeighbor_TableSelect = "{}/PolygonNeighbor_TableSelect".format(gdb)
45
46inFeatures_lyr = "{}/inFeatures_1yr".format(gdb)
47
48# Process: Polygon Neighbors
49arcpy.PolygonNeighbors_analysis(MapUnitPolys, Polygon_Neighbors, "OBJECTID;MapUnit", "NO_AREA_OVERLAP", "BOTH_SIDES",
50 "", "METERS", "SQUARE_METERS")
51
52
53Polygon_Neighbors_tv = arcpy.MakeTableView_management (Polygon_Neighbors, "Polygon_Neighbors_tv") [0]
54# Process: Select Layer By Attribute
55arcpy.SelectLayerByAttribute_management(Polygon_Neighbors_tv, "NEW_SELECTION", "src_MapUnit = nbr_MapUnit")
56
57# Process: Table Select
58arcpy.TableSelect_analysis(Polygon_Neighbors, PolygonNeighbor_TableSelect, "src_MapUnit = nbr_MapUnit")
59
60arcpy.GetCount_management(PolygonNeighbor_TableSelect)
61
62arcpy.AddMessage(arcpy.GetMessages())
63
64if int(arcpy.GetCount_management(PolygonNeighbor_TableSelect)[0]) > 0:
65 arcpy.MakeFeatureLayer_management(MapUnitPolys, inFeatures_lyr)
66else:
67 print ("done")
68
69# Process: Add Join
70arcpy.AddJoin_management(inFeatures_lyr, "OBJECTID", PolygonNeighbor_TableSelect, "src_OBJECTID", "KEEP_COMMON")
71
72# Process: Copy Features
73arcpy.CopyFeatures_management(inFeatures_lyr, MapUnitPolys_CopyFeatures, "", "0", "0", "0")
74
75# Process: Remove Join
76arcpy.RemoveJoin_management(inFeatures_lyr, "")
77
78# Execute Delete
79arcpy.Delete_management(PolygonNeighbor_TableSelect)
80arcpy.Delete_management(Polygon_Neighbors)
81
82arcpy.AddMessage('All done! Check Polygons')
83
84Traceback (most recent call last):
85 File "C:DownloadsGeMS-ArcPro-Toolbox-masterGeMS-ArcPro-Toolbox-masterScriptsPolyChecker2.py", line 72, in <module>
86 arcpy.AddJoin_management(inFeatures_lyr, "OBJECTID", PolygonNeighbor_TableSelect, "src_OBJECTID", "KEEP_COMMON")
87 File "c:program files (x86)arcgisdesktop10.6arcpyarcpymanagement.py", line 6546, in AddJoin
88 raise e
89ExecuteError: Failed to execute. Parameters are not valid.
90ERROR 000732: Layer Name or Table View: Dataset C:Users/AppData/Local/Temp/PolyChecker/PolyCheck.gdb/inFeatures_1yr does not exist or is not supported
91Failed to execute (AddJoin).
92
93
94Failed to execute (PolyChecker).