如何在使用R terra读取COG时忽略/强制缩放和偏移?

huangapple go评论73阅读模式
英文:

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(&quot;GDAL_HTTP_UNSAFESSL&quot;, value = &quot;YES&quot;)
setGDALconfig(&quot;GDAL_HTTP_COOKIEFILE&quot;, value = &quot;.rcookies&quot;)
setGDALconfig(&quot;GDAL_HTTP_COOKIEJAR&quot;, value = &quot;.rcookies&quot;)
setGDALconfig(&quot;GDAL_DISABLE_READDIR_ON_OPEN&quot;, value = &quot;EMPTY_DIR&quot;)
setGDALconfig(&quot;CPL_VSIL_CURL_ALLOWED_EXTENSIONS&quot;, value = &quot;TIF&quot;)

url1 &lt;- &quot;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&quot;
url2 &lt;- &quot;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&quot;

r1 &lt;- rast(url1, vsi=TRUE)
r2 &lt;- 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) &lt;- NULL
scoff(r2) &lt;- NULL

Or the equivalent

scoff(r1) &lt;- cbind(1, 0)
scoff(r2) &lt;- cbind(1, 0)

huangapple
  • 本文由 发表于 2023年6月13日 15:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定