Creating a shapefile in R

First create a file structure. The containing folder can be called anything, and it can be located anywhere on your computer, but must contain the following three files.

  1. out
  2. shape
  3. GEEraw

We must create a shape file to upload as an asset to Google Earth Engine (GEE). This can be done in R as long as you have Longitude and Latitude for the spatial points you want to retrieve data for. Resulting spatial object must have columns “Lon”, “Lat”, and “geeID”. geeID must be a unique identifier for each point in space.

raw.data <- read.csv("/Volumes/My Book/GABON.NRI.DATA/Locations for Chase.csv")
names(raw.data)[names(raw.data) == 'long'] <- 'Lon'
names(raw.data)[names(raw.data) == 'lat'] <- 'Lat'
raw.data$geeID <- 1:nrow(raw.data)
head(raw.data)
##       Code      Lat       Lon Forest_type   Habitat geeID
## 1  AKOK.91 0.536528  9.735458   Exploitee     Coast     1
## 2 ALMT.106 0.781590 11.141146   Exploitee  Aucoumea     2
## 3 ANGA.124 1.609785 11.374861  Secondaire  Aucoumea     3
## 4 ASES.103 0.676597 10.030535  Secondaire     Coast     4
## 5 AVZK.125 1.486951 11.844181  Secondaire Congolian     5
## 6  AYMV.83 0.117090 11.457458    Primaire  Aucoumea     6
dim(raw.data)
## [1] 104   6

Creating shapefile using functions in rgdal and sp, then saving it into the “shape” file you created at the beginning of the process.

coordinates(raw.data) <- ~Lon+Lat
class(raw.data)
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
proj4string(raw.data) <- CRS("+init=epsg:4326") #WGS84
raw.data <-  spTransform(raw.data,CRS("+init=epsg:4326"))
writeOGR(obj=raw.data, dsn="/Volumes/My Book/GABON.NRI.DATA/shape", layer="gabonnriTRIM", driver="ESRI Shapefile")

Uploading shapefile to Earth Engine

next navigate to https://code.earthengine.google.com/. In the left hand column, under the “Assets” tab, click the “NEW” button and select “Table upload.” Then select the folder containging your multiple shape files, type in a asset id, the click OK.

Visualize points in Earth Engine

Once your points have been added to Earth Engine (could take several minutes), it will appear under the assets tab under the name you used as asset id. Hover your mouse over the name, and three icons will appear. Click the arrow to import your points to the console.

Now, in the center “Script” console, Earth Engine wil list your imports. To the right of the “Imports” title, and above youre imported vars, there will be a blue box. click on it to get the file path to your points. e.g. “ee.FeatureCollection(”users/chasenunez/gabonnri)"

On the first line of your console, type:

Map.addLayer(

followed by the filepath taken from above and a closing parentheses.

Map.addLayer(ee.FeatureCollection("users/chasenunez/gabonnri"))

Click the “run” button in the tookbar in the center column. Your points should now be projected on the map in the lower half of your screen. No analysis has been conducted - this is just to QC your points to make sure they are in the right locations.

Using Python to run Earth Engine

In order to expedite large, multifarious datadownoad, we leave behind the graphical user interface, and embrace Python. To do this, you will create a folder in Google Drive that your Earth Engine data will be saved into. I have names the example file “layers_gjam”

Now, download the Python script file “downloadData.py”. This holds a collection of python function for commonly used datasets in Earth Engine.

In the same file as downloadData.py, create a new text document with any name. In it, you may call the python function of your choice from downloadData.py, personalizing the arguments to suit your needs.

For example, if I wanted to pull the historical precipitation and maximum temperature from the TerraClimate dataset, I would use the following code in my new text document specifying my asset id (‘congabon’), my variable names (pr, tmax), startYear, endYear, buffer, is it a polygon?, the name of the folder you created at the beginning of this chunk (layers_gjam), your google username (chasenunez), and the scalePixels.

All arguments and explainations can be found in the downloadData.py file.

ScalePix is important to set low for points as it determine how spatially “far off” GEE data values can be from your point. For more on this see GEE developers page.

from downloadData.py import GEEterraClimatePtsAvgMonth
GEEterraClimatePtsAvgMonth('congabon',['pr','tmmx'],1985,2017,0,0, folderOut = "layers_gjam", username = 'chasenunez', scalePix = 100)

save this new text file (e.g.congabon.py) in the same directory are ‘downloadData.py’. Then, in the terminal, navigate to the folder containing both files and run the following:

python congabon.py

If this runs smoothly, you should be able to navigate to your GEE code editor page, and in the right hand column, click the “Tasks” tab to see if the job is being processed.

If, however, you get a series of errors, it likley you have to prepare your computer to use python and earth engine. Instructions for how to do this can be found here.

Processing the raw GEE data in R

Once the job has been completed, look in your googleDrive folder for the raw.output. Download these files to the “GEEraw” folder you created at the beginning of this page.

source the function “funct_preprocessGEE” in the same folder as “GEEraw”, “out”, and “shape”, then run