英文:
How do I ignore/force scale and offset when reading a COG with R terra
问题
我正在使用terra
包从LPCLOUD加载大量的HLS.S30和HLS.L30 COGs作为SpatRasters。我注意到其中一些像素值是整数(介于0和1000之间),而其他一些是浮点数(介于0和1之间)。使用terra::describe
检查文件时,发现其中一些具有" Offset: 0, Scale:0.0001"字段,而其他一些没有,我认为这就是导致这种不一致性的原因。
是否有一种方法可以忽略这些比例/偏移,或者在使用terra::rast
读取数据时强制设置scale=1,offset=0,也许可以在GDAL选项中实现?
可重现的示例(需要NASA Earthdata登录):
library(terra)
setGDALconfig("GDAL_HTTP_UNSAFESSL", value = "YES")
setGDALconfig("GDAL_HTTP_COOKIEFILE", value = ".rcookies")
setGDALconfig("GDAL_HTTP_COOKIEJAR", value = ".rcookies")
setGDALconfig("GDAL_DISABLE_READDIR_ON_OPEN", value = "EMPTY_DIR")
setGDALconfig("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", value = "TIF")
url1 <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T32ULC.2022237T103641.v2.0/HLS.S30.T32ULC.2022237T103641.v2.0.B08.tif"
url2 <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T31UGT.2022237T103641.v2.0/HLS.S30.T31UGT.2022237T103641.v2.0.B08.tif"
r1 <- rast(url1, vsi=TRUE)
r2 <- rast(url2, vsi=TRUE)
r1[1]
r2[2]
describe(url1)
describe(url2)
英文:
I am loading a large number of HLS.S30 and HLS.L30 COGs from LPCLOUD as SpatRasters using the terra
package. I noticed that the pixel values in some of them ended up being integers (between 0 and 1000), and others floats (between 0 and 1). Inspecting the files with terra::describe
showed that some had a " Offset: 0, Scale:0.0001" field, and others didn't, which is what I assume creates this inconsistency.
Is there a way to ignore these scale/offset, or force a scale=1, offset=0 when reading date with terra::rast
, perhaps in the GDAL options?
Reproducible example (requires NASA Earthdata login):
library(terra)
setGDALconfig("GDAL_HTTP_UNSAFESSL", value = "YES")
setGDALconfig("GDAL_HTTP_COOKIEFILE", value = ".rcookies")
setGDALconfig("GDAL_HTTP_COOKIEJAR", value = ".rcookies")
setGDALconfig("GDAL_DISABLE_READDIR_ON_OPEN", value = "EMPTY_DIR")
setGDALconfig("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", value = "TIF")
url1 <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T32ULC.2022237T103641.v2.0/HLS.S30.T32ULC.2022237T103641.v2.0.B08.tif"
url2 <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T31UGT.2022237T103641.v2.0/HLS.S30.T31UGT.2022237T103641.v2.0.B08.tif"
r1 <- rast(url1, vsi=TRUE)
r2 <- rast(url2, vsi=TRUE)
r1[1]
r2[2]
describe(url1)
describe(url2)
答案1
得分: 0
你应该能够使用 terra::scoff
来设置或移除比例和偏移。例如,你可以执行以下操作:
scoff(r1) <- NULL
scoff(r2) <- NULL
或者等效地执行:
scoff(r1) <- cbind(1, 0)
scoff(r2) <- cbind(1, 0)
英文:
You should be able to set or remove the scale and offset with terra::scoff
. For example, you can do
scoff(r1) <- NULL
scoff(r2) <- NULL
Or the equivalent
scoff(r1) <- cbind(1, 0)
scoff(r2) <- cbind(1, 0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论