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.tifn"))
} else {
cat(paste0("Error downloading HeatRisk_", day_number, "_Mercator.tifn"))
}
}
# 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.
[![Screenshot of downloaded .tif files][1]][1]
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!