Extract and aggregate nighttime lights data from NASA Black Marble data
Usage
bm_extract(
roi_sf,
product_id,
date,
bearer,
aggregation_fun = c("mean"),
add_n_pixels = TRUE,
variable = NULL,
quality_flag_rm = NULL,
check_all_tiles_exist = TRUE,
interpol_na = FALSE,
output_location_type = "memory",
file_dir = NULL,
file_prefix = NULL,
file_skip_if_exists = TRUE,
file_return_null = FALSE,
h5_dir = NULL,
quiet = FALSE,
...
)
Arguments
- roi_sf
Region of interest; sf polygon. Must be in the WGS 84 (epsg:4326) coordinate reference system.
- product_id
One of the following:
"VNP46A1"
: Daily (raw)"VNP46A2"
: Daily (corrected)"VNP46A3"
: Monthly"VNP46A4"
: Annual
- date
Date of raster data. Entering one date will produce a
SpatRaster
object. Entering multiple dates will produce aSpatRaster
object with multiple bands; one band per date.For
product_id
s"VNP46A1"
and"VNP46A2"
, a date (eg,"2021-10-03"
).For
product_id
"VNP46A3"
, a date or year-month (e.g.,"2021-10-01"
, where the day will be ignored, or"2021-10"
).For
product_id
"VNP46A4"
, year or date (e.g.,"2021-10-01"
, where the month and day will be ignored, or2021
).
- bearer
NASA bearer token. For instructions on how to create a token, see here.
- aggregation_fun
Function used to aggregate nighttime lights data to polygons; this values is passed to the
fun
argument in exactextractr::exact_extract (Default:mean
).- add_n_pixels
Whether to add a variable indicating the number of nighttime light pixels used to compute nighttime lights statistics (eg, number of pixels used to compute average of nighttime lights). When
TRUE
, it adds three values:n_non_na_pixels
(the number of non-NA
pixels used for computing nighttime light statistics);n_pixels
(the total number of pixels); andprop_non_na_pixels
the proportion of the two. (Default:TRUE
).- variable
Variable to used to create raster (default:
NULL
). IfNULL
, uses the following default variables:For
product_id
:VNP46A1"
, usesDNB_At_Sensor_Radiance_500m
.For
product_id
"VNP46A2"
, usesGap_Filled_DNB_BRDF-Corrected_NTL
.For
product_id
s"VNP46A3"
and"VNP46A4"
, usesNearNadir_Composite_Snow_Free
. For information on other variable choices, see here; forVNP46A1
, see Table 3; forVNP46A2
see Table 6; forVNP46A3
andVNP46A4
, see Table 9.
- quality_flag_rm
Quality flag values to use to set values to
NA
. Each pixel has a quality flag value, where low quality values can be removed. Values are set toNA
for each value in therquality_flag_rm
vector. (Default:NULL
).For
VNP46A1
andVNP46A2
(daily data):0
: High-quality, Persistent nighttime lights1
: High-quality, Ephemeral nighttime Lights2
: Poor-quality, Outlier, potential cloud contamination, or other issues
For
VNP46A3
andVNP46A4
(monthly and annual data):0
: Good-quality, The number of observations used for the composite is larger than 31
: Poor-quality, The number of observations used for the composite is less than or equal to 32
: Gap filled NTL based on historical data
- check_all_tiles_exist
Check whether all Black Marble nighttime light tiles exist for the region of interest. Sometimes not all tiles are available, so the full region of interest may not be covered. If
TRUE
, skips cases where not all tiles are available. (Default:TRUE
).- interpol_na
When data for more than one date is downloaded, whether to interpolate
NA
values in rasters using theterra::approximate
function. Additional arguments for theterra::approximate
function can also be passed intobm_extract
(eg,method
,rule
,f
,ties
,z
,NA_rule
). (Default:FALSE
).- output_location_type
Where to produce output; either
memory
orfile
. Ifmemory
, functions returns a dataframe in R. Iffile
, function exports a.csv
file and returnsNULL
.- file_dir
(If
output_location_type = file
). The directory where data should be exported (default:NULL
, so the working directory will be used)- file_prefix
(If
output_location_type = file
). Prefix to add to the file to be saved. The file will be saved as the following:[file_prefix][product_id]_t[date].csv
- file_skip_if_exists
(If
output_location_type = file
). Whether the function should first check wither the file already exists, and to skip downloading or extracting data if the data for that date if the file already exists (default:TRUE
).- file_return_null
Whether to return
NULL
instead of adataframe
. Whenoutput_location_type = 'file'
, the function will export data to thefile_dir
directory. Whenfile_return_null = FALSE
, the function will also return adataframe
of the queried data---so the data is available in R memory. Settingfile_return_null = TRUE
, data will be saved tofile_dir
but no data will be returned by the function to R memory (default:FALSE
).- h5_dir
Black Marble data are originally downloaded as
h5
files. Ifh5_dir = NULL
, the function downloads to a temporary directory then deletes the directory. Ifh5_dir
is set to a path,h5
files are saved to that directory and not deleted. The function will then check if the neededh5
file already exists in the directory; if it exists, the function will not re-download theh5
file.- quiet
Suppress output that show downloading progress and other messages. (Default:
FALSE
).- ...
Additional arguments for
terra::approximate
, ifinterpol_na = TRUE
Examples
if (FALSE) {
# Define bearer token
bearer <- "BEARER-TOKEN-HERE"
# sf polygon of Ghana
library(geodata)
roi_sf <- gadm(country = "GHA", level=1, path = tempdir()) %>% st_as_sf()
# Daily data: raster for October 3, 2021
ken_20210205_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2021-10-03",
bearer = bearer)
# Monthly data: raster for March 2021
ken_202103_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A3",
date = "2021-03-01",
bearer = bearer)
# Annual data: raster for 2021
ken_2021_r <- bm_extract(roi_sf = roi_sf,
product_id = "VNP46A4",
date = 2021,
bearer = bearer)
}