简体   繁体   English

在 R 中的单个 STARS 像素和 SF 纬度/经度坐标之间进行转换

[英]Convert between individual STARS pixels and SF lat/long coordinates in R

I have a STARS raster object raster in R (eg of size 100x100) and can get the coordinates using st_coordinates(raster)我在 R 中有一个 STARS 光栅对象raster (例如,大小为 100x100)并且可以使用st_coordinates(raster)获取坐标

But I'm having problems converting between the two.但是我在两者之间转换时遇到了问题。 For example, I'd like to know the lat/long coordinates for only a particular raster pixel, eg (50, 50).例如,我想知道的经度/纬度坐标为在特定光栅像素,例如(50,50)。

I was assuming that st_coordinates would give me a 1D array, so I can simply convert from the 2D raster matrix to the 1D array (eg by converting the 2D index (50, 50) into a 1D index using something like #columns*i+j , which in my example is 100*50+50 ).我假设 st_coordinates 会给我一个一维数组,所以我可以简单地从二维栅格矩阵转换为一维数组(例如,通过使用#columns*i+j类的东西将二维索引 (50, 50) 转换为一维索引#columns*i+j ,在我的例子中是100*50+50 )。

This isn't working though, which makes me think I'm misunderstanding how the STARS raster object and coordinates indices map onto each other.但这不起作用,这让我觉得我误解了 STARS 光栅对象和坐标索引如何相互映射。 Does anyone know how to bridge between the two, on a pixel-by-coordinate basis?有谁知道如何在逐个像素的基础上在两者之间架起桥梁?

=== ===

Updated with an example of what I'm trying to do:更新了我正在尝试做的一个例子:

    r <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))

    ## Let's say I want to grab the pixel 50x50 from the first band. 
    ## I can do that going into the raster matrix like this:

    pixel <- r[[1]][50,50,1]

   ## now I can get the coordinates for the raster
   ## using sf like this
   ## this gives me a coordinate df with columns x, y and band.
 
    coordinates <- st_coordinates(r)

   ## what I want to do now is get the coordinate for the
   ## pixel I listed earlier, (50, 50, 1)  -- in my case, I only
   ## have one band so I'm not going to worry about band index logic

   ## to do this, I assume that (50, 50) is the same as
   ## ncol(r)*(50-1)+50 or 17298. I say 50-1 since R is using 1 indexing.
   ## I use this to get a coordinate from coordinates like this:

   coord <- coordinates[ncol(r)*(50-1)+50,]

   ## This returns me the following:
   

>    17298 294376.5 9119350    1

   ## If I wanted to do this many times, I could make a list 
   ## of coordinates for various pixels, then put them into 
   ## a new sf object using st_as_sf(...)

When I tried doing the above in a loop and plotting the results, there was a substantial mismatch... the raster pixels did not map to the right coordinates after plotting them in a new sf object.当我尝试在循环中执行上述操作并绘制结果时,存在很大的不匹配......在将它们绘制到新的 sf 对象中后,光栅像素没有映射到正确的坐标。 This has me thinking my conversion from raster 2D array to coordinate 1D list is not right.这让我认为我从光栅 2D 数组到坐标 1D 列表的转换是不正确的。 In fact, I'm realizing I have no idea at all what logic sf uses to convert the raster to a 1D list, which might explain the problem... Do you have any ideas about how these map to each other and how to index the coordinates array for a given raster pixel?事实上,我意识到我完全不知道 sf 使用什么逻辑将栅格转换为一维列表,这可能解释了这个问题......你对这些如何相互映射以及如何索引有任何想法吗?给定光栅像素的坐标数组? Please let me know if I still need to clarify further.如果我还需要进一步澄清,请告诉我。 Thanks!谢谢!

I think the key here is to subset a stars object then feed the subsetted object into st_coordinates .我认为这里的关键是对一个stars对象进行子集化,然后将子集化的对象提供给st_coordinates

r <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))

# extract band 1 raster value at position 50, 50
# two methods with same result

# convert to matrix then subset
r[[1]][50,50,1] 
#[1] 56


# subset stars raster then extract value
r[,50,50,1][[1]] 
#, , 1
#
#     [,1]
#[1,]   56

If you use the subset stars raster then extract value workflow, you can use the subsetted raster in the st_coordinates function.如果使用子集星栅格然后提取值工作流,则可以在st_coordinates函数中使用子集栅格。

st_coordinates(r[,50,50,1])
#        x       y band
#1 290187 9119350    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM