Stars: no as(<stars_proxy>) method to convert stars_proxy objects in to raster* etc.

Created on 10 Jul 2019  路  7Comments  路  Source: r-spatial/stars

There is no as() method for stars_proxy objects to convert them into the equivalent Raster object.

IIRC a stars_proxy should have an equivalent Raster* (RasterBrick I think) object, which also stores data on disk.

Reprex:

library(raster)
library(stars)

tif = system.file("tif/L7_ETMs.tif", package = "stars")

x = read_stars(tif, proxy = TRUE)
x.r = as(x, "Raster")
#> Error in as(x, "Raster"): no method or default for coercing "stars_proxy" to "Raster"

Created on 2019-07-10 by the reprex package (v0.3.0.9000)

All 7 comments

That's interesting,a brick could take over the proxy task (with limitations), or transfer to native RRASTER .grd (it's a gdal driver), or take it into memory. Which are you more interested in?

Not familiar enough with gdal to know what RRASTER.grd is. My expectation would be that converting a stars_proxy to a raster* would keep it on disk though. Does that answer your question?

Not really, it's a bit subtle but raster is often "proxy" by default, but it only supports one-step of laziness - as soon as you crop or modify a layer it will cut ties with the source and a) pull into memory OR b) offload to native raster file (.grd), whereas stars is going to accumulate a number of ops before pulling any data. (There's a third option with the ubiquitous 'filename =' argument in many raster functions, where you can divert the operation straight to disk without raster making that decision for you).

So, in simplest terms we should just raster::brick(<stars-source-files>) which is kind of trivial and unnecessary for stars to do, or work to transfer data from stars to raster, either via memory or file. To me that implies a huge breadth of possibilities, so I'm looking for a specific use-case motivation.

I think it's simply

raster::brick(unlist(obj))

I will add that as a method.

@mdsumner :
Thanks for the explanation. That makes sense. The problem with raster::brick(<stars-source-files>) is that it wouldn't preserve transformations applied to the stars_proxy object.

The specific use case is for working with other packages that require raster* objects. I'm doing most of my manipulations and cropping of rasters in stars, but then I need to convert to a raster for use with other packages (in particular, gdistance).

I've tried both:
raster::brick(<stars-source-files>)
raster::brick(unlist(obj))

And neither preserve transformations of the stars_proxy.

Good point. This patch processes the call list, when present, and writes to disk before calling raster::brick; pls check.

Thanks! This works at least in my case.

Also, dude, you are incredibly fast and helpful with these things. It's great.

Was this page helpful?
0 / 5 - 0 ratings