worldbank / blackmarbler

Georeferenced Rasters and Statistics of Nighttime Lights from NASA Black Marble
https://worldbank.github.io/blackmarbler/
Other
16 stars 3 forks source link

Migrate from raster to terra #8

Open ramarty opened 6 months ago

alexyshr commented 5 months ago

If you will use terra::extract to update the function bm_extract. You can use points, points with radius (buffer), polygons, or polygons with radius (buffer) to extract the values. In addition, setting the parameters exact and weights to TRUE helps create a smoother time series (those parameters can be part of bm_extract).

   ...
   ...
   buffer <- 0.1

   #the geometry to extract time series (SF of points or polygons)
   the_geometry 

   #this variable is created from HDF5 files using terra::
   ntl_raster_terra
   ... 
   ...

  #check if the buffer is different from 0
  if (buffer != 0) {
    #before to use st_buffer we need set sf_use_s2 to FALSE
    temp <- sf_use_s2()
    sf_use_s2(FALSE)
    #make a buffer from initial geometry
    the_geometry <- sf::st_buffer(the_geometry, buffer)
    #return sf_use_s2 to the original value
    sf_use_s2(temp)
  } 

  #extract the values with terra
  values_df <- terra::extract(ntl_raster_terra,
    the_geometry, #this can be points or polygons
    fun = mean,
    exact = FALSE, #TRUE, exact fraction of cells
    weights = FALSE, #TRUE, approximate fraction of cells
    method = "simple",
    na.rm = TRUE,
    ID = FALSE
  )

#convert the values_df to long format
...
...
alexyshr commented 5 months ago

Hi @ramarty

I have been working Black Marble data in HDF5 format with stars; it is faster than terra. I can share the code with you to evaluate if stars is a better option. Another option is that you implement functions using terra and I can contribute with the functions using stars, then after some use of the tools you can decide. Best

ramarty commented 4 months ago

@alexyshr If you have some code to share that'd be great -- I think stars could be a good option for this, even with a set of parallel functions that use a stars approach

alexyshr commented 4 months ago

If already implemented different functions. I want to check the latest version you posted and I would love to contribute to the project. I can suggest (push-commit) small changes or new functionality and you can check and decide if leaving them as part of the package. Please let me know if we can work collaboratively. Thanks

ramarty commented 4 months ago

@alexyshr This sounds great - and absolutely welcome contributions / would be great to work collaboratively here. The goal is to make the package as useful as possible to different workflows, so think your contributions would be particularly useful here.