top of page

Interactive soil database with shiny, leaflet and SOAP

Hellooo there

How are you doing? I'm here today, to share one of my projects with you.

Ok, in this project I tried to integrate my shiny +leaflet knowledge with some soil information. Actually, I tried to use web service provided by USDA and NRCS to get access to their SSURGO database(The SSURGO database contains information about soil as collected by the National Cooperative Soil Survey over the course of a century) .

So how does the project look like ? (https://para2x.shinyapps.io/SoilDB) Simply the user clicks on wherever he/she wants and picks a coordinate. Then the app goes to the soil database and retrieves some specific parameters corresponding to that point. It also lets users to download and also plot those data.

Now, how does it work ? there are different steps need to be taken in order to make this idea happen. First, the location needs to be converted to the map unit key exsited in their databse. This conversion was done using the web service provided by UC Davis Soil Resource Laboratory using the following code :

f <- paste("&lon=", long, "&lat=", lat,sep = "") the.url <- paste("http://casoilresource.lawr.ucdavis.edu/soil_web/api/ssurgo.php?what=mapunit", f, sep = "") try(res <- jsonlite::fromJSON(the.url), silent = TRUE) mukey<-res[[4]]

It simply takes the latitude and longitude and sends a request to the web server and parse the response.

Now, that we have map unit key we can use it to send request to SSURGO database using RCurl package. In this page you can find detailed information about different APIs provided by USDA and NRCS for soil database. Here is a sample of my code for sending the request :

headerFields = c(Accept = "text/xml", Accept = "multipart/*", 'Content-Type' = "text/xml; charset=utf-8", SOAPAction = "http://SDMDataAccess.nrcs.usda.gov/Tabular/SDMTabularService.asmx/RunQuery") body = paste('<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <RunQuery xmlns="http://SDMDataAccess.nrcs.usda.gov/Tabular/SDMTabularService.asmx"> <Query> SELECT muaggatt.mukey, mapunit.mukey, mapunit.muname from mapunit where mapunit.mukey=', mukey,'; </Query> </RunQuery> </soap:Body> </soap:Envelope>')

Then as a response of this request you'll get a xml file containing your requested data which needs to be parsed. Using the xml2 package for parsing the xml result I was able to to use XPath and get a direct access to the elements I needed. For example by just this follwing line of code I could extract all tables I requested for:


xml_children(xml_find_all(xml_data, ".//NewDataSet"))


Then the rest would be just putting those parametres into a dataframe and plot them.

Hope you all liked my post !

Featured Posts
Recent Posts
Archive
Search By Tags
bottom of page