The Soil Survey Geographic Database (SSURGO) Soils API provides soil type and soil type attribute averages (e.g., NCCPI) by field for a shape entered. This API uses POST and GET request with a subscription key. Soil Type data is derived from NRCS USDA; average soil type attribute data are available for processing for the continental USA. It uses data provided by the USDA NRCS, which can be downloaded from http://sdmdataaccess.nrcs.usda.gov. Those soil type attributes contain both numerical data type and categorical (ordinal) data type, which can be found from the links in the reference.
1). AOI (GeoJSON String): Area of interest as Geojson string containing boundary coordinates. file(GeoTiff or Shapfile in Zip): Alternative AOI format when GeoJSON is not available. A GeoTiff(.TIFF,.Tif, .etc) or a Shapefile in Zip (must include shp, shx, dbf, but others files such as .prj, .xml, .cpg are recommended for better processing)
2).Projection(String):Output projection of result GeoTIFF.Provided as EPSG code, ex: "EPSG:4326"
3).Resolution(Float):Output resolution of result GeoTIFF.
4).Soil_Parameter(String): The soil metric to return, see the NRCS variables PDF for full list with descriptions
import requests
import json
import os
from collections import defaultdict
import pandas as pd
from IPython.display import Image
%matplotlib inline
%autosave 0
def ssurgo_service(values, files, headers):
try:
url = 'https://ag-analytics.azure-api.net/ssurgo-v2'
if files == None:
response = requests.post(url, data=values,headers = headers).json()
else:
response = requests.post(url, files = files, data = values,headers = headers).json()
print(response)
return response
except Exception as e:
print(e)
raise e
#Parameters to call SSURGO API
values = {'AOI': '{"geometryType": "esriGeometryPolygon", "features": [{"geometry": {"rings": [[[-85.179, 42.74], [-85.17858886748223, 42.74188232450973], [-85.17858886748223, 42.742675781062474], [-85.1782836915391, 42.742675781062474], [-85.1782226563505, 42.74230956993074], [-85.17529296909521, 42.74230956993074], [-85.17529296909521, 42.74353027370324], [-85.17529296909521, 42.74371337926908], [-85.17492675796348, 42.74389648393566], [-85.17437744126585, 42.744079589501496], [-85.17340087914721, 42.744079589501496], [-85.17327880876996, 42.74749755826576], [-85.17401123013411, 42.74749755826576], [-85.17401123013411, 42.74847412128372], [-85.17529296909521, 42.74847412128372], [-85.17590332008211, 42.74829101571788], [-85.17749023408697, 42.74792480458609], [-85.17761230446422, 42.7470703128447], [-85.1782836915391, 42.746704101712965], [-85.18072509728535, 42.746704101712965], [-85.179, 42.74]]], "spatialReference": {"wkid": 4326}}}]}',
'Soil_Parameter': 'nccpi2all',
'Projection': 'EPSG:4326',
'Resolution': 0.00001,
'Product':'GeoJSON'}
file = None
# Basic Header Pattern.
# headers=None
# Header for using a subscription key.
# headers={'Content-Type':'application/x-www-form-urlencoded','Ocp-Apim-Subscription-Key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
SSURGO_response_GeoJSON = ssurgo_service(values, file, headers)
#Path to raster in your local machine
# rasterpath = r"raster path on your local machine"
rasterpath = r"C:\Users\Xuewei\Documents\Xueweilocal\test_raster\test_raster.tif"
files = [('file',(open(rasterpath, 'rb')))]
values = {'Soil_Parameter': 'nccpi2all',
'Projection': 'EPSG:4326',
'Resolution': 0.0001,
'Product':'raster'}
headers={'Ocp-Apim-Subscription-Key': "XXXXXXXXXXXXXXXXXXXXXXXXXX"}
SSURGO_response_raster = ssurgo_service(values, files, headers)
# import required library
import folium
import mplleaflet
#Display result GeoJSON on map
result_geojson = SSURGO_response_GeoJSON['attributes']['GeoJSON']
zoom_point=result_geojson['features'][0]['geometry']['coordinates'][0][0]
zoom_point
m = folium.Map([zoom_point[1],zoom_point[0]],tiles='Cartodb Positron', zoom_start=11,width='70%', height='100%')
for shape in result_geojson['features']:
folium.GeoJson(shape).add_to(m)
m
result_geojson = SSURGO_response_GeoJSON['attributes']['GeoJSON']
zoom_point=result_geojson['features'][0]['geometry']['coordinates'][0][0]
zoom_point
m = folium.Map([zoom_point[1],zoom_point[0]],tiles='Cartodb Positron', zoom_start=11,width='70%', height='100%')
for shape in result_geojson['features']:
m.add_child(folium.GeoJson(data=shape).add_child(folium.Popup(str(shape["properties"]))))
m
# pngb64
attributes =SSURGO_response_raster['attributes']
pngb64 = attributes['pngb64']
#Image Legend
df=defaultdict(list)
Legend = attributes['Legend']
for val in Legend:
df['Acres'].append(float(val['Acres']))
df['Area'].append(val['Area'])
df['Value'].append(val['Value'])
df['Soil_Type'].append(val['Soil_Type'])
df['color'].append(val['color'])
legenddf = pd.DataFrame.from_dict(df)
legenddf.style.applymap(lambda x:"background-color: %s"%x, subset=['color'])
# Visualize CDL
Image(url = pngb64, width = 400, height = 400)
# Specify local path where file will be downloaded.
# local_path = r"C:\<Path to download directory>" # E.g., r"C:\Users\John_Doe\Documents\rasters"
local_path = r'C:'
# Create GET request payload.
values = {'filename': SSURGO_response_raster['filename']}
print(values)
# Get SSURGO file
def SSURGO_get(values, local_path,headers):
try:
url = 'https://ag-analytics.azure-api.net/ssurgo-v2'
download_path = os.path.join(local_path, values['filename'])
print(values)
response = requests.get(url, params=values,headers=headers)
open(download_path, 'wb').write(response.content)
print(response.url)
return response
except Exception as e:
print(e)
raise e
SSURGO_get(values, local_path,headers)