I'm trying to download and parse GeoTiff files and extract alphanumeric data from them. The files are provided here:
https://www.wpc.ncep.noaa.gov/heatrisk/data.html
The files are categorical projections of heat risk (e.g., "High", "Very High", etc.) at various California substate geographies. The data I want are the projections and geo IDs (e.g., county names) associated with each projection, not raster data (for now).
(Note: the .kml
files provided on the data page are empty, per correspondence with NWS.)
Downloading the .tif files
This code appears to work:
#Load Packages
#install.packages("raster")
#install.packages("terra")
#install.packages("vapour")
library(raster)
library(httr)
library(terra)
#Read In GeoTiff Files
#Base URL
base_url <- https://www.wpc.ncep.noaa.gov/heatrisk/data/
#Function to Read in Files
download_heatrisk_geotiff <- function(day_number) {
url <- paste0(base_url, "?day=", day_number)
response <- GET(url)
if (status_code(response) == 200) {
# Parse the TIFF content
tif_content <- content(response, "text")
# Save KML content to a file
writeLines(tif_content, paste0(getwd(), "/data/HeatRisk_", day_number, "_Mercator.tif"))
cat(paste0("Downloaded HeatRisk_", day_number, "_Mercator.tif\n"))
} else {
cat(paste0("Error downloading HeatRisk_", day_number, "_Mercator.tif\n"))
}
}
# Download .tif files for individual days
for (day in 1:7) {
download_heatrisk_geotiff(day)
}
Image of downloaded files
It's not clear that the data I want is in the downloaded files.
Extracting Data
#Extract Data
geotiff_file <- raster(paste0(getwd(), "/data/HeatRisk_", day_number, "_Mercator.tif"))
geotiff_file2 <- terra::readValues(paste0(getwd(), "/data/HeatRisk_", day_number, "_Mercator.tif"))
geotiff_file3 <- vapour::vapour_read_raster(paste0(getwd(), "/data/HeatRisk_", day_number, "_Mercator.tif"))
I've already consulted these pages:
https://inbo.github.io/tutorials/tutorials/spatial_standards_raster/
R: reading geotiff data straight from web url (httr::GET raw content)
How to read a GeoTIFF file and return in a specific datatype in R language?
Thanks!
You have made two assumptions about the data you have linked to that are incorrect:
Here is a workflow to at least get you started. It involves downloading the relevant .tif files and writing them to a directory. If you do need the data at substate level and you cannot find a source, you can post a new question asking how to create these data using the data in this example.
library(terra)
library(httr)
#Base URL
base_url <- "https://www.wpc.ncep.noaa.gov/heatrisk/data/"
#Function to Read in Files
download_heatrisk_geotiff <- function(day_number) {
url <- paste0(base_url, "HeatRisk_", 1, "_Mercator.tif")
response <- GET(url)
if (status_code(response) == 200) {
r <- rast(response$url)
writeRaster(r, paste0(getwd(), "/data/HeatRisk_", day_number, "_Mercator.tif"),
overwrite = TRUE)
cat(paste0("Downloaded HeatRisk_", day_number, "_Mercator.tif\n"))
} else {
cat(paste0("Error downloading HeatRisk_", day_number, "_Mercator.tif\n"))
}
}
# Download .tif files for individual days
for (day in 1:7) {
download_heatrisk_geotiff(day)
}
# Load raster from directory
geotiff_file <- rast(paste0(getwd(), "/data/HeatRisk_", 1, "_Mercator.tif"))
# Returin metadata
geotiff_file
# class : SpatRaster
# dimensions : 1672, 2866, 1 (nrow, ncol, nlyr)
# resolution : 2539.703, 2539.703 (x, y)
# extent : -14326022, -7047233, 2475760, 6722144 (xmin, xmax, ymin, ymax)
# coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
# source : HeatRisk_1_Mercator.tif
# color table : 1
# name : HeatRisk_1_Mercator
# min value : 0
# max value : 4