r-spatial / rgee

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

reduceRegions not adding property to Feature Collection #258

Closed btavernia closed 2 years ago

btavernia commented 2 years ago

The rgee session indicates that I have earthengine-api 0.1.302.

I have been able to locate the credentials file on my system:

C:\Users\Brian.Tavernia.config\earthengine\credentials

I have been able to run simply EE commands from R (e.g., reading Feature and Image Collections).

My Python (reticulate configuration is:

python: C:/Users/Brian.Tavernia/AppData/Local/r-miniconda/envs/rgee/python.exe libpython: C:/Users/Brian.Tavernia/AppData/Local/r-miniconda/envs/rgee/python38.dll pythonhome: C:/Users/Brian.Tavernia/AppData/Local/r-miniconda/envs/rgee version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)] Architecture: 64bit numpy: C:/Users/Brian.Tavernia/AppData/Local/r-miniconda/envs/rgee/Lib/site-packages/numpy numpy_version: 1.22.3 ee: C:\Users\BRIAN~1.TAV\AppData\Local\R-MINI~1\envs\rgee\lib\site-packages\ee__init__.p

Description

I am attempting to use reduceRegions to determine the mean value for band 10 from Landsat 8 for polygons representing US cities.
The returned FeatureCollection does not have the mean property attribute. There is no error thrown, the FeatureCollection simply does not have the mean property.

What I Did

Getting temperature band from Landsat 8

start <- ee$Date$fromYMD(2021,6,1) #Start date for filtering end <- ee$Date$fromYMD(2021,9,30) #End date for filtering

imgC <- ee$ImageCollection("LANDSAT/LC08/C02/T1_L2")%>% #Landsat 8 ee$ImageCollection$filterDate(start,end)%>% #filtering by date ee$ImageCollection$map(qFn) #Masking image collection

l8 <- imgC$select("ST_B10")%>% #Selecting thermal band ee$ImageCollection$median()%>% #Getting image by taking median ee$Image$clip(city)#Clipping image to city boundaries

l8m <- l8$multiply(0.00341802)$add(149.0)%>% #Converting to Kelvin ee$Image$set('yr',end$format('YYYY')) #Setting year property

Calculating mean of cities - tempMean feature collection does not contain a mean property

tempMean = l8ma$reduceRegions(

collection = city, reducer = ee$Reducer$mean(), scale = 30

)

ee_print(tempMean)

ambarja commented 2 years ago

Hello @btavernia, please Can you share your featurecollection (city)? Also, I recommend you to use ee_extract function instead of Reduce$Region

btavernia commented 2 years ago

Note that I tried ee_extract, and it did not return the mean.

Here's the link to the asset.

https://code.earthengine.google.com/?asset=projects/ee-btavernia/assets/tl_2021_us_uac10_1_500

I appreciate your assistance.

ambarja commented 2 years ago

@btavernia Here is a reproducible code :point_down: , to extract the value take 2 min max, If you have heavier geometries it is necessary to loop and pass every 1000 geometries.

library(rgee)
library(tidyverse)
ee_Initialize()

# Reading featurecollection
city <- ee$FeatureCollection("projects/ee-btavernia/assets/tl_2021_us_uac10_1_500")

# Getting temperature band from Landsat 8
start <- ee$Date$fromYMD(2021,6,1) # Start date for filtering
end <- ee$Date$fromYMD(2021,9,30)  # End date for filtering

imgC <- ee$ImageCollection("LANDSAT/LC08/C02/T1_L2") %>% 
  ee$ImageCollection$filterDate(start,end) %>% 
  # ee$ImageCollection$map(qFn) 

l8 <- imgC$select("ST_B10") %>%   # Selecting thermal band
  ee$ImageCollection$median() %>% # Getting image by taking median
  ee$Image$clip(city)             # Clipping image to city boundaries

l8m <- l8$multiply(0.00341802)$add(149.0)%>% # Converting to Kelvin
  ee$Image$set('yr',end$format('YYYY'))      # Setting year property

# Calculating mean of cities - tempMean feature collection does not contain a mean property

data_city <- ee_extract(
  x = l8m,
  y = city,
  sf = TRUE,
  fun = ee$Reducer$mean(),
  scale = 30
)
> data_city
Simple feature collection with 500 features and 13 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -124.4373 ymin: 24.84476 xmax: -67.23016 ymax: 48.57694
Geodetic CRS:  WGS 84
First 10 features:
     ALAND10  AWATER10 FUNCSTAT10 GEOID10  INTPTLAT10   INTPTLON10 LSAD10 MTFCC10                         NAME10
1  164312964     39512          S   90946 +36.2934877 -119.3006857     75   G3500                    Visalia, CA
2  122953531     11735          S   56251 +37.3307025 -120.5253059     75   G3500                     Merced, CA
3  102399933    318810          S   28657 +38.2591432 -122.0360609     75   G3500                  Fairfield, CA
4  109704370   3802117          S   04951 +44.8356248 -068.7466293     75   G3500                     Bangor, ME
5  376242591   7140215          S   23500 +33.0930390 -097.0662763     75   G3500         Denton--Lewisville, TX
6   78376393     50937          S   81901 +31.5355677 -110.2854923     75   G3500               Sierra Vista, AZ
7  215110226    916506          S   51256 +32.5180379 -094.7809335     75   G3500                   Longview, TX
8  309094284  25853461          S   63838 +27.0065126 -082.1238434     75   G3500 North Port--Port Charlotte, FL
9  854973036 107859842          S   13510 +26.6025896 -081.8423316     75   G3500                 Cape Coral, FL
10 298583607   9788293          S   84024 +28.4879946 -082.5242931     75   G3500                Spring Hill, FL
                                      NAMELSAD10 UACE10 UATYP10   ST_B10                       geometry
1                     Visalia, CA Urbanized Area  90946       U 322.9515 MULTIPOLYGON (((-119.243 36...
2                      Merced, CA Urbanized Area  56251       U 319.6030 MULTIPOLYGON (((-120.4709 3...
3                   Fairfield, CA Urbanized Area  28657       U 312.5783 MULTIPOLYGON (((-122.0177 3...
4                      Bangor, ME Urbanized Area  04951       U 288.7482 MULTIPOLYGON (((-68.62432 4...
5          Denton--Lewisville, TX Urbanized Area  23500       U 308.7109 MULTIPOLYGON (((-97.16255 3...
6                Sierra Vista, AZ Urbanized Area  81901       U 318.1878 MULTIPOLYGON (((-110.2828 3...
7                    Longview, TX Urbanized Area  51256       U 309.0440 MULTIPOLYGON (((-94.69924 3...
8  North Port--Port Charlotte, FL Urbanized Area  63838       U 296.3076 MULTIPOLYGON (((-81.99675 2...
9                  Cape Coral, FL Urbanized Area  13510       U 287.3954 MULTIPOLYGON (((-81.59186 2...
10                Spring Hill, FL Urbanized Area  84024       U 302.8116 MULTIPOLYGON (((-82.615 28....
btavernia commented 2 years ago

Thanks! This works. It doesn't work when I map the user-defined qFn function on image collection (which is commented out in the code above). I developed this function (and bitwise function) to mask shadow, cloud, and snow from the images. Any notion why the user defined functions might be causing an issue?

Helper function to extract the values from specific bits

The input parameter can be a ee.Number() or ee.Image()

Code adapted from https://gis.stackexchange.com/a/349401/5160

bitwiseExtract <- function(input,fromBit,toBit){

maskSize <- ee$Number(1)$add(toBit)$subtract(1) mask <- ee$Number(1)$leftShift(maskSize)$subtract(1) input$rightShift(fromBit)$bitwiseAnd(mask)

}

Function to mask cloud, shadow, and snow pixels

qFn <- function(image){

qCdata <- image$select("QA_PIXEL") cloudMask <- bitwiseExtract(qCdata,6,6)$eq(1) shadowMask <- bitwiseExtract(qCdata,4,4)$eq(0) snowMask <- bitwiseExtract(qCdata,5,5)$eq(0) mask <- cloudMask$And(shadowMask)$And(snowMask) image$updateMask(mask)

}