Skip to content

Commit

Permalink
nara for warper
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Jan 29, 2024
1 parent 8216dd2 commit e26510d
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 52 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: vapour
Title: Access to the 'Geospatial Data Abstraction Library' ('GDAL')
Version: 0.9.5.9008
Version: 0.9.5.9009
Authors@R: c(person("Michael", "Sumner", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2471-7511")),
person("Simon", "Wotherspoon", role = "ctb", comment = "RasterIO configuration for resampling options"),
person("Mark", "Padgham", role = "ctb", comment = "helped get started :)"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(buildvrt)
export(gdal_raster_data)
export(gdal_raster_dsn)
export(gdal_raster_image)
export(gdal_raster_nara)
export(vapour_all_drivers)
export(vapour_create)
export(vapour_create_options)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vapour dev

* New function `gdal_raster_nara()` to return nativeRaster in a list understood by `ximage()`.

* New function `vector_vrt()` to generate VRT for SQL and/or reprojection.

* Fix cross-compilation for ARM on universe, thanks to Jeroen Ooms.
Expand Down
4 changes: 2 additions & 2 deletions R/00_read_block.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ vapour_create_copy <- function(dsource, filename, overwrite = FALSE, driver = "G
#' @examples
#' f <- system.file("extdata", "sst.tif", package = "vapour")
#' v <- vapour_read_raster_block(f, c(0L, 0L), dimension = c(2L, 3L), band = 1L)
vapour_read_raster_block <- function(dsource, offset, dimension, band = 1L, band_output_type = "", unscale = TRUE) {
vapour_read_raster_block <- function(dsource, offset, dimension, band = 1L, band_output_type = "", unscale = TRUE, nara = FALSE) {
dsource <- .check_dsn_single(dsource)
if (anyNA(band) || length(band) < 1L) stop("missing band value")
if (file.exists(dsource)) {
Expand All @@ -146,7 +146,7 @@ vapour_read_raster_block <- function(dsource, offset, dimension, band = 1L, band
vapour_read_raster_block_cpp(dsource, as.integer(rep(offset, length.out = 2L)),
as.integer(rep(dimension, length.out = 2L)), band = as.integer(band[1L]),
band_output_type = band_output_type,
unscale = unscale)
unscale = unscale, nara = nara)
}
#' Write data to a block *in an existing file*.
#'
Expand Down
36 changes: 32 additions & 4 deletions R/00_warpgeneral.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ gdal_raster_data <- function(dsn, target_crs = NULL, target_dim = NULL, target_e
silent = FALSE, band_output_type = band_output_type,
options = options,
dsn_outname = "",
include_meta = include_meta)
include_meta = include_meta, nara = FALSE)
}

#' @name gdal_raster_data
Expand Down Expand Up @@ -153,7 +153,7 @@ gdal_raster_dsn <- function(dsn, target_crs = NULL, target_dim = NULL, target_ex
resample = resample,
silent = FALSE, band_output_type = band_output_type,
options = options,
dsn_outname = out_dsn[1L], include_meta = include_meta)
dsn_outname = out_dsn[1L], include_meta = include_meta, nara = FALSE)
}

#' @name gdal_raster_data
Expand All @@ -166,7 +166,7 @@ gdal_raster_image <- function(dsn, target_crs = NULL, target_dim = NULL, target_
if (is.null(target_ext)) target_ext <- numeric()
if (is.null(target_dim)) target_dim <- integer() #info$dimension
if (is.null(target_res)) target_res <- numeric() ## TODO
if (is.null(band_output_type)) band_output_type <- "UInt8"
if (is.null(band_output_type)) band_output_type <- "Byte"
if (is.null(bands)) {
nbands <- vapour_raster_info(dsn[1])$bands
bands <- seq(min(c(nbands, 4L)))
Expand All @@ -179,11 +179,39 @@ gdal_raster_image <- function(dsn, target_crs = NULL, target_dim = NULL, target_
bands = bands,
resample = resample,
silent = FALSE, band_output_type = band_output_type,
options = options, dsn_outname = "", include_meta = include_meta)
options = options, dsn_outname = "", include_meta = include_meta, nara = FALSE)
atts <- attributes(bytes)
out <- list(as.vector(grDevices::as.raster(array(unlist(bytes, use.names = FALSE), c(length(bytes[[1]]), 1, max(c(3, length(bytes))))))))
attributes(out) <- atts
out
}


#' @name gdal_raster_data
#' @export
gdal_raster_nara <- function(dsn, target_crs = NULL, target_dim = NULL, target_ext = NULL, target_res = NULL,
resample = "near", bands = NULL, band_output_type = NULL, options = character(), include_meta = TRUE) {

if (length(target_res) > 0 ) target_res <- as.numeric(rep(target_res, length.out = 2L))
if (is.null(target_crs)) target_crs <- ""
if (is.null(target_ext)) target_ext <- numeric()
if (is.null(target_dim)) target_dim <- integer() #info$dimension
if (is.null(target_res)) target_res <- numeric() ## TODO
band_output_type <- "Byte"
if (is.null(bands)) {
nbands <- vapour_raster_info(dsn[1])$bands
bands <- seq(min(c(nbands, 4L)))
}
include_meta <- isTRUE(include_meta)
out <- warp_general_cpp(dsn, target_crs,
target_ext,
target_dim,
target_res,
bands = bands,
resample = resample,
silent = FALSE, band_output_type = band_output_type,
options = options, dsn_outname = "", include_meta = include_meta, nara = TRUE)
out
}


16 changes: 8 additions & 8 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

warp_general_cpp <- function(dsn, target_crs, target_extent, target_dim, target_res, bands, resample, silent, band_output_type, options, dsn_outname, include_meta) {
.Call('_vapour_warp_general_cpp', PACKAGE = 'vapour', dsn, target_crs, target_extent, target_dim, target_res, bands, resample, silent, band_output_type, options, dsn_outname, include_meta)
warp_general_cpp <- function(dsn, target_crs, target_extent, target_dim, target_res, bands, resample, silent, band_output_type, options, dsn_outname, include_meta, nara) {
.Call('_vapour_warp_general_cpp', PACKAGE = 'vapour', dsn, target_crs, target_extent, target_dim, target_res, bands, resample, silent, band_output_type, options, dsn_outname, include_meta, nara)
}

warp_suggest_cpp <- function(dsn, target_crs) {
Expand Down Expand Up @@ -153,8 +153,8 @@ raster_extent_cpp <- function(dsn) {
.Call('_vapour_raster_extent_cpp', PACKAGE = 'vapour', dsn)
}

raster_io_gdal_cpp <- function(dsn, window, band, resample, band_output_type, unscale) {
.Call('_vapour_raster_io_gdal_cpp', PACKAGE = 'vapour', dsn, window, band, resample, band_output_type, unscale)
raster_io_gdal_cpp <- function(dsn, window, band, resample, band_output_type, unscale, nara) {
.Call('_vapour_raster_io_gdal_cpp', PACKAGE = 'vapour', dsn, window, band, resample, band_output_type, unscale, nara)
}

sds_list_gdal_cpp <- function(dsn) {
Expand All @@ -165,12 +165,12 @@ sds_list_list_gdal_cpp <- function(dsn) {
.Call('_vapour_sds_list_list_gdal_cpp', PACKAGE = 'vapour', dsn)
}

warp_in_memory_gdal_cpp <- function(dsn, source_WKT, target_WKT, target_extent, target_dim, bands, source_extent, resample, silent, band_output_type, options, nomd, overview) {
.Call('_vapour_warp_in_memory_gdal_cpp', PACKAGE = 'vapour', dsn, source_WKT, target_WKT, target_extent, target_dim, bands, source_extent, resample, silent, band_output_type, options, nomd, overview)
warp_in_memory_gdal_cpp <- function(dsn, source_WKT, target_WKT, target_extent, target_dim, bands, source_extent, resample, silent, band_output_type, options, nomd, overview, nara) {
.Call('_vapour_warp_in_memory_gdal_cpp', PACKAGE = 'vapour', dsn, source_WKT, target_WKT, target_extent, target_dim, bands, source_extent, resample, silent, band_output_type, options, nomd, overview, nara)
}

vapour_read_raster_block_cpp <- function(dsource, offset, dimension, band, band_output_type, unscale) {
.Call('_vapour_vapour_read_raster_block_cpp', PACKAGE = 'vapour', dsource, offset, dimension, band, band_output_type, unscale)
vapour_read_raster_block_cpp <- function(dsource, offset, dimension, band, band_output_type, unscale, nara) {
.Call('_vapour_vapour_read_raster_block_cpp', PACKAGE = 'vapour', dsource, offset, dimension, band, band_output_type, unscale, nara)
}

vapour_write_raster_block_cpp <- function(dsource, data, offset, dimension, band) {
Expand Down
4 changes: 2 additions & 2 deletions R/raster-input.R
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ vapour_warp_raster <- function(x, bands = NULL,
transformation_options = "",
open_options = "",
options = "",
nomd = FALSE, overview = -1L) {
nomd = FALSE, overview = -1L, nara = FALSE) {
x <- .check_dsn_multiple(x)
if (!is.null(bands) && (anyNA(bands) || length(bands) < 1 || !is.numeric(bands))) {
stop("'bands' must be a valid set of band integers (1-based)")
Expand Down Expand Up @@ -516,7 +516,7 @@ vapour_warp_raster <- function(x, bands = NULL,
resample = resample,
silent = silent,
band_output_type = band_output_type,
options = options, nomd = nomd, overview)
options = options, nomd = nomd, overview, nara = nara)
# ##// if we Dataset->RasterIO we don't have separated bands'
# nbands <- length(vals[[1L]]) / prod(as.integer(dimension))
# if (nbands > 1) vals <- split(vals[[1L]], rep(seq_len(nbands), each = prod(as.integer(dimension))))
Expand Down
77 changes: 74 additions & 3 deletions inst/include/gdalraster/gdalraster.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include "vrtdataset.h"
#include <ogr_spatialref.h>

#include <Rinternals.h>


#define R_RGB(r,g,b) ((r)|((g)<<8)|((b)<<16)|0xFF000000)
#define R_RGBA(r,g,b,a) ((r)|((g)<<8)|((b)<<16)|((a)<<24))


namespace gdalraster {
using namespace Rcpp;

Expand All @@ -21,6 +28,63 @@ using namespace Rcpp;
// gdalmin:::open_gdal(c(file, topos))
//

inline SEXP C_native_rgb(SEXP b0, SEXP b1, SEXP b2, SEXP dm) {
SEXP res_ = PROTECT(Rf_allocVector(INTSXP, Rf_length(b0)));
for (int i = 0; i < Rf_length(b0); i++) {
INTEGER(res_)[i] = (int)R_RGB(RAW(b0)[i], RAW(b1)[i], RAW(b2)[i]);
}
SEXP dim;
dim = Rf_allocVector(INTSXP, 2);
INTEGER(dim)[0] = INTEGER(dm)[1];
INTEGER(dim)[1] = INTEGER(dm)[0];
Rf_setAttrib(res_, R_DimSymbol, dim);
Rf_setAttrib(res_, R_ClassSymbol, Rf_mkString("nativeRaster"));
{
SEXP chsym = Rf_install("channels");
Rf_setAttrib(res_, chsym, Rf_ScalarInteger(3));
}
UNPROTECT(1);
return res_;
}


inline SEXP C_native_rgba(SEXP b0, SEXP b1, SEXP b2, SEXP b3, SEXP dm) {
SEXP res_ = PROTECT(Rf_allocVector(INTSXP, Rf_length(b0)));
for (int i = 0; i < Rf_length(b0); i++) {
INTEGER(res_)[i] = (int)R_RGBA(RAW(b0)[i], RAW(b1)[i], RAW(b2)[i], RAW(b3)[i]);
}
SEXP dim;
dim = Rf_allocVector(INTSXP, 2);
INTEGER(dim)[0] = INTEGER(dm)[1];
INTEGER(dim)[1] = INTEGER(dm)[0];
Rf_setAttrib(res_, R_DimSymbol, dim);
Rf_setAttrib(res_, R_ClassSymbol, Rf_mkString("nativeRaster"));
{
SEXP chsym = Rf_install("channels");
Rf_setAttrib(res_, chsym, Rf_ScalarInteger(4));
}
UNPROTECT(1);
return res_;
}


inline List replace_nativeRaster(List inputlist, R_xlen_t dimx, R_xlen_t dimy) {
List outlist_nara = List();

// GREY
if (inputlist.size() == 1) {
outlist_nara.push_back(C_native_rgb(inputlist[0], inputlist[0], inputlist[0], IntegerVector::create(dimx, dimy)));
}
// RGB
if (inputlist.size() == 3) {
outlist_nara.push_back(C_native_rgb(inputlist[0], inputlist[1], inputlist[2], IntegerVector::create(dimx, dimy)));
}
// RGBA (we ignore bands above 4)
if (inputlist.size() >= 4) {
outlist_nara.push_back(C_native_rgba(inputlist[0], inputlist[1], inputlist[2], inputlist[3], IntegerVector::create(dimx, dimy)));
}
return outlist_nara;
}

// does it have subdatasets?
inline bool gdal_has_subdataset(GDALDataset *poDataset) {
Expand Down Expand Up @@ -570,7 +634,8 @@ inline List gdal_read_band_values(GDALDataset *hRet,
std::vector<int> bands_to_read,
CharacterVector band_output_type,
CharacterVector resample,
LogicalVector unscale)
LogicalVector unscale,
LogicalVector nara)
{
int Xoffset = window[0];
int Yoffset = window[1];
Expand Down Expand Up @@ -747,7 +812,12 @@ inline List gdal_read_band_values(GDALDataset *hRet,
GDALClose(hRet);
Rcpp::stop("band type not supported (is it Complex? report at hypertidy/vapour/issues)");
}
Rprintf("%s\n", (const char *)band_output_type[0]);
Rprintf("%i\n", (int)nara[0]);

if (nara[0] && band_output_type[0] == "Byte") {
return replace_nativeRaster(outlist, (R_xlen_t) outXSize, (R_xlen_t) outYSize);
}
return outlist;
}

Expand Down Expand Up @@ -1152,7 +1222,8 @@ inline List gdal_raster_io(CharacterVector dsn,
IntegerVector band,
CharacterVector resample,
CharacterVector band_output_type,
LogicalVector unscale)
LogicalVector unscale,
LogicalVector nara)
{

GDALDataset *poDataset;
Expand All @@ -1171,7 +1242,7 @@ inline List gdal_raster_io(CharacterVector dsn,
} else {
for (int i = 0; i < band.size(); i++) bands_to_read[static_cast<size_t>(i)] = band[i];
}
List out = gdal_read_band_values(poDataset, window, bands_to_read, band_output_type, resample, unscale);
List out = gdal_read_band_values(poDataset, window, bands_to_read, band_output_type, resample, unscale, nara);
// close up
GDALClose(poDataset );
return out;
Expand Down
4 changes: 2 additions & 2 deletions inst/include/gdalreadwrite/gdalreadwrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ inline CharacterVector gdal_create_copy(CharacterVector dsource, CharacterVector
}
inline List gdal_read_block(CharacterVector dsn, IntegerVector offset,
IntegerVector dimension, IntegerVector band,
CharacterVector band_output_type, LogicalVector unscale) {
CharacterVector band_output_type, LogicalVector unscale, LogicalVector nara) {
IntegerVector window(6);
window[0] = offset[0];
window[1] = offset[1];
window[2] = dimension[0];
window[3] = dimension[1];
window[4] = dimension[0];
window[5] = dimension[1];
return gdalraster::gdal_raster_io(dsn, window, band, "nearestneighbour", band_output_type, unscale);
return gdalraster::gdal_raster_io(dsn, window, band, "nearestneighbour", band_output_type, unscale, nara);
}

inline LogicalVector gdal_write_block(CharacterVector dsn, NumericVector data,
Expand Down
5 changes: 3 additions & 2 deletions inst/include/gdalwarpgeneral/gdalwarpgeneral.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ inline List gdal_warp_general(CharacterVector dsn,
CharacterVector band_output_type,
CharacterVector options,
CharacterVector dsn_outname,
LogicalVector include_meta) {
LogicalVector include_meta,
LogicalVector nara) {


GDALDatasetH *poSrcDS;
Expand Down Expand Up @@ -253,7 +254,7 @@ inline List gdal_warp_general(CharacterVector dsn,
bands_to_read,
band_output_type,
resample,
unscale);
unscale, nara);
}


Expand Down
5 changes: 3 additions & 2 deletions inst/include/gdalwarpmem/gdalwarpmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ inline List gdal_warp_in_memory(CharacterVector source_filename,
// CharacterVector output_dataset_options,
CharacterVector options,
LogicalVector nomd,
IntegerVector overview) {
IntegerVector overview,
LogicalVector nara) {


GDALDatasetH *poSrcDS;
Expand Down Expand Up @@ -190,7 +191,7 @@ inline List gdal_warp_in_memory(CharacterVector source_filename,
bands_to_read,
band_output_type,
resample,
unscale);
unscale, nara);

GDALClose( hRet );
return outlist;
Expand Down
14 changes: 14 additions & 0 deletions man/gdal_raster_data.Rd

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

Loading

0 comments on commit e26510d

Please sign in to comment.