hypertidy / ximage

good image()
https://hypertidy.github.io/ximage/
Other
7 stars 1 forks source link

support gdalraster attributed output #11

Open mdsumner opened 9 months ago

mdsumner commented 9 months ago

https://github.com/USDAForestService/gdalraster/pull/198

mdsumner commented 9 months ago

done in

https://github.com/hypertidy/ximage/commit/daeff302faab67879013419f8eab8875ee20f38c

I fixed up a couple of other things there, so may well have broken other parts. All needs a bit of a rethink (after I explore a bit more).

library(gdalraster)
  tms <- "<GDAL_WMS><Service name=\"TMS\"><ServerUrl>http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/${z}/${y}/${x}</ServerUrl></Service><DataWindow><UpperLeftX>-20037508.34</UpperLeftX><UpperLeftY>20037508.34</UpperLeftY><LowerRightX>20037508.34</LowerRightX><LowerRightY>-20037508.34</LowerRightY><TileLevel>17</TileLevel><TileCountX>1</TileCountX><TileCountY>1</TileCountY><YOrigin>top</YOrigin></DataWindow><Projection>EPSG:900913</Projection><BlockSizeX>256</BlockSizeX><BlockSizeY>256</BlockSizeY><BandsCount>3</BandsCount><MaxConnections>10</MaxConnections><Cache /></GDAL_WMS>"
  gebco <- "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif"

  ds <- new(GDALRaster, tms)  

r <-   read_ds(ds, out_xsize = 1024, out_ysize = 1024, as_list = TRUE)
ximage(r)

ds <- new(GDALRaster, gebco)  

r <-   read_ds(ds, out_xsize = 1024, out_ysize = 512, as_list = TRUE)
ximage(r)
ctoney commented 9 months ago

It works for me. Nice examples.

ctoney commented 9 months ago

Could there be something like this where x comes in? (no worries if not!)

if (is.numeric(x) && "gis" %in% names(attributes(x))) {
    ## gdalraster output
    gis <- attr(x, "gis")
    x_list <- asplit(array(x, dim = gis$dim), 3)
    attr(x_list, "gis") <- gis
    ximage.list(x_list, extent = extent, zlim = zlim, add = add, ..., xlab = xlab, ylab = ylab, col = col)
}

Then the output from gdalraster could be either list, or vector interleaved by band. With the above, the list elements become 2-D arrays instead of vectors but it doesn't seem to matter. This works (drops as_list = TRUE so r is vector):

base_url <- "/vsicurl/https://raw.githubusercontent.com/usdaforestservice/gdalraster/main/sample-data/"
f <- paste0(base_url, "landsat_c2ard_sr_mt_hood_jul2022_utm.tif")
ds <- new(GDALRaster, f)
r <- read_ds(ds, bands=c(7,5,4))

gis <- attr(r, "gis")
r <- asplit(array(r, dim = gis$dim), 3)
attr(r, "gis") <- gis
ximage(r)

This is an aside, but the default normalization must be slightly different. Not a problem or anything, just noticing:

plot_raster(r)
mdsumner commented 9 months ago

ah yes for sure, normalizing is one of those things I've let slip in terms of details - I'm hoping to have a deeper review of the entire code base here in coming weeks

could you please PR the change for the vector form, just so I don't lose track of it? got a busy week but will be back at this ~23 Feb ... if not I will keep this issue in my pile

(I had no idea about asplit!! that's awesome, I always do split with an awkward rep/seq ...)

ctoney commented 9 months ago

will do, thanks