-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdapoly.py
297 lines (179 loc) · 8.16 KB
/
sdapoly.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
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 26 11:45:45 2021
@author: Charles.Ferguson
"""
class Terminate(Exception):
pass
def msg(s):
print(s)
# this function was migrated to the module sdatab.tabular
# def tab(q, meta = False):
# import json, requests, pandas as pd
# from json.decoder import JSONDecodeError
# from requests import exceptions
# try:
# theURL = "https://sdmdataaccess.nrcs.usda.gov"
# theURL = theURL + "/Tabular/SDMTabularService/post.rest"
# rDic = {}
# if meta:
# rDic["format"] = "JSON+COLUMNNAME+METADATA"
# else:
# rDic["format"] = "JSON+COLUMNNAME"
# rDic["query"] = q
# rData = json.dumps(rDic)
# results = requests.post(data=rData, url=theURL)
# qData = results.json()
# cols = qData.get('Table')[0]
# data = qData.get('Table')[1:]
# df = pd.DataFrame(data, columns = cols)
# return df
# except (exceptions.InvalidURL, exceptions.HTTPError, exceptions.Timeout):
# print('Requests error, Soil Data Access offline??')
# except JSONDecodeError as err:
# print('JSON Decode error: ' + err.msg)
# print('This usually happens when the extent is too large, try smaller extent.')
# except Exception as e:
# print('Unhandled error')
# print(e)
def sdaCall(gdf, meta=False):
import json, requests, geopandas as gpd, pandas as pd, shapely
from json.decoder import JSONDecodeError
from requests import exceptions
pd.set_option('display.max_colwidth', None)
invalid = ['POINT','MULTIPOINT','LINESTRING','MULTILINESTRING']
gtype = [g.upper() for g in gdf.geom_type.to_list()]
test = any(g in gtype for g in invalid)
if test:
raise TypeError('Only (MULTI)POLYGON geometry type allowed')
if not gdf.crs == 'WGS 84':
msg('Transforming shp to WGS 84')
gdf = gdf.to_crs("EPSG:4326")
if len(gdf) > 1:
dVal = 1
gdf['df'] = dVal
gdf = gdf.dissolve(by = 'df')
# make the smallest request possible
hull = gdf["geometry"].convex_hull
# get the wkt representation of the convex hull
wkt_str = hull.geometry.to_string(index = False, header = False)
"""Grab SSURGO geometry from user define AOI in wkt format
:param str wkt: the wkt representation of the AOI extent\n
:param boolen meta: get the column metadata returned in the JSON string, only suitable for arcgis features classes:
:return: SSURGO spatial data in wkt format"""
q = """~DeclareGeometry(@aoi)~
select @aoi = geometry::STPolyFromText('""" + wkt_str + """' , 4326)
~DeclareIdGeomTable(@outtable)~
~GetClippedMapunits(@aoi,polygon,geo,@outtable)~
select *
into #temp
from @outtable;
select areasymbol, areaname, muname, musym, mukey, nationalmusym as nat_musym, geom
from #temp, legend, mapunit
where #temp.id = mapunit.mukey and mapunit.lkey = legend.lkey"""
# print(q)
try:
theURL = "https://sdmdataaccess.nrcs.usda.gov"
theURL = theURL + "/Tabular/SDMTabularService/post.rest"
rDic = {}
if meta:
rDic["format"] = "JSON+COLUMNNAME+METADATA"
else:
rDic["format"] = "JSON+COLUMNNAME"
rDic["query"] = q
rData = json.dumps(rDic)
results = requests.post(data=rData, url=theURL)
qData = results.json()
cols = qData.get('Table')[0]
data = qData.get('Table')[1:]
df = pd.DataFrame(data, columns = cols)
geometry = df['geom'].map(shapely.wkt.loads)
sda_gdf = gpd.GeoDataFrame(df, crs = "EPSG:4326", geometry = geometry)
# assume it has to be clipped to original polygon
result = gpd.clip(sda_gdf, gdf)
return result
except (exceptions.InvalidURL, exceptions.HTTPError, exceptions.Timeout):
print('Requests error, Soil Data Access offline??')
except JSONDecodeError as err:
print('JSON Decode error: ' + err.msg)
print('This usually happens when the extent is too large, try smaller extent.')
except Exception as e:
print('Unhandled error')
print(e)
def shp(shp=str, meta=False, export=False, name=None):
"""Grab SSURGO soil polygons using input shp for extent
:str shp: path to shp file for AOI
:boolean meta: get the column metadata returned in the JSON string, only suitable for arcgis features classes:\n
:boolean export: write results to source directory
:str name: provide a shapefile name. If None, SSURGO_WGS84.shp used
:return: geopandas data frame epsg 4326"""
import os, geopandas as gpd, pandas as pd
pd.set_option('display.max_colwidth', None)
err = None
if not shp.endswith(".shp"):
err = 'input ' + shp + ' does not appear to be a shapefile'
raise TypeError(err)
else:
gdf = gpd.read_file(shp)
soils = sdaCall(gdf)
if soils is not None:
soils.drop(['geom'], axis = 1, inplace=True)
if export:
dest = os.path.dirname(shp)
if name is not None:
if not name.endswith('.shp'):
name = name + '.shp'
else:
name = 'SSURGO_WGS84.shp'
soils.to_file(os.path.join(dest, name))
return soils
def gpkg(gpkg=str, layer=str, meta=False, export=False, name=None):
"""Grab SSURGO soil polygons using input geopackage layer for extent
:str gpkg: path to geopackage\n
:str layer: layer to use for extent\n
:boolean meta: column metadata returned in JSON string, arcgis features classes only\n
:boolean export: write results to source geopackage\n
:str name: output layer name with, if None SSURGO_WGS8 used\n
:return: geopandas data frame epsg 4326"""
import geopandas as gpd, pandas as pd
pd.set_option('display.max_colwidth', None)
gdf = gpd.read_file(filename=gpkg, layer=layer)
soils = sdaCall(gdf, meta=False)
if export:
soils.drop(['geom'], axis = 1, inplace=True)
if name is None:
name = "SSURGO_WGS84"
soils.to_file(gpkg, layer=name, driver="GPKG")
return soils
def fgdb(gdb=str, layer=str, meta=False, export=False, name=None):
"""Grab SSURGO soil polygons using file geodatabse layer for extent
:str gdb: path to file geodatabase\n
:str layer: layer to use for extent\n
:boolean meta: column metadata returned in JSON string, arcgis features classes only\n
:boolean export: write a .shp result to parent directory of the file geodatabse\n
:str name: output layer name with, if None SSURGO_WGS8 used\n
:return: geopandas data frame epsg 4326"""
import os, fiona, geopandas as gpd, pandas as pd
pd.set_option('display.max_colwidth', None)
# get layerindex
lyrs = fiona.listlayers(gdb)
idx = lyrs.index(layer)
gdf = gpd.read_file(filename=gdb, layer=idx)
soils = sdaCall(gdf)
if export:
dest = os.path.dirname(gdb)
soils.drop(['geom'], axis = 1, inplace=True)
if name is None:
name = "SSURGO_WGS84"
soils.to_file(dest, layer=name)
return soils
def gdf(geodf, meta=False):
"""Grab SSURGO soil polygons using existing GeoDataFrame
:object geodf: GeoDataFrame\n
:boolean meta: column metadata returned in JSON string, arcgis features classes only\n
:return: geopandas data frame epsg 4326"""
if str(type(geodf)) != "<class 'geopandas.geodataframe.GeoDataFrame'>":
err = 'input does not appear to be a valid GeoDataFrame'
raise TypeError(err)
soils = sdaCall(geodf)
return soils