diff --git a/DESCRIPTION b/DESCRIPTION index cf86c6c7..5d5abdc3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: vapour Title: Access to the 'Geospatial Data Abstraction Library' ('GDAL') -Version: 0.9.5.9005 +Version: 0.9.5.9006 Authors@R: c(person("Michael", "Sumner", email = "mdsumner@gmail.com", 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 :)"), diff --git a/NAMESPACE b/NAMESPACE index 7f777664..9d2137d5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -49,5 +49,6 @@ export(vapour_warp_raster_hex) export(vapour_warp_raster_int) export(vapour_warp_raster_raw) export(vapour_write_raster_block) +export(vector_vrt) importFrom(Rcpp,sourceCpp) useDynLib(vapour) diff --git a/NEWS.md b/NEWS.md index fe5cce2e..cb26e6a3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # vapour dev +* New function `vector_vrt()` to generate VRT for SQL and/or reprojection. + * `vapour_vrt()` gains 'options' argument, so we can in particular do `options = c("-expand", "rgb", "-ot", "Byte")` to warp 16-bit integer colour palettes from GTiff to PNG. :) diff --git a/R/vapour_vrt.R b/R/vapour_vrt.R index ab0ee5dc..e7503bb3 100644 --- a/R/vapour_vrt.R +++ b/R/vapour_vrt.R @@ -146,3 +146,78 @@ vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = N } + +#' Vector VRT +#' +#' Just a simple text generator to generate the VRT for a vector layer, First layer is chosen if not +#' othwerwise specified. +#' +#' Using 'sql' overrides the 'layer', and using 'projection' results in the geometries being transformed. +#' No check is made of the layer source projection. +#' +#' Use 'a_srs' to ensure the source has a source crs (that might be the only thing you use this for, even if not reprojecting). +#' +#' It's expected that if you use this with a source without a source projection, +#' you'll get "Failed to import source SRS", so use argument "a_srs" to set it +#' if needed (or many other GDAL other facilities that do this). +#' @param x data source name +#' @param layer layer index (1-based) or name +#' @param projection crs of the output +#' @param sql SQL for ExecuteSQL to define the layer +#' @param a_srs set the source crs +#' @return single element character vector +#' @export +#' +#' @examples +#' file <- "list_locality_postcode_meander_valley.tab" +#' ## A MapInfo TAB file with polygons +#' mvfile <- system.file(file.path("extdata/tab", file), package="vapour") +#' vector_vrt(mvfile, sql = "SELECT * FROM list_locality_postcode_meander_valley LIMIT 5 OFFSET 4") +#' +#' ## read this with vapour_read_geometry() and it will be projected to VicGrid +#' vector_vrt(mvfile, projection = "EPSG:3111") +#' +vector_vrt <- function(x, layer = 1L, projection = NULL, sql = NULL, a_srs = NULL) { + + if(!is.character(layer)) { + layer <- vapour::vapour_layer_names(x)[layer] + } + layer_srs <- "" + if (!is.null(a_srs)) { + layer_srs <- sprintf('%s', a_srs) + } + if (is.null(sql)) { + + + layer <- sprintf(' + %s + %s %s + ', layer, x, layer, layer_srs) + } else { + layer <- sprintf(' + %s + %s %s + ', layer, x, sql, layer_srs) + } + + + if (file.exists(x)) { + x <- normalizePath(x) + } + + if (is.null(projection)) { + out <- sprintf(' + %s + ', layer) + + } else { + out <- sprintf(' + + %s + %s + + ', layer, projection) + } + out +} + diff --git a/man/vector_vrt.Rd b/man/vector_vrt.Rd new file mode 100644 index 00000000..2b708d14 --- /dev/null +++ b/man/vector_vrt.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/vapour_vrt.R +\name{vector_vrt} +\alias{vector_vrt} +\title{Vector VRT} +\usage{ +vector_vrt(x, layer = 1L, projection = NULL, sql = NULL, a_srs = NULL) +} +\arguments{ +\item{x}{data source name} + +\item{layer}{layer index (1-based) or name} + +\item{projection}{crs of the output} + +\item{sql}{SQL for ExecuteSQL to define the layer} + +\item{a_srs}{set the source crs} +} +\value{ +single element character vector +} +\description{ +Just a simple text generator to generate the VRT for a vector layer, First layer is chosen if not +othwerwise specified. +} +\details{ +Using 'sql' overrides the 'layer', and using 'projection' results in the geometries being transformed. +No check is made of the layer source projection. + +Use 'a_srs' to ensure the source has a source crs (that might be the only thing you use this for, even if not reprojecting). + +It's expected that if you use this with a source without a source projection, +you'll get "Failed to import source SRS", so use argument "a_srs" to set it +if needed (or many other GDAL other facilities that do this). +} +\examples{ +file <- "list_locality_postcode_meander_valley.tab" +## A MapInfo TAB file with polygons +mvfile <- system.file(file.path("extdata/tab", file), package="vapour") +vector_vrt(mvfile, sql = "SELECT * FROM list_locality_postcode_meander_valley LIMIT 5 OFFSET 4") + +## read this with vapour_read_geometry() and it will be projected to VicGrid +vector_vrt(mvfile, projection = "EPSG:3111") + +}