• Daniel Romero-Alvarez

SoilGrids with Google Earth Engine

Updated: Oct 31

.=.=.=.===.==.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=..=.=.=.===.==.=.=.=.=.=.=.=.=.=.=.=.=.=.=


Make sure to also check the tutorial on how to download vegetation indexes (NDVI or EVI) using Google Earth Engine. ​​


.=.=.=.===.==.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=..=.=.=.===.==.=.=.=.=.=.=.=.=.=.=.=.=.=.=


There was a time when I used to go to this website and download properties of the soil of the world. Apparently you can still do that but you need to use VRT files and pipelines in R or python. Yet, there is another option for downloading the information using Google Earth Engine (GEE).


GEE is an incredible tool to leverage satellite information. I will dedicate another tutorial for GEE basics such as creating repositories or uploading shapefiles. Here, a straightforward implementation to obtain worldwide soil variables. SoilGrids was presented in 2017 in a PLoS One paper. The database offers physical and chemical properties of soils as described in the official paper and the updated version.


Let's get familiar with the GEE code editor:

It has four parts: A is the section that keeps your repositories and allows you to create new projects. Notice that A has three tabs: Scripts, Docs and Assets, the latter will allow you to upload shape files in your working repository. B is the area where you should develop your script. For this example, the section in which you want to copy and paste the offered script. C is the console, you can see the progress of your code here. Notice that C contains three tabs: Inspector, Console, and Tasks, the latter will be important to actually upload the outputs in google drive. D is the map viewer where you may want to check visually the output of your satellite images. The red arrow in D (left) shows the tool to manually create polygons to collect specific sections in the map viewer.


For practical reasons, the following code is a fully commented version of the one available online. I use to fully comment code because non-programmers like me struggle a lot to understand what is happening between lines. If you copy the following script you might want to replace specific sections with the variable you are interested on or with any other particularity. Crucial stuff to remember:

  1. GEE is using JAVA as programming language.

  2. Commenting in JAVA is done via //, so everything starting with those will not be read.

  3. The code is read in batches so every time you hit run, everything is going to be read sequentially.

  4. If you want to actually see what happens while you run your code you have to PRINT your actions so you can examine them in the CONSOLE (section C in the figure).

  5. Check the official tutorials, they are really clear and useful.

  6. Don't forget to export the output clicking run in the TASK (section C in the figure).


SCRIPT:

//This tutorial come from the available script to download soilgrids using GEE: //https://git.wur.nl/isric/soilgrids/soilgrids.notebooks/-/blob/master/markdown/access_on_gee.md


//we are interested in downloading NITROGEN CONTENT

// which is defined as: nitrogen_mean


//load soil layers in GEE.


var nitrogen = ee.Image("projects/soilgrids-isric/nitrogen_mean");

print ('nitrogen', nitrogen)


//by printing you will see the variable in the console. Each variable is a raster with 6 bands, each //representing the different depths of soil information from 0-5 cm to 100-200cm depth.


//set visualization parameters

//min and maximum values are obtained from http://www.soilgrids.org

// and watching the variable in question, in this case nitrogen: min = 0, max = 1240

//

var imageVisParam = {"opacity":1,"bands":["nitrogen_0-5cm_mean",],"min":10,"max":1240,"palette":["ffef29","0000ff"]};


//add layer to the map: this allows you to see the actual map in the map viewer of GEE (section //D in the figure)


Map.addLayer (nitrogen, imageVisParam)

Map.setCenter (-15, 48, 2) //zoom: close to 0 = zooming out || larger values = zooming in


//Because I am interested only in the 0-5cm band, I have to create a variable for it

// and then download only that specific layer. The correct name "nitrogen_0-5cm_mean"

//can be found by examining the variable in the console and copying the name from the //FEATURES section (see C, in the figure).


var nitrogen_01 = nitrogen.select ("nitrogen_0-5cm_mean")

print (nitrogen_01, 'nitrogen_01')


//we can see that only one feature, meaning ONE BAND is selected for the nitrogen_01 variable


//EXPORTING IMAGE:


//you can export the image via shapefiles (cropping an specific section), but because we are //interested in the entire world a shapefile of the world causes a weird error. To solve that, we //define a particular GEOMETRY (section D, red arrow) that octuply the area that we are //interested on. Geometries are drawn manually using the corresponding tools


Export.image.toDrive({

image: nitrogen_01, //name of the variable

description: 'nitrogen_01', //change name accordingly

folder: '_CHAPTER_3', //the folder in your GOOGLE DRIVE to upload the output

region: geometry, //you can use a shapefile for smaller areas, for larger areas a drawn is best

scale: 1000, //resolution of the pixels in METERS, 30 means high resolution, 1000 = 1km

crs: 'EPSG:4326',

maxPixels: 1e12}); //you cannot ask for a larger amount of pixels, apparently this is the limit

//Click RUN on the TASK section of the code editor website (C in the figure) to actually complete the upload of the //output to your specific Google Drive folder


//If the amount of pixels is too large (for example while asking for vegetation indexes for MODIS, //multiple rasters are going to be created that you might want to join using function such as //MERGE in R. A way to effectively merge rasters in R can be read in this other tutorial.


//End of the script.


Nitrogen content at 0-5 cm depth as defined by SoilGrids, dowloaded with this script and visualized in QGIS 3.26.1-Buenos Aires.


Thanks to Giulio Genova, the host of the original script for downloading SoilGrids through GEE.



32 views0 comments

Recent Posts

See All