Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #48 from bluegreen-labs/master
Browse files Browse the repository at this point in the history
removing raster dependencies (raster, rgdal, maptools)
  • Loading branch information
khufkens authored Dec 19, 2022
2 parents 4112550 + 2ab6726 commit 8d89813
Show file tree
Hide file tree
Showing 36 changed files with 1,042 additions and 916 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ Imports:
httr,
utils,
sf,
raster,
sp,
terra,
stats,
memoise,
jsonlite
License: AGPL-3
LazyData: true
ByteCompile: true
RoxygenNote: 7.1.2
RoxygenNote: 7.2.1
Suggests:
knitr,
rmarkdown,
markdown,
covr,
testthat,
rgdal,
ggplot2,
dplyr
VignetteBuilder: knitr
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export(mt_dates)
export(mt_products)
export(mt_sites)
export(mt_subset)
export(mt_to_raster)
export(mt_to_terra)
export(sin_to_ll)
import(sp)
importFrom(memoise,memoise)
importFrom(raster,stack)
importFrom(terra,rast)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# MODISTools 1.1.3

* removing {raster} dependencies

# MODISTools 1.1.2

* removing parallel processing in batch tool due to timeout issues
Expand Down
19 changes: 12 additions & 7 deletions R/coordinate_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ sin_to_ll <- function(x, y){
}

# convert to sf object
coords <- sf::st_as_sf(x = data.frame(as.numeric(x),
as.numeric(y),
stringsAsFactors = FALSE),
coords = c("as.numeric.x.", "as.numeric.y."),
crs = "+proj=sinu +a=6371007.181 +b=6371007.181 +units=m")
coords <- suppressWarnings(
sf::st_as_sf(
x = data.frame(as.numeric(x),
as.numeric(y),
stringsAsFactors = FALSE),
coords = c("as.numeric.x.", "as.numeric.y."),
crs = "+proj=sinu +a=6371007.181 +b=6371007.181 +units=m +type=crs"
))

# reproject coordinates
coords <- sf::st_transform(coords, "+init=epsg:4326")
coords <- sf::st_transform(coords, crs = 4326)

# unravel the sf dataframe into a normal one
coords <- as.data.frame(do.call("rbind", lapply(coords$geometry, unlist)))
Expand Down Expand Up @@ -134,7 +137,9 @@ mt_bbox <- function(
# projection etc.
p <- sf::st_linestring(m)
p <- sf::st_cast(p, "POLYGON")
p <- sf::st_sfc(p, crs = "+proj=sinu +a=6371007.181 +b=6371007.181 +units=m")
p <- suppressWarnings(
sf::st_sfc(p, crs = "+proj=sinu +a=6371007.181 +b=6371007.181 +units=m +type=crs")
)

# a full description of the sinusoidal projection is provided on the
# lpdaac page:
Expand Down
2 changes: 1 addition & 1 deletion R/mt_batch_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' @export
#' @examples
#'
#' \donttest{
#' \dontrun{
#' # create data frame with a site_name, lat and lon column
#' # holding the respective names of sites and their location
#' df <- data.frame("site_name" = paste("test",1:2))
Expand Down
110 changes: 0 additions & 110 deletions R/mt_to_raster.R

This file was deleted.

15 changes: 10 additions & 5 deletions R/mt_to_terra.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Convert tidy MODISTools data to terra SpatRaster
#'
#' Convert tidy MODISTools data to a terra SpatRaster
#' Convert tidy MODISTools data to a terra SpatRaster for easy
#' spatial processing and plotting.
#'
#' @param df a valid MODISTools data frame with a single band (filter for a
#' particular band using the dplyr \code{filter()} function or base \code{subset()}
Expand Down Expand Up @@ -32,6 +33,7 @@
#' LC_r <- mt_to_terra(df = LC)
#'}
#' @importFrom terra rast
#' @import sp

mt_to_terra <- function(
df,
Expand Down Expand Up @@ -70,12 +72,14 @@ mt_to_terra <- function(
# loop over all dates, format rasters and return
r <- do.call("c",
lapply(dates, function(date){

# stuff values into raster
m <- matrix(as.numeric(df$value[df$calendar_date == date]) *
as.numeric(df$scale[df$calendar_date == date]),
df$nrows[1],
df$ncols[1],
byrow = TRUE)
byrow = TRUE
)

# convert to raster and return
return(terra::rast(m))
Expand All @@ -89,21 +93,22 @@ mt_to_terra <- function(
cellsize = df$cellsize[1],
nrows = df$nrows[1],
ncols = df$ncols[1],
transform = FALSE)
transform = FALSE
)

# convert to Spatial object (easier to get extent)
bb <- sf::as_Spatial(bb)

# assign extent + projection bb to raster
terra::ext(r) <- terra::ext(bb)
terra::crs(r) <- as.character(terra::crs(bb))
terra::crs(r) <- bb@proj4string@projargs
names(r) <- as.character(dates)

# reproject to lat long when desired
if(reproject){
r <- terra::project(
r,
"epsg:4326"
crs = 4326
)
}

Expand Down
4 changes: 3 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Hufkens (2022). The MODISTools package: an interface to the MODIS Land Products

## Acknowledgements

Original development was supported by the UK Natural Environment Research Council (NERC; grants NE/K500811/1 and NE/J011193/1), and the Hans Rausing Scholarship. Refactoring was supported through the Belgian Science Policy office COBECORE project (BELSPO; grant BR/175/A3/COBECORE). Logo design elements are taken from the FontAwesome library according to [these terms](https://fontawesome.com/license), where the globe element was inverted and intersected.
Original development was supported by the UK Natural Environment Research Council (NERC; grants NE/K500811/1 and NE/J011193/1), and the Hans Rausing Scholarship. Refactoring was supported through the Belgian Science Policy office COBECORE project (BELSPO; grant BR/175/A3/COBECORE). Logo design elements are taken from the FontAwesome library according to [these terms](https://fontawesome.com/license), where the globe element was inverted and intersected. Continued support for MODISTools is provided by [BlueGreen Labs](https://bluegreenlabs.org).

<a href='https://bluegreenlabs.org'><img src='https://bluegreenlabs.org/img/logo_text_small.png' width="200"/></a>

[![ropensci_footer](https://ropensci.org/public_images/ropensci_footer.png)](https://ropensci.org)
Loading

0 comments on commit 8d89813

Please sign in to comment.