Skip to content

Commit

Permalink
vector_vrt
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Aug 5, 2023
1 parent 35139e0 commit ff86145
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
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.9005
Version: 0.9.5.9006
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 @@ -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)
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 `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. :)

Expand Down
75 changes: 75 additions & 0 deletions R/vapour_vrt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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('<LayerSRS>%s</LayerSRS>', a_srs)
}
if (is.null(sql)) {


layer <- sprintf('<OGRVRTLayer name="%s">
<SrcDataSource>%s</SrcDataSource>
<SrcLayer>%s</SrcLayer> %s
</OGRVRTLayer>', layer, x, layer, layer_srs)
} else {
layer <- sprintf('<OGRVRTLayer name="%s">
<SrcDataSource>%s</SrcDataSource>
<SrcSQL>%s</SrcSQL> %s
</OGRVRTLayer>', layer, x, sql, layer_srs)
}


if (file.exists(x)) {
x <- normalizePath(x)
}

if (is.null(projection)) {
out <- sprintf('<OGRVRTDataSource>
%s
</OGRVRTDataSource>', layer)

} else {
out <- sprintf('<OGRVRTDataSource>
<OGRVRTWarpedLayer>
%s
<TargetSRS>%s</TargetSRS>
</OGRVRTWarpedLayer>
</OGRVRTDataSource>', layer, projection)
}
out
}

46 changes: 46 additions & 0 deletions man/vector_vrt.Rd

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

0 comments on commit ff86145

Please sign in to comment.