cjerzak's picture
Update scripts/tif2csv.R
b5d9091 verified
raw
history blame contribute delete
984 Bytes
{
setwd("~/Downloads")
# Download the GeoTIFF file
url <- "https://huggingface.co/datasets/cjerzak/TemporalNeighborhoodMaterialWealthAfrica/resolve/main/wealth_map.tif"
download.file(url, "wealth_map.tif", mode = "wb")
# Load the terra package
library(terra)
# Read the multiband raster
rast <- rast("wealth_map.tif")
# Assign names to the bands based on time periods
periods <- c("1990-1992", "1993-1995", "1996-1998", "1999-2001", "2002-2004", "2005-2007", "2008-2010", "2011-2013", "2014-2016", "2017-2019")
names(rast) <- periods
# Convert to data frame with longitude and latitude, removing rows with NA values
df <- as.data.frame(rast, xy = TRUE, na.rm = TRUE)
df <- df[rowMeans(df[,-c(1:2)])!=0,]
# Rename coordinate columns
names(df)[1:2] <- c("longitude", "latitude")
# Write to CSV
write.csv(df, "wealth_africa.csv", row.names = FALSE)
# Write to paraquet
arrow::write_parquet(df, "wealth_africa.parquet")
}