Skip to content

Commit

Permalink
add coverage_fraction
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 18, 2023
1 parent 13bf1e5 commit 7fa1c2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 31 additions & 2 deletions ext/RbaseTerraExt/RbaseTerraExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ using RCall, NetCDFTools

function init_pkgs()
R"""
library(terra)
library(exactextractr)
suppressMessages({
library(Ipaper)
library(terra)
library(exactextractr)
library(data.table)
})
aperm_array <- function(x) {
dims = dim(x)
Expand Down Expand Up @@ -89,4 +93,29 @@ function NetCDFTools.exact_extract(data, lon, lat, shp, date=nothing; plot=false
""" |> rcopy
end


function NetCDFTools.coverage_fraction(f, shp)
init_pkgs()

R"""
ra = rast($f, lyrs=1)
shp = sf::read_sf($shp)
fraction = exactextractr::coverage_fraction(ra, shp)[[1]]
area = cellSize(ra, unit = "km")
r = c(area, fraction = fraction)
rast_df(r) %>%
mutate(area2 = area * fraction) %>%
.[fraction > 0] %>%
.[, .(I = cell, cell, lon, lat, fraction, area, area2)]
# print(info)
# info
""" |> rcopy
end

# TODO: Test convert julia `Raster` to R `terra::rast`



end
4 changes: 3 additions & 1 deletion src/tools_rbase.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function exact_extract end

export exact_extract
function coverage_fraction end

export exact_extract, coverage_fraction

0 comments on commit 7fa1c2a

Please sign in to comment.