convert: Land conversion module

Description Usage Arguments Details Note Examples

Description

This module takes inputs from the targets and constraints modules and determines which pixels are converted. This is an alternate version of convert, which is relies on data.table functions.

Usage

1
convert(conv_prob, target, pot_yield, cropnames, base, ha, keep_index = FALSE)

Arguments

conv_prob

Conversion probability brick

pot_yield

RasterBrick of potential crop yields (modified, if necessary)

cropnames

Vector of crop names in analysis

base

Name of base data.table that provides grid numbers and coordinates

keep_index

FALSE, otherwise valind is retained in the output

prod_targ

Production target data.frame

Details

This function relies on vectorization of input rasters to rank pixels for each crop by their conversion probability, calculate the cumulative production from most likely to least likely to be converted, and then finding the pixels which are less than the production target. These are marked as converted. This function uses the as.data.table.raster function by etiennebr, the link to which is shown in the note below.

Note

https://gist.github.com/etiennebr/9515738

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# We'll compare both raster and data.table methods here
rc <- run_code(input_key = "ZA")  
il <- fetch_inputs(input_key = "ZA")  # fetch all necessary inputs
ybetas <- list(1, 1)
ybeta <- yield_mod(inlist = il[c("p_yield")], ybetas = ybetas, 
                   code = rc, cropnames = il$cropnames) 
clist <- list("y_std" = ybeta$y_std, "C" = il$carbon_p, "bd" = il$cons_p, 
              "cost" = il$cost)
CRSobj <- projection(raster("external/ext_data/ZA-carbon.tif"))
ha <- res(raster("external/ext_data/ZA-carbon.tif"))[2]^2 / 10000
prod_targ <- c("maize" = 4, "soy" = 2)
target <- targets(prod_targ = prod_targ, currprod = il$currprod, 
                  currprodmod = 1)

cbetas <- c("y_std" = 0, "C" = 1, "bd" = 1, "cost" = 1)
conv_prob <- constraints(inlist = clist, cbetas = cbetas, code = rc, 
                         cropnames = il$cropnames)
valind <- which(values(il$mask) == 1) 
system.time(conv <- convert(conv_prob, target = target, 
                            pot_yield = ybeta$p_yield, valind = valind, 
                            cropnames = il$cropnames, 
                            base = il$grid, ha = ha))  # 0.135 seconds
convr2 <- dt_to_raster(dt = conv, CRSobj = projection(il$currprod))
plot(convr - convr2)  # both approaches equal

cbetas <- c("y_std" = 1, "C" = 1, "bd" = 1, "cost" = 0)
conv_prob <- constraints(inlist = clist, cbetas = cbetas, code = rc, 
                         cropnames = il$cropnames, base = il$mask)
conv_probr1 <- dt_list_to_raster(base = base, inlist = list(conv_prob), 
                                 CRSobj = prj)[[1]]

valind <- which(values(il$mask) == 1) 
system.time(conv <- convert(conv_prob, target = target, 
                            pot_yield = ybeta$p_yield, 
                            cropnames = il$cropnames, 
                            base = il$mask, ha = ha))  # 0.135 seconds
convr2 <- dt_to_raster(dt = conv, CRSobj = CRSobj)
#plot(convr - convr2)  # both approaches equal
plot(convr2)

PrincetonUniversity/agroEcoTradeoff documentation built on May 8, 2019, 3:12 p.m.