inst/doc/custom-geometry.R

## ----setup, include = FALSE---------------------------------------------------
library(lightson)
# Live downloads write to the user cache, so skip them on CRAN.
live_ok <- identical(Sys.getenv("NOT_CRAN"), "true")
if (live_ok) {
  rasters <- tryCatch(bhuvan_raster("IND", years = 2020:2022), error = function(e) list())
  live_ok <- length(rasters) > 0
}

knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  dev.args = list(bg = "transparent"),
  eval     = live_ok
)

## ----offline-notice, echo = FALSE, results = "asis", eval = !live_ok----------
# cat("_Live data were not downloaded when this vignette was built, so its examples were not run. Run this vignette locally to see the output._")

## ----state-boundaries---------------------------------------------------------
library(sf)

states <- get_india_admin("state")
nrow(states)
head(states[, c("state_name", "state_code")])

## ----state-rasters------------------------------------------------------------
names(rasters)
rasters[["2022"]]

## ----state-panel--------------------------------------------------------------
panel <- extract_panel(rasters, states, id_col = "state_name")
head(panel)

## ----state-trend, fig.width = 7, fig.height = 4-------------------------------
library(ggplot2)
sel <- c("Bihar", "Maharashtra", "Tamil Nadu", "Uttar Pradesh")
plot_ntl_trend(
  panel[panel$region_id %in% sel, ],
  region   = sel,
  caption  = "Source: ISRO Bhuvan NTL portal"
)

## ----state-map, fig.width = 7, fig.height = 6---------------------------------
plot_ntl_map(
  rasters[["2022"]],
  polygons = states,
  title    = "India nighttime lights, 2022"
)

## ----district-boundaries, eval = FALSE----------------------------------------
# BHARATVIZ <- "https://bharatviz.org"
# 
# districts <- sf::read_sf(
#   file.path(BHARATVIZ, "India-bhuvan-districts.geojson")
# )
# nrow(districts)

## ----download-rasters, eval = FALSE-------------------------------------------
# rasters_dist <- bhuvan_raster(districts, years = 2018:2023)
# names(rasters_dist)

## ----extract-panel, eval = FALSE----------------------------------------------
# panel_dist <- extract_panel(rasters_dist, districts, id_col = "district_name")
# head(panel_dist)

## ----plot-trend, eval = FALSE, fig.width = 7, fig.height = 4------------------
# plot_ntl_trend(panel_dist, region = c("Mumbai", "Delhi", "Chennai", "Bengaluru"))

## ----plot-map, eval = FALSE, fig.width = 7, fig.height = 6--------------------
# plot_ntl_map(rasters_dist[["2022"]], polygons = districts)

## ----lgd, eval = FALSE--------------------------------------------------------
# lgd <- sf::read_sf(file.path(BHARATVIZ, "India_LGD_districts.geojson"))
# panel_lgd <- extract_panel(rasters_dist, lgd, id_col = "district_name")
# head(panel_lgd)

## ----historical, eval = FALSE-------------------------------------------------
# dist_2011 <- sf::read_sf(file.path(BHARATVIZ, "India-2011-districts.geojson"))
# panel_2011 <- extract_panel(rasters_dist, dist_2011)
# head(panel_2011)

## ----viirs, eval = FALSE------------------------------------------------------
# token <- earthdata_token()
# rasters_viirs <- ntl_download(region = districts, years = 2018:2023, token = token)
# if (length(rasters_viirs) > 0) {
#   panel_viirs <- extract_panel(rasters_viirs, districts, id_col = "district_name")
#   head(panel_viirs)
# }

Try the lightson package in your browser

Any scripts or data that you put into this service are public.

lightson documentation built on Nov. 3, 2026, 5:07 p.m.