Skip to content

Commit

Permalink
no dep on recent GDAL
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Apr 21, 2024
1 parent d721147 commit 7c060ec
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 39 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^README\.Rmd$
^gebco$
^\.github$
^data-raw$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: filearchy
Title: Write Imagery to Tiles
Version: 0.1.0
Version: 0.1.0.9001
Authors@R:
c(person(given = "Michael D.",
family = "Sumner",
Expand Down
17 changes: 9 additions & 8 deletions R/gdal_tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ write_tile <- function(tile, dataset, output_dir = tempdir(), overwrite = FALSE)
#' @importFrom furrr future_map
#' @importFrom methods new
#' @examples
#' #p <- system.file("help/figures/logo.png", package = "survival", mustWork = TRUE)
#' #ss <- paste0(c("vrt://%s?outsize=500%s,500%s&a_ullr=",
#' # "-20037508,20037508,20037508,-20037508&a_srs=EPSG:3857"), collapse = "")
#' #dsn <- sprintf(ss, p, "%", "%")
#' dsn <- sprintf("vrt://%s?ovr=5&ot=Byte&scale=true",
#' "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif")
#' dsn <- system.file("extdata/gebco_ovr5.vrt", package = "filearchy", mustWork = TRUE)
#' ## parallelize here
#' #future::plan(multicore)
#' tiles <- gdal_tiles(dsn)
#' if (!interactive()) unlink(tiles$path)
#' #future::plan(sequential)
gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857", update = TRUE, output_dir = tempfile(), overwrite = FALSE) {
opt <- gdalraster::get_config_option("GDAL_PAM_ENABLED")
#browser()
opt <- gdalraster::get_config_option("GDAL_PAM_ENABLED")
gdalraster::set_config_option("GDAL_PAM_ENABLED", "NO")
on.exit(gdalraster::set_config_option("GDAL_PAM_ENABLED", opt), add = TRUE)

Expand Down Expand Up @@ -71,8 +67,13 @@ gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857",
ex[4] <- min(c(ex[4], 20037508.342789244))

filelist <- base$getFileList()
print(filelist)
#[1] "/vsimem/file70fd4d3a0623.vrt"
#[2] "/perm_storage/home/mdsumner/R/x86_64-pc-linux-gnu-library/4.3/filearchy/extdata/gebco_ovr5.vrt"

base$close()
unlink(filelist)
## this would delete our source, so how do we only clean up the vsimem? do we?
#unlink(filelist)

for (i in seq_len(nzoom)) {
isize <- basesize * 2^(i-1)
Expand Down
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ devtools::install_github("hypertidy/filearchy")

## Example

'gebco_ovr5.vrt' is a pre-prepared version of GEBCO 2023 elevation COG that has been Byte-scaled and reduced to the zoom overview 5

```{r example}
library(filearchy)
#options(parallelly.fork.enable = TRUE, future.rng.onMisuse = "ignore")
#library(future); plan(multicore)
dsn <- sprintf("vrt://%s?ovr=5&ot=Byte&scale=true", "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif")
dsn <- system.file("extdata/gebco_ovr5.vrt", package = "filearchy", mustWork = TRUE)
tiles <- gdal_tiles(dsn)
#plan(sequential)
fs::dir_ls(dirname(dirname(dirname(tiles$path[1]))), recurse = TRUE, type = "f")
Expand Down
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,41 @@ devtools::install_github("hypertidy/filearchy")

## Example

‘gebco_ovr5.vrt’ is a pre-prepared version of GEBCO 2023 elevation COG
that has been Byte-scaled and reduced to the zoom overview 5

``` r
library(filearchy)
#options(parallelly.fork.enable = TRUE, future.rng.onMisuse = "ignore")
#library(future); plan(multicore)
dsn <- sprintf("vrt://%s?ovr=5&ot=Byte&scale=true", "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif")
dsn <- system.file("extdata/gebco_ovr5.vrt", package = "filearchy", mustWork = TRUE)
tiles <- gdal_tiles(dsn)
#> [1] "tiles in directory: /tmp/RtmpdO9jh2/file6c22479521953"
#> [1] "/vsimem/file71922344929b1.vrt"
#> [2] "/perm_storage/home/mdsumner/R/x86_64-pc-linux-gnu-library/4.3/filearchy/extdata/gebco_ovr5.vrt"
#> [1] "tiles in directory: /tmp/RtmpJu1hLW/file71922726e876e"
#plan(sequential)
fs::dir_ls(dirname(dirname(dirname(tiles$path[1]))), recurse = TRUE, type = "f")
#> /tmp/RtmpdO9jh2/file6c22479521953/0/0/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/1/0/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/1/0/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/1/1/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/1/1/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/0/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/0/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/0/2.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/0/3.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/1/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/1/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/1/2.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/1/3.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/2/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/2/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/2/2.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/2/3.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/3/0.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/3/1.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/3/2.png
#> /tmp/RtmpdO9jh2/file6c22479521953/2/3/3.png
#> /tmp/RtmpJu1hLW/file71922726e876e/0/0/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/1/0/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/1/0/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/1/1/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/1/1/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/0/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/0/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/0/2.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/0/3.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/1/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/1/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/1/2.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/1/3.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/2/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/2/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/2/2.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/2/3.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/3/0.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/3/1.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/3/2.png
#> /tmp/RtmpJu1hLW/file71922726e876e/2/3/3.png

gdalraster::createCopy("GTiff", tf <- tempfile(fileext = ".tif"), tiles$path[1])
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
Expand Down
4 changes: 4 additions & 0 deletions data-raw/gebco_vrt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dir.create("inst/extdata")
system(sprintf("gdal_translate %s inst/extdata/gebco_ovr5.vrt",
sprintf("\"vrt://%s?ovr=5&ot=Byte&scale=true\"", "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif")))

26 changes: 26 additions & 0 deletions inst/extdata/gebco_ovr5.vrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<VRTDataset rasterXSize="1350" rasterYSize="675">
<SRS dataAxisToSRSAxisMapping="2,1">GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]</SRS>
<GeoTransform> -1.8000000000000000e+02, 2.6666666666666666e-01, 0.0000000000000000e+00, 9.0000000000000000e+01, 0.0000000000000000e+00, -2.6666666666666666e-01</GeoTransform>
<Metadata>
<MDI key="AREA_OR_POINT">Area</MDI>
</Metadata>
<Metadata domain="IMAGE_STRUCTURE">
<MDI key="INTERLEAVE">BAND</MDI>
<MDI key="COMPRESSION">DEFLATE</MDI>
</Metadata>
<VRTRasterBand dataType="Byte" band="1" blockXSize="512" blockYSize="512">
<ColorInterp>Gray</ColorInterp>
<ComplexSource>
<SourceFilename relativeToVRT="0">/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif</SourceFilename>
<OpenOptions>
<OOI key="OVERVIEW_LEVEL">5 only</OOI>
</OpenOptions>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="1350" RasterYSize="675" DataType="Int16" BlockXSize="512" BlockYSize="512" />
<SrcRect xOff="0" yOff="0" xSize="1350" ySize="675" />
<DstRect xOff="0" yOff="0" xSize="1350" ySize="675" />
<ScaleOffset>164.795</ScaleOffset>
<ScaleRatio>0.0172344</ScaleRatio>
</ComplexSource>
</VRTRasterBand>
</VRTDataset>
Binary file modified man/figures/README-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 1 addition & 6 deletions man/gdal_tiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c060ec

Please sign in to comment.