Anton
Anton

Reputation: 254

Use netCDF file in R as panel data sf object

I have a netCDF file from: Kummu, Matti; Taka, Maija; Guillaume, Joseph H. A. (2020), Data from: Gridded global datasets for Gross Domestic Product and Human Development Index over 1990-2015, Dryad, Dataset, https://doi.org/10.5061/dryad.dk1j0

More specifically, I want to use this dataset: https://drive.google.com/file/d/1-PUa0R0ZcUCJbA1VIMe0KKdElHX2C3Cc/view?usp=sharing which is in netCDF format. I would like to import it as a panel dataset sf-object such that I can spatia-join it with a different sf-object. I tried using

ppp <- stars::read_stars('~/GDP_per_capita_PPP_1990_2015_v2.nc', var="GDP_per_capita_PPP")
ppp <- sf::st_as_sf(ppp)

The result is a panel-dataset with over 9 million observations but the geometry seems to be off. I am only interested in African countries, maybe this helps reduce the dataset, but from the sf-object I get I do not know how to proceed.

Upvotes: 0

Views: 426

Answers (1)

akshaymoorthy
akshaymoorthy

Reputation: 346

The code below to combine the gridded gdp with the centroid of an administrative region (which was an SF object - represented by obj2 in the code snippet).

If you need to aggregate the grids into an administrative region (say by averaging over the region), have a look at exactextractr

library(sf)
library(raster)
library(terra)

obj1 <- stack("./doi_10.5061_dryad.dk1j0__v2/GDP_per_capita_PPP_1990_2015_v2.nc")

extract.df <- terra::extract(obj1, obj2, df = T)

Upvotes: 1

Related Questions