forked from NYNatHeritage/PROs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.py
More file actions
124 lines (109 loc) · 4.47 KB
/
Copy pathFunctions.py
File metadata and controls
124 lines (109 loc) · 4.47 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
##Functions File
##This module contains all the functions that can be called in the routine for processing files
##After they have been edited by the botanists and zoologists
##The clean_name function goes through the files and removes the unecesssary punctuation and the "Current" signifier
import arcpy
import os
import shutil
def clean_name(x):
clean_name=x.replace("_Current","")
clean_name=clean_name.replace("var._","var_")
clean_name=clean_name.replace("ssp._","ssp_")
arcpy.Rename_management(x,clean_name)
#Make a function that takes a feature class, pulls out the features that meet the right criteria, and turns them into a new feature class
## pull_selected_features take the argument a(the feature class to cut) and y (the output location)
def pull_selected_features(x,y):
infile=x
out_path=y
#root=x[:-12]
#new=root+".shp"
where_clause='"IN_2015_PR" = ' + "'Y'" +" OR " + ' "IN_2015_PR" = '+"'M'" +" OR " + '"IN_2015_PR" = ' + "'y'" + " OR " + ' "IN_2015_PR" = '+"'m'"
try:
arcpy.FeatureClassToFeatureClass_conversion(infile,out_path,infile,where_clause)
except:
print "Something went wrong converting " +str(x)
##The check_nulls function goes through the files and checks for null values in the EO_ID field that might throw off the script
def check_nulls(x):
Null_counter=0
test_fc=x
fields=["FID","EO_ID","SCIEN_NAME"]
##The where statement is the most finicky part of the code. If something doesn't work,it is probably the WHERE clause mucking it up
alt_where= '"EO_ID" = ' + "''"
try:
with arcpy.da.SearchCursor(test_fc,fields,where_clause=alt_where) as cursor:
for row in cursor:
#print ("FID {0}".format(row[0]))
Null_counter+=1
if row:
del row
if cursor:
del cursor
count=Null_counter
return count
except:
#print "This file has all its EO_IDs"
return 0
##Check zeros does the same thing, but checks if the value is 0
def check_zeros(x):
test_fc=x
Null_counter=0
fields=["FID","EO_ID","SCIEN_NAME"]
zero_where= '"EO_ID" = ' + '0'
try:
zero_where= '"EO_ID" = ' + "0"
with arcpy.da.SearchCursor(test_fc,fields,where_clause=zero_where) as cursor:
for row in cursor:
print ("FID {0}".format(row[0]))
Null_counter+=1
if row:
del row
if cursor:
del cursor
count=Null_counter
return count
except:
#print "This file has all its EO_IDs"
return 0
#The check_names function examines where the names fields are missing or left blank
def check_names(x,y):
Null_counter=0
test_fc=x #The renaming is messy and unnecessary,the test-fc terminology was a hold over from earlier versions of the code. You could just eliminate both the place holder names and replace with "x" and "y"
field_try=y
fields=["FID","EO_ID","SCIEN_NAME","COMMONNAME"]
#sci_where= '"SCIEN_NAME" = ' + "''"
auto_where=arcpy.AddFieldDelimiters(test_fc,field_try)+' = '+ "''"
try:
with arcpy.da.SearchCursor(test_fc,fields,where_clause=auto_where) as cursor:
for row in cursor:
#print ("FID {0}".format(row[0]))
Null_counter+=1
if row:
del row
if cursor:
del cursor
count=Null_counter
return count
except:
#print "This file has all its EO_IDs"
return 0
##Check_emptys test if a featureclass is empty
def check_emptys(x):
try:
result=arcpy.GetCount_management(x)
count=int(result.getOutput(0))
#print count
if count==0:
print str(x)+" has an empty set!"
#empty_features.append(feature)
else:
print str(x)+ "isn't empty! Good job!"
return count
except:
print "Checking for empty feature sets raised a flag"
##Function to move the files out of the input folder so I won't process them twice
def archive_originals(x,y):
for file in os.listdir(x):
#print file
src_file=os.path.join(x,file)
dst_file=os.path.join(y,file)
shutil.move(src_file,dst_file)