r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
677 stars 146 forks source link

ee_extract() using points instead of polygons geometry #176

Closed malmistry closed 2 years ago

malmistry commented 3 years ago

R.version _
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 6.3
year 2020
month 02
day 29
svn rev 77875
language R
version.string R version 3.6.3 (2020-02-29) nickname Holding the Windsock

rgee version 1.0.9.999

I am able to run the example here to extract sample data from GEE using the netcdf file and spatially aggregating the daily maximum temperature.

I would like to:

(i) use multiple point locations (lon,lat) from a .csv file or input manually in R, and then extract the sample data from GEE instead of spatially averaging it over the polygons geometry.

(ii) instead of the sample data in the above example (which is daily data from a climate model), I would like to use ERA5 hourly data and do temporal aggregation (to daily values) before extracting the values for the multiple point locations

(iii) the required daily time series will be for years 1981-2020. So a way to speed the process in parallel if possible.

Below is what I did (to addres point 1 above). I don't get any errors. But the output does not include the temperature time-series.

## Load packages

library(rgee)
library(raster)
library(sf)

ee_check() # Check non-R dependencies

# 1. Initialize the Python Environment
ee_Initialize()

# # 2. Install geemap in the same Python ENV that use rgee
# py_install("geemap")
gm <- import("geemap")

## Create spatial points using 8 random geo locations

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
                                     126.7328, 153.2439, 142.8673, 152.689), 
                       latitude = c(-7.4197, -4.7089, -6.7541, 4.7817, 2.1643, 
                                    -5.65, 23.3882, -5.571)), 
                  .Names = c("longitude", "latitude"), class = "data.frame", 
                  row.names = c(NA, -8L))

### Get long and lat from your data.frame. Make sure that the order is in lon/lat.

xy <- mydf[,c(1,2)]

## Convert to spatial points dataframe

spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
                               proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

# Convert to sf object
spdf_sf    <- st_as_sf(spdf)
spdf_sf$ID <- as.factor(seq(1:8))

## Move that geometry from local to earth engine
ee_spdf_sf <- sf_as_ee(spdf_sf)

## Load your ImageCollection

x <- ee$ImageCollection("NASA/NEX-DCP30_ENSEMBLE_STATS")$
  filterDate("1980-01-01","1980-01-02")$
  map(function(img) img$select("tasmax_mean"))

## calculate the nominal scale
scale <- x$first()$projection()$nominalScale()$getInfo() 

## Does not extract

tasmax_mean_data_spdf <- ee_extract(x = x,
                                    y =  spdf_sf,                    # No difference in results if I replace spdf_sf with ee_spdf_sf
                                    fun = ee$Reducer$mean(),
                                    scale = scale,
                                    sf=FALSE)

## As seen below, there is no column for temperature that I was expecting to be extracted.

> tasmax_mean_data_spdf
  longitude latitude ID
1  128.6979  -7.4197  1
2  153.0046  -4.7089  2
3  104.3261  -6.7541  3
4  124.9019   4.7817  4
5  126.7328   2.1643  5
6  153.2439  -5.6500  6
7  142.8673  23.3882  7
8  152.6890  -5.5710  8

####

Thanks Malcolm

csaybar commented 3 years ago

1) You do not obtain values because the image and the points are not in the same geographical location. (We will add some warnings to catch this issue). image

2) Aggregating an ee.ImageCollection is possible using reduce operators. https://developers.google.com/earth-engine/guides/ic_reducing

3) Since your dataset is big, you must follow a batch approach. See https://developers.google.com/earth-engine/guides/best_practices#export-intermediate-results

malmistry commented 3 years ago

Thank you. Very useful! The warning to catch this issue could be useful! Malcolm

kateburrows commented 2 years ago

I found this really helpful but wanted to ask how I would add in a buffer around the points - I'd like to do the same thing but using a 300m buffer around a list of points. Would appreciate any suggestions!