-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_data.py
69 lines (50 loc) · 2.08 KB
/
convert_data.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
"""
Given a set of HUCs that are dissolved so that they only have one feature per HUC ID (and that already have a field
that's an integer ID
"""
import os
import six
import arcpy
main_db = r"C:\Users\dsx\Projects\eflows_working\eflows_arcgis.gdb"
output_db = r"C:\Users\dsx\Projects\eflows_working\eflows_matrices.gdb"
base_hucs = "HUCs_dissolved"
species_groups = [
"Native_Fish",
"All_Fish",
#"Narrow_25",
#"Wide_Ranging",
#"Anadromous",
#"Flow_Sensitive",
]
presence_types = [
"current",
"historical",
]
hucs = os.path.join(main_db, base_hucs)
filtered_hucs = os.path.join(output_db, "hucs_filtered")
hucs_layer = "hucs_layer"
if arcpy.Exists(filtered_hucs):
arcpy.Delete_management(filtered_hucs)
print("Filtering HUCs to only in-state")
arcpy.MakeFeatureLayer_management(hucs, hucs_layer, where_clause="HUC_12 not Like 'M%'")
arcpy.CopyFeatures_management(hucs_layer, filtered_hucs)
arcpy.Delete_management(hucs_layer)
hucs = filtered_hucs
try:
print("Converting HUC_12 ID to Double Precision")
arcpy.AddField_management(hucs, "huc_id", "DOUBLE")
except: # this could fail for reasons we don't want to pass on, but we'll still fail in the next step - putting this here because it's saying field already exists
pass
arcpy.CalculateField_management(hucs, "huc_id", "float(!HUC_12!)", "Python3" if six.PY3 else "Python9.3")
arcpy.MakeFeatureLayer_management(hucs, hucs_layer, where_clause="HUC_12 not Like 'M%'")
for name in species_groups: # run this for every group/presence type combo
for ptype in presence_types:
print("Working on {}:{}".format(name, ptype))
table = os.path.join(main_db, "{}_{}".format(name, ptype))
#arcpy.AddField_management(table, "huc_id", "DOUBLE")
#arcpy.CalculateField_management(table, "huc_id", "float(!HUC_12!)", "Python3" if six.PY3 else "Python9.3")
arcpy.AddJoin_management(hucs_layer, "huc_id", table, "HUC_12")
features = os.path.join(output_db, "HUCs_{}_{}".format(name, ptype))
arcpy.CopyFeatures_management(hucs_layer, features)
print("Removing Join {}".format(table))
arcpy.RemoveJoin_management(hucs_layer, os.path.split(table)[1])