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