-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssurgo_api.py
289 lines (241 loc) · 8.78 KB
/
ssurgo_api.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
import csv
import numpy as np
import re
import sys
from collections import defaultdict
# @@@ - assert for data format?
class SSURGO(dict):
def __init__(self, path, mode="esri"):
super(SSURGO, self).__init__()
self.path = path
self.mode = mode
if self.mode == "esri":
self.folder_format = "gSSURGO_([A-Z]{2}).gdb"
elif self.mode == "streamline":
self.folder_format = "([A-Z]{2})"
else:
sys.exit("Invalid mode \"{}\"".format(self.mode))
self.states = self.fetch_states()
def __getattr__(self, item):
item = item.upper()
if item not in self.__dict__.keys():
if item in self.states:
return self[item]
else:
print("State \"{}\" not in SSURGO".format(item))
input()
else:
return getattr(self, item)
def __getitem__(self, state):
if state not in self.keys():
state = state.upper()
state_path = self.states.get(state)
if not os.path.isdir(state_path):
print("{} was not found in the specified SSURGO dataset".format(state))
return
else:
val = State(state, state_path, self.mode)
else:
val = self[state]
return val
def __iter__(self):
for s in sorted(self.states):
yield self[s]
def fetch_states(self):
states = {}
for path, subdirs, _ in os.walk(self.path):
for subdir in subdirs:
match = re.match(self.folder_format, subdir)
if match:
states[match.group(1).upper()] = os.path.join(path, subdir)
return states
# TBD
def states_containing(self, map_unit_or_component):
pass
class State:
def __init__(self, state, path, mode):
self.name = state
self.path = path
self.mode = mode
self.grid = os.path.join(path, state)
# Variables that are generated upon being called
self.ds = None
self._array = None
self._components = None
self._horizons = None
self._map_units = None
self._headings = None
self._tables = None
def __getattr__(self, item):
if item not in self.__dict__:
if item in self.tables.keys():
return self.tables[item]
else:
print("Table {} not in {}".format(item, self.name))
else:
return getattr(self, item)
def __repr__(self):
return "SSURGO Data for {}".format(self.name)
@property
def array(self):
import gdal
if not self._array:
if not self.ds:
self.ds = gdal.Open(self.grid)
self._array = np.array(self.ds.GetRasterBand(1).ReadAsArray())
self._array[self._array < 0] = 0
return self._array
@property
def components(self):
if not self._components:
self._components = self.tables['component'].map_components("mukey", "cokey", "comppct_r")
return self._components
@property
def headings(self):
"""
Match up all possible attributes with the tables in which they're located, and whether the attribute matches
with a map unit, component, or horizon
:return: {heading: [table1, table2,..], ...}
"""
if not self._headings:
self._headings = defaultdict(list)
for table in self.tables.values():
for heading in table.headings:
self._headings[heading].append(table)
return self._headings
@property
def horizons(self):
if not self._horizons:
self._horizons = self.tables['chorizon'].map_components("cokey", "chkey")
return self._horizons
@property
def map_units(self):
if not self._map_units:
self._map_units = self.components.keys()
return self._map_units
@property
def tables(self):
def from_gdb():
import arcpy
old_workspace = arcpy.env.workspace
arcpy.env.workspace = self.path
for name in arcpy.ListTables():
yield name, name
else:
arcpy.env.workspace = old_workspace
def from_folder():
for f in os.listdir(self.path):
name, ext = os.path.splitext(f)
if ext == ".csv":
yield name, f
if not self._tables:
self._tables = {}
if self.mode == "esri":
source = from_gdb()
elif self.mode == "streamline":
source = from_folder()
for name, f in source:
self._tables[name] = Table(os.path.join(self.path, f), name, self.mode)
return self._tables
class Table:
def __init__(self, path, name, mode):
self.path = path
self.name = name
self.mode = mode
self._headings = []
self._index = None
def __getattr__(self, item):
if item in self.headings:
return self.read_field(item)
else:
print("Field {} not in {}".format(item, self.name))
@property
def index(self):
if not self._index:
types = []
for key in ("mukey", "cokey", "chkey"):
if key in self.headings:
types.append(key)
if len(types) > 1:
print("Unable to identify an index for table {}".format(self.name))
elif len(types) == 1:
self._index = types.pop()
else:
self._index = "N/A"
return self._index
@property
def headings(self):
if not self._headings:
if self.mode == "esri":
import arcpy
self._headings = {field.name for field in arcpy.ListFields(self.path)}
else:
with open(self.path) as f:
self._headings = f.readline().strip().split(",")
return self._headings
@property
def indexed(self):
if self.index == "N/A":
return False
else:
return True
def map_components(self, super_key, sub_key, third_key=None):
out_dict = defaultdict(list)
def gdb_reader():
import arcpy
fields = sorted(self.headings)
for row in arcpy.da.SearchCursor(self.path, fields):
yield dict(zip(fields, row))
def csv_reader():
with open(self.path) as f:
reader = csv.DictReader(f)
for row in reader:
yield row
if self.mode == "esri":
iterator = gdb_reader()
else:
iterator = csv_reader()
for line in iterator:
super_val = int(line[super_key])
sub_val = int(line[sub_key])
if third_key:
out_dict[super_val].append((sub_val, line[third_key]))
else:
out_dict[super_val].append(sub_val)
return out_dict
def read_field(self, field_name):
def from_csv():
with open(self.path) as f:
reader = csv.DictReader(f)
if field_name in reader.fieldnames and self.index in reader.fieldnames:
return {r[self.index]: r[field_name] for r in reader}
def from_gdb():
import arcpy
fields = {field.name for field in arcpy.ListFields(self.path)}
if field_name in fields and self.index in fields:
return dict(arcpy.da.SearchCursor(self.path, [self.index, field_name]))
if self.mode == "esri":
data = from_gdb()
else:
data = from_csv()
if data:
return data
else:
print("Unable to match field \"{}\" to index \"{}\" in table \"{}\"".format(
field_name, self.index, self.name))
def map_unit_average(data, component_map):
"""
Converts component level data into map unit level data by taking an area-weighted average
:param data: A dictionary with component ids as keys and values as values
:param component_map: A dictionary with map unit as keys, and tuple of component id and area as values.
Generated automatically by State.components
:return: A dictionary with map unit ids as keys and spatially averaged values as values
"""
out_data = dict.fromkeys(component_map.keys(), 0.0)
for map_unit, component in component_map.items():
component_id, component_pct = component
component_value = float(data.get(component_id, 0.0))
proportional_value = (component_pct / 100.0) * component_value
out_data += proportional_value
return out_data