-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrim_DEF_seabed-habitat-maps_2022.py
70 lines (65 loc) · 2.75 KB
/
trim_DEF_seabed-habitat-maps_2022.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
##########################
## Removes fields not required by TRANSLATED HABITAT DEF within ESRI Shapefiles or geodatabase featureclasses
## Enter folder (or geodatabase/dataset) containing the input shapefiles in the command prompt
## IMPORTANT: Ensure that the location entered contains ONLY SHAPEFILES TO BE EDITED.
##
## Created by: Graeme Duncan, JNCC for EMODnet Seabed Habitats 2014.
## Contact: [email protected]
###########################
import arcpy
import os
print "IMPORTANT: Ensure that the directory path entered below contains ONLY SHAPEFILES TO BE EDITED."
print "This script will remove fields from ALL SHAPEFILES within the supplied directory"
#set work space:
arcpy.env.overwriteOutput = True
root_workspace = raw_input('Paste the full directory path to the folder containing your habitat maps here: ')
arcpy.env.workspace = root_workspace
#define list of feature classes to work with:
fclist = arcpy.ListFeatureClasses()
add_fields = [
("GUI","TEXT","#","#",8),
("POLYGON","TEXT","#","#",10),
("ORIG_HAB","TEXT","#","#",254),
("ORIG_CLASS","TEXT","#","#",150),
("HAB_TYPE","TEXT","#","#",100),
("HAB_CLASS","TEXT","#","#",150), # Previously called 'VERSION' in Translated Habitat DEF
("DET_MTHD","TEXT","#","#",254),
("DET_NAME","TEXT","#","#",254),
("DET_DATE","DATE","#","#","#"),
("TRAN_COM","TEXT","#","#",254),
("T_RELATE","TEXT","#","#",1),
("VAL_COMM","TEXT","#","#",254),
("COMP","TEXT","#","#",10),
("COMP_TYPE","TEXT","#","#",20),
("SUM_CONF", "SHORT",5,"#","#"),
("TEXT_CONF","TEXT","#","#",100)]
errorCounter = 0
for fc in fclist:
errorList = []
field_name_list = [field.name for field in arcpy.ListFields(fc) if not (field.type in ["OID","Geometry"] or field.name.lower() in ["shape_length","shape_area"])]
print fc
for requiredField in add_fields:
try:
field_name_list.remove(requiredField[0])
except ValueError as e:
print "%s does not exist in featureclass %s" % (requiredField[0], fc)
errorList.append(requiredField[0])
#else:
#print "Removing " + requiredField[0]
if len(errorList) > 0:
print "Could not find the following fields: %s" % errorList
print "______________"
print "Removing the following fields: "
print field_name_list
try:
arcpy.DeleteField_management(fc,field_name_list)
except Exception as e:
print "error in removing fields from %s" % fc
print e.message
errorCounter += 1
else:
print "Fields deleted"
finally:
print "________________________________________________"
print "________________________________________________"
print "There were %s errors" % str(int(errorCounter))