Title: | Bindings to the 'Geospatial Data Abstraction Library' Raster API |
---|---|
Description: | Interface to the Raster API of the 'Geospatial Data Abstraction Library' ('GDAL', <https://gdal.org>). Bindings are implemented in an exposed C++ class encapsulating a 'GDALDataset' and its raster band objects, along with several stand-alone functions. These support manual creation of uninitialized datasets, creation from existing raster as template, read/set dataset parameters, low level I/O, color tables, raster attribute tables, virtual raster (VRT), and 'gdalwarp' wrapper for reprojection and mosaicing. Includes 'GDAL' algorithms ('dem_proc()', 'polygonize()', 'rasterize()', etc.), and functions for coordinate transformation and spatial reference systems. Calling signatures resemble the native C, C++ and Python APIs provided by the 'GDAL' project. Includes raster 'calc()' to evaluate a given R expression on a layer or stack of layers, with pixel x/y available as variables in the expression; and raster 'combine()' to identify and count unique pixel combinations across multiple input layers, with optional output of the pixel-level combination IDs. Provides raster display using base 'graphics'. Bindings to a subset of the 'OGR' API are also included for managing vector data sources. Bindings to a subset of the Virtual Systems Interface ('VSI') are also included to support operations on 'GDAL' virtual file systems. These are general utility functions that abstract file system operations on URLs, cloud storage services, 'Zip'/'GZip'/'7z'/'RAR' archives, and in-memory files. 'gdalraster' may be useful in applications that need scalable, low-level I/O, or prefer a direct 'GDAL' API. |
Authors: | Chris Toney [aut, cre] (R interface/additional functionality), Michael D. Sumner [ctb], Frank Warmerdam [ctb, cph] (GDAL API documentation; src/progress_r.cpp from gdal/port/cpl_progress.cpp), Even Rouault [ctb, cph] (GDAL API documentation), Marius Appel [ctb, cph] (configure.ac based on https://github.com/appelmar/gdalcubes), Daniel James [ctb, cph] (Boost combine hashes method in src/cmb_table.h), Peter Dimov [ctb, cph] (Boost combine hashes method in src/cmb_table.h) |
Maintainer: | Chris Toney <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.11.1.9515 |
Built: | 2024-12-07 03:08:39 UTC |
Source: | https://github.com/usdaforestservice/gdalraster |
gdalraster
is an interface to the Geospatial Data Abstraction
Library (GDAL) for low level raster I/O. Calling signatures resemble those
of the native C, C++ and Python APIs provided by the GDAL project.
See https://gdal.org/api/ for details of the GDAL Raster API.
Core functionality is contained in class GDALRaster
and several
related stand-alone functions:
GDALRaster-class
is an exposed C++ class that allows
opening a raster dataset and calling methods on the GDALDataset
,
GDALDriver
and GDALRasterBand
objects in the underlying API
(e.g., get/set parameters, read/write pixel data).
raster creation: create()
,
createCopy()
,
rasterFromRaster()
,
translate()
,
getCreationOptions()
virtual raster:
autoCreateWarpedVRT()
,
buildVRT()
,
rasterToVRT()
reproject/resample/crop/mosaic: warp()
algorithms: dem_proc()
,
fillNodata()
,
footprint()
,
polygonize()
,
rasterize()
,
sieveFilter()
,
GDALRaster$getChecksum()
raster attribute tables: buildRAT()
,
displayRAT()
,
GDALRaster$getDefaultRAT()
,
GDALRaster$setDefaultRAT()
geotransform conversion:
apply_geotransform()
,
get_pixel_line()
,
inv_geotransform()
coordinate transformation:
transform_xy()
,
inv_project()
spatial reference convenience functions:
epsg_to_wkt()
,
srs_to_wkt()
,
srs_find_epsg()
,
srs_get_name()
,
srs_is_geographic()
,
srs_is_projected()
,
srs_is_same()
geometry convenience functions:
bbox_from_wkt()
,
bbox_to_wkt()
,
bbox_intersect()
,
bbox_union()
,
bbox_transform()
,
g_area()
,
g_buffer()
,
g_centroid()
,
g_contains()
,
g_crosses()
,
g_difference()
,
g_disjoint()
,
g_distance()
,
g_equals()
,
g_intersection()
,
g_intersects()
,
g_is_empty()
,
g_is_valid()
,
g_length()
,
g_make_valid()
,
g_name()
,
g_overlaps()
,
g_summary()
,
g_sym_difference()
,
g_touches()
,
g_transform()
,
g_union()
,
g_within()
,
g_wk2wk()
,
geos_version()
data management: addFilesInZip()
,
copyDatasetFiles()
,
deleteDataset()
,
renameDataset()
,
bandCopyWholeRaster()
,
identifyDriver()
,
inspectDataset()
OGR vector utilities:
ogr2ogr()
,
ogrinfo()
,
ogr_manage
,
ogr_define
virtual file systems:
VSIFile
,
vsi_clear_path_options()
,
vsi_copy_file()
,
vsi_curl_clear_cache()
,
vsi_get_disk_free_space()
,
vsi_get_file_metadata()
,
vsi_get_fs_options()
,
vsi_get_fs_prefixes()
,
vsi_mkdir()
,
vsi_read_dir()
,
vsi_rename()
,
vsi_rmdir()
,
vsi_set_path_option()
,
vsi_stat()
,
vsi_supports_rnd_write()
,
vsi_supports_seq_write()
,
vsi_sync()
,
vsi_unlink()
,
vsi_unlink_batch()
GDAL configuration:
gdal_version()
,
gdal_formats()
,
get_cache_used()
,
get_config_option()
,
set_config_option()
,
get_num_cpus()
,
get_usable_physical_ram()
,
has_spatialite()
,
http_enabled()
,
push_error_handler()
,
pop_error_handler()
,
dump_open_datasets()
PROJ configuration:
proj_version()
,
proj_search_paths()
,
proj_networking()
Additional functionality includes:
RunningStats-class
calculates mean and variance in one
pass. The min, max, sum, and count are also tracked (efficient summary
statistics on data streams).
CmbTable-class
implements a hash table for counting
unique combinations of integer values.
combine()
overlays multiple rasters so that a
unique ID is assigned to each unique combination of input values. Pixel
counts for each unique combination are obtained, and combination IDs are
optionally written to an output raster.
calc()
evaluates an R expression for each pixel in
a raster layer or stack of layers. Individual pixel coordinates are available
as variables in the R expression, as either x/y in the raster projected
coordinate system or inverse projected longitude/latitude.
plot_raster()
displays raster data using
base R graphics
. Supports single-band grayscale, RGB, color tables and
color map functions (e.g., color ramp).
Documentation for GDALRaster-class
and several wrapper functions
borrows from the GDAL API documentation, (c) 1998-2024, Frank Warmerdam,
Even Rouault, and others, MIT license.
Sample datasets included with the package are used in examples throughout the documentation. The sample data include LANDFIRE raster layers describing terrain, vegetation and wildland fuels (LF 2020 version), Landsat C2 Analysis Ready Data from USGS Earth Explorer, and Monitoring Trends in Burn Severity (MTBS) fire perimeters from 1984-2022. Metadata for the sample datasets are in inst/extdata/metadata.zip.
system.file()
is used in the examples to access the sample datasets.
This enables the code to run regardless of where R is installed. Users will
normally give file names as a regular full path or relative to the current
working directory.
Temporary files are created in some examples which have cleanup code wrapped
in dontshow{}
. While the cleanup code is not shown in the
documentation, note that this code runs by default if examples are run with
example()
.
GDAL is by: Frank Warmerdam, Even Rouault and others
(see https://github.com/OSGeo/gdal/graphs/contributors)
R interface/additional functionality: Chris Toney
Maintainer: Chris Toney <chris.toney at usda.gov>
GDAL Raster Data Model:
https://gdal.org/user/raster_data_model.html
Raster format descriptions:
https://gdal.org/drivers/raster/index.html
Geotransform tutorial:
https://gdal.org/tutorials/geotransforms_tut.html
GDAL Virtual File Systems:
https://gdal.org/user/virtual_file_systems.html
addFilesInZip()
will create new or open existing ZIP file, and
add one or more compressed files potentially using the seek optimization
extension. This function is basically a wrapper for CPLAddFileInZip()
in the GDAL Common Portability Library, but optionally creates a new ZIP
file first (with CPLCreateZip()
). It provides a subset of functionality
in the GDAL sozip
command-line utility
(https://gdal.org/programs/sozip.html). Requires GDAL >= 3.7.
addFilesInZip( zip_file, add_files, overwrite = FALSE, full_paths = TRUE, sozip_enabled = NULL, sozip_chunk_size = NULL, sozip_min_file_size = NULL, num_threads = NULL, content_type = NULL, quiet = FALSE )
addFilesInZip( zip_file, add_files, overwrite = FALSE, full_paths = TRUE, sozip_enabled = NULL, sozip_chunk_size = NULL, sozip_min_file_size = NULL, num_threads = NULL, content_type = NULL, quiet = FALSE )
zip_file |
Filename of the ZIP file. Will be created if it does not
exist or if |
add_files |
Character vector of one or more input filenames to add. |
overwrite |
Logical scalar. Overwrite the target zip file if it already exists. |
full_paths |
Logical scalar. By default, the full path will be stored
(relative to the current directory). |
sozip_enabled |
String. Whether to generate a SOZip index for the file.
One of |
sozip_chunk_size |
The chunk size for a seek-optimized file. Defaults to 32768 bytes. The value is specified in bytes, or K and M suffix can be used respectively to specify a value in kilo-bytes or mega-bytes. Will be coerced to string. |
sozip_min_file_size |
The minimum file size to decide if a file
should be seek-optimized, in |
num_threads |
Number of threads used for SOZip generation. Defaults to
|
content_type |
String Content-Type value for the file. This is stored as a key-value pair in the extra field extension 'KV' (0x564b) dedicated to storing key-value pair metadata. |
quiet |
Logical scalar. |
A Seek-Optimized ZIP file (SOZip) contains one or more compressed files organized and annotated such that a SOZip-aware reader can perform very fast random access within the .zip file (see https://github.com/sozip/sozip-spec). Large compressed files can be accessed directly from SOZip without prior decompression. The .zip file is otherwise fully backward compatible.
If sozip_enabled="AUTO"
(the default), a file is seek-optimized only if
its size is above the values of sozip_min_file_size
(default 1 MB) and
sozip_chunk_size
(default 32768
).
In "YES"
mode, all input files will be seek-optimized. In "NO"
mode, no
input files will be seek-optimized. The default can be changed with the
CPL_SOZIP_ENABLED
configuration option.
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
The GDAL_NUM_THREADS
configuration option can be set to ALL_CPUS
or an
integer value to specify the number of threads to use for SOZip-compressed
files (see set_config_option()
).
SOZip can be validated with:
vsi_get_file_metadata(zip_file, domain="ZIP")
where zip_file
uses the /vsizip/ prefix.
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") zip_file <- file.path(tempdir(), "storml_lcp.zip") # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { addFilesInZip(zip_file, lcp_file, full_paths=FALSE, sozip_enabled="YES", num_threads=1) print("Files in zip archive:") print(unzip(zip_file, list=TRUE)) # Open with GDAL using Virtual File System handler '/vsizip/' # see: https://gdal.org/user/virtual_file_systems.html#vsizip-zip-archives lcp_in_zip <- file.path("/vsizip", zip_file, "storm_lake.lcp") print("SOZip metadata:") print(vsi_get_file_metadata(lcp_in_zip, domain="ZIP")) ds <- new(GDALRaster, lcp_in_zip) ds$info() ds$close() }
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") zip_file <- file.path(tempdir(), "storml_lcp.zip") # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { addFilesInZip(zip_file, lcp_file, full_paths=FALSE, sozip_enabled="YES", num_threads=1) print("Files in zip archive:") print(unzip(zip_file, list=TRUE)) # Open with GDAL using Virtual File System handler '/vsizip/' # see: https://gdal.org/user/virtual_file_systems.html#vsizip-zip-archives lcp_in_zip <- file.path("/vsizip", zip_file, "storm_lake.lcp") print("SOZip metadata:") print(vsi_get_file_metadata(lcp_in_zip, domain="ZIP")) ds <- new(GDALRaster, lcp_in_zip) ds$info() ds$close() }
apply_geotransform()
applies geotransform coefficients to raster
coordinates in pixel/line space (column/row), converting into
georeferenced (x/y) coordinates. Wrapper of GDALApplyGeoTransform()
in
the GDAL API, operating on matrix input.
apply_geotransform(col_row, gt)
apply_geotransform(col_row, gt)
col_row |
Numeric matrix of raster column/row (pixel/line) coordinates (or two-column data frame that will be coerced to numeric matrix). |
gt |
Either a numeric vector of length six containing the affine
geotransform for the raster, or an object of class |
Numeric matrix of geospatial x/y coordinates.
Bounds checking on the input coordinates is done if gt
is obtained from an
object of class GDALRaster
. See Note for get_pixel_line()
.
GDALRaster$getGeoTransform()
, get_pixel_line()
raster_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, raster_file) # compute some raster coordinates in column/row space set.seed(42) col_coords <- runif(10, min = 0, max = ds$getRasterXSize() - 0.00001) row_coords <- runif(10, min = 0, max = ds$getRasterYSize() - 0.00001) col_row <- cbind(col_coords, row_coords) # convert to geospatial x/y coordinates gt <- ds$getGeoTransform() apply_geotransform(col_row, gt) # or, using the class method ds$apply_geotransform(col_row) # bounds checking col_row <- rbind(col_row, c(ds$getRasterXSize(), ds$getRasterYSize())) ds$apply_geotransform(col_row) ds$close()
raster_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, raster_file) # compute some raster coordinates in column/row space set.seed(42) col_coords <- runif(10, min = 0, max = ds$getRasterXSize() - 0.00001) row_coords <- runif(10, min = 0, max = ds$getRasterYSize() - 0.00001) col_row <- cbind(col_coords, row_coords) # convert to geospatial x/y coordinates gt <- ds$getGeoTransform() apply_geotransform(col_row, gt) # or, using the class method ds$apply_geotransform(col_row) # bounds checking col_row <- rbind(col_row, c(ds$getRasterXSize(), ds$getRasterYSize())) ds$apply_geotransform(col_row) ds$close()
autoCreateWarpedVRT()
creates a warped virtual dataset representing the
input raster warped into a target coordinate system. The output virtual
dataset will be "north-up" in the target coordinate system. GDAL
automatically determines the bounds and resolution of the output virtual
raster which should be large enough to include all the input raster.
Wrapper of GDALAutoCreateWarpedVRT()
in the GDAL Warper API.
autoCreateWarpedVRT( src_ds, dst_wkt, resample_alg, src_wkt = "", max_err = 0, alpha_band = FALSE )
autoCreateWarpedVRT( src_ds, dst_wkt, resample_alg, src_wkt = "", max_err = 0, alpha_band = FALSE )
src_ds |
An object of class |
dst_wkt |
WKT string specifying the coordinate system to convert to.
If empty string ( |
resample_alg |
Character string specifying the sampling method to use. One of NearestNeighbour, Bilinear, Cubic, CubicSpline, Lanczos, Average, RMS or Mode. |
src_wkt |
WKT string specifying the coordinate system of the source raster. If empty string it will be read from the source raster (the default). |
max_err |
Numeric scalar specifying the maximum error measured in
input pixels that is allowed in approximating the transformation ( |
alpha_band |
Logical scalar, |
An object of class GDALRaster
for the new virtual dataset. An
error is raised if the operation fails.
The returned dataset will have no associated filename for itself. If you
want to write the virtual dataset to a VRT file, use the
$setFilename()
method on the returned GDALRaster
object to assign a
filename before it is closed.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) ds2 <- autoCreateWarpedVRT(ds, epsg_to_wkt(5070), "Bilinear") ds2$info() ## set filename before close if a VRT file is needed for the virtual dataset # ds2$setFilename("/path/to/file.vrt") ds2$close() ds$close()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) ds2 <- autoCreateWarpedVRT(ds, epsg_to_wkt(5070), "Bilinear") ds2$info() ## set filename before close if a VRT file is needed for the virtual dataset # ds2$setFilename("/path/to/file.vrt") ds2$close() ds$close()
bandCopyWholeRaster()
copies the complete raster contents of one band to
another similarly configured band. The source and destination bands must
have the same xsize and ysize. The bands do not have to have the same data
type. It implements efficient copying, in particular "chunking" the copy in
substantial blocks. This is a wrapper for GDALRasterBandCopyWholeRaster()
in the GDAL API.
bandCopyWholeRaster( src_filename, src_band, dst_filename, dst_band, options = NULL, quiet = FALSE )
bandCopyWholeRaster( src_filename, src_band, dst_filename, dst_band, options = NULL, quiet = FALSE )
src_filename |
Filename of the source raster. |
src_band |
Band number in the source raster to be copied. |
dst_filename |
Filename of the destination raster. |
dst_band |
Band number in the destination raster to copy into. |
options |
Optional list of transfer hints in a vector of
|
quiet |
Logical scalar. If |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
GDALRaster-class
, create()
, createCopy()
,
rasterFromRaster()
## copy Landsat data from a single-band file to a new multi-band image b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") dst_file <- file.path(tempdir(), "sr_multi.tif") rasterFromRaster(b5_file, dst_file, nbands=7, init=0) opt <- c("COMPRESSED=YES", "SKIP_HOLES=YES") bandCopyWholeRaster(b5_file, 1, dst_file, 5, options=opt) ds <- new(GDALRaster, dst_file) ds$getStatistics(band=5, approx_ok=FALSE, force=TRUE) ds$close()
## copy Landsat data from a single-band file to a new multi-band image b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") dst_file <- file.path(tempdir(), "sr_multi.tif") rasterFromRaster(b5_file, dst_file, nbands=7, init=0) opt <- c("COMPRESSED=YES", "SKIP_HOLES=YES") bandCopyWholeRaster(b5_file, 1, dst_file, 5, options=opt) ds <- new(GDALRaster, dst_file) ds$getStatistics(band=5, approx_ok=FALSE, force=TRUE) ds$close()
bbox_from_wkt()
returns the bounding box of a WKT 2D geometry
(e.g., LINE, POLYGON, MULTIPOLYGON).
bbox_from_wkt(wkt, extend_x = 0, extend_y = 0)
bbox_from_wkt(wkt, extend_x = 0, extend_y = 0)
wkt |
Character. OGC WKT string for a simple feature 2D geometry. |
extend_x |
Numeric scalar. Distance to extend the output bounding box
in both directions along the x-axis
(results in |
extend_y |
Numeric scalar. Distance to extend the output bounding box
in both directions along the y-axis
(results in |
Numeric vector of length four containing the xmin, ymin,
xmax, ymax of the geometry specified by wkt
(possibly extended by values
in extend_x
, extend_y
).
bnd <- "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" bbox_from_wkt(bnd, 100, 100)
bnd <- "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" bbox_from_wkt(bnd, 100, 100)
bbox_intersect()
returns the bounding box intersection, and
bbox_union()
returns the bounding box union, for input of
either raster file names or list of bounding boxes. All of the inputs
must be in the same projected coordinate system.
bbox_intersect(x, as_wkt = FALSE) bbox_union(x, as_wkt = FALSE)
bbox_intersect(x, as_wkt = FALSE) bbox_union(x, as_wkt = FALSE)
x |
Either a character vector of raster file names, or a list with each element a bounding box numeric vector (xmin, ymin, xmax, ymax). |
as_wkt |
Logical. |
The intersection (bbox_intersect()
) or union (bbox_union()
)
of inputs.
If as_wkt = FALSE
, a numeric vector of length four containing
xmin, ymin, xmax, ymax. If as_wkt = TRUE
, a character string
containing OGC WKT for the bbox as POLYGON.
bbox_from_wkt()
, bbox_to_wkt()
bbox_list <-list() elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) bbox_list[[1]] <- ds$bbox() ds$close() b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") ds <- new(GDALRaster, b5_file) bbox_list[[2]] <- ds$bbox() ds$close() bnd <- "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" bbox_list[[3]] <- bbox_from_wkt(bnd) print(bbox_list) bbox_intersect(bbox_list) bbox_union(bbox_list)
bbox_list <-list() elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) bbox_list[[1]] <- ds$bbox() ds$close() b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") ds <- new(GDALRaster, b5_file) bbox_list[[2]] <- ds$bbox() ds$close() bnd <- "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" bbox_list[[3]] <- bbox_from_wkt(bnd) print(bbox_list) bbox_intersect(bbox_list) bbox_union(bbox_list)
bbox_to_wkt()
returns a WKT POLYGON string for the given bounding box.
Requires GDAL built with the GEOS library.
bbox_to_wkt(bbox, extend_x = 0, extend_y = 0)
bbox_to_wkt(bbox, extend_x = 0, extend_y = 0)
bbox |
Numeric vector of length four containing xmin, ymin, xmax, ymax. |
extend_x |
Numeric scalar. Distance in units of |
extend_y |
Numeric scalar. Distance in units of |
Character string for an OGC WKT polygon.
NA
is returned if GDAL was built without the GEOS library.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file, read_only=TRUE) bbox_to_wkt(ds$bbox()) ds$close()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file, read_only=TRUE) bbox_to_wkt(ds$bbox()) ds$close()
bbox_transform()
is a convenience function for:
bbox_to_wkt(bbox) |> g_transform(srs_from, srs_to) |> bbox_from_wkt()
bbox_transform(bbox, srs_from, srs_to)
bbox_transform(bbox, srs_from, srs_to)
bbox |
Numeric vector of length four containing a bounding box (xmin, ymin, xmax, ymax) to transform. |
srs_from |
Character string in OGC WKT format specifying the
spatial reference system for |
srs_to |
Character string in OGC WKT format specifying the target spatial reference system. |
Numeric vector of length four containing a transformed bounding box (xmin, ymin, xmax, ymax).
g_transform()
, bbox_from_wkt()
, bbox_to_wkt()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) ds$bbox() bbox_transform(ds$bbox(), ds$getProjection(), epsg_to_wkt(4326)) ds$close()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) ds$bbox() bbox_transform(ds$bbox(), ds$getProjection(), epsg_to_wkt(4326)) ds$close()
buildRAT()
reads all pixels of an input raster to obtain the set of
unique values and their counts. The result is returned as a data frame
suitable for use with the class method GDALRaster$setDefaultRAT()
. The
returned data frame might be further modified before setting as a Raster
Attribute Table in a dataset, for example, by adding columns containing
class names, color values, or other information (see Details).
An optional input data frame containing such attributes may be given, in
which case buildRAT()
will attempt to join the additional columns and
automatically assign the appropriate metadata on the output data frame
(i.e., assign R attributes on the data frame and its columns that define
usage in a GDAL Raster Attribute Table).
buildRAT( raster, band = 1L, col_names = c("VALUE", "COUNT"), table_type = "athematic", na_value = NULL, join_df = NULL, quiet = FALSE )
buildRAT( raster, band = 1L, col_names = c("VALUE", "COUNT"), table_type = "athematic", na_value = NULL, join_df = NULL, quiet = FALSE )
raster |
Either a |
band |
Integer scalar, band number to read (default |
col_names |
Character vector of length two containing names to use for
column 1 (pixel values) and column 2 (pixel counts) in the output data
frame (defaults are |
table_type |
Character string describing the type of the attribute
table. One of either |
na_value |
Numeric scalar. If the set of unique pixel values has an
|
join_df |
Optional data frame for joining additional attributes. Must
have a column of unique values with the same name as |
quiet |
Logical scalar. If |
A GDAL Raster Attribute Table (or RAT) provides attribute information about pixel values. Raster attribute tables can be used to represent histograms, color tables, and classification information. Each row in the table applies to either a single pixel value or a range of values, and might have attributes such as the histogram count for that value (or range), the color that pixels of that value (or range) should be displayed, names of classes, or various other information.
Each column in a raster attribute table has a name, a type (integer,
double, or string), and a GDALRATFieldUsage
. The usage
distinguishes columns with particular understood purposes (such as color,
histogram count, class name), and columns that have other purposes not
understood by the library (long labels, ancillary attributes, etc).
In the general case, each row has a field indicating the minimum pixel
value falling into that category, and a field indicating the maximum pixel
value. In the GDAL API, these are indicated with usage values of GFU_Min
and GFU_Max
. In the common case where each row is a discrete pixel value,
a single column with usage GFU_MinMax
would be used instead.
In R, the table is represented as a data frame with column attribute "GFU"
containing the field usage as a string, e.g., "Max"
, "Min"
or "MinMax"
(case-sensitive).
The full set of possible field usage descriptors is:
GFU attr | GDAL enum | Description |
"Generic" |
GFU_Generic |
General purpose field |
"PixelCount" |
GFU_PixelCount |
Histogram pixel count |
"Name" |
GFU_Name |
Class name |
"Min" |
GFU_Min |
Class range minimum |
"Max" |
GFU_Max |
Class range maximum |
"MinMax" |
GFU_MinMax |
Class value (min=max) |
"Red" |
GFU_Red |
Red class color (0-255) |
"Green" |
GFU_Green |
Green class color (0-255) |
"Blue" |
GFU_Blue |
Blue class color (0-255) |
"Alpha" |
GFU_Alpha |
Alpha transparency (0-255) |
"RedMin" |
GFU_RedMin |
Color range red minimum |
"GreenMin" |
GFU_GreenMin |
Color range green minimum |
"BlueMin" |
GFU_BlueMin |
Color range blue minimum |
"AlphaMin" |
GFU_AlphaMin |
Color range alpha minimum |
"RedMax" |
GFU_RedMax |
Color range red maximum |
"GreenMax" |
GFU_GreenMax |
Color range green maximum |
"BlueMax" |
GFU_BlueMax |
Color range blue maximum |
"AlphaMax" |
GFU_AlphaMax |
Color range alpha maximum |
buildRAT()
assigns GFU "MinMax"
on the column of pixel values (named
"VALUE"
by default) and GFU "PixelCount"
on the column of counts (named
"COUNT"
by default).
If join_df
is given, the additional columns that result from joining will
have GFU assigned automatically based on the column names (ignoring case).
First, the additional column names are checked for containing
the string "name"
(e.g., "classname"
, "TypeName"
, "EVT_NAME"
, etc).
The first matching column (if any) will be assigned a GFU of "Name"
(=GFU_Name
, the field usage descriptor for class names). Next, columns
named "R"
or "Red"
will be assigned GFU "Red"
, columns named "G"
or
"Green"
will be assigned GFU "Green"
, columns named "B"
or "Blue"
will be assigned GFU "Blue"
, and columns named "A"
or "Alpha"
will be
assigned GFU "Alpha"
. Finally, any remaining columns that have not been
assigned a GFU will be assigned "Generic"
.
In a variation of RAT, all the categories are of equal size and regularly
spaced, and the categorization can be determined by knowing the value at
which the categories start and the size of a category. This is called
"Linear Binning" and the information is kept specially on the raster
attribute table as a whole. In R, a RAT that uses linear binning would
have the following attributes set on the data frame:
attribute "Row0Min"
= the numeric lower bound (pixel value) of the first
category, and attribute "BinSize"
= the numeric width of each category (in
pixel value units). buildRAT()
does not create tables with linear binning,
but one could be created manually based on the specifications above, and
applied to a raster with the class method GDALRaster$setDefaultRAT()
.
A raster attribute table is thematic or athematic (continuous). In R, this
is defined by an attribute on the data frame named "GDALRATTableType"
with
value of either "thematic"
or "athematic"
.
A data frame with at least two columns containing the set of unique
pixel values and their counts. These columns have attribute "GFU"
set to
"MinMax"
for the values, and "PixelCount"
for the counts. If join_df
is
given, the returned data frame will have additional columns that result from
merge()
. The "GFU"
attribute of the additional columns will be assigned
automatically based on the column names (case-insensitive matching, see
Details). The returned data frame has attribute "GDALRATTableType"
set to
table_type
.
The full raster will be scanned.
If na_value
is not specified, then an NA
pixel value (if present)
will not be recoded in the output data frame. This may have implications
if joining to other data (NA
will not match), or when using the returned
data frame to set a default RAT on a dataset (NA
will be interpreted
as the value that R uses internally to represent it for the type, e.g.,
-2147483648 for NA_integer_
). In some cases, removing the row in the output
data frame with value NA
, rather than recoding, may be desirable (i.e., by
removing manually or by side effect of joining via merge()
, for example).
Users should consider what is appropriate for a particular case.
GDALRaster$getDefaultRAT()
,
GDALRaster$setDefaultRAT()
,
displayRAT()
vignette("raster-attribute-tables")
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") # make a copy to modify f <- file.path(tempdir(), "storml_evt_tmp.tif") file.copy(evt_file, f) ds <- new(GDALRaster, f, read_only=FALSE) ds$getDefaultRAT(band=1) # NULL # get the full attribute table for LANDFIRE EVT from the CSV file evt_csv <- system.file("extdata/LF20_EVT_220.csv", package="gdalraster") evt_df <- read.csv(evt_csv) nrow(evt_df) head(evt_df) evt_df <- evt_df[,1:7] tbl <- buildRAT(ds, table_type = "thematic", na_value = -9999, join_df = evt_df) nrow(tbl) head(tbl) # attributes on the data frame and its columns define usage in a GDAL RAT attributes(tbl) attributes(tbl$VALUE) attributes(tbl$COUNT) attributes(tbl$EVT_NAME) attributes(tbl$EVT_LF) attributes(tbl$EVT_PHYS) attributes(tbl$R) attributes(tbl$G) attributes(tbl$B) ds$setDefaultRAT(band=1, tbl) ds$flushCache() tbl2 <- ds$getDefaultRAT(band=1) nrow(tbl2) head(tbl2) ds$close() # Display evt_gt <- displayRAT(tbl2, title = "Storm Lake EVT Raster Attribute Table") class(evt_gt) # an object of class "gt_tbl" from package gt # To show the table: # evt_gt # or simply call `displayRAT()` as above but without assignment # `vignette("raster-attribute-tables")` has example output
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") # make a copy to modify f <- file.path(tempdir(), "storml_evt_tmp.tif") file.copy(evt_file, f) ds <- new(GDALRaster, f, read_only=FALSE) ds$getDefaultRAT(band=1) # NULL # get the full attribute table for LANDFIRE EVT from the CSV file evt_csv <- system.file("extdata/LF20_EVT_220.csv", package="gdalraster") evt_df <- read.csv(evt_csv) nrow(evt_df) head(evt_df) evt_df <- evt_df[,1:7] tbl <- buildRAT(ds, table_type = "thematic", na_value = -9999, join_df = evt_df) nrow(tbl) head(tbl) # attributes on the data frame and its columns define usage in a GDAL RAT attributes(tbl) attributes(tbl$VALUE) attributes(tbl$COUNT) attributes(tbl$EVT_NAME) attributes(tbl$EVT_LF) attributes(tbl$EVT_PHYS) attributes(tbl$R) attributes(tbl$G) attributes(tbl$B) ds$setDefaultRAT(band=1, tbl) ds$flushCache() tbl2 <- ds$getDefaultRAT(band=1) nrow(tbl2) head(tbl2) ds$close() # Display evt_gt <- displayRAT(tbl2, title = "Storm Lake EVT Raster Attribute Table") class(evt_gt) # an object of class "gt_tbl" from package gt # To show the table: # evt_gt # or simply call `displayRAT()` as above but without assignment # `vignette("raster-attribute-tables")` has example output
buildVRT()
is a wrapper of the gdalbuildvrt
command-line
utility for building a VRT (Virtual Dataset) that is a mosaic of the list
of input GDAL datasets
(see https://gdal.org/programs/gdalbuildvrt.html).
buildVRT(vrt_filename, input_rasters, cl_arg = NULL, quiet = FALSE)
buildVRT(vrt_filename, input_rasters, cl_arg = NULL, quiet = FALSE)
vrt_filename |
Character string. Filename of the output VRT. |
input_rasters |
Character vector of input raster filenames. |
cl_arg |
Optional character vector of command-line arguments to
|
quiet |
Logical scalar. If |
Several command-line options are described in the GDAL documentation at the
URL above. By default, the input files are considered as tiles of a larger
mosaic and the VRT file has as many bands as one of the input files.
Alternatively, the -separate
argument can be used to put each input
raster into a separate band in the VRT dataset.
Some amount of checks are done to assure that all files that will be put in
the resulting VRT have similar characteristics: number of bands,
projection, color interpretation.... If not, files that do not match the
common characteristics will be skipped. (This is true in the default
mode for virtual mosaicing, and not when using the -separate
option).
In a virtual mosaic, if there is spatial overlap between input rasters then the order of files appearing in the list of sources matter: files that are listed at the end are the ones from which the data will be fetched. Note that nodata will be taken into account to potentially fetch data from less priority datasets.
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
# build a virtual 3-band RGB raster from individual Landsat band files b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b6_file <- system.file("extdata/sr_b6_20200829.tif", package="gdalraster") band_files <- c(b6_file, b5_file, b4_file) vrt_file <- file.path(tempdir(), "storml_b6_b5_b4.vrt") buildVRT(vrt_file, band_files, cl_arg = "-separate") ds <- new(GDALRaster, vrt_file) ds$getRasterCount() plot_raster(ds, nbands=3, main="Landsat 6-5-4 (vegetative analysis)") ds$close()
# build a virtual 3-band RGB raster from individual Landsat band files b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b6_file <- system.file("extdata/sr_b6_20200829.tif", package="gdalraster") band_files <- c(b6_file, b5_file, b4_file) vrt_file <- file.path(tempdir(), "storml_b6_b5_b4.vrt") buildVRT(vrt_file, band_files, cl_arg = "-separate") ds <- new(GDALRaster, vrt_file) ds$getRasterCount() plot_raster(ds, nbands=3, main="Landsat 6-5-4 (vegetative analysis)") ds$close()
calc()
evaluates an R expression for each pixel in a raster layer or
stack of layers. Each layer is defined by a raster filename, band number,
and a variable name to use in the R expression. If not specified, band
defaults to 1 for each input raster.
Variable names default to LETTERS
if not specified
(A
(layer 1), B
(layer 2), ...).
All of the input layers must have the same extent and cell size.
The projection will be read from the first raster in the list
of inputs.
Individual pixel coordinates are also available as variables in the
R expression, as either x/y in the raster projected coordinate system or
inverse projected longitude/latitude.
Multiband output is supported as of gdalraster 1.11.0.
calc( expr, rasterfiles, bands = NULL, var.names = NULL, dstfile = tempfile("rastcalc", fileext = ".tif"), fmt = NULL, dtName = "Int16", out_band = NULL, options = NULL, nodata_value = NULL, setRasterNodataValue = FALSE, usePixelLonLat = NULL, write_mode = "safe", quiet = FALSE )
calc( expr, rasterfiles, bands = NULL, var.names = NULL, dstfile = tempfile("rastcalc", fileext = ".tif"), fmt = NULL, dtName = "Int16", out_band = NULL, options = NULL, nodata_value = NULL, setRasterNodataValue = FALSE, usePixelLonLat = NULL, write_mode = "safe", quiet = FALSE )
expr |
An R expression as a character string (e.g., |
rasterfiles |
Character vector of source raster filenames. |
bands |
Integer vector of band numbers to use for each raster layer. |
var.names |
Character vector of variable names to use for each raster layer. |
dstfile |
Character filename of output raster. |
fmt |
Output raster format name (e.g., "GTiff" or "HFA"). Will attempt to guess from the output filename if not specified. |
dtName |
Character name of output data type (e.g., Byte, Int16, UInt16, Int32, UInt32, Float32). |
out_band |
Integer band number(s) in |
options |
Optional list of format-specific creation options in a
vector of "NAME=VALUE" pairs
(e.g., |
nodata_value |
Numeric value to assign if |
setRasterNodataValue |
Logical. |
usePixelLonLat |
This argument is deprecated and will be removed in a
future version. Variable names |
write_mode |
Character. Name of the file write mode for output. One of:
|
quiet |
Logical scalar. If |
The variables in expr
are vectors of length raster xsize
(row vectors of the input raster layer(s)).
The expression should return a vector also of length raster xsize
(an output row).
Four special variable names are available in expr
:
pixelX
and pixelY
provide pixel center coordinates in projection units.
pixelLon
and pixelLat
can also be used, in which case the pixel x/y
coordinates will be inverse projected to longitude/latitude
(in the same geographic coordinate system used by the input projection,
which is read from the first input raster). Note that inverse projection
adds computation time.
To refer to specific bands in a multi-band input file, repeat the filename in
rasterfiles
and specify corresponding band numbers in bands
, along with
optional variable names in var.names
, for example,
rasterfiles = c("multiband.tif", "multiband.tif") bands = c(4, 5) var.names = c("B4", "B5")
Output will be written to dstfile
. To update a file that already
exists, set write_mode = "update"
and set out_band
to an existing
band number(s) in dstfile
(new bands cannot be created in dstfile
).
To write multiband output, expr
must return a vector of values
interleaved by band. This is equivalent to, and can also be returned as,
a matrix m
with nrow(m)
equal to length()
of an input vector, and
ncol(m)
equal to the number of output bands. In matrix form, each column
contains a vector of output values for a band.
length(m)
must be equal to the length()
of an input vector multiplied by
length(out_band)
. The dimensions described above are assumed and not
read from the return value of expr
.
Returns the output filename invisibly.
GDALRaster-class
, combine()
, rasterToVRT()
## Using pixel longitude/latitude # Hopkins bioclimatic index (HI) as described in: # Bechtold, 2004, West. J. Appl. For. 19(4):245-251. # Integrates elevation, latitude and longitude into an index of the # phenological occurrence of springtime. Here it is relativized to # mean values for an eight-state region in the western US. # Positive HI means spring is delayed by that number of days relative # to the reference position, while negative values indicate spring is # advanced. The original equation had elevation units as feet, so # converting m to ft in `expr`. elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") # expression to calculate HI expr <- "round( ((ELEV_M * 3.281 - 5449) / 100) + ((pixelLat - 42.16) * 4) + ((-116.39 - pixelLon) * 1.25) )" # calc() writes to a tempfile by default hi_file <- calc(expr = expr, rasterfiles = elev_file, var.names = "ELEV_M", dtName = "Int16", nodata_value = -32767, setRasterNodataValue = TRUE) ds <- new(GDALRaster, hi_file) # min, max, mean, sd ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close() ## Calculate normalized difference vegetation index (NDVI) # Landast band 4 (red) and band 5 (near infrared): b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") expr <- "((B5 * 0.0000275 - 0.2) - (B4 * 0.0000275 - 0.2)) / ((B5 * 0.0000275 - 0.2) + (B4 * 0.0000275 - 0.2))" ndvi_file <- calc(expr = expr, rasterfiles = c(b4_file, b5_file), var.names = c("B4", "B5"), dtName = "Float32", nodata_value = -32767, setRasterNodataValue = TRUE) ds <- new(GDALRaster, ndvi_file) ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close() ## Reclassify a variable by rule set # Combine two raster layers and look for specific combinations. Then # recode to a new value by rule set. # # Based on example in: # Stratton, R.D. 2009. Guidebook on LANDFIRE fuels data acquisition, # critique, modification, maintenance, and model calibration. # Gen. Tech. Rep. RMRS-GTR-220. U.S. Department of Agriculture, # Forest Service, Rocky Mountain Research Station. 54 p. # Context: Refine national-scale fuels data to improve fire simulation # results in localized applications. # Issue: Areas with steep slopes (40+ degrees) were mapped as # GR1 (101; short, sparse dry climate grass) and # GR2 (102; low load, dry climate grass) but were not carrying fire. # Resolution: After viewing these areas in Google Earth, # NB9 (99; bare ground) was selected as the replacement fuel model. # look for combinations of slope >= 40 and FBFM 101 or 102 lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") rasterfiles <- c(lcp_file, lcp_file) var.names <- c("SLP", "FBFM") bands <- c(2, 4) tbl <- combine(rasterfiles, var.names, bands) nrow(tbl) tbl_subset <- subset(tbl, SLP >= 40 & FBFM %in% c(101,102)) print(tbl_subset) # twelve combinations meet the criteria sum(tbl_subset$count) # 85 total pixels # recode these pixels to 99 (bare ground) # the LCP driver does not support in-place write so make a copy as GTiff tif_file <- file.path(tempdir(), "storml_lndscp.tif") createCopy("GTiff", tif_file, lcp_file) expr <- "ifelse( SLP >= 40 & FBFM %in% c(101,102), 99, FBFM)" calc(expr = expr, rasterfiles = c(lcp_file, lcp_file), bands = c(2, 4), var.names = c("SLP", "FBFM"), dstfile = tif_file, out_band = 4, write_mode = "update") # verify the ouput rasterfiles <- c(tif_file, tif_file) tbl <- combine(rasterfiles, var.names, bands) tbl_subset <- subset(tbl, SLP >= 40 & FBFM %in% c(101,102)) print(tbl_subset) sum(tbl_subset$count) # if LCP file format is needed: # createCopy("LCP", "storml_edited.lcp", tif_file)
## Using pixel longitude/latitude # Hopkins bioclimatic index (HI) as described in: # Bechtold, 2004, West. J. Appl. For. 19(4):245-251. # Integrates elevation, latitude and longitude into an index of the # phenological occurrence of springtime. Here it is relativized to # mean values for an eight-state region in the western US. # Positive HI means spring is delayed by that number of days relative # to the reference position, while negative values indicate spring is # advanced. The original equation had elevation units as feet, so # converting m to ft in `expr`. elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") # expression to calculate HI expr <- "round( ((ELEV_M * 3.281 - 5449) / 100) + ((pixelLat - 42.16) * 4) + ((-116.39 - pixelLon) * 1.25) )" # calc() writes to a tempfile by default hi_file <- calc(expr = expr, rasterfiles = elev_file, var.names = "ELEV_M", dtName = "Int16", nodata_value = -32767, setRasterNodataValue = TRUE) ds <- new(GDALRaster, hi_file) # min, max, mean, sd ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close() ## Calculate normalized difference vegetation index (NDVI) # Landast band 4 (red) and band 5 (near infrared): b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") expr <- "((B5 * 0.0000275 - 0.2) - (B4 * 0.0000275 - 0.2)) / ((B5 * 0.0000275 - 0.2) + (B4 * 0.0000275 - 0.2))" ndvi_file <- calc(expr = expr, rasterfiles = c(b4_file, b5_file), var.names = c("B4", "B5"), dtName = "Float32", nodata_value = -32767, setRasterNodataValue = TRUE) ds <- new(GDALRaster, ndvi_file) ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close() ## Reclassify a variable by rule set # Combine two raster layers and look for specific combinations. Then # recode to a new value by rule set. # # Based on example in: # Stratton, R.D. 2009. Guidebook on LANDFIRE fuels data acquisition, # critique, modification, maintenance, and model calibration. # Gen. Tech. Rep. RMRS-GTR-220. U.S. Department of Agriculture, # Forest Service, Rocky Mountain Research Station. 54 p. # Context: Refine national-scale fuels data to improve fire simulation # results in localized applications. # Issue: Areas with steep slopes (40+ degrees) were mapped as # GR1 (101; short, sparse dry climate grass) and # GR2 (102; low load, dry climate grass) but were not carrying fire. # Resolution: After viewing these areas in Google Earth, # NB9 (99; bare ground) was selected as the replacement fuel model. # look for combinations of slope >= 40 and FBFM 101 or 102 lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") rasterfiles <- c(lcp_file, lcp_file) var.names <- c("SLP", "FBFM") bands <- c(2, 4) tbl <- combine(rasterfiles, var.names, bands) nrow(tbl) tbl_subset <- subset(tbl, SLP >= 40 & FBFM %in% c(101,102)) print(tbl_subset) # twelve combinations meet the criteria sum(tbl_subset$count) # 85 total pixels # recode these pixels to 99 (bare ground) # the LCP driver does not support in-place write so make a copy as GTiff tif_file <- file.path(tempdir(), "storml_lndscp.tif") createCopy("GTiff", tif_file, lcp_file) expr <- "ifelse( SLP >= 40 & FBFM %in% c(101,102), 99, FBFM)" calc(expr = expr, rasterfiles = c(lcp_file, lcp_file), bands = c(2, 4), var.names = c("SLP", "FBFM"), dstfile = tif_file, out_band = 4, write_mode = "update") # verify the ouput rasterfiles <- c(tif_file, tif_file) tbl <- combine(rasterfiles, var.names, bands) tbl_subset <- subset(tbl, SLP >= 40 & FBFM %in% c(101,102)) print(tbl_subset) sum(tbl_subset$count) # if LCP file format is needed: # createCopy("LCP", "storml_edited.lcp", tif_file)
CmbTable
implements a hash table having a vector of integers as the key,
and the count of occurrences of each unique integer combination as the
value. A unique ID is assigned to each unique combination of input values.
keyLen |
The number of integer values comprising each combination. |
varNames |
Optional character vector of names for the variables in the combination. |
An object of class CmbTable
. Contains a hash table having a
vector of keyLen
integers as the key and the count of occurrences of
each unique integer combination as the value, along with methods that
operate on the table as described in Details.
CmbTable
is a C++ class exposed directly to R (via RCPP_EXPOSED_CLASS
).
Methods of the class are accessed in R using the $
operator.
## Constructors cmb <- new(CmbTable, keyLen) # or, giving the variable names: cmb <- new(CmbTable, keyLen, varNames) ## Methods cmb$update(int_cmb, incr) cmb$updateFromMatrix(int_cmbs, incr) cmb$updateFromMatrixByRow(int_cmbs, incr) cmb$asDataFrame() cmb$asMatrix()
new(CmbTable, keyLen)
Default variable names will be assigned as V1
, V2
, ....
Returns an object of class CmbTable
.
new(CmbTable, keyLen, varNames)
Alternate constructor to specify variable names.
Returns an object of class CmbTable
.
$update(int_cmb, incr)
Updates the hash table for the integer combination in the numeric vector
int_cmb
(coerced to integer by truncation).
If this combination exists in the table, its count will be
incremented by incr
. If the combination is not found in the table,
it will be inserted with count set to incr
.
Returns the unique ID assigned to this combination.
Combination IDs are sequential integers starting at 1
.
$updateFromMatrix(int_cmbs, incr)
This method is the same as $update()
but for a numeric matrix of
integer combinations int_cmbs
(coerced to integer by truncation).
The matrix is arranged with each column vector forming an integer
combination. For example, the rows of the matrix could be
one row each from a set of keyLen
rasters all read at the
same extent and pixel resolution (i.e., row-by-row raster overlay).
The method calls $update()
on each combination (each column of
int_cmbs
), incrementing count by incr
for existing
combinations, or inserting new combinations with count set to incr
.
Returns a numeric vector of length ncol(int_cmbs)
containing the
IDs assigned to the combinations.
$updateFromMatrixByRow(int_cmbs, incr)
This method is the same as $updateFromMatrix()
above except the
integer combinations are in rows of the matrix int_cmbs
(columns
are the variables).
The method calls $update()
on each combination (each row of
int_cmbs
), incrementing count by incr
for existing
combinations, or inserting new combinations with count set to incr
.
Returns a numeric vector of length nrow(int_cmbs)
containing the
IDs assigned to the combinations.
$asDataFrame()
Returns the CmbTable
as a data frame with column cmbid
containing
the unique combination IDs, column count
containing the counts of
occurrences, and keyLen
columns (with names from varNames
) containing
the integer values comprising each unique combination.
$asMatrix()
Returns the CmbTable
as a matrix with column 1
(cmbid
)
containing the unique combination IDs, column 2
(count
)
containing the counts of occurrences, and columns 3:keyLen+2
(with names from varNames
) containing the integer values comprising each
unique combination.
m <- matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1), 3, 6, byrow=FALSE) rownames(m) <- c("layer1", "layer2", "layer3") print(m) cmb <- new(CmbTable, 3, rownames(m)) cmb$updateFromMatrix(m, 1) cmb$asDataFrame() cmb$update(c(4,5,6), 1) cmb$update(c(1,3,5), 1) cmb$asDataFrame() # same as above but matrix arranged with integer combinations in the rows m <- matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1), 6, 3, byrow=TRUE) colnames(m) <- c("V1", "V2", "V3") print(m) cmb <- new(CmbTable, 3) cmb$updateFromMatrixByRow(m, 1) cmb$asDataFrame() cmb$update(c(4,5,6), 1) cmb$update(c(1,3,5), 1) cmb$asDataFrame()
m <- matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1), 3, 6, byrow=FALSE) rownames(m) <- c("layer1", "layer2", "layer3") print(m) cmb <- new(CmbTable, 3, rownames(m)) cmb$updateFromMatrix(m, 1) cmb$asDataFrame() cmb$update(c(4,5,6), 1) cmb$update(c(1,3,5), 1) cmb$asDataFrame() # same as above but matrix arranged with integer combinations in the rows m <- matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1), 6, 3, byrow=TRUE) colnames(m) <- c("V1", "V2", "V3") print(m) cmb <- new(CmbTable, 3) cmb$updateFromMatrixByRow(m, 1) cmb$asDataFrame() cmb$update(c(4,5,6), 1) cmb$update(c(1,3,5), 1) cmb$asDataFrame()
combine()
overlays multiple rasters so that a unique ID is assigned to
each unique combination of input values. The input raster layers
typically have integer data types (floating point will be coerced to
integer by truncation), and must have the same projection, extent and cell
size. Pixel counts for each unique combination are obtained, and
combination IDs are optionally written to an output raster.
combine( rasterfiles, var.names = NULL, bands = NULL, dstfile = NULL, fmt = NULL, dtName = "UInt32", options = NULL, quiet = FALSE )
combine( rasterfiles, var.names = NULL, bands = NULL, dstfile = NULL, fmt = NULL, dtName = "UInt32", options = NULL, quiet = FALSE )
rasterfiles |
Character vector of raster filenames to combine. |
var.names |
Character vector of |
bands |
Numeric vector of |
dstfile |
Character. Optional output raster filename for writing the per-pixel combination IDs. The output raster will be created (and overwritten if it already exists). |
fmt |
Character. Output raster format name (e.g., "GTiff" or "HFA"). |
dtName |
Character. Output raster data type name. Combination IDs are sequential integers starting at 1. The data type for the output raster should be large enough to accommodate the potential number of unique combinations of the input values (e.g., "UInt16" or the default "UInt32"). |
options |
Optional list of format-specific creation options in a
vector of "NAME=VALUE" pairs
(e.g., |
quiet |
Logical scalar. If |
To specify input raster layers that are bands of a multi-band
raster file, repeat the filename in rasterfiles
and provide the
corresponding band numbers in bands
. For example:
rasterfiles <- c("multi-band.tif", "multi-band.tif", "other.tif") bands <- c(4, 5, 1) var.names <- c("multi_b4", "multi_b5", "other")
rasterToVRT()
provides options for virtual clipping, resampling and pixel
alignment, which may be helpful here if the input rasters are not already
aligned on a common extent and cell size.
If an output raster of combination IDs is written, the user should verify that the number of combinations obtained did not exceed the range of the output data type. Combination IDs are sequential integers starting at 1. Typical output data types are the unsigned types: Byte (0 to 255), UInt16 (0 to 65,535) and UInt32 (the default, 0 to 4,294,967,295).
A data frame with column cmbid
containing the combination IDs,
column count
containing the pixel counts for each combination,
and length(rasterfiles)
columns named var.names
containing the integer
values comprising each unique combination.
CmbTable-class
, GDALRaster-class
, calc()
,
rasterToVRT()
buildRAT()
to compute a table of the unique pixel values and their counts
for a single raster layer
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") evc_file <- system.file("extdata/storml_evc.tif", package="gdalraster") evh_file <- system.file("extdata/storml_evh.tif", package="gdalraster") rasterfiles <- c(evt_file, evc_file, evh_file) var.names <- c("veg_type", "veg_cov", "veg_ht") tbl <- combine(rasterfiles, var.names) nrow(tbl) tbl <- tbl[order(-tbl$count),] head(tbl, n = 20) # combine two bands from a multi-band file and write the combination IDs # to an output raster lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") rasterfiles <- c(lcp_file, lcp_file) bands <- c(4, 5) var.names <- c("fbfm", "tree_cov") cmb_file <- file.path(tempdir(), "fbfm_cov_cmbid.tif") opt <- c("COMPRESS=LZW") tbl <- combine(rasterfiles, var.names, bands, cmb_file, options = opt) head(tbl) ds <- new(GDALRaster, cmb_file) ds$info() ds$close()
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") evc_file <- system.file("extdata/storml_evc.tif", package="gdalraster") evh_file <- system.file("extdata/storml_evh.tif", package="gdalraster") rasterfiles <- c(evt_file, evc_file, evh_file) var.names <- c("veg_type", "veg_cov", "veg_ht") tbl <- combine(rasterfiles, var.names) nrow(tbl) tbl <- tbl[order(-tbl$count),] head(tbl, n = 20) # combine two bands from a multi-band file and write the combination IDs # to an output raster lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") rasterfiles <- c(lcp_file, lcp_file) bands <- c(4, 5) var.names <- c("fbfm", "tree_cov") cmb_file <- file.path(tempdir(), "fbfm_cov_cmbid.tif") opt <- c("COMPRESS=LZW") tbl <- combine(rasterfiles, var.names, bands, cmb_file, options = opt) head(tbl) ds <- new(GDALRaster, cmb_file) ds$info() ds$close()
copyDatasetFiles()
copies all the files associated with a dataset.
Wrapper for GDALCopyDatasetFiles()
in the GDAL API.
copyDatasetFiles(new_filename, old_filename, format = "")
copyDatasetFiles(new_filename, old_filename, format = "")
new_filename |
New name for the dataset (copied to). |
old_filename |
Old name for the dataset (copied from). |
format |
Raster format short name (e.g., "GTiff"). If set to empty
string |
Logical TRUE
if no error or FALSE
on failure.
If format
is set to an empty string ""
(the default) then the function
will try to identify the driver from old_filename
. This is done
internally in GDAL by invoking the Identify
method of each registered
GDALDriver
in turn. The first driver that successful identifies the file
name will be returned. An error is raised if a format cannot be determined
from the passed file name.
GDALRaster-class
, create()
, createCopy()
,
deleteDataset()
, renameDataset()
, vsi_copy_file()
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ds$getFileList() ds$close() lcp_tmp <- file.path(tempdir(), "storm_lake_copy.lcp") copyDatasetFiles(lcp_tmp, lcp_file) ds_copy <- new(GDALRaster, lcp_tmp) ds_copy$getFileList() ds_copy$close() deleteDataset(lcp_tmp)
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ds$getFileList() ds$close() lcp_tmp <- file.path(tempdir(), "storm_lake_copy.lcp") copyDatasetFiles(lcp_tmp, lcp_file) ds_copy <- new(GDALRaster, lcp_tmp) ds_copy$getFileList() ds_copy$close() deleteDataset(lcp_tmp)
create()
makes an empty raster in the specified format.
create( format, dst_filename, xsize, ysize, nbands, dataType, options = NULL, return_obj = FALSE )
create( format, dst_filename, xsize, ysize, nbands, dataType, options = NULL, return_obj = FALSE )
format |
Character string giving a raster format short name
(e.g., |
dst_filename |
Character string giving the filename to create. |
xsize |
Integer width of raster in pixels. |
ysize |
Integer height of raster in pixels. |
nbands |
Integer number of bands. |
dataType |
Character string containing the data type name. (e.g., common data types include Byte, Int16, UInt16, Int32, Float32). |
options |
Optional list of format-specific creation options in a
character vector of |
return_obj |
Logical scalar. If |
By default, returns a logical value indicating success (invisible
TRUE
, output written to dst_filename
). An error is raised if the
operation fails. An object of class GDALRaster
open on the
output dataset will be returned if return_obj = TRUE
.
dst_filename
may be an empty string (""
) with format = "MEM"
and
return_obj = TRUE
to create an In-memory Raster
(https://gdal.org/drivers/raster/mem.html).
GDALRaster-class
, createCopy()
, getCreationOptions()
,
rasterFromRaster()
new_file <- file.path(tempdir(), "newdata.tif") ds <- create(format="GTiff", dst_filename = new_file, xsize = 143, ysize = 107, nbands = 1, dataType = "Int16", return_obj=TRUE) # EPSG:26912 - NAD83 / UTM zone 12N ds$setProjection(epsg_to_wkt(26912)) gt <- c(323476, 30, 0, 5105082, 0, -30) ds$setGeoTransform(gt) ds$setNoDataValue(band = 1, -9999) ds$fillRaster(band = 1, -9999, 0) # ... # close the dataset when done ds$close()
new_file <- file.path(tempdir(), "newdata.tif") ds <- create(format="GTiff", dst_filename = new_file, xsize = 143, ysize = 107, nbands = 1, dataType = "Int16", return_obj=TRUE) # EPSG:26912 - NAD83 / UTM zone 12N ds$setProjection(epsg_to_wkt(26912)) gt <- c(323476, 30, 0, 5105082, 0, -30) ds$setGeoTransform(gt) ds$setNoDataValue(band = 1, -9999) ds$fillRaster(band = 1, -9999, 0) # ... # close the dataset when done ds$close()
createColorRamp()
is a wrapper for GDALCreateColorRamp()
in the GDAL
API. It automatically creates a color ramp from one color entry to another.
Output is an integer matrix in color table format for use with
GDALRaster$setColorTable()
.
createColorRamp( start_index, start_color, end_index, end_color, palette_interp = "RGB" )
createColorRamp( start_index, start_color, end_index, end_color, palette_interp = "RGB" )
start_index |
Integer start index (raster value). |
start_color |
Integer vector of length three or four. A color entry value to start the ramp (e.g., RGB values). |
end_index |
Integer end index (raster value). |
end_color |
Integer vector of length three or four. A color entry value to end the ramp (e.g., RGB values). |
palette_interp |
One of "Gray", "RGB" (the default), "CMYK" or "HLS"
describing interpretation of |
Integer matrix with five columns containing the color ramp from
start_index
to end_index
, with raster index values in column 1 and
color entries in columns 2:5).
createColorRamp()
could be called several times, using rbind()
to
combine multiple ramps into the same color table. Possible duplicate rows
in the resulting table are not a problem when used in
GDALRaster$setColorTable()
(i.e., when end_color
of one ramp is the
same as start_color
of the next ramp).
GDALRaster$getColorTable()
,
GDALRaster$getPaletteInterp()
# create a color ramp for tree canopy cover percent # band 5 of an LCP file contains canopy cover lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ds$getDescription(band=5) ds$getMetadata(band=5, domain="") ds$close() # create a GTiff file with Byte data type for the canopy cover band # recode nodata -9999 to 255 tcc_file <- calc(expr = "ifelse(CANCOV == -9999, 255, CANCOV)", rasterfiles = lcp_file, bands = 5, var.names = "CANCOV", fmt = "GTiff", dtName = "Byte", nodata_value = 255, setRasterNodataValue = TRUE) ds_tcc <- new(GDALRaster, tcc_file, read_only=FALSE) # create a color ramp from 0 to 100 and set as the color table colors <- createColorRamp(start_index = 0, start_color = c(211, 211, 211), end_index = 100, end_color = c(0, 100, 0)) print(colors) ds_tcc$setColorTable(band=1, col_tbl=colors, palette_interp="RGB") ds_tcc$setRasterColorInterp(band=1, col_interp="Palette") # close and re-open the dataset in read_only mode ds_tcc$open(read_only=TRUE) plot_raster(ds_tcc, interpolate=FALSE, legend=TRUE, main="Storm Lake Tree Canopy Cover (%)") ds_tcc$close()
# create a color ramp for tree canopy cover percent # band 5 of an LCP file contains canopy cover lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ds$getDescription(band=5) ds$getMetadata(band=5, domain="") ds$close() # create a GTiff file with Byte data type for the canopy cover band # recode nodata -9999 to 255 tcc_file <- calc(expr = "ifelse(CANCOV == -9999, 255, CANCOV)", rasterfiles = lcp_file, bands = 5, var.names = "CANCOV", fmt = "GTiff", dtName = "Byte", nodata_value = 255, setRasterNodataValue = TRUE) ds_tcc <- new(GDALRaster, tcc_file, read_only=FALSE) # create a color ramp from 0 to 100 and set as the color table colors <- createColorRamp(start_index = 0, start_color = c(211, 211, 211), end_index = 100, end_color = c(0, 100, 0)) print(colors) ds_tcc$setColorTable(band=1, col_tbl=colors, palette_interp="RGB") ds_tcc$setRasterColorInterp(band=1, col_interp="Palette") # close and re-open the dataset in read_only mode ds_tcc$open(read_only=TRUE) plot_raster(ds_tcc, interpolate=FALSE, legend=TRUE, main="Storm Lake Tree Canopy Cover (%)") ds_tcc$close()
createCopy()
copies a raster dataset, optionally changing the format.
The extent, cell size, number of bands, data type, projection, and
geotransform are all copied from the source raster.
createCopy( format, dst_filename, src_filename, strict = FALSE, options = NULL, quiet = FALSE, return_obj = FALSE )
createCopy( format, dst_filename, src_filename, strict = FALSE, options = NULL, quiet = FALSE, return_obj = FALSE )
format |
Character string giving the format short name for the
output raster (e.g., |
dst_filename |
Character string giving the filename to create. |
src_filename |
Either a character string giving the filename of the
source raster, or object of class |
strict |
Logical. |
options |
Optional list of format-specific creation options in a
vector of |
quiet |
Logical scalar. If |
return_obj |
Logical scalar. If |
By default, returns a logical value indicating success (invisible
TRUE
, output written to dst_filename
). An error is raised if the
operation fails. An object of class GDALRaster
open on the
output dataset will be returned if return_obj = TRUE
.
dst_filename
may be an empty string (""
) with format = "MEM"
and
return_obj = TRUE
to create an In-memory Raster
(https://gdal.org/drivers/raster/mem.html).
GDALRaster-class
, create()
, getCreationOptions()
,
rasterFromRaster()
, translate()
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") tif_file <- file.path(tempdir(), "storml_lndscp.tif") ds <- createCopy(format = "GTiff", dst_filename = tif_file, src_filename = lcp_file, options = "COMPRESS=LZW", return_obj = TRUE) ds$getMetadata(band = 0, domain = "IMAGE_STRUCTURE") for (band in 1:ds$getRasterCount()) ds$setNoDataValue(band, -9999) ds$getStatistics(band = 1, approx_ok = FALSE, force = TRUE) ds$close()
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") tif_file <- file.path(tempdir(), "storml_lndscp.tif") ds <- createCopy(format = "GTiff", dst_filename = tif_file, src_filename = lcp_file, options = "COMPRESS=LZW", return_obj = TRUE) ds$getMetadata(band = 0, domain = "IMAGE_STRUCTURE") for (band in 1:ds$getRasterCount()) ds$setNoDataValue(band, -9999) ds$getStatistics(band = 1, approx_ok = FALSE, force = TRUE) ds$close()
These values are used in dem_proc()
as the default processing options:
list( "hillshade" = c("-z", "1", "-s", "1", "-az", "315", "-alt", "45", "-alg", "Horn", "-combined", "-compute_edges"), "slope" = c("-s", "1", "-alg", "Horn", "-compute_edges"), "aspect" = c("-alg", "Horn", "-compute_edges"), "color-relief" = character(), "TRI" = c("-alg", "Riley", "-compute_edges"), "TPI" = c("-compute_edges"), "roughness" = c("-compute_edges"))
DEFAULT_DEM_PROC
DEFAULT_DEM_PROC
An object of class list
of length 7.
https://gdal.org/programs/gdaldem.html for a description of all available command-line options for each processing mode
These values are currently used in gdalraster
when a nodata value is
needed but has not been specified:
list("Byte" = 255, "Int8" = -128, "UInt16" = 65535, "Int16" = -32767, "UInt32" = 4294967293, "Int32" = -2147483647, "Float32" = -99999.0, "Float64" = -99999.0)
DEFAULT_NODATA
DEFAULT_NODATA
An object of class list
of length 8.
deleteDataset()
will attempt to delete the named dataset in a format
specific fashion. Full featured drivers will delete all associated files,
database objects, or whatever is appropriate. The default behavior when no
format specific behavior is provided is to attempt to delete all the files
that would be returned by GDALRaster$getFileList()
on the dataset.
The named dataset should not be open in any existing GDALRaster
objects
when deleteDataset()
is called. Wrapper for GDALDeleteDataset()
in the
GDAL API.
deleteDataset(filename, format = "")
deleteDataset(filename, format = "")
filename |
Filename to delete (should not be open in a |
format |
Raster format short name (e.g., "GTiff"). If set to empty
string |
Logical TRUE
if no error or FALSE
on failure.
If format
is set to an empty string ""
(the default) then the function
will try to identify the driver from filename
. This is done internally in
GDAL by invoking the Identify
method of each registered GDALDriver
in
turn. The first driver that successful identifies the file name will be
returned. An error is raised if a format cannot be determined from the
passed file name.
GDALRaster-class
, create()
, createCopy()
,
copyDatasetFiles()
, renameDataset()
b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b5_tmp <- file.path(tempdir(), "b5_tmp.tif") file.copy(b5_file, b5_tmp) ds <- new(GDALRaster, b5_tmp) ds$buildOverviews("BILINEAR", levels = c(2, 4, 8), bands = c(1)) files <- ds$getFileList() print(files) ds$close() file.exists(files) deleteDataset(b5_tmp) file.exists(files)
b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b5_tmp <- file.path(tempdir(), "b5_tmp.tif") file.copy(b5_file, b5_tmp) ds <- new(GDALRaster, b5_tmp) ds$buildOverviews("BILINEAR", levels = c(2, 4, 8), bands = c(1)) files <- ds$getFileList() print(files) ds$close() file.exists(files) deleteDataset(b5_tmp) file.exists(files)
dem_proc()
generates DEM derivatives from an input elevation raster. This
function is a wrapper for the gdaldem
command-line utility.
See https://gdal.org/programs/gdaldem.html for details.
dem_proc( mode, srcfile, dstfile, mode_options = DEFAULT_DEM_PROC[[mode]], color_file = NULL, quiet = FALSE )
dem_proc( mode, srcfile, dstfile, mode_options = DEFAULT_DEM_PROC[[mode]], color_file = NULL, quiet = FALSE )
mode |
Character. Name of the DEM processing mode. One of hillshade, slope, aspect, color-relief, TRI, TPI or roughness. |
srcfile |
Filename of the source elevation raster. |
dstfile |
Filename of the output raster. |
mode_options |
An optional character vector of command-line options (see DEFAULT_DEM_PROC for default values). |
color_file |
Filename of a text file containing lines formatted as:
"elevation_value red green blue". Only used when |
quiet |
Logical scalar. If |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
Band 1 of the source elevation raster is read by default, but this can be
changed by including a -b
command-line argument in mode_options
.
See the documentation for
gdaldem
for a description of all available options for each processing
mode.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") slp_file <- file.path(tempdir(), "storml_slp.tif") dem_proc("slope", elev_file, slp_file)
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") slp_file <- file.path(tempdir(), "storml_slp.tif") dem_proc("slope", elev_file, slp_file)
displayRAT()
generates a presentation table. Colors are shown if the
Raster Attribute Table contains RGB columns.
This function requires package gt
.
displayRAT(tbl, title = "Raster Attribute Table")
displayRAT(tbl, title = "Raster Attribute Table")
tbl |
A data frame formatted as a GDAL RAT (e.g., as returned by
|
title |
Character string to be used in the table title. |
An object of class "gt_tbl"
(i.e., a table created with
gt::gt()
).
buildRAT()
, GDALRaster$getDefaultRAT()
vignette("raster-attribute-tables")
# see examples for `buildRAT()`
# see examples for `buildRAT()`
dump_open_datasets()
dumps a list of all open datasets (shared or not) to
the console. This function is primarily intended to assist in debugging
"dataset leaks" and reference counting issues. The information reported
includes the dataset name, referenced count, shared status, driver name,
size, and band count. This a wrapper for GDALDumpOpenDatasets()
with
output to the console.
dump_open_datasets()
dump_open_datasets()
Number of open datasets.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) dump_open_datasets() ds2 <- new(GDALRaster, elev_file) dump_open_datasets() # open without using shared mode ds3 <- new(GDALRaster, elev_file, read_only = TRUE, open_options = NULL, shared = FALSE) dump_open_datasets() ds$close() dump_open_datasets() ds2$close() dump_open_datasets() ds3$close() dump_open_datasets()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) dump_open_datasets() ds2 <- new(GDALRaster, elev_file) dump_open_datasets() # open without using shared mode ds3 <- new(GDALRaster, elev_file, read_only = TRUE, open_options = NULL, shared = FALSE) dump_open_datasets() ds$close() dump_open_datasets() ds2$close() dump_open_datasets() ds3$close() dump_open_datasets()
epsg_to_wkt()
exports the spatial reference for an EPSG code to
WKT format.
epsg_to_wkt(epsg, pretty = FALSE)
epsg_to_wkt(epsg, pretty = FALSE)
epsg |
Integer EPSG code. |
pretty |
Logical. |
As of GDAL 3.0, the default format for WKT export is OGC WKT 1.
The WKT version can be overridden by using the OSR_WKT_FORMAT
configuration option (see set_config_option()
).
Valid values are one of: SFSQL, WKT1_SIMPLE, WKT1, WKT1_GDAL,
WKT1_ESRI, WKT2_2015, WKT2_2018, WKT2, DEFAULT.
If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or
EXTENSION node is returned. If WKT1_SIMPLE, a WKT1 string without
AXIS, AUTHORITY or EXTENSION node is returned. WKT1 is an alias of
WKT1_GDAL. WKT2 will default to the latest revision implemented
(currently WKT2_2018). WKT2_2019 can be used as an alias of
WKT2_2018 since GDAL 3.2
Character string containing OGC WKT.
epsg_to_wkt(5070) writeLines(epsg_to_wkt(5070, pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "WKT2") writeLines(epsg_to_wkt(5070, pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "")
epsg_to_wkt(5070) writeLines(epsg_to_wkt(5070, pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "WKT2") writeLines(epsg_to_wkt(5070, pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "")
fillNodata()
is a wrapper for GDALFillNodata()
in the GDAL Algorithms
API. This algorithm will interpolate values for all designated nodata
pixels (pixels having an intrinsic nodata value, or marked by zero-valued
pixels in the optional raster specified in mask_file
). For each nodata
pixel, a four direction conic search is done to find values to interpolate
from (using inverse distance weighting).
Once all values are interpolated, zero or more smoothing iterations
(3x3 average filters on interpolated pixels) are applied to smooth out
artifacts.
fillNodata( filename, band, mask_file = "", max_dist = 100, smooth_iterations = 0L, quiet = FALSE )
fillNodata( filename, band, mask_file = "", max_dist = 100, smooth_iterations = 0L, quiet = FALSE )
filename |
Filename of input raster in which to fill nodata pixels. |
band |
Integer band number to modify in place. |
mask_file |
Optional filename of raster to use as a validity mask (band 1 is used, zero marks nodata pixels, non-zero marks valid pixels). |
max_dist |
Maximum distance (in pixels) that the algorithm will search out for values to interpolate (100 pixels by default). |
smooth_iterations |
The number of 3x3 average filter smoothing iterations to run after the interpolation to dampen artifacts (0 by default). |
quiet |
Logical scalar. If |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
The input raster will be modified in place. It should not be open in a
GDALRaster
object while processing with fillNodata()
.
## fill nodata edge pixels in the elevation raster elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ## get count of nodata tbl <- buildRAT(elev_file) head(tbl) tbl[is.na(tbl$VALUE),] ## make a copy that will be modified mod_file <- file.path(tempdir(), "storml_elev_fill.tif") file.copy(elev_file, mod_file) fillNodata(mod_file, band=1) mod_tbl = buildRAT(mod_file) head(mod_tbl) mod_tbl[is.na(mod_tbl$VALUE),]
## fill nodata edge pixels in the elevation raster elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ## get count of nodata tbl <- buildRAT(elev_file) head(tbl) tbl[is.na(tbl$VALUE),] ## make a copy that will be modified mod_file <- file.path(tempdir(), "storml_elev_fill.tif") file.copy(elev_file, mod_file) fillNodata(mod_file, band=1) mod_tbl = buildRAT(mod_file) head(mod_tbl) mod_tbl[is.na(mod_tbl$VALUE),]
footprint()
is a wrapper of the gdal_footprint
command-line
utility (see https://gdal.org/programs/gdal_footprint.html).
The function can be used to compute the footprint of a raster file, taking
into account nodata values (or more generally the mask band attached to
the raster bands), and generating polygons/multipolygons corresponding to
areas where pixels are valid, and write to an output vector file.
Refer to the GDAL documentation at the URL above for a list of command-line
arguments that can be passed in cl_arg
. Requires GDAL >= 3.8.
footprint(src_filename, dst_filename, cl_arg = NULL)
footprint(src_filename, dst_filename, cl_arg = NULL)
src_filename |
Character string. Filename of the source raster. |
dst_filename |
Character string. Filename of the destination vector.
If the file and the output layer exist, the new footprint is appended to
them, unless the |
cl_arg |
Optional character vector of command-line arguments for
|
Post-vectorization geometric operations are applied in the following order:
optional splitting (-split_polys
)
optional densification (-densify
)
optional reprojection (-t_srs
)
optional filtering by minimum ring area (-min_ring_area
)
optional application of convex hull (-convex_hull
)
optional simplification (-simplify
)
limitation of number of points (-max_points
)
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") out_file <- file.path(tempdir(), "storml.geojson") # Requires GDAL >= 3.8 if (as.integer(gdal_version()[2]) >= 3080000) { # command-line arguments for gdal_footprint args <- c("-t_srs", "EPSG:4326") footprint(evt_file, out_file, args) }
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") out_file <- file.path(tempdir(), "storml.geojson") # Requires GDAL >= 3.8 if (as.integer(gdal_version()[2]) >= 3080000) { # command-line arguments for gdal_footprint args <- c("-t_srs", "EPSG:4326") footprint(evt_file, out_file, args) }
g_area()
computes the area for a LinearRing
, Polygon
or
MultiPolygon
. Undefined for all other geometry types (returns zero).
g_area(wkt)
g_area(wkt)
wkt |
Character. OGC WKT string for a simple feature geometry. |
Numeric scalar. Area of the geometry or 0
.
LinearRing
is a non-standard geometry type, used in GDAL just for geometry
creation.
These functions implement operations on pairs of geometries in OGC WKT format.
g_intersection(this_geom, other_geom) g_union(this_geom, other_geom) g_difference(this_geom, other_geom) g_sym_difference(this_geom, other_geom)
g_intersection(this_geom, other_geom) g_union(this_geom, other_geom) g_difference(this_geom, other_geom) g_sym_difference(this_geom, other_geom)
this_geom |
Character. OGC WKT string for a simple feature geometry. |
other_geom |
Character. OGC WKT string for a simple feature geometry. |
These functions use the GEOS library via GDAL headers.
g_intersection()
returns a new geometry which is the region of
intersection of the two geometries operated on. g_intersects()
can be used
to test if two geometries intersect.
g_union()
returns a new geometry which is the region of
union of the two geometries operated on.
g_difference()
returns a new geometry which is the region of this geometry
with the region of the other geometry removed.
g_sym_difference()
returns a new geometry which is the symmetric
difference of this geometry and the other geometry (union minus
intersection).
Character string. The resulting geometry as OGC WKT.
Geometry validity is not checked. In case you are unsure of the validity
of the input geometries, call g_is_valid()
before, otherwise the result
might be wrong.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) g1 <- ds$bbox() |> bbox_to_wkt() ds$close() g2 <- "POLYGON ((327381.9 5104541.2, 326824.0 5104092.5, 326708.8 5103182.9, 327885.2 5102612.9, 329334.5 5103322.4, 329304.2 5104474.5,328212.7 5104656.4, 328212.7 5104656.4, 327381.9 5104541.2))" # see spatial predicate defintions at https://en.wikipedia.org/wiki/DE-9IM g_intersects(g1, g2) # TRUE g_overlaps(g1, g2) # TRUE # therefore, g_contains(g1, g2) # FALSE g_sym_difference(g1, g2) |> g_area() g3 <- g_intersection(g1, g2) g4 <- g_union(g1, g2) g_difference(g4, g3) |> g_area()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) g1 <- ds$bbox() |> bbox_to_wkt() ds$close() g2 <- "POLYGON ((327381.9 5104541.2, 326824.0 5104092.5, 326708.8 5103182.9, 327885.2 5102612.9, 329334.5 5103322.4, 329304.2 5104474.5,328212.7 5104656.4, 328212.7 5104656.4, 327381.9 5104541.2))" # see spatial predicate defintions at https://en.wikipedia.org/wiki/DE-9IM g_intersects(g1, g2) # TRUE g_overlaps(g1, g2) # TRUE # therefore, g_contains(g1, g2) # FALSE g_sym_difference(g1, g2) |> g_area() g3 <- g_intersection(g1, g2) g4 <- g_union(g1, g2) g_difference(g4, g3) |> g_area()
These functions implement tests for pairs of geometries in OGC WKT format.
g_intersects(this_geom, other_geom) g_disjoint(this_geom, other_geom) g_touches(this_geom, other_geom) g_contains(this_geom, other_geom) g_within(this_geom, other_geom) g_crosses(this_geom, other_geom) g_overlaps(this_geom, other_geom) g_equals(this_geom, other_geom)
g_intersects(this_geom, other_geom) g_disjoint(this_geom, other_geom) g_touches(this_geom, other_geom) g_contains(this_geom, other_geom) g_within(this_geom, other_geom) g_crosses(this_geom, other_geom) g_overlaps(this_geom, other_geom) g_equals(this_geom, other_geom)
this_geom |
Character. OGC WKT string for a simple feature geometry. |
other_geom |
Character. OGC WKT string for a simple feature geometry. |
These functions use the GEOS library via GDAL headers.
g_intersects()
tests whether two geometries intersect.
g_disjoint()
tests if this geometry and the other geometry are disjoint.
g_touches()
tests if this geometry and the other geometry are touching.
g_contains()
tests if this geometry contains the other geometry.
g_within()
tests if this geometry is within the other geometry.
g_crosses()
tests if this geometry and the other geometry are crossing.
g_overlaps()
tests if this geometry and the other geometry overlap, that
is, their intersection has a non-zero area (they have some but not all
points in common).
g_equals()
tests whether two geometries are equivalent.
The GDAL documentation says: "This operation implements the SQL/MM
ST_OrderingEquals()
operation. The comparison is done in a structural way,
that is to say that the geometry types must be identical, as well as the
number and ordering of sub-geometries and vertices. Or equivalently, two
geometries are considered equal by this method if their WKT/WKB
representation is equal. Note: this must be distinguished from equality in
a spatial way."
Logical scalar
Geometry validity is not checked. In case you are unsure of the validity
of the input geometries, call g_is_valid()
before, otherwise the result
might be wrong.
https://en.wikipedia.org/wiki/DE-9IM
g_buffer()
builds a new geometry containing the buffer region around
the geometry on which it is invoked. The buffer is a polygon containing
the region within the buffer distance of the original geometry.
g_buffer( geom, dist, quad_segs = 30L, as_wkb = TRUE, as_iso = FALSE, byte_order = "LSB", quiet = FALSE )
g_buffer( geom, dist, quad_segs = 30L, as_wkb = TRUE, as_iso = FALSE, byte_order = "LSB", quiet = FALSE )
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
dist |
Numeric buffer distance in units of the |
quad_segs |
Integer number of segments used to define a 90 degree curve (quadrant of a circle). Large values result in large numbers of vertices in the resulting buffer geometry while small numbers reduce the accuracy of the result. |
as_wkb |
Logical, |
as_iso |
Logical, |
byte_order |
Character string specifying the byte order when output is
WKB. One of |
quiet |
Logical, |
A polygon as WKB raw vector or WKT string, or a list/character vector of
polygons as WKB/WKT with length equal to length(geom)
. NA
is returned
with a warning if an input geometry cannot be converted into an OGR
geometry object, or if an error occurs in the call to Buffer() in the
underlying OGR API.
g_buffer("POINT (0 0)", dist = 10, as_wkb = FALSE)
g_buffer("POINT (0 0)", dist = 10, as_wkb = FALSE)
g_centroid()
returns a vector of point X, point Y.
g_centroid(wkt)
g_centroid(wkt)
wkt |
Character. OGC WKT string for a simple feature geometry. |
The GDAL documentation states "This method relates to the SFCOM
ISurface::get_Centroid()
method however the current implementation based
on GEOS can operate on other geometry types such as multipoint, linestring,
geometrycollection such as multipolygons. OGC SF SQL 1.1 defines the
operation for surfaces (polygons). SQL/MM-Part 3 defines the operation for
surfaces and multisurfaces (multipolygons)."
Numeric vector of length 2 containing the centroid (X, Y).
g_distance()
returns the distance between two geometries or -1
if an
error occurs. Returns the shortest distance between the two geometries.
The distance is expressed into the same unit as the coordinates of the
geometries.
g_distance(this_geom, other_geom)
g_distance(this_geom, other_geom)
this_geom |
Character. OGC WKT string for a simple feature geometry. |
other_geom |
Character. OGC WKT string for a simple feature geometry. |
Numeric. Distance or '-1' if an error occurs.
Geometry validity is not checked. In case you are unsure of the validity
of the input geometries, call g_is_valid()
before, otherwise the result
might be wrong.
g_distance("POINT (0 0)", "POINT (5 12)")
g_distance("POINT (0 0)", "POINT (5 12)")
g_is_empty()
tests whether a geometry has no points.
g_is_empty(geom, quiet = FALSE)
g_is_empty(geom, quiet = FALSE)
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
quiet |
Logical, |
logical vector of the same length as the number of input
geometries in geom
, containing TRUE
for the corresponding geometies
that are empty or FALSE
for non-empty geometries.
g1 <- "POLYGON ((0 0, 10 10, 10 0, 0 0))" g2 <- "POLYGON ((5 1, 9 5, 9 1, 5 1))" g_difference(g2, g1) |> g_is_empty()
g1 <- "POLYGON ((0 0, 10 10, 10 0, 0 0))" g2 <- "POLYGON ((5 1, 9 5, 9 1, 5 1))" g_difference(g2, g1) |> g_is_empty()
g_is_valid()
tests whether a geometry is valid.
g_is_valid(geom, quiet = FALSE)
g_is_valid(geom, quiet = FALSE)
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
quiet |
Logical, |
logical vector of the same length as the number of input
geometries in geom
, containing TRUE
for the corresponding geometies
that are valid or FALSE
for invalid geometries.
g1 <- "POLYGON ((0 0, 10 10, 10 0, 0 0))" g2 <- "POLYGON ((0 0, 10 10, 10 0))" g3 <- "POLYGON ((0 0, 10 10, 10 0, 0 1))" g_is_valid(c(g1, g2, g3))
g1 <- "POLYGON ((0 0, 10 10, 10 0, 0 0))" g2 <- "POLYGON ((0 0, 10 10, 10 0))" g3 <- "POLYGON ((0 0, 10 10, 10 0, 0 1))" g_is_valid(c(g1, g2, g3))
g_length()
computes the length for LineString
or MultiCurve
objects.
Undefined for all other geometry types (returns zero).
g_length(wkt)
g_length(wkt)
wkt |
Character. OGC WKT string for a simple feature geometry. |
Numeric scalar. Length of the geometry or 0
.
g_length("LINESTRING (0 0, 3 4)")
g_length("LINESTRING (0 0, 3 4)")
g_make_valid()
attempts to make an invalid geometry valid without losing
vertices. Already-valid geometries are cloned without further intervention.
Wrapper of OGR_G_MakeValid()
/OGR_G_MakeValidEx()
in the GDAL Vector C
API.
g_make_valid( geom, method = "LINEWORK", keep_collapsed = FALSE, as_wkb = TRUE, as_iso = FALSE, byte_order = "LSB", quiet = FALSE )
g_make_valid( geom, method = "LINEWORK", keep_collapsed = FALSE, as_wkb = TRUE, as_iso = FALSE, byte_order = "LSB", quiet = FALSE )
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
method |
Character string. One of |
keep_collapsed |
Logical, applies only to the STRUCTURE method.
Defaults to |
as_wkb |
Logical, |
as_iso |
Logical, |
byte_order |
Character string specifying the byte order when output is
WKB. One of |
quiet |
Logical, |
LINEWORK is the default method, which combines all rings into a set of noded lines and then extracts valid polygons from that linework. The STRUCTURE method (requires GEOS >= 3.10 and GDAL >= 3.4) first makes all rings valid, then merges shells and subtracts holes from shells to generate a valid result. Assumes that holes and shells are correctly categorized.
KEEP_COLLAPSED only applies to the STRUCTURE method:
FALSE
(the default): collapses are converted to empty geometries
TRUE
: collapses are converted to a valid geometry of lower dimension
A geometry as WKB raw vector or WKT string, or a list/character vector of
geometries as WKB/WKT with length equal to length(geom)
. NA
is returned
with a warning if an input geometry cannot be converted into an OGR
geometry object, or if an error occurs in the call to MakeValid() in the
underlying OGR API.
This function is built on the GEOS >= 3.8 library, check it for the
definition of the geometry operation. If OGR is built without GEOS >= 3.8,
this function will return a clone of the input geometry if it is valid, or
NA
if it is invalid.
# requires GEOS >= 3.8, otherwise is only a validity test (see Note) geos_version() # valid wkt <- "POINT (0 0)" g_make_valid(wkt, as_wkb = FALSE) # invalid to valid wkt <- "POLYGON ((0 0,10 10,0 10,10 0,0 0))" g_make_valid(wkt, as_wkb = FALSE) # invalid - error wkt <- "LINESTRING (0 0)" g_make_valid(wkt) # NA
# requires GEOS >= 3.8, otherwise is only a validity test (see Note) geos_version() # valid wkt <- "POINT (0 0)" g_make_valid(wkt, as_wkb = FALSE) # invalid to valid wkt <- "POLYGON ((0 0,10 10,0 10,10 0,0 0))" g_make_valid(wkt, as_wkb = FALSE) # invalid - error wkt <- "LINESTRING (0 0)" g_make_valid(wkt) # NA
g_name()
returns geometry type names in well known text format.
g_name(geom, quiet = FALSE)
g_name(geom, quiet = FALSE)
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
quiet |
Logical, |
character vector of the same length as the number of input
geometries in geom
, containing the WKT names for the corresponding
geometies.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) bbox_to_wkt(ds$bbox()) |> g_name() ds$close()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) bbox_to_wkt(ds$bbox()) |> g_name() ds$close()
g_summary()
returns text summaries of WKB/WKT geometries.
Requires GDAL >= 3.7.
g_summary(geom, quiet = FALSE)
g_summary(geom, quiet = FALSE)
geom |
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. |
quiet |
Logical, |
character vector of the same length as the number of input
geometries in geom
, containing summaries for the corresponding geometies.
# Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster") lyr <- new(GDALVector, f, "mtbs_perims") feat <- lyr$getNextFeature() g_summary(feat$geom) feat_set <- lyr$fetch(5) g_summary(feat_set$geom) lyr$close() }
# Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster") lyr <- new(GDALVector, f, "mtbs_perims") feat <- lyr$getNextFeature() g_summary(feat$geom) feat_set <- lyr$fetch(5) g_summary(feat_set$geom) lyr$close() }
g_transform()
will transform the coordinates of a geometry from their
current spatial reference system to a new target spatial reference system.
Normally this means reprojecting the vectors, but it could include datum
shifts, and changes of units.
g_transform( wkt, srs_from, srs_to, wrap_date_line = FALSE, date_line_offset = 10L )
g_transform( wkt, srs_from, srs_to, wrap_date_line = FALSE, date_line_offset = 10L )
wkt |
Character. OGC WKT string for a simple feature geometry. |
srs_from |
Character string in OGC WKT format specifying the
spatial reference system for the geometry given by |
srs_to |
Character string in OGC WKT format specifying the target spatial reference system. |
wrap_date_line |
Logical scalar. |
date_line_offset |
Integer scalar. Longitude gap in degree. Defaults
to |
Character string for a transformed OGC WKT geometry.
This function uses the OGR_GeomTransformer_Create()
and
OGR_GeomTransformer_Transform()
functions in the GDAL API: "This is an
enhanced version of OGR_G_Transform()
. When reprojecting geometries from
a Polar Stereographic projection or a projection naturally crossing the
antimeridian (like UTM Zone 60) to a geographic CRS, it will cut geometries
along the antimeridian. So a LineString
might be returned as a
MultiLineString
."
The wrap_date_line = TRUE
option might be specified for circumstances to
correct geometries that incorrectly go from a longitude on a side of the
antimeridian to the other side, e.g., LINESTRING (-179 0,179 0)
will be
transformed to MULTILINESTRING ((-179 0,-180 0),(180 0,179 0))
. For that
use case, srs_to
might be the same as srs_from
.
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) # the convenience function bbox_transform() does this: bbox_to_wkt(ds$bbox()) |> g_transform(ds$getProjection(), epsg_to_wkt(4326)) |> bbox_from_wkt() ds$close() # correct geometries that incorrectly go from a longitude on a side of the # antimeridian to the other side geom <- "LINESTRING (-179 0,179 0)" srs <- epsg_to_wkt(4326) g_transform(geom, srs, srs, wrap_date_line = TRUE)
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) # the convenience function bbox_transform() does this: bbox_to_wkt(ds$bbox()) |> g_transform(ds$getProjection(), epsg_to_wkt(4326)) |> bbox_from_wkt() ds$close() # correct geometries that incorrectly go from a longitude on a side of the # antimeridian to the other side geom <- "LINESTRING (-179 0,179 0)" srs <- epsg_to_wkt(4326) g_transform(geom, srs, srs, wrap_date_line = TRUE)
g_wk2wk()
converts geometries between Well Known Binary (WKB) and
Well Known Text (WKT) formats. A geometry given as a raw vector of WKB will
be converted to a WKT string, while a geometry given as a WKT string will be
converted to a WKB raw vector. Input may also be a list of WKB raw vectors
or a character vector of WKT strings.
g_wk2wk(geom, as_iso = FALSE, byte_order = "LSB")
g_wk2wk(geom, as_iso = FALSE, byte_order = "LSB")
geom |
Either a raw vector of WKB or list of raw vectors to convert to WKT, or a character vector containing one or more WKT strings to convert to WKB. |
as_iso |
Logical scalar. |
byte_order |
Character string specifying the byte order when converting
to WKB. One of |
For input of a WKB raw vector or list of raw vectors, returns a character vector of WKT strings, with length of the returned vector equal to the number of input raw vectors. For input of a single WKT string, returns a raw vector of WKB. For input of a character vector containing more than one WKT string, returns a list of WKB raw vectors, with length of the returned list equal to the number of input strings.
With as_iso = FALSE
(the default), geometries are exported as extended
dimension (Z) WKB/WKT for types Point, LineString, Polygon, MultiPoint,
MultiLineString, MultiPolygon and GeometryCollection. For other geometry
types, it is equivalent to ISO.
When the return value is a list of WKB raw vectors, an element in the
returned list will contain NA
if the corresponding input string was NA
or empty (""
).
When input is a list of WKB raw vectors, a corresponding element in the
returned character vector will be an empty string (""
) if the input was
a raw vector of length 0
(raw(0)
). If an input list element is not a raw
vector, then the corresponding element in the returned character vector will
be NA
.
GEOS reference for geometry formats:
https://libgeos.org/specifications/
wkt <- "POINT (-114 47)" wkb <- g_wk2wk(wkt) print(wkb) g_wk2wk(wkb)
wkt <- "POINT (-114 47)" wkb <- g_wk2wk(wkt) print(wkb) g_wk2wk(wkb)
gdal_formats()
returns a table of the supported raster and vector
formats, with information about the capabilities of each format driver.
gdal_formats(format = "")
gdal_formats(format = "")
format |
A character string containing a driver short name. By default, information for all configured raster and vector format drivers will be returned. |
A data frame containing the format short name, long name, raster
(logical), vector (logical), read/write flag (ro
is read-only,
w
supports CreateCopy, w+
supports Create), virtual I/O supported
(logical), and subdatasets (logical).
Virtual I/O refers to operations on GDAL Virtual File Systems. See https://gdal.org/user/virtual_file_systems.html#virtual-file-systems.
nrow(gdal_formats()) head(gdal_formats()) gdal_formats("GPKG")
nrow(gdal_formats()) head(gdal_formats()) gdal_formats("GPKG")
gdal_version()
returns runtime version information.
gdal_version()
gdal_version()
Character vector of length four containing:
"–version" - one line version message, e.g., “GDAL 3.6.3, released 2023/03/12”
"GDAL_VERSION_NUM" - formatted as a string, e.g., “3060300” for GDAL 3.6.3.0
"GDAL_RELEASE_DATE" - formatted as a string, e.g., “20230312”
"GDAL_RELEASE_NAME" - e.g., “3.6.3”
gdal_version()
gdal_version()
GDALRaster
provides an interface for accessing a raster dataset via GDAL
and calling methods on the underlying GDALDataset
, GDALDriver
and
GDALRasterBand
objects. See https://gdal.org/api/index.html for
details of the GDAL Raster API.
filename |
Character string containing the file name of a raster
dataset to open, as full path or relative to the current working directory.
In some cases, |
read_only |
Logical. |
open_options |
Optional character vector of |
shared |
Logical. |
An object of class GDALRaster
which contains a pointer to the
opened dataset, and methods that operate on the dataset as described in
Details. GDALRaster
is a C++ class exposed directly to R (via
RCPP_EXPOSED_CLASS
). Fields and methods of the class are accessed using
the $
operator. The read/write fields can be used for per-object settings.
## Constructors # read-only by default: ds <- new(GDALRaster, filename) # for update access: ds <- new(GDALRaster, filename, read_only = FALSE) # to use dataset open options: ds <- new(GDALRaster, filename, read_only = TRUE|FALSE, open_options) # to open without shared mode: new(GDALRaster, filename, read_only, open_options, shared = FALSE) ## Read/write fields ds$infoOptions ds$quiet ds$readByteAsRaw ## Methods ds$getFilename() ds$setFilename(filename) ds$open(read_only) ds$isOpen() ds$getFileList() ds$info() ds$infoAsJSON() ds$getDriverShortName() ds$getDriverLongName() ds$getRasterXSize() ds$getRasterYSize() ds$getRasterCount() ds$addBand(dataType, options) ds$getGeoTransform() ds$setGeoTransform(transform) ds$getProjection() ds$getProjectionRef() ds$setProjection(projection) ds$bbox() ds$res() ds$dim() ds$apply_geotransform(col_row) ds$get_pixel_line(xy) ds$getDescription(band) ds$setDescription(band, desc) ds$getBlockSize(band) ds$getActualBlockSize(band, xblockoff, yblockoff) ds$getOverviewCount(band) ds$buildOverviews(resampling, levels, bands) ds$getDataTypeName(band) ds$getNoDataValue(band) ds$setNoDataValue(band, nodata_value) ds$deleteNoDataValue(band) ds$getMaskFlags(band) ds$getMaskBand(band) ds$getUnitType(band) ds$setUnitType(band, unit_type) ds$getScale(band) ds$setScale(band, scale) ds$getOffset(band) ds$setOffset(band, offset) ds$getRasterColorInterp(band) ds$setRasterColorInterp(band, col_interp) ds$getMinMax(band, approx_ok) ds$getStatistics(band, approx_ok, force) ds$clearStatistics() ds$getHistogram(band, min, max, num_buckets, incl_out_of_range, approx_ok) ds$getDefaultHistogram(band, force) ds$getMetadata(band, domain) ds$setMetadata(band, metadata, domain) ds$getMetadataItem(band, mdi_name, domain) ds$setMetadataItem(band, mdi_name, mdi_value, domain) ds$getMetadataDomainList(band) ds$read(band, xoff, yoff, xsize, ysize, out_xsize, out_ysize) ds$write(band, xoff, yoff, xsize, ysize, rasterData) ds$fillRaster(band, value, ivalue) ds$getColorTable(band) ds$getPaletteInterp(band) ds$setColorTable(band, col_tbl, palette_interp) ds$clearColorTable(band) ds$getDefaultRAT(band) ds$setDefaultRAT(band, df) ds$flushCache() ds$getChecksum(band, xoff, yoff, xsize, ysize) ds$close()
new(GDALRaster, filename, read_only)
Returns an object of class GDALRaster
. The read_only
argument defaults
to TRUE
if not specified.
new(GDALRaster, filename, read_only, open_options)
Alternate constructor for passing dataset open_options
, a character
vector of NAME=VALUE
pairs.
read_only
is required for this form of the constructor, TRUE
for
read-only access, or FALSE
to open with write access.
Returns an object of class GDALRaster
.
new(GDALRaster, filename, read_only, open_options, shared)
Alternate constructor for specifying the shared
mode for dataset opening.
The shared
argument defaults to TRUE
but can be set to FALSE
with this
constructor (see Note).
All arguments are required with this form of the constructor, but
open_options
can be NULL
. Returns an object of class GDALRaster
.
$infoOptions
A character vector of command-line arguments to control the output of
$info()
and $infoAsJSON()
(see below).
Defaults to character(0)
. Can be set to a vector of strings specifying
arguments to the gdalinfo
command-line utility, e.g.,
c("-nomd", "-norat", "-noct")
.
Restore the default by setting to empty string (""
) or character(0)
.
$quiet
A logical value, FALSE
by default. This field can be set to TRUE
which
will suppress various messages as well as progress reporting for potentially
long-running processes such as building overviews and computation of
statistics and histograms.
$readByteAsRaw
A logical value, FALSE
by default. This field can be set to TRUE
which
will affect the data type returned by $read()
and read_ds()
. When the
underlying band data type is Byte and readByteAsRaw
is TRUE
the output
type will be raw rather than integer. See also the as_raw
argument to
read_ds()
to control this in a non-persistent setting. If the underlying
band data type is not Byte this setting has no effect.
$getFilename()
Returns a character string containing the filename
associated with this
GDALRaster
object (filename
originally used to open the dataset). May
be a regular filename, database connection string, URL, etc.
$setFilename(filename)
Sets the filename
if the underlying dataset does not already have an
associated filename. Explicitly setting the filename is an advanced
setting that should only be used when the user has determined that it is
needed. Writing certain virtual datasets to file is one potential use case
(e.g., a dataset returned by autoCreateWarpedVRT()
).
$open(read_only)
(Re-)opens the raster dataset on the existing filename. Use this method to
open a dataset that has been closed using $close()
. May be used to
re-open a dataset with a different read/write access (read_only
set to
TRUE
or FALSE
). The method will first close an open dataset, so it is
not required to call $close()
explicitly in this case.
No return value, called for side effects.
$isOpen()
Returns logical indicating whether the associated raster dataset is open.
$getFileList()
Returns a character vector of files believed to be part of this dataset.
If it returns an empty string (""
) it means there is believed to be no
local file system files associated with the dataset (e.g., a virtual file
system). The returned filenames will normally be relative or absolute
paths depending on the path used to originally open the dataset.
$info()
Prints various information about the raster dataset to the console (no
return value, called for that side effect only).
Equivalent to the output of the gdalinfo
command-line utility
(gdalinfo filename
, if using the default infoOptions
).
See the field $infoOptions
above for setting the arguments to gdalinfo
.
$infoAsJSON()
Returns information about the raster dataset as a JSON-formatted string.
Equivalent to the output of the gdalinfo
command-line utility
(gdalinfo -json filename
, if using the default infoOptions
).
See the field $infoOptions
above for setting the arguments to gdalinfo
.
$getDriverShortName()
Returns the short name of the raster format driver.
$getDriverLongName()
Returns the long name of the raster format driver.
$getRasterXSize()
Returns the number of pixels along the x dimension.
$getRasterYSize()
Returns the number of pixels along the y dimension.
$getRasterCount()
Returns the number of raster bands on this dataset. For the methods
described below that operate on individual bands, the band
argument is the integer band number (1-based).
$addBand(dataType, options)
Adds a band to a dataset if the underlying format supports this action.
Most formats do not, but "MEM"
and "VRT"
are notable exceptions
that support adding bands. The added band will always be the last band.
dataType
is a character string containing the data type name
(e.g., "Byte"
, "Int16"
, "UInt16"
, "Int32"
, "Float32"
, etc).
The options
argument is a character vector of NAME=VALUE option strings.
Supported options are format specific. Note that the options
argument is
required but may be given as NULL
. Returns logical TRUE
on success
or FALSE
if a band could not be added.
$getGeoTransform()
Returns the affine transformation coefficients for transforming between
pixel/line raster space (column/row) and projection coordinate space
(geospatial x/y). The return value is a numeric vector of length six.
See https://gdal.org/tutorials/geotransforms_tut.html
for details of the affine transformation. With 1-based indexing
in R, the geotransform vector contains (in map units of the raster spatial
reference system):
GT[1] | x-coordinate of upper-left corner of the upper-left pixel |
GT[2] | x-component of pixel width |
GT[3] | row rotation (zero for north-up raster) |
GT[4] | y-coordinate of upper-left corner of the upper-left pixel |
GT[5] | column rotation (zero for north-up raster) |
GT[6] | y-component of pixel height (negative for north-up raster) |
$setGeoTransform(transform)
Sets the affine transformation coefficients on this dataset.
transform
is a numeric vector of length six.
Returns logical TRUE
on success or FALSE
if the geotransform
could not be set.
$getProjection()
Returns the coordinate reference system of the raster as an OGC WKT
format string. Equivalent to ds$getProjectionRef()
.
$getProjectionRef()
Returns the coordinate reference system of the raster as an OGC WKT
format string.
An empty string is returned when a projection definition is not available.
$setProjection(projection)
Sets the projection reference for this dataset.
projection
is a string in OGC WKT format.
Returns logical TRUE
on success or FALSE
if the projection
could not be set.
$bbox()
Returns a numeric vector of length four containing the bounding box
(xmin, ymin, xmax, ymax) assuming this is a north-up raster.
$res()
Returns a numeric vector of length two containing the resolution
(pixel width, pixel height as positive values) assuming this is a north-up
raster.
$dim()
Returns an integer vector of length three containing the raster dimensions.
Equivalent to:
c(ds$getRasterXSize(), ds$getRasterYSize(), ds$getRasterCount())
$apply_geotransform(col_row)
Applies geotransform coefficients to raster coordinates in pixel/line space
(column/row), converting into georeferenced (x/y) coordinates.
col_row
is a numeric matrix of raster col/row coordinates (or two-column
data frame that will be coerced to numeric matrix).
Returns a numeric matrix of geospatial x/y coordinates.
See the stand-alone function of the same name (apply_geotransform()
) for
more info and examples.
$get_pixel_line(xy)
Converts geospatial coordinates to pixel/line (raster column/row numbers).
xy
is a numeric matrix of geospatial x,y coordinates in the same spatial
reference system as the raster (or two-column data frame that will be
coerced to numeric matrix). Returns an integer matrix of raster pixel/line.
See the stand-alone function of the same name (get_pixel_line()
) for more
info and examples.
$getDescription(band)
Returns a string containing the description for band
. An empty
string is returned if no description is set for the band.
Passing band = 0
will return the dataset-level description.
$setDescription(band, desc)
Sets a description for band
. desc
is the character string
to set. No return value. (Passing band = 0
can be used to set the
dataset-level description. Note that the dataset description is generally
the filename that was used to open the dataset. It usually should not be
changed by calling this method on an existing dataset.)
$getBlockSize(band)
Returns an integer vector of length two (xsize, ysize) containing the
"natural" block size of band
. GDAL has a concept of the natural block
size of rasters so that applications can organize data access efficiently
for some file formats. The natural block size is the block size that is
most efficient for accessing the format. For many formats this is simply a
whole row in which case block xsize is the same as $getRasterXSize()
and block ysize is 1. However, for tiled images block size will typically
be the tile size. Note that the X and Y block sizes don't have to divide
the image size evenly, meaning that right and bottom edge blocks may be
incomplete.
$getActualBlockSize(band, xblockoff, yblockoff)
Returns an integer vector of length two (xvalid, yvalid) containing the
actual block size for a given block offset in band
. Handles partial
blocks at the edges of the raster and returns the true number of pixels.
xblockoff
is an integer scalar, the horizontal block offset for which to
calculate the number of valid pixels, with zero indicating the left most
block, 1 the next block, etc. yblockoff
is likewise the vertical block
offset, with zero indicating the top most block, 1 the next block, etc.
$getOverviewCount(band)
Returns the number of overview layers (a.k.a. pyramids) available for
band
.
$buildOverviews(resampling, levels, bands)
Build one or more raster overview images using the specified downsampling
algorithm.
resampling
is a character string, one of AVERAGE
,
AVERAGE_MAGPHASE
, RMS
, BILINEAR
, CUBIC
, CUBICSPLINE
, GAUSS
,
LANCZOS
, MODE
, NEAREST
or NONE
.
levels
is an integer vector giving the list of overview decimation
factors to build (e.g., c(2, 4, 8)
), or 0
to delete all overviews
(at least for external overviews (.ovr) and GTiff internal overviews).
bands
is an integer vector giving a list of band numbers to build
overviews for, or 0
to build for all bands.
Note that for GTiff, overviews will be created internally if the dataset is
open in update mode, while external overviews (.ovr) will be created if the
dataset is open read-only.
External overviews created in GTiff format may be compressed using the
COMPRESS_OVERVIEW
configuration option. All compression methods supported
by the GTiff driver are available
(e.g., set_config_option("COMPRESS_OVERVIEW", "LZW")
).
Since GDAL 3.6, COMPRESS_OVERVIEW
is honoured when creating internal
overviews of GTiff files. The GDAL documentation for gdaladdo
command-line utility
describes additional configuration for overview building.
See also set_config_option()
. No return value, called for side effects.
$getDataTypeName(band)
Returns the name of the pixel data type for band
. The possible data
types are:
Unknown | Unknown or unspecified type |
Byte | 8-bit unsigned integer |
Int8 | 8-bit signed integer (GDAL >= 3.7) |
UInt16 | 16-bit unsigned integer |
Int16 | 16-bit signed integer |
UInt32 | 32-bit unsigned integer |
Int32 | 32-bit signed integer |
UInt64 | 64-bit unsigned integer (GDAL >= 3.5) |
Int64 | 64-bit signed integer (GDAL >= 3.5) |
Float32 | 32-bit floating point |
Float64 | 64-bit floating point |
CInt16 | Complex Int16 |
CInt32 | Complex Int32 |
CFloat32 | Complex Float32 |
CFloat64 | Complex Float64 |
Some raster formats including GeoTIFF ("GTiff") and Erdas Imagine .img
("HFA") support sub-byte data types. Rasters can be created with these
data types by specifying the "NBITS=n" creation option where n=1...7 for
GTiff or n=1/2/4 for HFA. In these cases, $getDataTypeName()
reports
the apparent type "Byte"
. GTiff also supports n=9...15 (UInt16 type) and
n=17...31 (UInt32 type), and n=16 is accepted for Float32 to generate
half-precision floating point values.
$getNoDataValue(band)
Returns the nodata value for band
if one exists.
This is generally a special value defined to mark pixels that are not
valid data. NA
is returned if a nodata value is not defined for
band
. Not all raster formats support a designated nodata value.
$setNoDataValue(band, nodata_value)
Sets the nodata value for band
.
nodata_value
is a numeric value to be defined as the nodata marker.
Depending on the format, changing the nodata value may or may not have an
effect on the pixel values of a raster that has just been created (often
not). It is thus advised to call $fillRaster()
explicitly if the
intent is to initialize the raster to the nodata value. In any case,
changing an existing nodata value, when one already exists on an initialized
dataset, has no effect on the pixels whose values matched the previous
nodata value.
Returns logical TRUE
on success or FALSE
if the nodata value
could not be set.
$deleteNoDataValue(band)
Removes the nodata value for band
.
This affects only the definition of the nodata value for raster formats
that support one (does not modify pixel values). No return value.
An error is raised if the nodata value cannot be removed.
$getMaskFlags(band)
Returns the status flags of the mask band associated with band
.
Masks are represented as Byte bands with a value of zero indicating nodata
and non-zero values indicating valid data. Normally the value 255
will be
used for valid data pixels. GDAL supports external (.msk) mask bands, and
normal Byte alpha (transparency) band as mask (any value other than 0
to
be treated as valid data). But masks may not be regular raster bands on the
datasource, such as an implied mask from a band nodata value or the
ALL_VALID
mask. See RFC 15: Band Masks for more details
(https://gdal.org/development/rfc/rfc15_nodatabitmask.html.
Returns a named list of GDAL mask flags and their logical values, with the following definitions:
ALL_VALID
: There are no invalid pixels, all mask values will be 255
.
When used this will normally be the only flag set.
PER_DATASET
: The mask band is shared between all bands on the dataset.
ALPHA
: The mask band is actually an alpha band and may have values other
than 0
and 255
.
NODATA
: Indicates the mask is actually being generated from nodata
values (mutually exclusive of ALPHA
).
$getMaskBand(band)
Returns the mask filename and band number associated with band
.
The return value is a named list with two elements. The MaskFile
element
gives the filename where the mask band is located, e.g., a file with the
same name as the main dataset but suffixed with .msk in the case of a GDAL
external mask file. MaskFile
will be an empty string for the derived
ALL_VALID
and NODATA
masks, which internally are freestanding bands not
considered to be a part of a dataset. The MaskBand
element gives the band
number for a mask that is a regular alpha band in the main dataset or
external mask file. BandNumber
will be 0
for the ALL_VALID
and NODATA
masks.
$getUnitType(band)
Returns the name of the unit type of the pixel values for band
(e.g., "m" or "ft").
An empty string (""
) is returned if no units are available.
$setUnitType(band, unit_type)
Sets the name of the unit type of the pixel values for band
.
unit_type
should be one of empty string ""
(the default indicating it is
unknown), "m" indicating meters, or "ft" indicating feet, though other
nonstandard values are allowed.
Returns logical TRUE
on success or FALSE
if the unit type
could not be set.
$getScale(band)
Returns the pixel value scale (units value = (raw value * scale) + offset)
for band
.
This value (in combination with the $getOffset()
value) can be used to
transform raw pixel values into the units returned by $getUnitType()
.
Returns NA
if a scale value is not defined for this band
.
$setScale(band, scale)
Sets the pixel value scale (units value = (raw value * scale) + offset)
for band
. Many raster formats do not implement this method.
Returns logical TRUE
on success or FALSE
if the scale could
not be set.
$getOffset(band)
Returns the pixel value offset (units value = (raw value * scale) + offset)
for band
.
This value (in combination with the $getScale()
value) can be used to
transform raw pixel values into the units returned by $getUnitType()
.
Returns NA
if an offset value is not defined for this band
.
$setOffset(band, offset)
Sets the pixel value offset (units value = (raw value * scale) + offset)
for band
. Many raster formats do not implement this method.
Returns logical TRUE
on success or FALSE
if the offset could
not be set.
$getRasterColorInterp(band)
Returns a string describing the color interpretation for band
.
The color interpretation values and their meanings are:
Undefined | Undefined |
Gray | Grayscale |
Palette | Paletted (see associated color table) |
Red | Red band of RGBA image |
Green | Green band of RGBA image |
Blue | Blue band of RGBA image |
Alpha | Alpha (0=transparent, 255=opaque) |
Hue | Hue band of HLS image |
Saturation | Saturation band of HLS image |
Lightness | Lightness band of HLS image |
Cyan | Cyan band of CMYK image |
Magenta | Magenta band of CMYK image |
Yellow | Yellow band of CMYK image |
Black | Black band of CMYK image |
YCbCr_Y | Y Luminance |
YCbCr_Cb | Cb Chroma |
YCbCr_Cr | Cr Chroma |
$setRasterColorInterp(band, col_interp)
Sets the color interpretation for band
. See above for the list of
valid values for col_interp
(passed as a string).
$getMinMax(band, approx_ok)
Returns a numeric vector of length two containing the min/max values for
band
. If approx_ok
is TRUE
and the raster format knows these
values intrinsically then those values will be returned. If that doesn't
work, a subsample of blocks will be read to get an approximate min/max. If
the band has a nodata value it will be excluded from the minimum and
maximum. If approx_ok
is FALSE
, then all pixels will be read and
used to compute an exact range.
$getStatistics(band, approx_ok, force)
Returns a numeric vector of length four containing the minimum, maximum,
mean and standard deviation of pixel values in band
(excluding
nodata pixels). Some raster formats will cache statistics allowing fast
retrieval after the first request.
approx_ok
:
TRUE
: Approximate statistics are sufficient, in which case overviews
or a subset of raster tiles may be used in computing the statistics.
FALSE
: All pixels will be read and used to compute statistics (if
computation is forced).
force
:
TRUE
: The raster will be scanned to compute statistics. Once computed,
statistics will generally be “set” back on the raster band if the format
supports caching statistics.
(Note: ComputeStatistics()
in the GDAL API is called automatically here.
This is a change in the behavior of GetStatistics()
in the API, to a
definitive force
.)
FALSE
: Results will only be returned if it can be done quickly (i.e.,
without scanning the raster, typically by using pre-existing
STATISTICS_xxx metadata items). NA
s will be returned if statistics
cannot be obtained quickly.
$clearStatistics()
Clear statistics. Only implemented for now in PAM supported datasets
(Persistable Auxiliary Metadata via .aux.xml file). GDAL >= 3.2.
$getHistogram(band, min, max, num_buckets, incl_out_of_range, approx_ok)
Computes raster histogram for band
. min
is the lower bound of
the histogram. max
is the upper bound of the histogram.
num_buckets
is the number of buckets to use (bucket size is
(max - min) / num_buckets
).
incl_out_of_range
is a logical scalar: if TRUE
values below the
histogram range will be mapped into the first bucket and values above will
be mapped into the last bucket, if FALSE
out of range values are discarded.
approx_ok
is a logical scalar: TRUE
if an approximate histogram is
OK (generally faster), or FALSE
for an exactly computed histogram.
Returns the histogram as a numeric vector of length num_buckets
.
$getDefaultHistogram(band, force)
Returns a default raster histogram for band
. In the GDAL API, this
method is overridden by derived classes (such as GDALPamRasterBand,
VRTDataset, HFADataset...) that may be able to fetch efficiently an already
stored histogram. force
is a logical scalar: TRUE
to force the
computation of a default histogram; or if FALSE
and no default histogram
is available, a warning is emitted and the returned list has a 0-length
histogram vector.
Returns a list of length four containing named elements $min
(lower
bound), $max
(upper bound), $num_buckets
(number of buckets), and
$histogram
(a numeric vector of length num_buckets
).
$getMetadata(band, domain)
Returns a character vector of all metadata NAME=VALUE
pairs that exist in
the specified domain
, or empty string (""
) if there are no
metadata items in domain
(metadata in the context of the GDAL
Raster Data Model: https://gdal.org/user/raster_data_model.html).
Set band = 0
to retrieve dataset-level metadata, or to an integer
band number to retrieve band-level metadata.
Set domain = ""
(empty string) to retrieve metadata in the
default domain.
$setMetadata(band, metadata, domain)
Sets metadata in the specified domain
. The metadata
argument
is given as a character vector of NAME=VALUE
pairs.
Pass band = 0
to set dataset-level metadata, or pass an integer
band number to set band-level metadata.
Use domain = ""
(empty string) to set an item in the default domain.
Returns logical TRUE
on success or FALSE
if metadata could
not be set.
$getMetadataItem(band, mdi_name, domain)
Returns the value of a specific metadata item named mdi_name
in the
specified domain
, or empty string (""
) if no matching item
is found.
Set band = 0
to retrieve dataset-level metadata, or to an integer
band number to retrieve band-level metadata.
Set domain = ""
(empty string) to retrieve an item in the
default domain.
$setMetadataItem(band, mdi_name, mdi_value, domain)
Sets the value (mdi_value
) of a specific metadata item named
mdi_name
in the specified domain
.
Pass band = 0
to set dataset-level metadata, or pass an integer
band number to set band-level metadata.
Use domain = ""
(empty string) to set an item in the default domain.
Returns logical TRUE
on success or FALSE
if metadata could
not be set.
$getMetadataDomainList(band)
Returns a character vector of metadata domains or empty string (""
).
Set band = 0
to retrieve dataset-level domains, or to an integer
band number to retrieve band-level domains.
$read(band, xoff, yoff, xsize, ysize, out_xsize, out_ysize)
Reads a region of raster data from band
. The method takes care of
pixel decimation / replication if the output size
(out_xsize * out_ysize
) is different than the size of the region
being accessed (xsize * ysize
).
xoff
is the pixel (column) offset to the top left corner of the
region of the band to be accessed (zero to start from the left side).
yoff
is the line (row) offset to the top left corner of the region of
the band to be accessed (zero to start from the top).
Note that raster row/column offsets use 0-based indexing.
xsize
is the width in pixels of the region to be accessed.
ysize
is the height in pixels of the region to be accessed.
out_xsize
is the width of the output array into which the desired
region will be read (typically the same value as xsize).
out_ysize
is the height of the output array into which the desired
region will be read (typically the same value as ysize).
Returns a numeric or complex vector containing the values that were read.
It is organized in left to right, top to bottom pixel order.
NA
will be returned in place of the nodata value if the
raster dataset has a nodata value defined for this band.
Data are read as R integer type when possible for the raster data type
(Byte
, Int8
, Int16
, UInt16
, Int32
), otherwise as type double
(UInt32
, Float32
, Float64
).
No rescaling of the data is performed (see $getScale()
and
$getOffset()
above).
An error is raised if the read operation fails. See also the setting
$readByteAsRaw
above.
$write(band, xoff, yoff, xsize, ysize, rasterData)
Writes a region of raster data to band
.
xoff
is the pixel (column) offset to the top left corner of the
region of the band to be accessed (zero to start from the left side).
yoff
is the line (row) offset to the top left corner of the region of
the band to be accessed (zero to start from the top).
Note that raster row/column offsets use 0-based indexing.
xsize
is the width in pixels of the region to write.
ysize
is the height in pixels of the region to write.
rasterData
is a numeric or complex vector containing values to write.
It is organized in left to right, top to bottom pixel order. NA
in
rasterData
should be replaced with a suitable nodata value prior to
writing (see $getNoDataValue()
and $setNoDataValue()
above).
An error is raised if the operation fails (no return value).
$fillRaster(band, value, ivalue)
Fills band
with a constant value. GDAL makes no guarantees about what
values the pixels in newly created files are set to, so this method can be
used to clear a band to a specified "default" value. The fill value
is
passed as numeric
, but this will be converted to the underlying raster
data type before writing to the file. The ivalue
argument allows setting
the imaginary component of a complex value. Note that ivalue
is a required
argument but can be set to 0
for real data types. No return value. An
error is raised if the operation fails.
$getColorTable(band)
Returns the color table associated with band
, or NULL
if
there is no associated color table. The color table is returned as an
integer matrix with five columns. To associate a color with a raster pixel,
the pixel value is used as a subscript into the color table. This means that
the colors are always applied starting at zero and ascending
(see GDAL
Color Table).
Column 1 contains the pixel values. Interpretation of columns 2:5 depends
on the value of $getPaletteInterp()
(see below). For "RGB", columns 2:5
contain red, green, blue, alpha as 0-255 integer values.
$getPaletteInterp(band)
If band
has an associated color table, this method returns a
character string with the palette interpretation for columns 2:5 of the
table. An empty string (""
) is returned if band
does not have
an associated color table. The palette interpretation values and their
meanings are:
Gray: column 2 contains grayscale values (columns 3:5 unused)
RGB: columns 2:5 contain red, green, blue, alpha
CMYK: columns 2:5 contain cyan, magenta, yellow, black
HLS: columns 2:4 contain hue, lightness, saturation (column 5 unused)
$setColorTable(band, col_tbl, palette_interp)
Sets the raster color table for band
(see GDAL
Color Table).
col_tbl
is an integer matrix or data frame with either four or five
columns (see $getColorTable()
above). Column 1 contains the pixel
values. Valid values are integers 0 and larger (note that GTiff format
supports color tables only for Byte and UInt16 bands). Negative values
will be skipped with a warning emitted. Interpretation of columns 2:5
depends on the value of $getPaletteInterp()
(see above). For RGB,
columns 2:4 contain red, green, blue as 0-255 integer values, and an
optional column 5 contains alpha transparency values (defaults to 255
opaque).
palette_interp
is a string, one of Gray
, RGB
, CMYK
or HLS
(see $getPaletteInterp()
above).
Returns logical TRUE
on success or FALSE
if the color table
could not be set.
$clearColorTable(band)
Clears the raster color table for band
.
Returns logical TRUE
on success or FALSE
if the color table
could not be cleared, e.g., if this action is not supported by the driver.
$getDefaultRAT(band)
Returns the Raster Attribute Table for band
as a data frame,
or NULL
if there is no associated Raster Attribute Table. See the
stand-alone function buildRAT()
for details of the Raster Attribute Table
format.
$setDefaultRAT(band, df)
Sets a default Raster Attribute Table for band
from data frame df
.
The input data frame will be checked for attribute "GDALRATTableType"
which can have values of "thematic"
or "athematic"
(for continuous data).
Columns of the data frame will be checked for attribute "GFU"
(for "GDAL
field usage"). If the "GFU"
attribute is missing, a value of "Generic"
will be used (corresponding to GFU_Generic
in the GDAL API, for general
purpose field). Columns with other, specific field usage values should
generally be present in df
, such as fields containing the set of unique
(discrete) pixel values (GFU "MinMax"
), pixel counts (GFU "PixelCount"
),
class names (GFU "Name"
), color values (GFUs "Red"
, "Green"
, "Blue"
),
etc. The data frame will also be checked for attributes "Row0Min"
and
"BinSize"
which can have numeric values that describe linear binning.
See the stand-alone function buildRAT()
for details of the GDAL Raster
Attribute Table format and its representation as data frame.
$flushCache()
Flush all write cached data to disk. Any raster data written via GDAL calls,
but buffered internally will be written to disk. Using this method does not
preclude calling $close()
to properly close the dataset and ensure that
important data not addressed by $flushCache()
is written in the file
(see also $open()
above). No return value, called for side effect.
$getChecksum(band, xoff, yoff, xsize, ysize)
Returns a 16-bit integer (0-65535) checksum from a region of raster data
on band
.
Floating point data are converted to 32-bit integer so decimal portions of
such raster data will not affect the checksum. Real and imaginary
components of complex bands influence the result.
xoff
is the pixel (column) offset of the window to read.
yoff
is the line (row) offset of the window to read.
Raster row/column offsets use 0-based indexing.
xsize
is the width in pixels of the window to read.
ysize
is the height in pixels of the window to read.
$close()
Closes the GDAL dataset (no return value, called for side effects).
Calling $close()
results in proper cleanup, and flushing of any
pending writes. Forgetting to close a dataset opened in update mode on some
formats such as GTiff could result in being unable to open it afterwards.
The GDALRaster
object is still available after calling $close()
.
The dataset can be re-opened on the existing filename
with
$open(read_only=TRUE)
or $open(read_only=FALSE)
.
If a dataset object is opened with update access (read_only = FALSE
), it
is not recommended to open a new dataset on the same underlying filename
.
Datasets are opened in shared mode by default. This allows the sharing of
GDALDataset
handles for a dataset with other callers that open shared on
the same filename
, if the dataset is opened from the same thread.
Functions in gdalraster
that do processing will open input datasets in
shared mode. This provides potential efficiency for cases when an object of
class GDALRaster
is already open in read-only mode on the same filename
(avoids overhead associated with initial dataset opening by using the
existing handle, and potentially makes use of existing data in the GDAL
block cache). Opening in shared mode can be disabled by specifying the
optional shared
parameter in the class constructor.
The $read()
method will perform automatic resampling if the
specified output size (out_xsize * out_ysize
) is different than
the size of the region being read (xsize * ysize
). In that case, the
GDAL_RASTERIO_RESAMPLING
configuration option could also be set to
override the default resampling to one of BILINEAR
, CUBIC
,
CUBICSPLINE
, LANCZOS
, AVERAGE
or MODE
(see set_config_option()
).
Package overview in help("gdalraster-package")
vignette("raster-api-tutorial")
read_ds()
is a convenience wrapper for GDALRaster$read()
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ## print information about the dataset to the console ds$info() ## retrieve the raster format name ds$getDriverShortName() ds$getDriverLongName() ## retrieve a list of files composing the dataset ds$getFileList() ## retrieve dataset parameters ds$getRasterXSize() ds$getRasterYSize() ds$getGeoTransform() ds$getProjection() ds$getRasterCount() ds$bbox() ds$res() ds$dim() ## retrieve some band-level parameters ds$getDescription(band = 1) ds$getBlockSize(band = 1) ds$getOverviewCount(band = 1) ds$getDataTypeName(band = 1) # LCP format does not support an intrinsic nodata value so this returns NA: ds$getNoDataValue(band = 1) ## LCP driver reports several dataset- and band-level metadata ## see the format description at https://gdal.org/drivers/raster/lcp.html ## set band = 0 to retrieve dataset-level metadata ## set domain = "" (empty string) for the default metadata domain ds$getMetadata(band = 0, domain = "") ## retrieve metadata for a band as a vector of name=value pairs ds$getMetadata(band = 4, domain = "") ## retrieve the value of a specific metadata item ds$getMetadataItem(band = 2, mdi_name = "SLOPE_UNIT_NAME", domain = "") ## read one row of pixel values from band 1 (elevation) ## raster row/column index are 0-based ## the upper left corner is the origin ## read the tenth row: ncols <- ds$getRasterXSize() rowdata <- ds$read(band = 1, xoff = 0, yoff = 9, xsize = ncols, ysize = 1, out_xsize = ncols, out_ysize = 1) head(rowdata) ds$close() ## create a new raster using lcp_file as a template new_file <- file.path(tempdir(), "storml_newdata.tif") rasterFromRaster(srcfile = lcp_file, dstfile = new_file, nbands = 1, dtName = "Byte", init = -9999) ds_new <- new(GDALRaster, new_file, read_only = FALSE) ## write random values to all pixels set.seed(42) ncols <- ds_new$getRasterXSize() nrows <- ds_new$getRasterYSize() for (row in 0:(nrows - 1)) { rowdata <- round(runif(ncols, 0, 100)) ds_new$write(band = 1, xoff = 0, yoff = row, xsize = ncols, ysize = 1, rowdata) } ## re-open in read-only mode when done writing ## this will ensure flushing of any pending writes (implicit $close) ds_new$open(read_only = TRUE) ## getStatistics returns min, max, mean, sd, and sets stats in the metadata ds_new$getStatistics(band = 1, approx_ok = FALSE, force = TRUE) ds_new$getMetadataItem(band = 1, "STATISTICS_MEAN", "") ## close the dataset for proper cleanup ds_new$close() ## using a GDAL Virtual File System handler '/vsicurl/' ## see: https://gdal.org/user/virtual_file_systems.html url <- "/vsicurl/https://raw.githubusercontent.com/" url <- paste0(url, "usdaforestservice/gdalraster/main/sample-data/") url <- paste0(url, "lf_elev_220_mt_hood_utm.tif") set_config_option("GDAL_HTTP_CONNECTTIMEOUT", "20") set_config_option("GDAL_HTTP_TIMEOUT", "20") if (http_enabled() && vsi_stat(url)) { ds <- new(GDALRaster, url) plot_raster(ds, legend = TRUE, main = "Mount Hood elevation (m)") ds$close() } set_config_option("GDAL_HTTP_CONNECTTIMEOUT", "") set_config_option("GDAL_HTTP_TIMEOUT", "")
lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) ## print information about the dataset to the console ds$info() ## retrieve the raster format name ds$getDriverShortName() ds$getDriverLongName() ## retrieve a list of files composing the dataset ds$getFileList() ## retrieve dataset parameters ds$getRasterXSize() ds$getRasterYSize() ds$getGeoTransform() ds$getProjection() ds$getRasterCount() ds$bbox() ds$res() ds$dim() ## retrieve some band-level parameters ds$getDescription(band = 1) ds$getBlockSize(band = 1) ds$getOverviewCount(band = 1) ds$getDataTypeName(band = 1) # LCP format does not support an intrinsic nodata value so this returns NA: ds$getNoDataValue(band = 1) ## LCP driver reports several dataset- and band-level metadata ## see the format description at https://gdal.org/drivers/raster/lcp.html ## set band = 0 to retrieve dataset-level metadata ## set domain = "" (empty string) for the default metadata domain ds$getMetadata(band = 0, domain = "") ## retrieve metadata for a band as a vector of name=value pairs ds$getMetadata(band = 4, domain = "") ## retrieve the value of a specific metadata item ds$getMetadataItem(band = 2, mdi_name = "SLOPE_UNIT_NAME", domain = "") ## read one row of pixel values from band 1 (elevation) ## raster row/column index are 0-based ## the upper left corner is the origin ## read the tenth row: ncols <- ds$getRasterXSize() rowdata <- ds$read(band = 1, xoff = 0, yoff = 9, xsize = ncols, ysize = 1, out_xsize = ncols, out_ysize = 1) head(rowdata) ds$close() ## create a new raster using lcp_file as a template new_file <- file.path(tempdir(), "storml_newdata.tif") rasterFromRaster(srcfile = lcp_file, dstfile = new_file, nbands = 1, dtName = "Byte", init = -9999) ds_new <- new(GDALRaster, new_file, read_only = FALSE) ## write random values to all pixels set.seed(42) ncols <- ds_new$getRasterXSize() nrows <- ds_new$getRasterYSize() for (row in 0:(nrows - 1)) { rowdata <- round(runif(ncols, 0, 100)) ds_new$write(band = 1, xoff = 0, yoff = row, xsize = ncols, ysize = 1, rowdata) } ## re-open in read-only mode when done writing ## this will ensure flushing of any pending writes (implicit $close) ds_new$open(read_only = TRUE) ## getStatistics returns min, max, mean, sd, and sets stats in the metadata ds_new$getStatistics(band = 1, approx_ok = FALSE, force = TRUE) ds_new$getMetadataItem(band = 1, "STATISTICS_MEAN", "") ## close the dataset for proper cleanup ds_new$close() ## using a GDAL Virtual File System handler '/vsicurl/' ## see: https://gdal.org/user/virtual_file_systems.html url <- "/vsicurl/https://raw.githubusercontent.com/" url <- paste0(url, "usdaforestservice/gdalraster/main/sample-data/") url <- paste0(url, "lf_elev_220_mt_hood_utm.tif") set_config_option("GDAL_HTTP_CONNECTTIMEOUT", "20") set_config_option("GDAL_HTTP_TIMEOUT", "20") if (http_enabled() && vsi_stat(url)) { ds <- new(GDALRaster, url) plot_raster(ds, legend = TRUE, main = "Mount Hood elevation (m)") ds$close() } set_config_option("GDAL_HTTP_CONNECTTIMEOUT", "") set_config_option("GDAL_HTTP_TIMEOUT", "")
GDALVector
provides an interface for accessing a vector layer in a GDAL
dataset and calling methods on the underlying OGRLayer
object.
An object of class GDALVector
persists an open connection to the dataset,
and exposes methods for retrieving layer information, setting attribute and
spatial filters, and reading/writing feature data.
See https://gdal.org/api/index.html for details of the GDAL
Vector API.
Class GDALVector
is currently under development. An initial
implementation supporting read access was added in gdalraster 1.11.1.9100.
A working document with draft specifications is available at:
https://usdaforestservice.github.io/gdalraster/articles/gdalvector-draft.html
and discussion thread/status updates at:
https://github.com/USDAForestService/gdalraster/issues/241.
dsn |
Character string containing the data source name (DSN), usually a filename or database connection string. |
layer |
Character string containing the name of a layer within the data source. May also be given as an SQL SELECT statement to be executed against the data source, defining a layer as the result set. |
read_only |
Logical scalar. |
open_options |
Optional character vector of |
spatial_filter |
Optional character string containing a geometry in Well Known Text (WKT) format which represents a spatial filter. |
dialect |
Optional character string to control the statement dialect
when SQL is used to define the layer. By default, the OGR SQL engine will
be used, except for RDBMS drivers that will use their dedicated SQL engine,
unless |
An object of class GDALVector
which contains pointers to the
opened layer and the dataset that owns it, and methods that operate on
the layer as described in Details. GDALVector
is a C++ class exposed
directly to R (via RCPP_EXPOSED_CLASS
). Fields and methods of the class
are accessed using the $
operator. Note that all arguments to exposed
class methods are required (but do not have to be named). The read/write
fields are per-object settings which can be changed as needed during the
lifetime of the object.
## Constructors # for single-layer file formats such as shapefile lyr <- new(GDALVector, dsn) # specifying the layer name, or SQL statement defining the layer lyr <- new(GDALVector, dsn, layer) # for update access lyr <- new(GDALVector, dsn, layer, read_only = FALSE) # using dataset open options lyr <- new(GDALVector, dsn, layer, read_only, open_options) # setting a spatial filter and/or specifying the SQL dialect lyr <- new(GDALVector, dsn, layer, read_only, open_options, spatial_filter, dialect) ## Read/write fields (per-object settings) lyr$defaultGeomFldName lyr$promoteToMulti lyr$quiet lyr$returnGeomAs lyr$wkbByteOrder ## Methods lyr$open(read_only) lyr$isOpen() lyr$getDsn() lyr$getFileList() lyr$getDriverShortName() lyr$getDriverLongName() lyr$getName() lyr$getFieldNames() lyr$testCapability() lyr$getFIDColumn() lyr$getGeomType() lyr$getGeometryColumn() lyr$getSpatialRef() lyr$bbox() lyr$getLayerDefn() lyr$setAttributeFilter(query) lyr$getAttributeFilter() lyr$setIgnoredFields(fields) lyr$setSelectedFields(fields) lyr$setSpatialFilter(wkt) lyr$setSpatialFilterRect(bbox) lyr$getSpatialFilter() lyr$clearSpatialFilter() lyr$getFeatureCount() lyr$getNextFeature() lyr$setNextByIndex(i) lyr$getFeature(fid) lyr$resetReading() lyr$fetch(n) lyr$setFeature(feature) lyr$createFeature(feature) lyr$upsertFeature(feature) lyr$getLastWriteFID() lyr$deleteFeature(fid) lyr$syncToDisk() lyr$startTransaction(force) lyr$commitTransaction() lyr$rollbackTransaction() ds$getMetadata() ds$setMetadata(metadata) ds$getMetadataItem(mdi_name) lyr$close()
new(GDALVector, dsn)
The first layer by index is assumed if the layer
argument is omitted, so
this form of the constructor might be used for single-layer formats like
shapefile.
new(GDALVector, dsn, layer)
Constructor specifying the name of a layer to open. The layer
argument
may also be given as an SQL SELECT statement to define a layer as the result
set.
new(GDALVector, dsn, layer, read_only)
Constructor specifying read/write access (read_only = {TRUE|FALSE})
.
The layer
argument is required in this form of the constructor, but may be
given as empty string (""
), in which case the first layer by index will be
assumed.
new(GDALVector, dsn, layer, read_only, open_options)
Constructor specifying dataset open options as a character vector of
NAME=VALUE
pairs.
new(GDALVector, dsn, layer, read_only, open_options, spatial_filter, dialect))
Constructor to specify a spatial filter and/or SQL dialect. All arguments
are required in this form of the constructor, but open_options
may be
NULL
, and spatial_filter
or dialect
may be an empty string (""
).
$defaultGeomFldName
Character string specifying a name to use for returned columns when the
geometry column name in the source layer is empty, like with shapefiles etc.
Defaults to "geometry"
.
$promoteToMulti
A logical value specifying whether to automatically promote geometries from
Polygon to MultiPolygon, Point to MultiPoint, or LineString to
MultiLineString during read operations (i.e., with methods $getFeature()
,
$getNextFeature()
, $fetch()
). Defaults to FALSE
. Setting to TRUE
may
be useful when reading from layers such as shapefiles that mix, e.g.,
Polygons and MultiPolygons.
$quiet
A logical value, FALSE
by default. Set to TRUE
to suppress various
messages and warnings.
$returnGeomAs
Character string specifying the return format of feature geometries.
Must be one of WKB
(the default), WKB_ISO
, WKT
, WKT_ISO
, BBOX
, or
NONE
.
Using WKB
/WKT
exports as 99-402 extended dimension (Z) types for Point,
LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon and
GeometryCollection. For other geometry types, it is equivalent to using
WKB_ISO
/WKT_ISO
(see https://libgeos.org/specifications/wkb/).
Using BBOX
exports as a list of numeric vectors, each of length 4 with values
xmin, ymin, xmax, ymax
. If an empty geometry is encountered these values will
be NA_real_
in the corresponding location.
Using NONE
will result in no geometry value being present in the feature
returned.
$wkbByteOrder
Character string specifying the byte order for WKB geometries.
Must be either LSB
(Least Significant Byte first, the default) or
MSB
(Most Significant Byte first).
$open(read_only)
(Re-)opens the vector layer on the existing DSN. Use this method to
open a layer that has been closed using $close()
. May be used to
re-open a layer with a different read/write access (read_only
set to
TRUE
or FALSE
). The method will first close an open dataset, so it is
not required to call $close()
explicitly in this case.
No return value, called for side effects.
$isOpen()
Returns a logical
scalar indicating whether the vector dataset is open.
$getDsn()
Returns a character string containing the dsn
associated with this
GDALVector
object (dsn
originally used to open the layer).
$getFileList()
Returns a character vector of files believed to be part of the data source.
If it returns an empty string (""
) it means there is believed to be no
local file system files associated with the dataset (e.g., a virtual file
system). The returned filenames will normally be relative or absolute
paths depending on the path used to originally open the dataset.
$getDriverShortName()
Returns the short name of the vector format driver.
$getDriverLongName()
Returns the long name of the vector format driver.
$getName()
Returns the layer name.
$getFieldNames()
Returns a character vector of the layer's field names.
$testCapability()
Tests whether the layer supports named capabilities based on the current
read/write access. Returns a list of capabilities with values TRUE
or
FALSE
. The returned list contains the following named elements:
RandomRead
, SequentialWrite
, RandomWrite
, UpsertFeature
,
FastSpatialFilter
, FastFeatureCount
, FastGetExtent
,
FastSetNextByIndex
, CreateField
, CreateGeomField
, DeleteField
,
ReorderFields
, AlterFieldDefn
, AlterGeomFieldDefn
, DeleteFeature
,
StringsAsUTF8
, Transactions
, CurveGeometries
.
(See the GDAL documentation for
OGR_L_TestCapability()
.)
$getFIDColumn()
Returns the name of the underlying database column being used as the FID
column, or empty string (""
) if not supported.
$getGeomType()
Returns the well known name of the layer geometry type as character string.
For layers with multiple geometry fields, this method only returns the
geometry type of the first geometry column. For other columns, use
$getLayerDefn()
. For layers without any geometry field, this method
returns "NONE"
.
$getGeometryColumn()
Returns he name of the underlying database column being used as the geometry
column, or an empty string (""
) if not supported.
For layers with multiple geometry fields, this method only returns the
name of the first geometry column. For other columns, use $getLayerDefn()
.
$getSpatialRef()
Returns a WKT string containing the spatial reference system for this layer,
or empty string (""
) if no spatial reference exists.
$bbox()
Returns a numeric vector of length four containing the bounding box
for this layer (xmin, ymin, xmax, ymax). Note that bForce = true
is set in
the underlying API call to OGR_L_GetExtent()
, so the entire layer may be
scanned to compute a minimum bounding rectangle (see FastGetExtent
in the
list returned by $testCapability()
). Depending on the format driver, a
spatial filter may or may not be taken into account, so it is safer to call
$bbox()
without setting a spatial filter.
$getLayerDefn()
Returns a list containing the OGR feature class definition for this layer
(a.k.a. layer definition). The list contains zero or more attribute field
definitions, along with one or more geometry field definitions.
See ogr_define for details of the field and feature class definitions.
$setAttributeFilter(query)
Sets an attribute query string to be used when fetching features via the
$getNextFeature()
or $fetch()
methods.
Only features for which query
evaluates as true will be returned.
The query string should be in the format of an SQL WHERE clause, described
in the "WHERE"
section of the OGR SQL dialect documentation (e.g.,
"population > 1000000 and population < 5000000"
, where population
is an
attribute in the layer).
In some cases (RDBMS backed drivers, SQLite, GeoPackage) the native
capabilities of the database may be used to to interpret the WHERE clause,
in which case the capabilities will be broader than those of OGR SQL.
Note that installing a query string will generally result in resetting the
current reading position (as with $resetReading()
described below).
The query
parameter may be set to empty string (""
) to clear the current
attribute filter.
$getAttributeFilter()
Returns the attribute query string currently in use, or empty string (""
)
if an attribute filter is not set.
$setIgnoredFields(fields)
Set which fields can be omitted when retrieving features from the layer.
The fields
argument is a character vector of field names. Passing an
empty string (""
) for fields
will reset to no ignored fields.
If the format driver supports this functionality (testable using
$testCapability()$IgnoreFields
), it will not fetch the specified fields
in subsequent calls to $getFeature()
/ $getNextFeature()
/ $fetch()
,
and thus save some processing time and/or bandwidth. Besides field names of
the layer, the following special fields can be passed: "OGR_GEOMETRY"
to
ignore geometry and "OGR_STYLE"
to ignore layer style. By default, no
fields are ignored. Note that fields that are used in an attribute filter
should generally not be set as ignored fields, as most drivers (such as
those relying on the OGR SQL engine) will be unable to correctly evaluate
the attribute filter. No return value, called for side effects.
$setSelectedFields(fields)
Set which fields will be included when retrieving features from the layer.
The fields
argument is a character vector of field names. Passing an
empty string (""
) for fields
will reset to no ignored fields.
See the $setIgnoredFields()
method above for more information. The data
source must provide IgnoreFields capability in order to set selected
fields. Note that geometry fields, if desired, must be specified when setting
selected fields, either by including named geometry field(s) or the special
field "OGR_GEOMETRY"
in the fields
argument.
No return value, called for side effects.
$setSpatialFilter(wkt)
Sets a new spatial filter from a geometry in WKT format. This method sets
the geometry to be used as a spatial filter when fetching features via the
$getNextFeature()
or $fetch()
methods. Only features that geometrically
intersect the filter geometry will be returned. Currently this test may be
inaccurately implemented (depending on the vector format driver), but it is
guaranteed that all features whose envelope overlaps the envelope of the
spatial filter will be returned. This can result in more shapes being
returned that should strictly be the case.
wkt
is a character string containing a WKT geometry in the same coordinate
system as the layer. An empty string (""
) may be passed indicating that
the current spatial filter should be cleared, but no new one instituted.
$setSpatialFilterRect(bbox)
Sets a new rectangular spatial filter. This method sets a rectangle to be
used as a spatial filter when fetching features via the $getNextFeature()
or $fetch()
methods. Only features that geometrically intersect the given
rectangle will be returned.
bbox
is a numeric vector of length four containing xmin, ymin, xmax, ymax
in the same coordinate system as the layer as a whole (as returned by
$getSpatialRef()
).
$getSpatialFilter()
Returns the current spatial filter geometry as a WKT string, or empty string
(""
) if a spatial filter is not set.
$clearSpatialFilter()
Clears a spatial filter that was set with $setSpatialFilterRect()
.
No return value, called for that side effect.
$getFeatureCount()
Returns the number of features in the layer. For dynamic databases the count
may not be exact. This method forces a count in the underlying API call
(i.e., bForce = TRUE
in the call to OGR_L_GetFeatureCount()
). Note that
some vector drivers will actually scan the entire layer once to count
features. The FastFeatureCount
element in the list returned by
the $testCapability()
method can be checked if this might be a concern.
The number of features returned takes into account the spatial and/or
attribute filters. Some driver implementations of this method may alter the
read cursor of the layer.
$getNextFeature()
Fetch the next available feature from this layer. Only features matching the
current spatial and/or attribute filter (if defined) will be returned.
This method implements sequential access to the features of a layer.
The $resetReading()
method can be used to start at the beginning again.
Returns a list with the unique feature identifier (FID), the attribute and
geometry field names, and their values. The returned list carries the
OGRFeature
class attribute with S3 methods for for print()
and plot()
.
NULL
is returned if no more features are available.
$setNextByIndex(i)
Moves the read cursor to the i
th feature in the current result set
(with 0-based indexing).
This method allows positioning of a layer such that a call to
$getNextFeature()
or fetch()
will read the requested feature(s), where
i
is an absolute index into the current result set. So, setting i = 3
would mean the next feature read with $getNextFeature()
would have been
the 4th feature read if sequential reading took place from the beginning of
the layer, including accounting for spatial and attribute filters.
This method is not implemented efficiently by all vector format drivers. The
default implementation simply resets reading to the beginning and then calls
GetNextFeature()
i
times.
To determine if fast seeking is available on the current layer, check
the FastSetNextByIndex
element in the list returned by the
$testCapability()
method. No return value, called for side effect.
$getFeature(fid)
Returns a feature by its identifier. The value of fid
must be a numeric
scalar, optionally carrying the bit64::integer64
class attribute.
Success or failure of this operation is unaffected by any spatial or
attribute filters that may be in effect.
The RandomRead
element in the list returned by $testCapability()
can
be checked to establish if this layer supports efficient random access
reading; however, the call should always work if the feature exists since a
fallback implementation just scans all the features in the layer looking for
the desired feature. Returns a list with the unique feature identifier (FID),
the attribute and geometry field names, and their values, or NULL
on
failure. Note that sequential reads (with $getNextFeature()
) are generally
considered interrupted by a call to $getFeature()
.
$resetReading()
Reset feature reading to start on the first feature. No return value, called
for that side effect.
$fetch(n)
Fetches the next n
features from the layer and returns them as a data
frame. This allows retrieving the entire set of features, one page of
features at a time, or the remaining features (from the current cursor
position). Returns a data frame with as many rows as features were fetched,
and as many columns as attribute plus geometry fields in the result set,
even if the result is a single value or has one or zero rows.
The returned data frame carries the OGRFeatureSet
class attribute with S3
methods for for print()
and plot()
.
This method is an analog of
DBI::dbFetch()
.
The n
argument is the maximum number of features to retrieve per fetch
given as integer
or numeric
but assumed to be a whole number (will
be truncated). Use n = -1
or n = Inf
to retrieve all pending features
(resets reading to the first feature).
Otherwise, $fetch()
can be called multiple times to perform forward paging
from the current cursor position. Passing n = NA
is also supported and
returns the remaining features.
Fetching zero features is possible to retrieve the structure of the feature
set as a data frame (columns fully typed).
OGR field types are returned as the following R types (NA
for OGR NULL
values):
OFTInteger
: integer
OFTInteger
subtype OFSTBoolean
: logical
OFTIntegerList
: vector of integer
(list column)
OFTInteger64
: numeric
carrying "integer64"
class attribute {bit64}
OFTInteger64
subtype OFSTBoolean
: logical
OFTInteger64List
: vector of bit64::integer64
(list column)
OFTReal
: numeric
OFTRealList
: vector of numeric
(list column)
OFTString
: character
string
OFTStringList
: vector of character
strings (list column)
OFTDate
: class "Date"
(numeric
)
OFTDateTime
: class "POSIXct"
(numeric
, millisecond accuracy)
OFTTime
: character
string ("HH:MM:SS"
)
OFTBinary
: raw
vector (list column, NULL
entries for OGR NULL values)
Geometries are not returned if the field returnGeomAs
is set to NONE
.
Omitting the geometries may be beneficial for performance and memory usage
when access only to feature attributes is needed. Geometries are returned
as raw
vectors in a data frame list column when returnGeomAs
is set to
WKB
(the default) or WKB_ISO
, or as character
strings when
returnGeomAs
is set to one of WKT
or WKT_ISO
.
Note that $getFeatureCount()
is called internally when fetching the full
feature set or all remaining features (but not for a page of features).
$setFeature(feature)
Rewrites/replaces an existing feature. This method writes a feature based on
the feature id within the input feature. The feature
argument is a named
list of fields and their values, and must include a $FID
element
referencing the existing feature to rewrite. The RandomWrite
element in
the list returned by $testCapability()
can be checked to establish if this
layer supports random access writing via $setFeature()
.
The way omitted fields in the passed feature
are processed is driver
dependent:
SQL-based drivers which implement set feature through SQL UPDATE will skip unset fields, and thus the content of the existing feature will be preserved.
The shapefile driver will write a NULL value in the DBF file.
The GeoJSON driver will take into account unset fields to remove the corresponding JSON member.
Returns logical TRUE
upon successful completion, or FALSE
if setting the
feature did not succeed. The FID of the last feature written to the layer
may be obtained with the method $getLastWriteFID()
(see below). To set a
feature, but create it if it doesn't exist see the $upsertFeature()
method.
$createFeature(feature)
Creates and writes a new feature within the layer. The feature
argument is
a named list of fields and their values.
The passed feature is written to the layer as a new feature, rather than
overwriting an existing one. If the feature has a $FID
element other than
NA
, then the vector format driver may use that as the feature id of the
new feature, but not necessarily. The FID of the last feature written
to the layer may be obtained with the method $getLastWriteFID()
(see
below).
Returns logical TRUE
upon successful completion, or FALSE
if creating
the feature did not succeed. To create a feature, but set it if it already
exists see the $upsertFeature()
method.
$upsertFeature(feature)
Rewrites/replaces an existing feature or creates a new feature within the
layer. This method will write a feature to the layer, based on the feature
id within the input feature. The feature
argument is a named list of
fields and their values, potentially including a $FID
element referencing
an existing feature to rewrite. If the feature id doesn't exist a new
feature will be written. Otherwise, the existing feature will be rewritten.
The UpsertFeature
element in the list returned by $testCapability()
can
be checked to determine if this layer supports upsert writing. See
$setFeature()
above for a description of how omitted fields in the passed
feature
are processed.
Returns logical TRUE
upon successful completion, or FALSE
if upserting
the feature did not succeed. Requires GDAL >= 3.6.
$getLastWriteFID()
Returns the FID of the last feature written (either newly created or updated
existing). NULL
is returned if no features have been written in the layer.
Note that OGRNullFID (-1
) may be returned after writing a feature in some
formats. This is the case if a FID has not been assigned yet, and generally
does not indicate an error (e.g., formats that do not store a persistent FID
and assign FIDs upon a sequential read operation). The returned FID is a
numeric scalar carrying the bit64::integer64
class attribute.
$deleteFeature(fid)
Deletes a feature from the layer. The feature with the indicated feature ID
is deleted from the layer if supported by the format driver. The value of
fid
must be a numeric scalar, optionally carrying the bit64::integer64
class attribute (should be a whole number, will be truncated).
The DeleteFeature
element in the list returned by $testCapability()
can
be checked to establish if this layer has delete feature capability. Returns
logical TRUE
if the operation succeeds, or FALSE
on failure.
$syncToDisk()
Flushes pending changes to disk. This call is intended to force the layer to
flush any pending writes to disk, and leave the disk file in a consistent
state. It would not normally have any effect on read-only datasources. Some
formats do not implement this method, and will still return no error. An
error is only returned if an error occurs while attempting to flush to disk.
In any event, you should always close any opened datasource with $close()
which will ensure all data is correctly flushed. Returns logical TRUE
if
no error occurs (even if nothing is done) or FALSE
on error.
$startTransaction(force)
Creates a transaction if supported by the vector data source. The force
argument is a logical value. If force = FALSE
, only "efficient"
transactions will be attempted. Some drivers may offer an emulation of
transactions, but sometimes with significant overhead, in which case the
user must explicitly allow for such an emulation by setting force =TRUE
.
The function ogr_ds_test_cap()
can be used to determine whether a vector
data source supports efficient or emulated transactions.
All changes done after the start of the transaction are definitely applied
in the data source if $commitTransaction()
is called. They can be canceled
by calling rollbackTransaction()
instead.
Nested transactions are not supported. Transactions are implemented at the
dataset level, so multiple GDALVector
objects using the same data source
should not have transactions active at the same time.
In case $startTransaction()
fails, neither $commitTransaction()
nor
$rollbackTransaction()
should be called.
If an error occurs after a successful $startTransaction()
, the whole
transaction may or may not be implicitly canceled, depending on the format
driver (e.g., the PostGIS driver will cancel it, SQLite/GPKG will not). In
any case, in the event of an error, an explicit call to
rollbackTransaction()
should be done to keep things balanced.
Returns logical TRUE
if the transaction is created, or FALSE
on failure.
$commitTransaction()
Commits a transaction if supported by the vector data source.
Returns a logical value, TRUE
if the transaction is successfully committed.
Returns FALSE
if no transaction is active, or the rollback fails, or if the
data source does not support transactions.
Depending on the format driver, this may or may not abort layer sequential
reading that may be active.
$rollbackTransaction()
Rolls back a data source to its state before the start of the current
transaction, if transactions are supported by the data source.
Returns a logical value, TRUE
if the transaction is successfully rolled
back. Returns FALSE
if no transaction is active, or the rollback fails,
or if the data source does not support transactions.
$getMetadata()
Returns a character vector of all metadata NAME=VALUE
pairs for the
layer or empty string (""
) if there are no metadata items.
$setMetadata(metadata)
Sets metadata on the layer if the format supports it. The metadata
argument is given as a character vector of NAME=VALUE
pairs.
Returns logical TRUE
on success or FALSE
if metadata could
not be set.
$getMetadataItem(mdi_name)
Returns the value of a specific metadata item named mdi_name
, or empty
string (""
) if no matching item is found.
$close()
Closes the vector dataset (no return value, called for side effects).
Calling $close()
results in proper cleanup, and flushing of any
pending writes.
The GDALVector
object is still available after calling $close()
.
The layer can be re-opened on the existing dsn
with
$open(read_only = {TRUE|FALSE})
.
ogr_define, ogr_manage, ogr2ogr()
, ogrinfo()
GDAL vector format descriptions:
https://gdal.org/drivers/vector/index.html
GDAL-supported SQL dialects:
https://gdal.org/user/ogr_sql_sqlite_dialect.html)
## MTBS fire perimeters in Yellowstone National Park 1984-2022 f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster") ## copy to a temporary file that is writeable dsn <- file.path(tempdir(), basename(f)) file.copy(f, dsn) lyr <- new(GDALVector, dsn, "mtbs_perims") ## object of class GDALVector lyr str(lyr) ## dataset info lyr$getDriverShortName() lyr$getDriverLongName() lyr$getFileList() ## layer info lyr$getName() lyr$getGeomType() lyr$getGeometryColumn() lyr$getFIDColumn() lyr$getSpatialRef() lyr$bbox() ## layer capabilities lyr$testCapability() ## re-open with write access lyr$open(read_only = FALSE) lyr$testCapability()$SequentialWrite lyr$testCapability()$RandomWrite ## feature class definition - a list of field names and their definitions defn <- lyr$getLayerDefn() names(defn) str(defn) ## default value of the read/write field 'returnGeomAs' lyr$returnGeomAs lyr$getFeatureCount() ## sequential read cursor feat <- lyr$getNextFeature() # a list of field names and their values, with class attribute `OGRFeature` feat ## set an attribute filter lyr$setAttributeFilter("ig_year = 2020") lyr$getFeatureCount() feat <- lyr$getNextFeature() plot(feat) ## NULL when no more features are available lyr$getNextFeature() ## reset reading to the start lyr$resetReading() lyr$getNextFeature() ## clear the attribute filter lyr$setAttributeFilter("") lyr$getFeatureCount() ## set a spatial filter ## get the bounding box of the largest 1988 fire and use as spatial filter ## first set a temporary attribute filter to do the lookup lyr$setAttributeFilter("ig_year = 1988 ORDER BY burn_bnd_ac DESC") feat <- lyr$getNextFeature() feat bbox <- g_wk2wk(feat$geom) |> bbox_from_wkt() ## set spatial filter on the full layer lyr$setAttributeFilter("") # clears lyr$setSpatialFilterRect(bbox) lyr$getFeatureCount() ## fetch in chunks and return as data frame (class `OGRFeatureSet`) feat_set <- lyr$fetch(20) head(feat_set) plot(feat_set) ## the next chunk feat_set <- lyr$fetch(20) nrow(feat_set) ## no features remaining feat_set <- lyr$fetch(20) nrow(feat_set) str(feat_set) # 0-row data frame with columns typed ## fetch all pending features feat_set <- lyr$fetch(-1) # resets reading to the first feature nrow(feat_set) plot(feat_set) lyr$clearSpatialFilter() lyr$getFeatureCount() lyr$close() ## simple example for feature write methods showing use of various data types ## create and write to a new layer in a GeoPackage data source dsn2 <- tempfile(fileext = ".gpkg") ## define a feature class defn <- ogr_def_layer("POINT", srs = epsg_to_wkt(4326)) ## add field definitions defn$unique_int <- ogr_def_field("OFTInteger", is_nullable = FALSE, is_unique = TRUE) defn$bool_data <- ogr_def_field("OFTInteger", fld_subtype = "OFSTBoolean") defn$large_ints <- ogr_def_field("OFTInteger64") defn$doubles <- ogr_def_field("OFTReal") defn$strings <- ogr_def_field("OFTString", fld_width = 50) defn$dates <- ogr_def_field("OFTDate") defn$dt_modified <- ogr_def_field("OFTDateTime", default_value = "CURRENT_TIMESTAMP") defn$blobs <- ogr_def_field("OFTBinary") ogr_ds_create("GPKG", dsn2, "test_layer", layer_defn = defn) lyr <- new(GDALVector, dsn2, "test_layer", read_only = FALSE) # lyr$getLayerDefn() |> str() ## define a feature to write feat1 <- list() ## $FID is omitted since it is assigned when written (could also be NA) ## $dt_modified is omitted since the datasource sets a default timestamp feat1$unique_int <- 1001 feat1$bool_data <- TRUE ## passing a string to as.integer64() ## this value is too large to be represented exactly as R numeric (double) feat1$large_ints <- bit64::as.integer64("90071992547409910") feat1$doubles <- 1.234 feat1$strings <- "A test string" feat1$dates <- as.Date("2024-01-01") feat1$blobs <- charToRaw("A binary object") feat1$geom <- "POINT (1 1)" # can be a WKT string or raw vector of WKB ## create as a new feature in the layer lyr$createFeature(feat1) ## the assigned FID lyr$getLastWriteFID() ## this fails due to the unique constraint lyr$createFeature(feat1) feat2 <- list() feat2$unique_int <- 1002 feat2$bool_data <- FALSE feat2$large_ints <- bit64::as.integer64("90071992547409920") feat2$doubles <- 2.345 feat2$strings <- "A test string 2" feat2$dates <- as.Date("2024-01-02") feat2$blobs <- charToRaw("A binary object 2") feat2$geom <- "POINT (2 2)" lyr$createFeature(feat2) lyr$getLastWriteFID() ## close and re-open as a read-only layer lyr$open(read_only = TRUE) lyr$getFeatureCount() feat_set <- lyr$fetch(-1) # -1 for all features reading from start str(feat_set) ## edit an existing feature, e.g., feat <- lyr$getFeature(2) ## here we copy a row of the data frame returned by lyr$fetch() above feat <- feat_set[2,] str(feat) Sys.sleep(1) # only to ensure a timestamp difference feat$bool_data <- TRUE feat$strings <- paste(feat$strings, "- edited") feat$dt_modified <- Sys.time() feat$geom <- "POINT (2.001 2.001)" lyr$open(read_only = FALSE) ## lyr$setFeature() re-writes the feature identified by the $FID element ## N.B., all fields are re-written: ## any fields omitted from the input feature, or set to NA, will be ## re-written as OGR NULL lyr$setFeature(feat) lyr$open(read_only = TRUE) lyr$getFeatureCount() lyr$returnGeomAs <- "WKT" feat_set <- lyr$fetch(-1) str(feat_set) lyr$close()
## MTBS fire perimeters in Yellowstone National Park 1984-2022 f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster") ## copy to a temporary file that is writeable dsn <- file.path(tempdir(), basename(f)) file.copy(f, dsn) lyr <- new(GDALVector, dsn, "mtbs_perims") ## object of class GDALVector lyr str(lyr) ## dataset info lyr$getDriverShortName() lyr$getDriverLongName() lyr$getFileList() ## layer info lyr$getName() lyr$getGeomType() lyr$getGeometryColumn() lyr$getFIDColumn() lyr$getSpatialRef() lyr$bbox() ## layer capabilities lyr$testCapability() ## re-open with write access lyr$open(read_only = FALSE) lyr$testCapability()$SequentialWrite lyr$testCapability()$RandomWrite ## feature class definition - a list of field names and their definitions defn <- lyr$getLayerDefn() names(defn) str(defn) ## default value of the read/write field 'returnGeomAs' lyr$returnGeomAs lyr$getFeatureCount() ## sequential read cursor feat <- lyr$getNextFeature() # a list of field names and their values, with class attribute `OGRFeature` feat ## set an attribute filter lyr$setAttributeFilter("ig_year = 2020") lyr$getFeatureCount() feat <- lyr$getNextFeature() plot(feat) ## NULL when no more features are available lyr$getNextFeature() ## reset reading to the start lyr$resetReading() lyr$getNextFeature() ## clear the attribute filter lyr$setAttributeFilter("") lyr$getFeatureCount() ## set a spatial filter ## get the bounding box of the largest 1988 fire and use as spatial filter ## first set a temporary attribute filter to do the lookup lyr$setAttributeFilter("ig_year = 1988 ORDER BY burn_bnd_ac DESC") feat <- lyr$getNextFeature() feat bbox <- g_wk2wk(feat$geom) |> bbox_from_wkt() ## set spatial filter on the full layer lyr$setAttributeFilter("") # clears lyr$setSpatialFilterRect(bbox) lyr$getFeatureCount() ## fetch in chunks and return as data frame (class `OGRFeatureSet`) feat_set <- lyr$fetch(20) head(feat_set) plot(feat_set) ## the next chunk feat_set <- lyr$fetch(20) nrow(feat_set) ## no features remaining feat_set <- lyr$fetch(20) nrow(feat_set) str(feat_set) # 0-row data frame with columns typed ## fetch all pending features feat_set <- lyr$fetch(-1) # resets reading to the first feature nrow(feat_set) plot(feat_set) lyr$clearSpatialFilter() lyr$getFeatureCount() lyr$close() ## simple example for feature write methods showing use of various data types ## create and write to a new layer in a GeoPackage data source dsn2 <- tempfile(fileext = ".gpkg") ## define a feature class defn <- ogr_def_layer("POINT", srs = epsg_to_wkt(4326)) ## add field definitions defn$unique_int <- ogr_def_field("OFTInteger", is_nullable = FALSE, is_unique = TRUE) defn$bool_data <- ogr_def_field("OFTInteger", fld_subtype = "OFSTBoolean") defn$large_ints <- ogr_def_field("OFTInteger64") defn$doubles <- ogr_def_field("OFTReal") defn$strings <- ogr_def_field("OFTString", fld_width = 50) defn$dates <- ogr_def_field("OFTDate") defn$dt_modified <- ogr_def_field("OFTDateTime", default_value = "CURRENT_TIMESTAMP") defn$blobs <- ogr_def_field("OFTBinary") ogr_ds_create("GPKG", dsn2, "test_layer", layer_defn = defn) lyr <- new(GDALVector, dsn2, "test_layer", read_only = FALSE) # lyr$getLayerDefn() |> str() ## define a feature to write feat1 <- list() ## $FID is omitted since it is assigned when written (could also be NA) ## $dt_modified is omitted since the datasource sets a default timestamp feat1$unique_int <- 1001 feat1$bool_data <- TRUE ## passing a string to as.integer64() ## this value is too large to be represented exactly as R numeric (double) feat1$large_ints <- bit64::as.integer64("90071992547409910") feat1$doubles <- 1.234 feat1$strings <- "A test string" feat1$dates <- as.Date("2024-01-01") feat1$blobs <- charToRaw("A binary object") feat1$geom <- "POINT (1 1)" # can be a WKT string or raw vector of WKB ## create as a new feature in the layer lyr$createFeature(feat1) ## the assigned FID lyr$getLastWriteFID() ## this fails due to the unique constraint lyr$createFeature(feat1) feat2 <- list() feat2$unique_int <- 1002 feat2$bool_data <- FALSE feat2$large_ints <- bit64::as.integer64("90071992547409920") feat2$doubles <- 2.345 feat2$strings <- "A test string 2" feat2$dates <- as.Date("2024-01-02") feat2$blobs <- charToRaw("A binary object 2") feat2$geom <- "POINT (2 2)" lyr$createFeature(feat2) lyr$getLastWriteFID() ## close and re-open as a read-only layer lyr$open(read_only = TRUE) lyr$getFeatureCount() feat_set <- lyr$fetch(-1) # -1 for all features reading from start str(feat_set) ## edit an existing feature, e.g., feat <- lyr$getFeature(2) ## here we copy a row of the data frame returned by lyr$fetch() above feat <- feat_set[2,] str(feat) Sys.sleep(1) # only to ensure a timestamp difference feat$bool_data <- TRUE feat$strings <- paste(feat$strings, "- edited") feat$dt_modified <- Sys.time() feat$geom <- "POINT (2.001 2.001)" lyr$open(read_only = FALSE) ## lyr$setFeature() re-writes the feature identified by the $FID element ## N.B., all fields are re-written: ## any fields omitted from the input feature, or set to NA, will be ## re-written as OGR NULL lyr$setFeature(feat) lyr$open(read_only = TRUE) lyr$getFeatureCount() lyr$returnGeomAs <- "WKT" feat_set <- lyr$fetch(-1) str(feat_set) lyr$close()
geos_version()
returns version information for the GEOS library in use by
GDAL. Requires GDAL >= 3.4.
geos_version()
geos_version()
A list of length four containing:
name
- a string formatted as "major.minor.patch"
major
- major version as integer
minor
- minor version as integer
patch
- patch version as integer
List elements will be NA
if GDAL < 3.4.
gdal_version()
, proj_version()
geos_version()
geos_version()
get_cache_used()
returns the amount of memory currently in use for
GDAL block caching. This a wrapper for GDALGetCacheUsed64()
with return
value as MB.
get_cache_used()
get_cache_used()
Integer. Amount of cache memory in use in MB.
get_cache_used()
get_cache_used()
get_config_option()
gets the value of GDAL runtime configuration option.
Configuration options are essentially global variables the user can set.
They are used to alter the default behavior of certain raster format
drivers, and in some cases the GDAL core. For a full description and
listing of available options see
https://gdal.org/user/configoptions.html.
get_config_option(key)
get_config_option(key)
key |
Character name of a configuration option. |
Character. The value of a (key, value) option previously set with
set_config_option()
. An empty string (""
) is returned if key
is not
found.
vignette("gdal-config-quick-ref")
## this option is set during initialization of the gdalraster package get_config_option("OGR_CT_FORCE_TRADITIONAL_GIS_ORDER")
## this option is set during initialization of the gdalraster package get_config_option("OGR_CT_FORCE_TRADITIONAL_GIS_ORDER")
get_num_cpus()
returns the number of processors detected by GDAL.
Wrapper of CPLGetNumCPUs()
in the GDAL Common Portability Library.
get_num_cpus()
get_num_cpus()
Integer scalar, number of CPUs.
get_num_cpus()
get_num_cpus()
get_pixel_line()
converts geospatial coordinates to pixel/line (raster
column, row numbers).
The upper left corner pixel is the raster origin (0,0) with column, row
increasing left to right, top to bottom.
get_pixel_line(xy, gt)
get_pixel_line(xy, gt)
xy |
Numeric matrix of geospatial x,y coordinates in the same spatial
reference system as |
gt |
Either a numeric vector of length six containing the affine
geotransform for the raster, or an object of class |
Integer matrix of raster pixel/line.
This function applies the inverse geotransform to the input points. If gt
is given as the numeric vector, no bounds checking is done (i.e., min
pixel/line could be less than zero and max pixel/line could be greater than
the raster x/y size). If gt
is obtained from an object of class
GDALRaster
, then NA
is returned for points that fall outside the
raster extent and a warning emitted giving the number points that were
outside. This latter case is equivalent to calling the $get_pixel_line()
class method on the GDALRaster
object (see Examples).
GDALRaster$getGeoTransform()
, inv_geotransform()
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") # id, x, y in NAD83 / UTM zone 12N pts <- read.csv(pt_file) print(pts) raster_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, raster_file) gt <- ds$getGeoTransform() get_pixel_line(pts[, -1], gt) # or, using the class method ds$get_pixel_line(pts[, -1]) # add a point outside the raster extent pts[11, ] <- c(11, 323318, 5105104) get_pixel_line(pts[, -1], gt) # with bounds checking on the raster extent ds$get_pixel_line(pts[, -1]) ds$close()
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") # id, x, y in NAD83 / UTM zone 12N pts <- read.csv(pt_file) print(pts) raster_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, raster_file) gt <- ds$getGeoTransform() get_pixel_line(pts[, -1], gt) # or, using the class method ds$get_pixel_line(pts[, -1]) # add a point outside the raster extent pts[11, ] <- c(11, 323318, 5105104) get_pixel_line(pts[, -1], gt) # with bounds checking on the raster extent ds$get_pixel_line(pts[, -1]) ds$close()
get_usable_physical_ram()
returns the total physical RAM, usable by a
process, in bytes. It will limit to 2 GB for 32 bit processes. Starting
with GDAL 2.4.0, it will also take into account resource limits (virtual
memory) on Posix systems. Starting with GDAL 3.6.1, it will also take into
account RLIMIT_RSS on Linux. Wrapper of CPLGetUsablePhysicalRAM()
in the
GDAL Common Portability Library.
get_usable_physical_ram()
get_usable_physical_ram()
Numeric scalar, number of bytes as bit64::integer64
type (or 0 in
case of failure).
This memory may already be partly used by other processes.
get_usable_physical_ram()
get_usable_physical_ram()
getCreationOptions()
returns the list of creation options supported by a
GDAL format driver as an XML string (invisibly).
Wrapper for GDALGetDriverCreationOptionList()
in the GDAL API.
Information about the available creation options is also printed to the
console by default.
getCreationOptions(format, filter = NULL)
getCreationOptions(format, filter = NULL)
format |
Raster format short name (e.g., "GTiff"). |
filter |
Optional character vector of creation option names. Controls
only the amount of information printed to the console.
By default, information for all creation options is printed. Can be set to
empty string |
Invisibly, an XML string that describes the full list of creation
options or empty string ""
(full output of
GDALGetDriverCreationOptionList()
in the GDAL API).
GDALRaster-class
, create()
, createCopy()
getCreationOptions("GTiff", filter="COMPRESS")
getCreationOptions("GTiff", filter="COMPRESS")
has_geos()
returns a logical value indicating whether GDAL was built
against the GEOS library. GDAL built with GEOS is a system requirement
as of gdalraster
1.10.0, so this function will always return TRUE
(may be removed in a future version).
has_geos()
has_geos()
Logical. TRUE
if GEOS is available, otherwise FALSE
.
has_geos()
has_geos()
has_spatialite()
returns a logical value indicating whether GDAL was
built with support for the SpatiaLite library. SpatiaLite extends the
SQLite core to support full Spatial SQL capabilities.
has_spatialite()
has_spatialite()
GDAL supports executing SQL statements against a datasource. For most file
formats (e.g. Shapefiles, GeoJSON, FlatGeobuf files), the built-in OGR SQL
dialect will be used by default. It is also possible to request the
alternate "SQLite"
dialect, which will use the SQLite engine to evaluate
commands on GDAL datasets. This assumes that GDAL is built with support for
SQLite, and preferably with Spatialite support too to benefit from spatial
functions.
Logical scalar. TRUE
if SpatiaLite is available to GDAL.
All GDAL/OGR drivers for database systems, e.g., PostgreSQL / PostGIS,
Oracle Spatial, SQLite / Spatialite RDBMS, GeoPackage, etc., override the
GDALDataset::ExecuteSQL()
function with a dedicated implementation and, by
default, pass the SQL statements directly to the underlying RDBMS. In these
cases the SQL syntax varies in some particulars from OGR SQL. Also, anything
possible in SQL can then be accomplished for these particular databases. For
those drivers, it is also possible to explicitly request the OGRSQL
or
SQLite
dialects, although performance will generally be much less than the
native SQL engine of those database systems.
OGR SQL dialect and SQLITE SQL dialect:
https://gdal.org/user/ogr_sql_sqlite_dialect.html
has_spatialite()
has_spatialite()
http_enabled()
returns TRUE
if libcurl
support is enabled.
Wrapper of CPLHTTPEnabled()
in the GDAL Common Portability Library.
http_enabled()
http_enabled()
Logical scalar, TRUE
if GDAL was built with libcurl
support.
http_enabled()
http_enabled()
identifyDriver()
will try to identify the driver that can open the passed
file name by invoking the Identify method of each registered GDALDriver in
turn. The short name of the first driver that successfully identifies the
file name will be returned as a character string. If all drivers fail then
NULL
is returned.
Wrapper of GDALIdentifyDriverEx()
in the GDAL C API.
identifyDriver( filename, raster = TRUE, vector = TRUE, allowed_drivers = NULL, file_list = NULL )
identifyDriver( filename, raster = TRUE, vector = TRUE, allowed_drivers = NULL, file_list = NULL )
filename |
Character string containing the name of the file to access. This may not refer to a physical file, but instead contain information for the driver on how to access a dataset (e.g., connection string, URL, etc.) |
raster |
Logical value indicating whether to include raster format
drivers in the search, |
vector |
Logical value indicating whether to include vector format
drivers in the search, |
allowed_drivers |
Optional character vector of driver short names
that must be considered. Set to |
file_list |
Optional character vector of filenames, including those
that are auxiliary to the main filename (see Note). May contain the input
|
A character string with the short name of the first driver that
successfully identifies the input file name, or NULL
on failure.
In order to reduce the need for such searches to touch the file system
machinery of the operating system, it is possible to give an optional list
of files. This is the list of all files at the same level in the file
system as the target file, including the target file. The filenames should
not include any path components. If the target object does not have
filesystem semantics then the file list should be NULL
.
At least one of the raster
or vector
arguments must be TRUE
.
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") identifyDriver(src) |> gdal_formats()
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") identifyDriver(src) |> gdal_formats()
inspectDataset()
returns information about the format and content
of a dataset. The function first calls identifyDriver()
, and then opens
the dataset as raster and/or vector to obtain information about its content.
The return value is a list with named elements.
inspectDataset(filename, ...)
inspectDataset(filename, ...)
filename |
Character string containing the name of the file to access. This may not refer to a physical file, but instead contain information for the driver on how to access a dataset (e.g., connection string, URL, etc.) |
... |
Additional arguments passed to |
A list with the following named elements:
$format
: character string, the format short name
$supports_raster
: logical, TRUE
if the format supports raster data
$contains_raster
: logical, TRUE
if this is a raster dataset or the
source contains raster subdatasets
$supports_subdatasets
: logical, TRUE
if the format supports raster
subdatasets
$contains_subdatasets
: logical, TRUE
if the source contains subdatasets
$subdataset_names
: character vector containing the subdataset names, or
empty vector if subdatasets are not supported or not present
$supports_vector
: logical, TRUE
if the format supports vector data
$contains_vector
: logical, TRUE
if the source contains one or more
vector layers
$layer_names
: character vector containing the vector layer names, or
empty vector if the format does not support vector or the source does not
contain any vector layers
Subdataset names are the character strings that can be used to
instantiate GDALRaster
objects.
See https://gdal.org/en/latest/user/raster_data_model.html#subdatasets-domain.
gdal_formats()
, identifyDriver()
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") inspectDataset(src)
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") inspectDataset(src)
inv_geotransform()
inverts a vector of geotransform coefficients. This
converts the equation from being:
raster pixel/line (column/row) -> geospatial x/y coordinate
to:
geospatial x/y coordinate -> raster pixel/line (column/row)
inv_geotransform(gt)
inv_geotransform(gt)
gt |
Numeric vector of length six containing the geotransform to invert. |
Numeric vector of length six containing the inverted geotransform. The output vector will contain NAs if the input geotransform is uninvertable.
GDALRaster$getGeoTransform()
, get_pixel_line()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) invgt <- ds$getGeoTransform() |> inv_geotransform() ds$close() ptX = 324181.7 ptY = 5103901.4 ## for a point x, y in the spatial reference system of elev_file ## raster pixel (column number): pixel <- floor(invgt[1] + invgt[2] * ptX + invgt[3] * ptY) ## raster line (row number): line <- floor(invgt[4] + invgt[5] * ptX + invgt[6] * ptY) ## get_pixel_line() applies this conversion
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) invgt <- ds$getGeoTransform() |> inv_geotransform() ds$close() ptX = 324181.7 ptY = 5103901.4 ## for a point x, y in the spatial reference system of elev_file ## raster pixel (column number): pixel <- floor(invgt[1] + invgt[2] * ptX + invgt[3] * ptY) ## raster line (row number): line <- floor(invgt[4] + invgt[5] * ptX + invgt[6] * ptY) ## get_pixel_line() applies this conversion
inv_project()
transforms geospatial x/y coordinates to
longitude/latitude in the same geographic coordinate system used by the
given projected spatial reference system. The output long/lat can
optionally be set to a specific geographic coordinate system by specifying
a well known name (see Details).
inv_project(pts, srs, well_known_gcs = "")
inv_project(pts, srs, well_known_gcs = "")
pts |
A two-column data frame or numeric matrix containing geospatial x/y coordinates. |
srs |
Character string specifying the projected spatial reference
system for |
well_known_gcs |
Optional character string containing a supported well known name of a geographic coordinate system (see Details for supported values). |
By default, the geographic coordinate system of the projection specified
by srs
will be used. If a specific geographic coordinate system is
desired, then well_known_gcs
can be set to one of the values below:
EPSG:n | where n is the code of a geographic coordinate system |
WGS84 | same as EPSG:4326 |
WGS72 | same as EPSG:4322 |
NAD83 | same as EPSG:4269 |
NAD27 | same as EPSG:4267 |
CRS84 | same as WGS84 |
CRS72 | same as WGS72 |
CRS27 | same as NAD27 |
The returned array will always be in longitude, latitude order (traditional GIS order) regardless of the axis order defined for the names above.
Numeric array of longitude, latitude. An error is raised if the transformation cannot be performed.
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") ## id, x, y in NAD83 / UTM zone 12N pts <- read.csv(pt_file) print(pts) inv_project(pts[,-1], "EPSG:26912")
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") ## id, x, y in NAD83 / UTM zone 12N pts <- read.csv(pt_file) print(pts) inv_project(pts[,-1], "EPSG:26912")
This topic contains documentation and helper functions for defining an
OGR feature class.
ogr_def_field()
creates an attribute field definition, a list
containing the field data type and potentially other optional field
properties.
ogr_def_geom_field()
similarly creates a geometry field definition.
A list containing zero or more attribute field definitions, along with one
or more geometry field definitions, comprise an OGR feature class definition
(a.k.a. layer definition). ogr_def_layer()
initializes such a list with a
geometry field. Attribute fields can be added to a feature class definition
with calls to ogr_def_field()
as in the examples.
ogr_def_field( fld_type, fld_subtype = NULL, fld_width = NULL, fld_precision = NULL, is_nullable = NULL, is_unique = NULL, default_value = NULL ) ogr_def_geom_field(geom_type, srs = NULL, is_nullable = NULL) ogr_def_layer(geom_type, geom_fld_name = "geometry", srs = NULL)
ogr_def_field( fld_type, fld_subtype = NULL, fld_width = NULL, fld_precision = NULL, is_nullable = NULL, is_unique = NULL, default_value = NULL ) ogr_def_geom_field(geom_type, srs = NULL, is_nullable = NULL) ogr_def_layer(geom_type, geom_fld_name = "geometry", srs = NULL)
fld_type |
Character string containing the name of a field data type
(e.g., |
fld_subtype |
Character string containing the name of a field subtype.
One of |
fld_width |
Optional integer scalar specifying max number of characters. |
fld_precision |
Optional integer scalar specifying number of digits after the decimal point. |
is_nullable |
Optional NOT NULL field constraint (logical scalar).
Defaults to |
is_unique |
Optional UNIQUE constraint on the field (logical scalar).
Defaults to |
default_value |
Optional default value for the field as a character string. |
geom_type |
Character string specifying a geometry type (see Details). |
srs |
Character string containing a spatial reference system definition
as OGC WKT or other well-known format (e.g., the input formats usable with
|
geom_fld_name |
Character string specifying a geometry field name
Defaults to |
All features in an OGR Layer share a common schema (feature class), modeled in GDAL as OGR Feature Definition. The feature class definition includes the set of attribute fields and their data types and the geometry field(s). In R, a feature class definition is represented as a list, having as names the attribute/geometry field names, with each list element holding a field definition.
An attribute field definition is a list with named elements:
$type : OGR Field Type ("OFTReal", "OFTString" etc.) $subtype : optional ("OFSTBoolean", ...) $width : optional max number of characters $precision : optional number of digits after the decimal point $is_nullable: optional NOT NULL constraint (logical scalar) $is_unique : optional UNIQUE constraint (logical scalar) $default : optional default value as character string $is_geom : FALSE (the default) for attribute fields
An OGR field type is specified as a character string with possible values:
OFTInteger
, OFTIntegerList
, OFTReal
, OFTRealList
, OFTString
,
OFTStringList
, OFTBinary
, OFTDate
, OFTTime
, OFTDateTime
,
OFTInteger64
, OFTInteger64List
.
An optional field subtype is specified as a character string with possible
values:
OFSTNone
, OFSTBoolean
, OFSTInt16
, OFSTFloat32
, OFSTJSON
,
OFSTUUID
.
By default, fields are nullable, have no unique constraint, and are not ignored (i.e., not omitted when fetching features). Not-null and unique constraints are not supported by all format drivers.
A default field value is taken into account by format drivers (generally
those with a SQL interface) that support it at field creation time.
If given in the field definition, $default
must be a character string.
The accepted values are "NULL"
, a numeric value (e.g., "0"
), a literal
value enclosed between single quote characters (e.g., "'a default value'"
,
with any inner single quote characters escaped by repetition of the single
quote character), "CURRENT_TIMESTAMP"
, "CURRENT_TIME"
, "CURRENT_DATE"
or a driver-specific expression (that might be ignored by other drivers).
For a datetime literal value, format should be
"'YYYY/MM/DD HH:MM:SS[.sss]'"
(considered as UTC time).
A geometry field definition is a list with named elements:
$type : geom type ("Point", "Polygon", etc.) $srs : optional spatial reference as WKT string $is_nullable: optional NOT NULL constraint (logical scalar) $is_geom : TRUE (required) for geometry fields
Typically, there is one geometry field on a layer, but some formats support more than one geometry column per table (e.g., PostGIS).
Geometry types are specified as a character string containing OGC WKT.
Common types include: Point
, LineString
, Polygon
, MultiPoint
,
MultiLineString
, MultiPolygon
. See the GDAL documentation for a list
of all supported geometry types:
https://gdal.org/api/vector_c_api.html#_CPPv418OGRwkbGeometryType
Format drivers may or may not support not-null constraints on attribute and
geometry fields. If they support creating fields with not-null constraints,
this is generally before creating any features to the layer. In some cases,
a not-null constraint may be available as a layer creation option. For
example, GeoPackage format has a layer creation option
GEOMETRY_NULLABLE=[YES/NO]
.
The feature id (FID) is a special property of a feature and not treated as an attribute of the feature. Additional information is given in the GDAL documentation for the OGR SQL and SQLite SQL dialects. Implications for SQL statements and result sets may depend on the dialect used.
Some vector formats do not support schema definition prior to creating features. For example, with GeoJSON only the Feature object has a member with name properties. The specification does not require all Feature objects in a collection to have the same schema of properties, nor does it require all Feature objects in a collection to have geometry of the same type (https://geojson.org/).
ogr_ds_create()
, ogr_layer_create()
, ogr_field_create()
, ogrinfo()
WKT representation of geometry:
https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry
dsn <- file.path(tempdir(), "test.sqlite") opt <- NULL if (has_spatialite()) opt <- "SPATIALITE=YES" ogr_ds_create("SQLite", dsn, dsco = opt) # define a layer defn <- ogr_def_layer("Point", srs = epsg_to_wkt(4326)) defn$fld1_int64 <- ogr_def_field("OFTInteger64") defn$fld2_string <- ogr_def_field("OFTString") if (ogr_ds_test_cap(dsn)$CreateLayer) ogr_layer_create(dsn, "layer1", layer_defn = defn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, "layer1") deleteDataset(dsn)
dsn <- file.path(tempdir(), "test.sqlite") opt <- NULL if (has_spatialite()) opt <- "SPATIALITE=YES" ogr_ds_create("SQLite", dsn, dsco = opt) # define a layer defn <- ogr_def_layer("Point", srs = epsg_to_wkt(4326)) defn$fld1_int64 <- ogr_def_field("OFTInteger64") defn$fld2_string <- ogr_def_field("OFTString") if (ogr_ds_test_cap(dsn)$CreateLayer) ogr_layer_create(dsn, "layer1", layer_defn = defn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, "layer1") deleteDataset(dsn)
This set of functions can be used to create new vector datasets, test existence of dataset/layer/field, test dataset and layer capabilities, create new layers in an existing dataset, delete layers, create new attribute and geometry fields on an existing layer, rename and delete fields, and edit data with SQL statements.
ogr_ds_exists(dsn, with_update = FALSE) ogr_ds_format(dsn) ogr_ds_test_cap(dsn, with_update = TRUE) ogr_ds_create( format, dsn, layer = NULL, layer_defn = NULL, geom_type = NULL, srs = NULL, fld_name = NULL, fld_type = NULL, dsco = NULL, lco = NULL, overwrite = FALSE, return_obj = FALSE ) ogr_ds_layer_count(dsn) ogr_ds_layer_names(dsn) ogr_layer_exists(dsn, layer) ogr_layer_test_cap(dsn, layer = NULL, with_update = TRUE) ogr_layer_create( dsn, layer, layer_defn = NULL, geom_type = NULL, srs = NULL, lco = NULL, return_obj = FALSE ) ogr_layer_field_names(dsn, layer = NULL) ogr_layer_rename(dsn, layer, new_name) ogr_layer_delete(dsn, layer) ogr_field_index(dsn, layer, fld_name) ogr_field_create( dsn, layer, fld_name, fld_defn = NULL, fld_type = "OFTInteger", fld_subtype = "OFSTNone", fld_width = 0L, fld_precision = 0L, is_nullable = TRUE, is_unique = FALSE, default_value = "" ) ogr_geom_field_create( dsn, layer, fld_name, geom_fld_defn = NULL, geom_type = NULL, srs = NULL, is_nullable = TRUE ) ogr_field_rename(dsn, layer, fld_name, new_name) ogr_field_delete(dsn, layer, fld_name) ogr_execute_sql(dsn, sql, spatial_filter = NULL, dialect = NULL)
ogr_ds_exists(dsn, with_update = FALSE) ogr_ds_format(dsn) ogr_ds_test_cap(dsn, with_update = TRUE) ogr_ds_create( format, dsn, layer = NULL, layer_defn = NULL, geom_type = NULL, srs = NULL, fld_name = NULL, fld_type = NULL, dsco = NULL, lco = NULL, overwrite = FALSE, return_obj = FALSE ) ogr_ds_layer_count(dsn) ogr_ds_layer_names(dsn) ogr_layer_exists(dsn, layer) ogr_layer_test_cap(dsn, layer = NULL, with_update = TRUE) ogr_layer_create( dsn, layer, layer_defn = NULL, geom_type = NULL, srs = NULL, lco = NULL, return_obj = FALSE ) ogr_layer_field_names(dsn, layer = NULL) ogr_layer_rename(dsn, layer, new_name) ogr_layer_delete(dsn, layer) ogr_field_index(dsn, layer, fld_name) ogr_field_create( dsn, layer, fld_name, fld_defn = NULL, fld_type = "OFTInteger", fld_subtype = "OFSTNone", fld_width = 0L, fld_precision = 0L, is_nullable = TRUE, is_unique = FALSE, default_value = "" ) ogr_geom_field_create( dsn, layer, fld_name, geom_fld_defn = NULL, geom_type = NULL, srs = NULL, is_nullable = TRUE ) ogr_field_rename(dsn, layer, fld_name, new_name) ogr_field_delete(dsn, layer, fld_name) ogr_execute_sql(dsn, sql, spatial_filter = NULL, dialect = NULL)
dsn |
Character string. The vector data source name, e.g., a filename or database connection string. |
with_update |
Logical scalar. |
format |
GDAL short name of the vector format as character string.
Examples of some common output formats include: |
layer |
Character string for a layer name in a vector dataset.
The |
layer_defn |
A feature class definition for |
geom_type |
Character string specifying a geometry type (see Details). |
srs |
Character string containing a spatial reference system definition
as OGC WKT or other well-known format (e.g., the input formats usable with
|
fld_name |
Character string containing the name of an attribute field
in |
fld_type |
Character string containing the name of a field data type
(e.g., |
dsco |
Optional character vector of format-specific creation options
for |
lco |
Optional character vector of format-specific creation options
for |
overwrite |
Logical scalar. |
return_obj |
Logical scalar. If |
new_name |
Character string containing a new name to assign. |
fld_defn |
A field definition as list (see |
fld_subtype |
Character string containing the name of a field subtype.
One of |
fld_width |
Optional integer scalar specifying max number of characters. |
fld_precision |
Optional integer scalar specifying number of digits after the decimal point. |
is_nullable |
Optional NOT NULL field constraint (logical scalar).
Defaults to |
is_unique |
Optional UNIQUE constraint on the field (logical scalar).
Defaults to |
default_value |
Optional default value for the field as a character string. |
geom_fld_defn |
A geometry field definition as list
(see |
sql |
Character string containing an SQL statement (see Note). |
spatial_filter |
Either a numeric vector of length four containing a bounding box (xmin, ymin, xmax, ymax), or a character string containing a geometry as OGC WKT, representing a spatial filter. |
dialect |
Character string specifying the SQL dialect to use.
The OGR SQL engine ( |
These functions are complementary to ogrinfo()
and ogr2ogr()
for
vector data management. They are also intended to support vector I/O in a
future release of gdalraster. Bindings to OGR wrap portions of the GDAL
Vector API (ogr_core.h and ogr_api.h,
https://gdal.org/api/vector_c_api.html).
ogr_ds_exists()
tests whether a vector dataset can be opened from the
given data source name (DSN), potentially testing for update access.
Returns a logical scalar.
ogr_ds_format()
returns a character string containing the short name of
the format driver for a given DSN, or NULL
if the dataset cannot be
opened as a vector source.
ogr_ds_test_cap()
tests the capabilities of a vector data source,
attempting to open it with update access by default.
Returns a list of capabilities with values TRUE
or FALSE
, or NULL
is
returned if dsn
cannot be opened with the requested access.
Wrapper of GDALDatasetTestCapability()
in the GDAL API.
The returned list contains the following named elements:
CreateLayer
: TRUE
if this datasource can create new layers
DeleteLayer
: TRUE
if this datasource can delete existing layers
CreateGeomFieldAfterCreateLayer
: TRUE
if the layers of this
datasource support geometry field creation just after layer creation
CurveGeometries
: TRUE
if this datasource supports curve geometries
Transactions
: TRUE
if this datasource supports (efficient)
transactions
EmulatedTransactions
: TRUE
if this datasource supports transactions
through emulation
RandomLayerRead
: TRUE
if this datasource has a dedicated
GetNextFeature()
implementation, potentially returning features from
layers in a non-sequential way
RandomLayerWrite
: TRUE
if this datasource supports calling
CreateFeature()
on layers in a non-sequential way
ogr_ds_create()
creates a new vector datasource, optionally also creating
a layer, and optionally creating one or more fields on the layer.
The attribute fields and geometry field(s) to create can be specified as a
feature class definition (layer_defn
as list, see ogr_define), or
alternatively, by giving the geom_type
and srs
, optionally along with
one fld_name
and fld_type
to be created in the layer.
By default, returns logical TRUE
indicating success (output written to
dst_filename
), or an object of class GDALVector
for the
output layer will be returned if return_obj = TRUE
. An error is raised if
the operation fails.
ogr_ds_layer_count()
returns the number of layers in a vector dataset.
ogr_ds_layer_names()
returns a character vector of layer names in a
vector dataset, or NULL
if no layers are found.
ogr_layer_exists()
tests whether a layer can be accessed by name in a
given vector dataset. Returns a logical scalar.
ogr_layer_test_cap()
tests whether a layer supports named capabilities,
attempting to open the dataset with update access by default.
Returns a list of capabilities with values TRUE
or FALSE
. NULL
is
returned if dsn
cannot be opened with the requested access, or layer
cannot be found. The returned list contains the following named elements:
RandomRead
, SequentialWrite
, RandomWrite
, UpsertFeature
,
FastSpatialFilter
, FastFeatureCount
, FastGetExtent
,
FastSetNextByIndex
, CreateField
, CreateGeomField
, DeleteField
,
ReorderFields
, AlterFieldDefn
, AlterGeomFieldDefn
, IgnoreFields
,
DeleteFeature
, Rename
, StringsAsUTF8
, CurveGeometries
.
See the GDAL documentation for
OGR_L_TestCapability()
.
ogr_layer_create()
creates a new layer in an existing vector data source,
with a specified geometry type and spatial reference definition.
This function also accepts a feature class definition given as a list of
field names and their definitions (see ogr_define).
(Note: use ogr_ds_create()
to create single-layer formats such as "ESRI
Shapefile", "FlatGeobuf", "GeoJSON", etc.)
By default, returns logical TRUE
indicating success, or an object of class
GDALVector
will be returned if return_obj = TRUE
.
An error is raised if the operation fails.
ogr_layer_field_names()
returns a character vector of field names on a
layer, or NULL
if no fields are found. The first layer by index is opened
if NULL
is given for the layer
argument.
ogr_layer_rename()
renames a layer in a vector dataset. This operation is
implemented only by layers that expose the Rename
capability (see
ogr_layer_test_cap()
above). This operation will fail if a layer with the
new name already exists. Returns a logical scalar, TRUE
indicating success.
Requires GDAL >= 3.5.
ogr_layer_delete()
deletes an existing layer in a vector dataset.
Returns a logical scalar, TRUE
indicating success.
ogr_field_index()
tests for existence of an attribute field by name.
Returns the field index on the layer (0-based), or -1
if the field does
not exist.
ogr_field_create()
creates a new attribute field of specified data type in
a given DSN/layer. Several optional field properties can be specified in
addition to the type. Returns a logical scalar, TRUE
indicating success.
ogr_geom_field_create()
creates a new geometry field of specified type in
a given DSN/layer. Returns a logical scalar, TRUE
indicating success.
ogr_field_rename()
renames an existing field on a vector layer.
Not all format drivers support this function. Some drivers may only support
renaming a field while there are still no features in the layer.
AlterFieldDefn
is the relevant layer capability to check.
Returns a logical scalar, TRUE
indicating success.
ogr_field_delete()
deletes an existing field on a vector layer.
Not all format drivers support this function. Some drivers may only support
deleting a field while there are still no features in the layer.
Returns a logical scalar, TRUE
indicating success.
ogr_execute_sql()
executes an SQL statement against the data store.
This function can be used to modify the schema or edit data using SQL
(e.g., ALTER TABLE
, DROP TABLE
, CREATE INDEX
, DROP INDEX
, INSERT
,
UPDATE
, DELETE
), or to execute a query (i.e., SELECT
).
Returns NULL
(invisibly) for statements that are in error, or that have no
results set, or an object of class GDALVector
representing a results set
from the query. Wrapper of GDALDatasetExecuteSQL()
in the GDAL API.
The OGR SQL document linked under See Also contains information on the
SQL dialect supported internally by GDAL/OGR. Some format drivers (e.g.,
PostGIS) pass the SQL directly through to the underlying RDBMS (unless
OGRSQL
is explicitly passed as the dialect). The SQLite dialect can also
be requested with the SQLite
string passed as the dialect
argument of
ogr_execute_sql()
. This assumes that GDAL/OGR is built with support for
SQLite, and preferably also with Spatialite support to benefit from spatial
functions. The GDAL document for SQLite dialect has detailed information.
Other SQL dialects may also be present for some vector formats.
For example, the "INDIRECT_SQLITE"
dialect might potentially be used with
GeoPackage format (https://gdal.org/drivers/vector/gpkg.html#sql).
The function ogrinfo()
can also be used to edit data with SQL statements
(GDAL >= 3.7).
The name of the geometry column of a layer is empty (""
) with some formats
such as ESRI Shapefile and FlatGeobuf. Implications for SQL may depend on the
dialect used. See the GDAL documentation for the "OGR SQL" and "SQLite"
dialects for details.
OGR SQL dialect and SQLite SQL dialect:
https://gdal.org/user/ogr_sql_sqlite_dialect.html
## Create GeoPackage and manage schema dsn <- file.path(tempdir(), "test1.gpkg") ogr_ds_create("GPKG", dsn) ogr_ds_exists(dsn, with_update = TRUE) # dataset capabilities ogr_ds_test_cap(dsn) ogr_layer_create(dsn, layer = "layer1", geom_type = "Polygon", srs = "EPSG:5070") ogr_field_create(dsn, layer = "layer1", fld_name = "field1", fld_type = "OFTInteger64", is_nullable = FALSE) ogr_field_create(dsn, layer = "layer1", fld_name = "field2", fld_type = "OFTString") ogr_ds_layer_count(dsn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, layer = "layer1") # delete a field if (ogr_layer_test_cap(dsn, "layer1")$DeleteField) { ogr_field_delete(dsn, layer = "layer1", fld_name = "field2") } ogr_layer_field_names(dsn, "layer1") # define a feature class and create layer defn <- ogr_def_layer("Point", srs = epsg_to_wkt(4326)) # add the attribute fields defn$id_field <- ogr_def_field(fld_type = "OFTInteger64", is_nullable = FALSE, is_unique = TRUE) defn$str_field <- ogr_def_field(fld_type = "OFTString", fld_width = 25, is_nullable = FALSE, default_value = "'a default string'") defn$numeric_field <- ogr_def_field(fld_type = "OFTReal", default_value = "0.0") ogr_layer_create(dsn, layer = "layer2", layer_defn = defn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, layer = "layer2") # add a field using SQL instead ogr_execute_sql(dsn, sql = "ALTER TABLE layer2 ADD field4 float") # rename a field if (ogr_layer_test_cap(dsn, "layer1")$AlterFieldDefn) { ogr_field_rename(dsn, layer = "layer2", fld_name = "field4", new_name = "renamed_field") } ogr_layer_field_names(dsn, layer = "layer2") # GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) ogrinfo(dsn, "layer2") ## Edit data using SQL src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") perims_shp <- file.path(tempdir(), "mtbs_perims.shp") ogr2ogr(src_dsn = src, dst_dsn = perims_shp, src_layers = "mtbs_perims") ogr_ds_format(dsn = perims_shp) ogr_ds_layer_names(dsn = perims_shp) ogr_layer_field_names(dsn = perims_shp, layer = "mtbs_perims") alt_tbl <- "ALTER TABLE mtbs_perims ADD burn_bnd_ha float" ogr_execute_sql(dsn = perims_shp, sql = alt_tbl) upd <- "UPDATE mtbs_perims SET burn_bnd_ha = (burn_bnd_ac / 2.471)" ogr_execute_sql(dsn = perims_shp, sql = upd, dialect = "SQLite") ogr_layer_field_names(dsn = perims_shp, layer = "mtbs_perims") # if GDAL >= 3.7: # ogrinfo(dsn = perims_shp, layer = "mtbs_perims") # or, for output incl. the feature data (omit the default "-so" arg): # ogrinfo(dsn = perims_shp, layer = "mtbs_perims", cl_arg = "-nomd")
## Create GeoPackage and manage schema dsn <- file.path(tempdir(), "test1.gpkg") ogr_ds_create("GPKG", dsn) ogr_ds_exists(dsn, with_update = TRUE) # dataset capabilities ogr_ds_test_cap(dsn) ogr_layer_create(dsn, layer = "layer1", geom_type = "Polygon", srs = "EPSG:5070") ogr_field_create(dsn, layer = "layer1", fld_name = "field1", fld_type = "OFTInteger64", is_nullable = FALSE) ogr_field_create(dsn, layer = "layer1", fld_name = "field2", fld_type = "OFTString") ogr_ds_layer_count(dsn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, layer = "layer1") # delete a field if (ogr_layer_test_cap(dsn, "layer1")$DeleteField) { ogr_field_delete(dsn, layer = "layer1", fld_name = "field2") } ogr_layer_field_names(dsn, "layer1") # define a feature class and create layer defn <- ogr_def_layer("Point", srs = epsg_to_wkt(4326)) # add the attribute fields defn$id_field <- ogr_def_field(fld_type = "OFTInteger64", is_nullable = FALSE, is_unique = TRUE) defn$str_field <- ogr_def_field(fld_type = "OFTString", fld_width = 25, is_nullable = FALSE, default_value = "'a default string'") defn$numeric_field <- ogr_def_field(fld_type = "OFTReal", default_value = "0.0") ogr_layer_create(dsn, layer = "layer2", layer_defn = defn) ogr_ds_layer_names(dsn) ogr_layer_field_names(dsn, layer = "layer2") # add a field using SQL instead ogr_execute_sql(dsn, sql = "ALTER TABLE layer2 ADD field4 float") # rename a field if (ogr_layer_test_cap(dsn, "layer1")$AlterFieldDefn) { ogr_field_rename(dsn, layer = "layer2", fld_name = "field4", new_name = "renamed_field") } ogr_layer_field_names(dsn, layer = "layer2") # GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) ogrinfo(dsn, "layer2") ## Edit data using SQL src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") perims_shp <- file.path(tempdir(), "mtbs_perims.shp") ogr2ogr(src_dsn = src, dst_dsn = perims_shp, src_layers = "mtbs_perims") ogr_ds_format(dsn = perims_shp) ogr_ds_layer_names(dsn = perims_shp) ogr_layer_field_names(dsn = perims_shp, layer = "mtbs_perims") alt_tbl <- "ALTER TABLE mtbs_perims ADD burn_bnd_ha float" ogr_execute_sql(dsn = perims_shp, sql = alt_tbl) upd <- "UPDATE mtbs_perims SET burn_bnd_ha = (burn_bnd_ac / 2.471)" ogr_execute_sql(dsn = perims_shp, sql = upd, dialect = "SQLite") ogr_layer_field_names(dsn = perims_shp, layer = "mtbs_perims") # if GDAL >= 3.7: # ogrinfo(dsn = perims_shp, layer = "mtbs_perims") # or, for output incl. the feature data (omit the default "-so" arg): # ogrinfo(dsn = perims_shp, layer = "mtbs_perims", cl_arg = "-nomd")
ogr_proc()
performs GIS overlay operations on vector layers
(https://en.wikipedia.org/wiki/Vector_overlay). It provides
an interface to the GDAL API methods for these operations
(OGRLayer::Intersection()
, OGRLayer::Union()
, etc).
Inputs are given as objects of class GDALVector
, which
may have spatial and/or attribute filters applied.
The output layer will be created if it does not exist, but output can also
be appended to an existing layer, or written to an existing empty layer that
has a custom schema defined.
ogr_proc()
is basically a port of the
ogr_layer_algebra
utility in the GDAL Python bindings.
ogr_proc( mode, input_lyr, method_lyr, out_dsn, out_lyr_name = NULL, out_geom_type = NULL, out_fmt = NULL, dsco = NULL, lco = NULL, mode_opt = NULL, overwrite = FALSE, quiet = FALSE, return_lyr_obj = TRUE )
ogr_proc( mode, input_lyr, method_lyr, out_dsn, out_lyr_name = NULL, out_geom_type = NULL, out_fmt = NULL, dsco = NULL, lco = NULL, mode_opt = NULL, overwrite = FALSE, quiet = FALSE, return_lyr_obj = TRUE )
mode |
Character string specifying the operation to perform. One of
|
input_lyr |
An object of class |
method_lyr |
An object of class |
out_dsn |
The destination vector filename or database connection string to which the output layer will be written. |
out_lyr_name |
Layer name where the output vector will be written. May
be |
out_geom_type |
Character string specifying the geometry type of the output layer. One of NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, GEOMETRY25D, POINT25D, LINESTRING25D, POLYGON25D, GEOMETRYCOLLECTION25D, MULTIPOINT25D, MULTIPOLYGON25D. Defaults to UNKNOWN if not specified. |
out_fmt |
GDAL short name of the output vector format. If unspecified,
the function will attempt to guess the format from the value of |
dsco |
Optional character vector of format-specific creation options
for |
lco |
Optional character vector of format-specific creation options
for |
mode_opt |
Optional character vector of |
overwrite |
Logical scalar. |
quiet |
Logical scalar. If |
return_lyr_obj |
Logical scalar. If |
Seven processing modes are available:
Intersection
: The output layer contains features whose geometries
represent areas that are common between features in the input layer and in
the method layer. The features in the output layer have attributes from
both input and method layers.
Union
: The output layer contains features whose geometries represent
areas that are either in the input layer, in the method layer, or in
both. The features in the output layer have attributes from both input and
method layers. For features which represent areas that are only in the
input layer or only in the method layer the respective attributes have
undefined values.
SymDifference
: The output layer contains features whose geometries
represent areas that are in either in the input layer or in the method layer
but not in both. The features in the output layer have attributes from both
input and method layers. For features which represent areas that are only in
the input or only in the method layer the respective attributes have
undefined values.
Identity
: Identifies the features of the input layer with the ones from
the method layer. The output layer contains features whose geometries
represent areas that are in the input layer. The features in the output layer
have attributes from both the input and method layers.
Update
: The update method creates a layer which adds features into the
input layer from the method layer, possibly cutting features in the input
layer. The features in the output layer have areas of the features of the
method layer or those areas of the features of the input layer that are not
covered by the method layer. The features of the output layer get their
attributes from the input layer.
Clip
: The clip method creates a layer which has features from the input
layer clipped to the areas of the features in the method layer. By default
the output layer has attributes of the input layer.
Erase
: The erase method creates a layer which has features from the input
layer whose areas are erased by the features in the method layer. By default,
the output layer has attributes of the input layer.
By default, ogr_proc()
will create the output layer with an empty schema.
It will be initialized by GDAL to contain all fields in the input layer, or
depending on the operation, all fields in both the input and method layers.
In the latter case, the prefixes "input_"
and "method_"
will be added to
the output field names by default. The default prefixes can be overridden in
the mode_opt
argument as described below.
Alternatively, the functions in the ogr_manage
interface
could be used to create an empty layer with user-defined schema (e.g.,
ogr_ds_create()
, ogr_layer_create()
and ogr_field_create()
). If the
schema of the output layer is set by the user and contains fields that have
the same name as a field in both the input and method layers, then the
attribute for an output feature will get the value from the feature of the
method layer.
Options that affect processing can be set as NAME=VALUE pairs passed in the
mode_opt
argument. Some options are specific to certain processing modes
as noted below:
SKIP_FAILURES=YES/NO. Set it to YES to go on, even when a feature could not be inserted or a GEOS call failed.
PROMOTE_TO_MULTI=YES/NO. Set to YES to convert Polygons into MultiPolygons, LineStrings to MultiLineStrings or Points to MultiPoints (only since GDAL 3.9.2 for the latter).
INPUT_PREFIX=string. Set a prefix for the field names that will be created from the fields of the input layer.
METHOD_PREFIX=string. Set a prefix for the field names that will be created from the fields of the method layer.
USE_PREPARED_GEOMETRIES=YES/NO. Set to NO to not use prepared geometries to
pretest intersection of features of method layer with features of input
layer. Applies to Intersection
, Union
, Identity
.
PRETEST_CONTAINMENT=YES/NO. Set to YES to pretest the containment of
features of method layer within the features of input layer. This will speed
up the operation significantly in some cases. Requires that the prepared
geometries are in effect. Applies to Intersection
.
KEEP_LOWER_DIMENSION_GEOMETRIES=YES/NO. Set to NO to skip result features
with lower dimension geometry that would otherwise be added to the output
layer. The default is YES, to add features with lower dimension geometry, but
only if the result output has an UNKNOWN geometry type. Applies to
Intersection
, Union
, Identity
.
The input and method layers should have the same spatial reference system. No
on-the-fly reprojection is done. When an output layer is created it will have
the SRS of input_lyr
.
Upon successful completion, an object of class
GDALVector
is returned by default (if
return_lyr_obj = TRUE
), or logical TRUE
is returned (invisibly) if
return_lyr_obj = FALSE
.
Logical FALSE
is returned (invisibly) if an error occurs during processing.
The first geometry field is always used.
For best performance use the minimum amount of features in the method layer and copy into a memory layer.
GDALVector-class
, ogr_define
,
ogr_manage
Vector overlay operators:
https://en.wikipedia.org/wiki/Vector_overlay
# MTBS fires in Yellowstone National Park 1984-2022 dsn <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # layer filtered to fires since year 2000 lyr1 <- new(GDALVector, dsn, "mtbs_perims") lyr1$setAttributeFilter("ig_year >= 2000") lyr1$getFeatureCount() # second layer for the 1988 North Fork fire perimeter sql <- paste0("SELECT incid_name, ig_year, geom ", "FROM mtbs_perims ", "WHERE incid_name = 'NORTH FORK'") lyr2 <- new(GDALVector, dsn, sql) lyr2$getFeatureCount() # intersect to obtain areas re-burned since 2000 tmp_dsn <- tempfile(fileext = ".gpkg") opt <- c("INPUT_PREFIX=layer1_", "METHOD_PREFIX=layer2_", "PROMOTE_TO_MULTI=YES") lyr_out <- ogr_proc(mode = "Intersection", input_lyr = lyr1, method_lyr = lyr2, out_dsn = tmp_dsn, out_lyr_name = "north_fork_reburned", out_geom_type = "MULTIPOLYGON", mode_opt = opt) # the output layer has attributes of both the input and method layers lyr_out$returnGeomAs <- "TYPE_NAME" d <- lyr_out$fetch(-1) print(d) # clean up lyr1$close() lyr2$close() lyr_out$close()
# MTBS fires in Yellowstone National Park 1984-2022 dsn <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # layer filtered to fires since year 2000 lyr1 <- new(GDALVector, dsn, "mtbs_perims") lyr1$setAttributeFilter("ig_year >= 2000") lyr1$getFeatureCount() # second layer for the 1988 North Fork fire perimeter sql <- paste0("SELECT incid_name, ig_year, geom ", "FROM mtbs_perims ", "WHERE incid_name = 'NORTH FORK'") lyr2 <- new(GDALVector, dsn, sql) lyr2$getFeatureCount() # intersect to obtain areas re-burned since 2000 tmp_dsn <- tempfile(fileext = ".gpkg") opt <- c("INPUT_PREFIX=layer1_", "METHOD_PREFIX=layer2_", "PROMOTE_TO_MULTI=YES") lyr_out <- ogr_proc(mode = "Intersection", input_lyr = lyr1, method_lyr = lyr2, out_dsn = tmp_dsn, out_lyr_name = "north_fork_reburned", out_geom_type = "MULTIPOLYGON", mode_opt = opt) # the output layer has attributes of both the input and method layers lyr_out$returnGeomAs <- "TYPE_NAME" d <- lyr_out$fetch(-1) print(d) # clean up lyr1$close() lyr2$close() lyr_out$close()
ogr2ogr()
is a wrapper of the ogr2ogr
command-line
utility (see https://gdal.org/programs/ogr2ogr.html).
This function can be used to convert simple features data between file
formats. It can also perform various operations during the process, such
as spatial or attribute selection, reducing the set of attributes, setting
the output coordinate system or even reprojecting the features during
translation.
Refer to the GDAL documentation at the URL above for a description of
command-line arguments that can be passed in cl_arg
.
ogr2ogr( src_dsn, dst_dsn, src_layers = NULL, cl_arg = NULL, open_options = NULL )
ogr2ogr( src_dsn, dst_dsn, src_layers = NULL, cl_arg = NULL, open_options = NULL )
src_dsn |
Character string. Data source name of the source vector dataset. |
dst_dsn |
Character string. Data source name of the destination vector dataset. |
src_layers |
Optional character vector of layer names in the source dataset. Defaults to all layers. |
cl_arg |
Optional character vector of command-line arguments for
the GDAL |
open_options |
Optional character vector of dataset open options. |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
For progress reporting, see command-line argument -progress
: Display
progress on terminal. Only works if input layers have the "fast feature
count" capability.
ogrinfo()
, the ogr_manage utilities
translate()
for raster data
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # Convert GeoPackage to Shapefile ynp_shp <- file.path(tempdir(), "ynp_fires.shp") ogr2ogr(src, ynp_shp, src_layers = "mtbs_perims") # Reproject to WGS84 ynp_gpkg <- file.path(tempdir(), "ynp_fires.gpkg") args <- c("-t_srs", "EPSG:4326", "-nln", "fires_wgs84") ogr2ogr(src, ynp_gpkg, cl_arg = args) # Clip to a bounding box (xmin, ymin, xmax, ymax in the source SRS) # This will select features whose geometry intersects the bounding box. # The geometries themselves will not be clipped unless "-clipsrc" is # specified. # The source SRS can be overridden with "-spat_srs" "<srs_def>" # Using -update mode to write a new layer in the existing DSN bb <- c(469685.97, 11442.45, 544069.63, 85508.15) args <- c("-update", "-nln", "fires_clip", "-spat", bb) ogr2ogr(src, ynp_gpkg, cl_arg = args) # Filter features by a -where clause sql <- "ig_year >= 2000 ORDER BY ig_year" args <- c("-update", "-nln", "fires_2000-2020", "-where", sql) ogr2ogr(src, ynp_gpkg, src_layers = "mtbs_perims", cl_arg = args) # Dissolve features based on a shared attribute value if (has_spatialite()) { sql <- "SELECT ig_year, ST_Union(geom) AS geom FROM mtbs_perims GROUP BY ig_year" args <- c("-update", "-sql", sql, "-dialect", "SQLITE") args <- c(args, "-nlt", "MULTIPOLYGON", "-nln", "dissolved_on_year") ogr2ogr(src, ynp_gpkg, cl_arg = args) }
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # Convert GeoPackage to Shapefile ynp_shp <- file.path(tempdir(), "ynp_fires.shp") ogr2ogr(src, ynp_shp, src_layers = "mtbs_perims") # Reproject to WGS84 ynp_gpkg <- file.path(tempdir(), "ynp_fires.gpkg") args <- c("-t_srs", "EPSG:4326", "-nln", "fires_wgs84") ogr2ogr(src, ynp_gpkg, cl_arg = args) # Clip to a bounding box (xmin, ymin, xmax, ymax in the source SRS) # This will select features whose geometry intersects the bounding box. # The geometries themselves will not be clipped unless "-clipsrc" is # specified. # The source SRS can be overridden with "-spat_srs" "<srs_def>" # Using -update mode to write a new layer in the existing DSN bb <- c(469685.97, 11442.45, 544069.63, 85508.15) args <- c("-update", "-nln", "fires_clip", "-spat", bb) ogr2ogr(src, ynp_gpkg, cl_arg = args) # Filter features by a -where clause sql <- "ig_year >= 2000 ORDER BY ig_year" args <- c("-update", "-nln", "fires_2000-2020", "-where", sql) ogr2ogr(src, ynp_gpkg, src_layers = "mtbs_perims", cl_arg = args) # Dissolve features based on a shared attribute value if (has_spatialite()) { sql <- "SELECT ig_year, ST_Union(geom) AS geom FROM mtbs_perims GROUP BY ig_year" args <- c("-update", "-sql", sql, "-dialect", "SQLITE") args <- c(args, "-nlt", "MULTIPOLYGON", "-nln", "dissolved_on_year") ogr2ogr(src, ynp_gpkg, cl_arg = args) }
ogrinfo()
is a wrapper of the ogrinfo
command-line
utility (see https://gdal.org/programs/ogrinfo.html).
This function lists information about an OGR-supported data source.
It is also possible to edit data with SQL statements.
Refer to the GDAL documentation at the URL above for a description of
command-line arguments that can be passed in cl_arg
.
Requires GDAL >= 3.7.
ogrinfo( dsn, layers = NULL, cl_arg = as.character(c("-so", "-nomd")), open_options = NULL, read_only = TRUE, cout = TRUE )
ogrinfo( dsn, layers = NULL, cl_arg = as.character(c("-so", "-nomd")), open_options = NULL, read_only = TRUE, cout = TRUE )
dsn |
Character string. Data source name (e.g., filename, database connection string, etc.) |
layers |
Optional character vector of layer names in the source dataset. |
cl_arg |
Optional character vector of command-line arguments for
the |
open_options |
Optional character vector of dataset open options. |
read_only |
Logical scalar. |
cout |
Logical scalar. |
Invisibly, a character string containing information about the
vector dataset, or empty string (""
) in case of error.
The command-line argument -so
provides a summary only, i.e., does not
include details about every single feature of a layer.
-nomd
suppresses metadata printing. Some datasets may contain a lot of
metadata strings.
ogr2ogr()
, the ogr_manage utilities
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { # Get the names of the layers in a GeoPackage file. ogrinfo(src) # Summary of a layer ogrinfo(src, "mtbs_perims") # JSON format args <- c("-json", "-nomd") json <- ogrinfo(src, "mtbs_perims", args, cout = FALSE) #info <- jsonlite::fromJSON(json) # Query an attribute to restrict the output of the features in a layer args <- c("-ro", "-nomd", "-where", "ig_year = 2020") ogrinfo(src, "mtbs_perims", args) # Copy to a temporary in-memory file that is writeable src_mem <- paste0("/vsimem/", basename(src)) vsi_copy_file(src, src_mem) print(src_mem) # Add a column to a layer args <- c("-sql", "ALTER TABLE mtbs_perims ADD burn_bnd_ha float") ogrinfo(src_mem, cl_arg = args, read_only = FALSE) # Update values of the column with SQL and specify a dialect sql <- "UPDATE mtbs_perims SET burn_bnd_ha = (burn_bnd_ac / 2.471)" args <- c("-dialect", "sqlite", "-sql", sql) ogrinfo(src_mem, cl_arg = args, read_only = FALSE) }
src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { # Get the names of the layers in a GeoPackage file. ogrinfo(src) # Summary of a layer ogrinfo(src, "mtbs_perims") # JSON format args <- c("-json", "-nomd") json <- ogrinfo(src, "mtbs_perims", args, cout = FALSE) #info <- jsonlite::fromJSON(json) # Query an attribute to restrict the output of the features in a layer args <- c("-ro", "-nomd", "-where", "ig_year = 2020") ogrinfo(src, "mtbs_perims", args) # Copy to a temporary in-memory file that is writeable src_mem <- paste0("/vsimem/", basename(src)) vsi_copy_file(src, src_mem) print(src_mem) # Add a column to a layer args <- c("-sql", "ALTER TABLE mtbs_perims ADD burn_bnd_ha float") ogrinfo(src_mem, cl_arg = args, read_only = FALSE) # Update values of the column with SQL and specify a dialect sql <- "UPDATE mtbs_perims SET burn_bnd_ha = (burn_bnd_ac / 2.471)" args <- c("-dialect", "sqlite", "-sql", sql) ogrinfo(src_mem, cl_arg = args, read_only = FALSE) }
plot_raster()
displays raster data using base graphics
.
plot_raster( data, xsize = NULL, ysize = NULL, nbands = NULL, max_pixels = 2.5e+07, col_tbl = NULL, maxColorValue = 1, normalize = TRUE, minmax_def = NULL, minmax_pct_cut = NULL, col_map_fn = NULL, xlim = NULL, ylim = NULL, interpolate = TRUE, asp = 1, axes = TRUE, main = "", xlab = "x", ylab = "y", xaxs = "i", yaxs = "i", legend = FALSE, digits = 2, na_col = rgb(0, 0, 0, 0), ... )
plot_raster( data, xsize = NULL, ysize = NULL, nbands = NULL, max_pixels = 2.5e+07, col_tbl = NULL, maxColorValue = 1, normalize = TRUE, minmax_def = NULL, minmax_pct_cut = NULL, col_map_fn = NULL, xlim = NULL, ylim = NULL, interpolate = TRUE, asp = 1, axes = TRUE, main = "", xlab = "x", ylab = "y", xaxs = "i", yaxs = "i", legend = FALSE, digits = 2, na_col = rgb(0, 0, 0, 0), ... )
data |
Either a |
xsize |
The number of pixels along the x dimension in |
ysize |
The number of pixels along the y dimension in |
nbands |
The number of bands in |
max_pixels |
The maximum number of pixels that the function will
attempt to display (per band). An error is raised if |
col_tbl |
A color table as a matrix or data frame with four or five
columns. Column 1 contains the numeric pixel values. Columns 2:4 contain
the intensities of the red, green and blue primaries ( |
maxColorValue |
A number giving the maximum of the color values range
in |
normalize |
Logical. |
minmax_def |
Normalize to user-defined min/max values (in terms of the pixel data, per band). For single-band grayscale, a numeric vector of length two containing min, max. For 3-band RGB, a numeric vector of length six containing b1_min, b2_min, b3_min, b1_max, b2_max, b3_max. |
minmax_pct_cut |
Normalize to a truncated range of the pixel data using
percentile cutoffs (removes outliers). A numeric vector of length two giving
the percentiles to use (e.g., |
col_map_fn |
An optional color map function (default is
|
xlim |
Numeric vector of length two giving the x coordinate range.
If |
ylim |
Numeric vector of length two giving the y coordinate range.
If |
interpolate |
Logical indicating whether to apply linear interpolation
to the image when drawing (default |
asp |
Numeric. The aspect ratio y/x (see |
axes |
Logical. |
main |
The main title (on top). |
xlab |
Title for the x axis (see |
ylab |
Title for the y axis (see |
xaxs |
The style of axis interval calculation to be used for the x axis
(see |
yaxs |
The style of axis interval calculation to be used for the y axis
(see |
legend |
Logical indicating whether to include a legend on the plot.
Currently, legends are only supported for continuous data. A color table
will be used if one is specified or the raster has a built-in color table,
otherwise the value for |
digits |
The number of digits to display after the decimal point in the legend labels when raster data are floating point. |
na_col |
Color to use for |
... |
Other parameters to be passed to |
By default, contrast enhancement by stretch to min/max is applied when
the input data are single-band grayscale with any raster data type, or
three-band RGB with raster data type larger than Byte. The minimum/maximum
of the input data are used by default (i.e., no outlier removal). No stretch
is applied by default when the input is an RGB byte raster. These defaults
can be overridden by specifying either the minmax_def
argument
(user-defined min/max per band), or the minmax_pct_cut
argument (ignore
outlier pixels based on a percentile range per band). These settings (and
the normalize
argument) are ignored if a color table is used.
plot_raster()
uses the function graphics::rasterImage()
for plotting
which is not supported on some devices (see ?rasterImage
).
If data
is an object of class GDALRaster
, then plot_raster()
will
attempt to read the entire raster into memory by default (unless the number
of pixels per band would exceed max_pixels
).
A reduced resolution overview can be read by setting xsize
, ysize
smaller than the raster size on disk.
(If data
is instead specified as a vector of pixel values, a reduced
resolution overview would be read by setting out_xsize
and out_ysize
smaller than the raster region defined by xsize
, ysize
in a call to
GDALRaster$read()
).
The GDAL_RASTERIO_RESAMPLING configuration option can be
defined to override the default resampling (NEAREST) to one of BILINEAR,
CUBIC, CUBICSPLINE, LANCZOS, AVERAGE or MODE, for example:
set_config_option("GDAL_RASTERIO_RESAMPLING", "BILINEAR")
GDALRaster$read()
, read_ds()
, set_config_option()
## Elevation elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) # grayscale plot_raster(ds, legend=TRUE, main="Storm Lake elevation (m)") # color ramp from user-defined palette elev_pal <- c("#00A60E","#63C600","#E6E600","#E9BD3B", "#ECB176","#EFC2B3","#F2F2F2") ramp <- scales::colour_ramp(elev_pal, alpha=FALSE) plot_raster(ds, col_map_fn=ramp, legend=TRUE, main="Storm Lake elevation (m)") ds$close() ## Landsat band combination b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b6_file <- system.file("extdata/sr_b6_20200829.tif", package="gdalraster") band_files <- c(b6_file, b5_file, b4_file) r <- vector("integer") for (f in band_files) { ds <- new(GDALRaster, f) dm <- ds$dim() r <- c(r, read_ds(ds)) ds$close() } plot_raster(r, xsize=dm[1], ysize=dm[2], nbands=3, main="Landsat 6-5-4 (vegetative analysis)") ## LANDFIRE Existing Vegetation Cover (EVC) with color map evc_file <- system.file("extdata/storml_evc.tif", package="gdalraster") # colors from the CSV attribute table distributed by LANDFIRE evc_csv <- system.file("extdata/LF20_EVC_220.csv", package="gdalraster") vat <- read.csv(evc_csv) head(vat) vat <- vat[,c(1,6:8)] ds <- new(GDALRaster, evc_file) plot_raster(ds, col_tbl=vat, interpolate=FALSE, main="Storm Lake LANDFIRE EVC") ds$close()
## Elevation elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file) # grayscale plot_raster(ds, legend=TRUE, main="Storm Lake elevation (m)") # color ramp from user-defined palette elev_pal <- c("#00A60E","#63C600","#E6E600","#E9BD3B", "#ECB176","#EFC2B3","#F2F2F2") ramp <- scales::colour_ramp(elev_pal, alpha=FALSE) plot_raster(ds, col_map_fn=ramp, legend=TRUE, main="Storm Lake elevation (m)") ds$close() ## Landsat band combination b4_file <- system.file("extdata/sr_b4_20200829.tif", package="gdalraster") b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b6_file <- system.file("extdata/sr_b6_20200829.tif", package="gdalraster") band_files <- c(b6_file, b5_file, b4_file) r <- vector("integer") for (f in band_files) { ds <- new(GDALRaster, f) dm <- ds$dim() r <- c(r, read_ds(ds)) ds$close() } plot_raster(r, xsize=dm[1], ysize=dm[2], nbands=3, main="Landsat 6-5-4 (vegetative analysis)") ## LANDFIRE Existing Vegetation Cover (EVC) with color map evc_file <- system.file("extdata/storml_evc.tif", package="gdalraster") # colors from the CSV attribute table distributed by LANDFIRE evc_csv <- system.file("extdata/LF20_EVC_220.csv", package="gdalraster") vat <- read.csv(evc_csv) head(vat) vat <- vat[,c(1,6:8)] ds <- new(GDALRaster, evc_file) plot_raster(ds, col_tbl=vat, interpolate=FALSE, main="Storm Lake LANDFIRE EVC") ds$close()
OGRFeature
objectPlot the geometry of an OGRFeature
object
## S3 method for class 'OGRFeature' plot(x, ...)
## S3 method for class 'OGRFeature' plot(x, ...)
x |
An |
... |
Optional arguments passed to |
The input, invisibly.
OGRFeatureSet
Plot the geometry column of an OGRFeatureSet
## S3 method for class 'OGRFeatureSet' plot(x, ...)
## S3 method for class 'OGRFeatureSet' plot(x, ...)
x |
An |
... |
Optional arguments passed to |
The input, invisibly.
polygonize()
creates vector polygons for all connected regions of pixels
in a source raster sharing a common pixel value. Each polygon is created
with an attribute indicating the pixel value of that polygon. A raster mask
may also be provided to determine which pixels are eligible for processing.
The function will create the output vector layer if it does not already
exist, otherwise it will try to append to an existing one.
This function is a wrapper of GDALPolygonize
in the GDAL Algorithms API.
It provides essentially the same functionality as the gdal_polygonize.py
command-line program (https://gdal.org/programs/gdal_polygonize.html).
polygonize( raster_file, out_dsn, out_layer, fld_name = "DN", out_fmt = NULL, connectedness = 4, src_band = 1, mask_file = NULL, nomask = FALSE, overwrite = FALSE, dsco = NULL, lco = NULL, quiet = FALSE )
polygonize( raster_file, out_dsn, out_layer, fld_name = "DN", out_fmt = NULL, connectedness = 4, src_band = 1, mask_file = NULL, nomask = FALSE, overwrite = FALSE, dsco = NULL, lco = NULL, quiet = FALSE )
raster_file |
Filename of the source raster. |
out_dsn |
The destination vector filename to which the polygons will be written (or database connection string). |
out_layer |
Name of the layer for writing the polygon features. For
single-layer file formats such as |
fld_name |
Name of an integer attribute field in |
out_fmt |
GDAL short name of the output vector format. If unspecified, the function will attempt to guess the format from the filename/connection string. |
connectedness |
Integer scalar. Must be either |
src_band |
The band on |
mask_file |
Use the first band of the specified raster as a
validity mask (zero is invalid, non-zero is valid). If not specified, the
default validity mask for the input band (such as nodata, or alpha masks)
will be used (unless |
nomask |
Logical scalar. If |
overwrite |
Logical scalar. If |
dsco |
Optional character vector of format-specific creation options
for |
lco |
Optional character vector of format-specific creation options
for |
quiet |
Logical scalar. If |
Polygon features will be created on the output layer, with polygon geometries representing the polygons. The polygon geometries will be in the georeferenced coordinate system of the raster (based on the geotransform of the source dataset). It is acceptable for the output layer to already have features. If the output layer does not already exist, it will be created with coordinate system matching the source raster.
The algorithm attempts to minimize memory use so that very large rasters can be processed. However, if the raster has many polygons or very large/complex polygons, the memory use for holding polygon enumerations and active polygon geometries may grow to be quite large.
The algorithm will generally produce very dense polygon geometries, with edges that follow exactly on pixel boundaries for all non-interior pixels. For non-thematic raster data (such as satellite images) the result will essentially be one small polygon per pixel, and memory and output layer sizes will be substantial. The algorithm is primarily intended for relatively simple thematic rasters, masks, and classification results.
The source pixel band values are read into a signed 64-bit integer buffer
(Int64
) by GDALPolygonize
, so floating point or complex bands will be
implicitly truncated before processing.
When 8-connectedness is used, many of the resulting polygons will likely be
invalid due to ring self-intersection (in the strict OGC definition of
polygon validity). They may be suitable as-is for certain purposes such as
calculating geometry attributes (area, perimeter). Package sf has
st_make_valid()
, PostGIS has ST_MakeValid()
, and QGIS has vector
processing utility "Fix geometries" (single polygons can become MultiPolygon
in the case of self-intersections).
If writing to a SQLite database format as either GPKG
(GeoPackage
vector) or SQLite
(Spatialite vector), setting the
SQLITE_USE_OGR_VFS
and OGR_SQLITE_JOURNAL
configuration options may
increase performance substantially. If writing to PostgreSQL
(PostGIS vector), setting PG_USE_COPY=YES
is faster:
# SQLite: GPKG (.gpkg) and Spatialite (.sqlite) # enable extra buffering/caching by the GDAL/OGR I/O layer set_config_option("SQLITE_USE_OGR_VFS", "YES") # set the journal mode for the SQLite database to MEMORY set_config_option("OGR_SQLITE_JOURNAL", "MEMORY") # PostgreSQL / PostGIS # use COPY for inserting data rather than INSERT set_config_option("PG_USE_COPY", "YES")
vignette("gdal-config-quick-ref")
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") dsn <- file.path(tempdir(), "storm_lake.gpkg") layer <- "lf_evt" fld <- "evt_value" set_config_option("SQLITE_USE_OGR_VFS", "YES") set_config_option("OGR_SQLITE_JOURNAL", "MEMORY") polygonize(evt_file, dsn, layer, fld) set_config_option("SQLITE_USE_OGR_VFS", "") set_config_option("OGR_SQLITE_JOURNAL", "")
evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") dsn <- file.path(tempdir(), "storm_lake.gpkg") layer <- "lf_evt" fld <- "evt_value" set_config_option("SQLITE_USE_OGR_VFS", "YES") set_config_option("OGR_SQLITE_JOURNAL", "MEMORY") polygonize(evt_file, dsn, layer, fld) set_config_option("SQLITE_USE_OGR_VFS", "") set_config_option("OGR_SQLITE_JOURNAL", "")
pop_error_handler()
is a wrapper for CPLPopErrorHandler()
in the GDAL
Common Portability Library.
Discards the current error handler on the error handler stack, and restores
the one in use before the last push_error_handler()
call. This method has
no effect if there are no error handlers on the current thread's error
handler stack.
pop_error_handler()
pop_error_handler()
No return value, called for side effects.
push_error_handler("quiet") # ... pop_error_handler()
push_error_handler("quiet") # ... pop_error_handler()
OGRFeature
objectPrint an OGRFeature
object
## S3 method for class 'OGRFeature' print(x, ...)
## S3 method for class 'OGRFeature' print(x, ...)
x |
An |
... |
Optional arguments passed to |
The input, invisibly.
OGRFeatureSet
Print an OGRFeatureSet
## S3 method for class 'OGRFeatureSet' print(x, ...)
## S3 method for class 'OGRFeatureSet' print(x, ...)
x |
An |
... |
Optional arguments passed to |
The input, invisibly.
proj_networking()
returns the status of PROJ networking capabilities,
optionally enabling or disabling first. Requires GDAL 3.4 or later and
PROJ 7 or later.
proj_networking(enabled = NULL)
proj_networking(enabled = NULL)
enabled |
Optional logical scalar. Set to |
Logical TRUE
if PROJ networking capabilities are enabled (as
indicated by the return value of OSRGetPROJEnableNetwork()
in the GDAL
Spatial Reference System C API). Logical NA
is returned if GDAL < 3.4.
proj_version()
, proj_search_paths()
PROJ-data on GitHub, PROJ Content Delivery Network
proj_networking()
proj_networking()
proj_search_paths()
returns the search path(s) for PROJ resource files,
optionally setting them first.
proj_search_paths(paths = NULL)
proj_search_paths(paths = NULL)
paths |
Optional character vector containing one or more directory paths to set. |
A character vector containing the currently used search path(s) for
PROJ resource files. An empty string (""
) is returned if no search paths
are returned by the function OSRGetPROJSearchPaths()
in the GDAL Spatial
Reference System C API.
proj_version()
, proj_networking()
proj_search_paths()
proj_search_paths()
proj_version()
returns version information for the PROJ library in use by
GDAL.
proj_version()
proj_version()
A list of length four containing:
name
- a string formatted as "major.minor.patch"
major
- major version as integer
minor
- minor version as integer
patch
- patch version as integer
gdal_version()
, geos_version()
, proj_search_paths()
,
proj_networking()
proj_version()
proj_version()
push_error_handler()
is a wrapper for
CPLPushErrorHandler()
in the GDAL Common Portability
Library.
This pushes a new error handler on the thread-local error handler stack.
This handler will be used until removed with pop_error_handler()
.
A typical use is to temporarily set CPLQuietErrorHandler()
which doesn't
make any attempt to report passed error or warning messages, but will
process debug messages via CPLDefaultErrorHandler
.
push_error_handler(handler)
push_error_handler(handler)
handler |
Character name of the error handler to push.
One of |
No return value, called for side effects.
Setting handler = "logging"
will use CPLLoggingErrorHandler()
, error
handler that logs into the file defined by the CPL_LOG
configuration
option, or stderr
otherwise.
This only affects error reporting from GDAL.
push_error_handler("quiet") # ... pop_error_handler()
push_error_handler("quiet") # ... pop_error_handler()
rasterFromRaster()
creates a new raster with spatial reference,
extent and resolution taken from a template raster, without copying data.
Optionally changes the format, number of bands, data type and nodata value,
sets driver-specific dataset creation options, and initializes to a value.
rasterFromRaster( srcfile, dstfile, fmt = NULL, nbands = NULL, dtName = NULL, options = NULL, init = NULL, dstnodata = init )
rasterFromRaster( srcfile, dstfile, fmt = NULL, nbands = NULL, dtName = NULL, options = NULL, init = NULL, dstnodata = init )
srcfile |
Source raster filename. |
dstfile |
Output raster filename. |
fmt |
Output raster format name (e.g., "GTiff" or "HFA"). Will attempt
to guess from the output filename if |
nbands |
Number of output bands. |
dtName |
Output raster data type name. Commonly used types include
|
options |
Optional list of format-specific creation options in a
vector of "NAME=VALUE" pairs
(e.g., |
init |
Numeric value to initialize all pixels in the output raster. |
dstnodata |
Numeric nodata value for the output raster. |
Returns the destination filename invisibly.
GDALRaster-class
, create()
, createCopy()
,
bandCopyWholeRaster()
, translate()
# band 2 in a FARSITE landscape file has slope degrees # convert slope degrees to slope percent in a new raster lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds_lcp <- new(GDALRaster, lcp_file) ds_lcp$getMetadata(band=2, domain="") slpp_file <- file.path(tempdir(), "storml_slpp.tif") opt = c("COMPRESS=LZW") rasterFromRaster(srcfile = lcp_file, dstfile = slpp_file, nbands = 1, dtName = "Int16", options = opt, init = -32767) ds_slp <- new(GDALRaster, slpp_file, read_only=FALSE) # slpp_file is initialized to -32767 and nodata value set ds_slp$getNoDataValue(band=1) # extent and cell size are the same as lcp_file ds_lcp$bbox() ds_lcp$res() ds_slp$bbox() ds_slp$res() # convert slope degrees in lcp_file band 2 to slope percent in slpp_file # bring through LCP nodata -9999 to the output nodata value ncols <- ds_slp$getRasterXSize() nrows <- ds_slp$getRasterYSize() for (row in 0:(nrows-1)) { rowdata <- ds_lcp$read(band=2, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) rowslpp <- tan(rowdata*pi/180) * 100 rowslpp[rowdata==-9999] <- -32767 dim(rowslpp) <- c(1, ncols) ds_slp$write(band=1, xoff=0, yoff=row, xsize=ncols, ysize=1, rowslpp) } # min, max, mean, sd ds_slp$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds_slp$close() ds_lcp$close()
# band 2 in a FARSITE landscape file has slope degrees # convert slope degrees to slope percent in a new raster lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds_lcp <- new(GDALRaster, lcp_file) ds_lcp$getMetadata(band=2, domain="") slpp_file <- file.path(tempdir(), "storml_slpp.tif") opt = c("COMPRESS=LZW") rasterFromRaster(srcfile = lcp_file, dstfile = slpp_file, nbands = 1, dtName = "Int16", options = opt, init = -32767) ds_slp <- new(GDALRaster, slpp_file, read_only=FALSE) # slpp_file is initialized to -32767 and nodata value set ds_slp$getNoDataValue(band=1) # extent and cell size are the same as lcp_file ds_lcp$bbox() ds_lcp$res() ds_slp$bbox() ds_slp$res() # convert slope degrees in lcp_file band 2 to slope percent in slpp_file # bring through LCP nodata -9999 to the output nodata value ncols <- ds_slp$getRasterXSize() nrows <- ds_slp$getRasterYSize() for (row in 0:(nrows-1)) { rowdata <- ds_lcp$read(band=2, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) rowslpp <- tan(rowdata*pi/180) * 100 rowslpp[rowdata==-9999] <- -32767 dim(rowslpp) <- c(1, ncols) ds_slp$write(band=1, xoff=0, yoff=row, xsize=ncols, ysize=1, rowslpp) } # min, max, mean, sd ds_slp$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds_slp$close() ds_lcp$close()
rasterize()
burns vector geometries (points, lines, or polygons) into
the band(s) of a raster dataset. Vectors are read from any GDAL
OGR-supported vector format.
This function is a wrapper for the gdal_rasterize
command-line
utility (https://gdal.org/programs/gdal_rasterize.html).
rasterize( src_dsn, dstfile, band = NULL, layer = NULL, where = NULL, sql = NULL, burn_value = NULL, burn_attr = NULL, invert = NULL, te = NULL, tr = NULL, tap = NULL, ts = NULL, dtName = NULL, dstnodata = NULL, init = NULL, fmt = NULL, co = NULL, add_options = NULL, quiet = FALSE )
rasterize( src_dsn, dstfile, band = NULL, layer = NULL, where = NULL, sql = NULL, burn_value = NULL, burn_attr = NULL, invert = NULL, te = NULL, tr = NULL, tap = NULL, ts = NULL, dtName = NULL, dstnodata = NULL, init = NULL, fmt = NULL, co = NULL, add_options = NULL, quiet = FALSE )
src_dsn |
Data source name for the input vector layer (filename or connection string). |
dstfile |
Filename of the output raster. Must support update mode access. This file will be created (or overwritten if it already exists - see Note). |
band |
Numeric vector. The band(s) to burn values into (for existing
|
layer |
Character vector of layer names(s) from |
where |
An optional SQL WHERE style query string to select features to
burn in from the input |
sql |
An SQL statement to be evaluated against |
burn_value |
A fixed numeric value to burn into a band for all features. A numeric vector can be supplied, one burn value per band being written to. |
burn_attr |
Character string. Name of an attribute field on the features to be used for a burn-in value. The value will be burned into all output bands. |
invert |
Logical scalar. |
te |
Numeric vector of length four. Sets the output raster extent. The values must be expressed in georeferenced units. If not specified, the extent of the output raster will be the extent of the vector layer. |
tr |
Numeric vector of length two. Sets the target pixel resolution. The values must be expressed in georeferenced units. Both must be positive. |
tap |
Logical scalar. (target aligned pixels) Align the coordinates of
the extent of the output raster to the values of |
ts |
Numeric vector of length two. Sets the output raster size in
pixels (xsize, ysize). Note that |
dtName |
Character name of output raster data type, e.g., |
dstnodata |
Numeric scalar. Assign a nodata value to output bands. |
init |
Numeric vector. Pre-initialize the output raster band(s) with these value(s). However, it is not marked as the nodata value in the output file. If only one value is given, the same value is used in all the bands. |
fmt |
Output raster format short name (e.g., |
co |
Optional list of format-specific creation options for the output
raster in a vector of "NAME=VALUE" pairs
(e.g., |
add_options |
An optional character vector of additional command-line
options to |
quiet |
Logical scalar. If |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
The function creates a new target raster when any of the fmt
, dstnodata
,
init
, co
, te
, tr
, tap
, ts
, or dtName
arguments are used. The
resolution or size must be specified using the tr
or ts
argument for all
new rasters. The target raster will be overwritten if it already exists and
any of these creation-related options are used.
# MTBS fire perimeters for Yellowstone National Park 1984-2022 dsn <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") sql <- "SELECT * FROM mtbs_perims ORDER BY mtbs_perims.ig_year" out_file <- file.path(tempdir(), "ynp_fires_1984_2022.tif") rasterize(src_dsn = dsn, dstfile = out_file, sql = sql, burn_attr = "ig_year", tr = c(90,90), tap = TRUE, dtName = "Int16", dstnodata = -9999, init = -9999, co = c("TILED=YES","COMPRESS=LZW")) ds <- new(GDALRaster, out_file) pal <- scales::viridis_pal(end = 0.8, direction = -1)(6) ramp <- scales::colour_ramp(pal) plot_raster(ds, legend = TRUE, col_map_fn = ramp, na_col = "#d9d9d9", main="YNP Fires 1984-2022 - Most Recent Burn Year") ds$close()
# MTBS fire perimeters for Yellowstone National Park 1984-2022 dsn <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") sql <- "SELECT * FROM mtbs_perims ORDER BY mtbs_perims.ig_year" out_file <- file.path(tempdir(), "ynp_fires_1984_2022.tif") rasterize(src_dsn = dsn, dstfile = out_file, sql = sql, burn_attr = "ig_year", tr = c(90,90), tap = TRUE, dtName = "Int16", dstnodata = -9999, init = -9999, co = c("TILED=YES","COMPRESS=LZW")) ds <- new(GDALRaster, out_file) pal <- scales::viridis_pal(end = 0.8, direction = -1)(6) ramp <- scales::colour_ramp(pal) plot_raster(ds, legend = TRUE, col_map_fn = ramp, na_col = "#d9d9d9", main="YNP Fires 1984-2022 - Most Recent Burn Year") ds$close()
rasterToVRT()
creates a virtual raster dataset (VRT format) derived from
one source dataset with options for virtual subsetting, virtually resampling
the source data at a different pixel resolution, or applying a virtual
kernel filter. (See buildVRT()
for virtual mosaicing.)
rasterToVRT( srcfile, relativeToVRT = FALSE, vrtfile = tempfile("tmprast", fileext = ".vrt"), resolution = NULL, subwindow = NULL, src_align = TRUE, resampling = "nearest", krnl = NULL, normalized = TRUE )
rasterToVRT( srcfile, relativeToVRT = FALSE, vrtfile = tempfile("tmprast", fileext = ".vrt"), resolution = NULL, subwindow = NULL, src_align = TRUE, resampling = "nearest", krnl = NULL, normalized = TRUE )
srcfile |
Source raster filename. |
relativeToVRT |
Logical. Indicates whether the source filename should
be interpreted as relative to the .vrt file ( |
vrtfile |
Output VRT filename. |
resolution |
A numeric vector of length two (xres, yres). The pixel
size must be expressed in georeferenced units. Both must be positive values.
The source pixel size is used if |
subwindow |
A numeric vector of length four (xmin, ymin, xmax, ymax).
Selects |
src_align |
Logical.
|
resampling |
The resampling method to use if xsize, ysize of the VRT is different than the size of the underlying source rectangle (in number of pixels). The values allowed are nearest, bilinear, cubic, cubicspline, lanczos, average and mode (as character). |
krnl |
A filtering kernel specified as pixel coefficients.
krnl <- c( 0.11111, 0.11111, 0.11111, 0.11111, 0.11111, 0.11111, 0.11111, 0.11111, 0.11111) A kernel cannot be applied to sub-sampled or over-sampled data. |
normalized |
Logical. Indicates whether the kernel is normalized.
Defaults to |
rasterToVRT()
can be used to virtually clip and pixel-align
various raster layers with each other or in relation to vector
polygon boundaries. It also supports VRT kernel filtering.
A VRT dataset is saved as a plain-text file with extension .vrt. This file
contains a description of the dataset in an XML format. The description
includes the source raster filename which can be a full path
(relativeToVRT = FALSE
) or relative path (relativeToVRT = TRUE
).
For relative path, rasterToVRT()
assumes that the .vrt file will be in
the same directory as the source file and uses basename(srcfile)
. The
elements of the XML schema describe how the source data will be read, along
with algorithms potentially applied and so forth. Documentation of the XML
format for .vrt is at:
https://gdal.org/drivers/raster/vrt.html.
Since .vrt is a small plain-text file it is fast to write and requires
little storage space. Read performance is not degraded for certain simple
operations (e.g., virtual clip without resampling). Reading will be
slower for virtual resampling to a different pixel resolution or virtual
kernel filtering since the operations are performed on-the-fly (but .vrt
does not require the up front writing of a resampled or kernel-filtered
raster to a regular format). VRT is sometimes useful as an intermediate
raster in a series of processing steps, e.g., as a tempfile
(the
default).
GDAL VRT format has several capabilities and uses beyond those
covered by rasterToVRT()
. See the URL above for a full discussion.
Returns the VRT filename invisibly.
Pixel alignment is specified in terms of the source raster pixels (i.e.,
srcfile
of the virtual raster). The use case in mind is virtually
clipping a raster to the bounding box of a vector polygon and keeping
pixels aligned with srcfile
(src_align = TRUE
). src_align
would be
set to FALSE
if the intent is "target alignment". For example, if
subwindow
is the bounding box of another raster with a different layout,
then also setting resolution
to the pixel resolution of the target raster
and src_align = FALSE
will result in a virtual raster pixel-aligned with
the target (i.e., pixels in the virtual raster are no longer aligned with
its srcfile
). Resampling defaults to nearest
if not specified.
Examples for both cases of src_align
are given below.
rasterToVRT()
assumes srcfile
is a north-up raster.
GDALRaster-class
, bbox_from_wkt()
, buildVRT()
warp()
can write VRT for virtual reprojection
## resample evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") ds <- new(GDALRaster, evt_file) ds$res() ds$bbox() ds$close() # table of the unique pixel values and their counts tbl <- buildRAT(evt_file) print(tbl) sum(tbl$COUNT) # resample at 90-m resolution # EVT is thematic vegetation type so use a majority value vrt_file <- rasterToVRT(evt_file, resolution=c(90,90), resampling="mode") # .vrt is a small xml file pointing to the source raster file.size(vrt_file) tbl90m <- buildRAT(vrt_file) print(tbl90m) sum(tbl90m$COUNT) ds <- new(GDALRaster, vrt_file) ds$res() ds$bbox() ds$close() ## clip evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") ds_evt <- new(GDALRaster, evt_file) ds_evt$bbox() # WKT string for a boundary within the EVT extent bnd = "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" # src_align = TRUE vrt_file <- rasterToVRT(evt_file, subwindow = bbox_from_wkt(bnd), src_align=TRUE) ds_vrt <- new(GDALRaster, vrt_file) # VRT is a virtual clip, pixel-aligned with the EVT raster bbox_from_wkt(bnd) ds_vrt$bbox() ds_vrt$res() ds_vrt$close() # src_align = FALSE vrt_file <- rasterToVRT(evt_file, subwindow = bbox_from_wkt(bnd), src_align=FALSE) ds_vrt_noalign <- new(GDALRaster, vrt_file) # VRT upper left corner (xmin, ymax) is exactly bnd xmin, ymax ds_vrt_noalign$bbox() ds_vrt_noalign$res() ds_vrt_noalign$close() ds_evt$close() ## subset and pixel align two rasters # FARSITE landscape file for the Storm Lake area lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds_lcp <- new(GDALRaster, lcp_file) # Landsat band 5 file covering the Storm Lake area b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") ds_b5 <- new(GDALRaster, b5_file) ds_lcp$bbox() # 323476.1 5101872.0 327766.1 5105082.0 ds_lcp$res() # 30 30 ds_b5$bbox() # 323400.9 5101815.8 327870.9 5105175.8 ds_b5$res() # 30 30 # src_align = FALSE because we need target alignment in this case: vrt_file <- rasterToVRT(b5_file, resolution = ds_lcp$res(), subwindow = ds_lcp$bbox(), src_align = FALSE) ds_b5vrt <- new(GDALRaster, vrt_file) ds_b5vrt$bbox() # 323476.1 5101872.0 327766.1 5105082.0 ds_b5vrt$res() # 30 30 # read the the Landsat file pixel-aligned with the LCP file # summarize band 5 reflectance where FBFM = 165 # LCP band 4 contains FBFM (a classification of fuel beds): ds_lcp$getMetadata(band=4, domain="") # verify Landsat nodata (0): ds_b5vrt$getNoDataValue(band=1) # will be read as NA and omitted from stats rs <- new(RunningStats, na_rm=TRUE) ncols <- ds_lcp$getRasterXSize() nrows <- ds_lcp$getRasterYSize() for (row in 0:(nrows-1)) { row_fbfm <- ds_lcp$read(band=4, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) row_b5 <- ds_b5vrt$read(band=1, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) rs$update(row_b5[row_fbfm == 165]) } rs$get_count() rs$get_mean() rs$get_min() rs$get_max() rs$get_sum() rs$get_var() rs$get_sd() ds_b5vrt$close() ds_lcp$close() ds_b5$close()
## resample evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") ds <- new(GDALRaster, evt_file) ds$res() ds$bbox() ds$close() # table of the unique pixel values and their counts tbl <- buildRAT(evt_file) print(tbl) sum(tbl$COUNT) # resample at 90-m resolution # EVT is thematic vegetation type so use a majority value vrt_file <- rasterToVRT(evt_file, resolution=c(90,90), resampling="mode") # .vrt is a small xml file pointing to the source raster file.size(vrt_file) tbl90m <- buildRAT(vrt_file) print(tbl90m) sum(tbl90m$COUNT) ds <- new(GDALRaster, vrt_file) ds$res() ds$bbox() ds$close() ## clip evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") ds_evt <- new(GDALRaster, evt_file) ds_evt$bbox() # WKT string for a boundary within the EVT extent bnd = "POLYGON ((324467.3 5104814.2, 323909.4 5104365.4, 323794.2 5103455.8, 324970.7 5102885.8, 326420.0 5103595.3, 326389.6 5104747.5, 325298.1 5104929.4, 325298.1 5104929.4, 324467.3 5104814.2))" # src_align = TRUE vrt_file <- rasterToVRT(evt_file, subwindow = bbox_from_wkt(bnd), src_align=TRUE) ds_vrt <- new(GDALRaster, vrt_file) # VRT is a virtual clip, pixel-aligned with the EVT raster bbox_from_wkt(bnd) ds_vrt$bbox() ds_vrt$res() ds_vrt$close() # src_align = FALSE vrt_file <- rasterToVRT(evt_file, subwindow = bbox_from_wkt(bnd), src_align=FALSE) ds_vrt_noalign <- new(GDALRaster, vrt_file) # VRT upper left corner (xmin, ymax) is exactly bnd xmin, ymax ds_vrt_noalign$bbox() ds_vrt_noalign$res() ds_vrt_noalign$close() ds_evt$close() ## subset and pixel align two rasters # FARSITE landscape file for the Storm Lake area lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds_lcp <- new(GDALRaster, lcp_file) # Landsat band 5 file covering the Storm Lake area b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") ds_b5 <- new(GDALRaster, b5_file) ds_lcp$bbox() # 323476.1 5101872.0 327766.1 5105082.0 ds_lcp$res() # 30 30 ds_b5$bbox() # 323400.9 5101815.8 327870.9 5105175.8 ds_b5$res() # 30 30 # src_align = FALSE because we need target alignment in this case: vrt_file <- rasterToVRT(b5_file, resolution = ds_lcp$res(), subwindow = ds_lcp$bbox(), src_align = FALSE) ds_b5vrt <- new(GDALRaster, vrt_file) ds_b5vrt$bbox() # 323476.1 5101872.0 327766.1 5105082.0 ds_b5vrt$res() # 30 30 # read the the Landsat file pixel-aligned with the LCP file # summarize band 5 reflectance where FBFM = 165 # LCP band 4 contains FBFM (a classification of fuel beds): ds_lcp$getMetadata(band=4, domain="") # verify Landsat nodata (0): ds_b5vrt$getNoDataValue(band=1) # will be read as NA and omitted from stats rs <- new(RunningStats, na_rm=TRUE) ncols <- ds_lcp$getRasterXSize() nrows <- ds_lcp$getRasterYSize() for (row in 0:(nrows-1)) { row_fbfm <- ds_lcp$read(band=4, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) row_b5 <- ds_b5vrt$read(band=1, xoff=0, yoff=row, xsize=ncols, ysize=1, out_xsize=ncols, out_ysize=1) rs$update(row_b5[row_fbfm == 165]) } rs$get_count() rs$get_mean() rs$get_min() rs$get_max() rs$get_sum() rs$get_var() rs$get_sd() ds_b5vrt$close() ds_lcp$close() ds_b5$close()
GDALRaster$read()
read_ds()
will read from a raster dataset that is already open in a
GDALRaster
object. By default, it attempts to read the full raster
extent from all bands at full resolution. read_ds()
is sometimes more
convenient than GDALRaster$read()
, e.g., to read specific multiple bands
for display with plot_raster()
, or simply for the argument defaults to
read an entire raster into memory (see Note).
read_ds( ds, bands = NULL, xoff = 0, yoff = 0, xsize = ds$getRasterXSize(), ysize = ds$getRasterYSize(), out_xsize = xsize, out_ysize = ysize, as_list = FALSE, as_raw = FALSE )
read_ds( ds, bands = NULL, xoff = 0, yoff = 0, xsize = ds$getRasterXSize(), ysize = ds$getRasterYSize(), out_xsize = xsize, out_ysize = ysize, as_list = FALSE, as_raw = FALSE )
ds |
An object of class |
bands |
Integer vector of band numbers to read. By default all bands will be read. |
xoff |
Integer. The pixel (column) offset to the top left corner of the raster region to be read (zero to start from the left side). |
yoff |
Integer. The line (row) offset to the top left corner of the raster region to be read (zero to start from the top). |
xsize |
Integer. The width in pixels of the region to be read. |
ysize |
Integer. The height in pixels of the region to be read. |
out_xsize |
Integer. The width in pixels of the output buffer into which the desired region will be read (e.g., to read a reduced resolution overview). |
out_ysize |
Integer. The height in pixels of the output buffer into which the desired region will be read (e.g., to read a reduced resolution overview). |
as_list |
Logical. If |
as_raw |
Logical. If |
NA
will be returned in place of the nodata value if the raster dataset has
a nodata value defined for the band. Data are read as R integer
type when
possible for the raster data type (Byte, Int8, Int16, UInt16, Int32),
otherwise as type double
(UInt32, Float32, Float64).
The output object has attribute gis
, a list containing:
$type = "raster" $bbox = c(xmin, ymin, xmax, ymax) $dim = c(xsize, ysize, nbands) $srs = <projection as WKT2 string> $datatype = <character vector of data type name by band>
The WKT version used for the projection string can be overridden by setting
the OSR_WKT_FORMAT
configuration option. See srs_to_wkt()
for a list of
supported values.
If as_list = FALSE
(the default), a numeric
or complex
vector
containing the values that were read. It is organized in left to right, top
to bottom pixel order, interleaved by band.
If as_list = TRUE
, a list with number of elements equal to the number of
bands read. Each element contains a numeric
or complex
vector
containing the pixel data read for the band.
There is small overhead in calling read_ds()
compared with
calling GDALRaster$read()
directly. This would only matter if calling
the function repeatedly to read a raster in chunks. For the case of reading
a large raster in many chunks, it will be optimal performance-wise to call
GDALRaster$read()
directly.
By default, this function will attempt to read the full raster into memory.
It generally should not be called on large raster datasets using the default
argument values. The memory size in bytes of the returned vector will be
approximately (xsize * ysize * number of bands * 4) for data read as
integer
, and (xsize * ysize * number of bands * 8) for data read as
double
(plus small object overhead for the vector).
# read three bands from a multi-band dataset lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) # as a vector of pixel data interleaved by band r <- read_ds(ds, bands=c(6,5,4)) typeof(r) length(r) object.size(r) # as a list of band vectors r <- read_ds(ds, bands=c(6,5,4), as_list=TRUE) typeof(r) length(r) object.size(r) # gis attribute list attr(r, "gis") ds$close()
# read three bands from a multi-band dataset lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") ds <- new(GDALRaster, lcp_file) # as a vector of pixel data interleaved by band r <- read_ds(ds, bands=c(6,5,4)) typeof(r) length(r) object.size(r) # as a list of band vectors r <- read_ds(ds, bands=c(6,5,4), as_list=TRUE) typeof(r) length(r) object.size(r) # gis attribute list attr(r, "gis") ds$close()
renameDataset()
renames a dataset in a format-specific way (e.g.,
rename associated files as appropriate). This could include moving the
dataset to a new directory or even a new filesystem.
The dataset should not be open in any existing GDALRaster
objects
when renameDataset()
is called. Wrapper for GDALRenameDataset()
in the
GDAL API.
renameDataset(new_filename, old_filename, format = "")
renameDataset(new_filename, old_filename, format = "")
new_filename |
New name for the dataset. |
old_filename |
Old name for the dataset (should not be open in a
|
format |
Raster format short name (e.g., "GTiff"). If set to empty
string |
Logical TRUE
if no error or FALSE
on failure.
If format
is set to an empty string ""
(the default) then the function
will try to identify the driver from old_filename
. This is done
internally in GDAL by invoking the Identify
method of each registered
GDALDriver
in turn. The first driver that successful identifies the file
name will be returned. An error is raised if a format cannot be determined
from the passed file name.
GDALRaster-class
, create()
, createCopy()
,
deleteDataset()
, copyDatasetFiles()
b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b5_tmp <- file.path(tempdir(), "b5_tmp.tif") file.copy(b5_file, b5_tmp) ds <- new(GDALRaster, b5_tmp) ds$buildOverviews("BILINEAR", levels = c(2, 4, 8), bands = c(1)) ds$getFileList() ds$close() b5_tmp2 <- file.path(tempdir(), "b5_tmp_renamed.tif") renameDataset(b5_tmp2, b5_tmp) ds <- new(GDALRaster, b5_tmp2) ds$getFileList() ds$close() deleteDataset(b5_tmp2)
b5_file <- system.file("extdata/sr_b5_20200829.tif", package="gdalraster") b5_tmp <- file.path(tempdir(), "b5_tmp.tif") file.copy(b5_file, b5_tmp) ds <- new(GDALRaster, b5_tmp) ds$buildOverviews("BILINEAR", levels = c(2, 4, 8), bands = c(1)) ds$getFileList() ds$close() b5_tmp2 <- file.path(tempdir(), "b5_tmp_renamed.tif") renameDataset(b5_tmp2, b5_tmp) ds <- new(GDALRaster, b5_tmp2) ds$getFileList() ds$close() deleteDataset(b5_tmp2)
RunningStats
computes summary statistics on a data stream efficiently.
Mean and variance are calculated with Welford's online algorithm
(https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance).
The min, max, sum and count are also tracked. The input data values are not
stored in memory, so this class can be used to compute statistics for very
large data streams.
na_rm |
Logical scalar. |
An object of class RunningStats
. A RunningStats
object
maintains the current minimum, maximum, mean, variance, sum and count of
values that have been read from the stream. It can be updated repeatedly
with new values (i.e., chunks of data read from the input stream), but its
memory footprint is negligible. Class methods for updating with new values
and retrieving current values of statistics are described in Details.
RunningStats
is a C++ class exposed directly to R (via
RCPP_EXPOSED_CLASS
). Methods of the class are accessed in R using the $
operator.
## Constructor rs <- new(RunningStats, na_rm) ## Methods rs$update(newvalues) rs$get_count() rs$get_mean() rs$get_min() rs$get_max() rs$get_sum() rs$get_var() rs$get_sd() rs$reset()
new(RunningStats, na_rm)
Returns an object of class RunningStats
. The na_rm
argument
defaults to TRUE
if omitted.
$update(newvalues)
Updates the RunningStats
object with a numeric vector of newvalues
(i.e., a chunk of values from the data stream). No return value, called
for side effects.
$get_count()
Returns the count of values received from the data stream.
$get_mean()
Returns the mean of values received from the data stream.
$get_min()
Returns the minimum value received from the data stream.
$get_max()
Returns the maximum value received from the data stream.
$get_sum()
Returns the sum of values received from the data stream.
$get_var()
Returns the variance of values from the data stream
(denominator n - 1).
$get_sd()
Returns the standard deviation of values from the data stream
(denominator n - 1).
$reset()
Clears the RunningStats
object to its initialized state (count = 0).
No return value, called for side effects.
The intended use is computing summary statistics for specific subsets or
zones of a raster that could be defined in various ways and are generally
not contiguous. The algorithm as implemented here incurs the cost of
floating point division for each new value updated (i.e., per pixel), but is
reasonably efficient for the use case. Note that GDAL internally uses an
optimized version of Welford's algorithm to compute raster statistics as
described in detail by Rouault, 2016
(https://github.com/OSGeo/gdal/blob/master/gcore/statistics.txt).
The class method GDALRaster$getStatistics()
is a GDAL API wrapper that
computes statistics for a whole raster band.
set.seed(42) rs <- new(RunningStats, na_rm=TRUE) chunk <- runif(1000) rs$update(chunk) object.size(rs) rs$get_count() length(chunk) rs$get_mean() mean(chunk) rs$get_min() min(chunk) rs$get_max() max(chunk) rs$get_var() var(chunk) rs$get_sd() sd(chunk) ## 10^9 values read in 10,000 chunks ## should take under 1 minute on most PC hardware for (i in 1:1e4) { chunk <- runif(1e5) rs$update(chunk) } rs$get_count() rs$get_mean() rs$get_var() object.size(rs)
set.seed(42) rs <- new(RunningStats, na_rm=TRUE) chunk <- runif(1000) rs$update(chunk) object.size(rs) rs$get_count() length(chunk) rs$get_mean() mean(chunk) rs$get_min() min(chunk) rs$get_max() max(chunk) rs$get_var() var(chunk) rs$get_sd() sd(chunk) ## 10^9 values read in 10,000 chunks ## should take under 1 minute on most PC hardware for (i in 1:1e4) { chunk <- runif(1e5) rs$update(chunk) } rs$get_count() rs$get_mean() rs$get_var() object.size(rs)
set_config_option()
sets a GDAL runtime configuration option.
Configuration options are essentially global variables the user can set.
They are used to alter the default behavior of certain raster format
drivers, and in some cases the GDAL core. For a full description and
listing of available options see
https://gdal.org/user/configoptions.html.
set_config_option(key, value)
set_config_option(key, value)
key |
Character name of a configuration option. |
value |
Character value to set for the option.
|
No return value, called for side effects.
vignette("gdal-config-quick-ref")
set_config_option("GDAL_CACHEMAX", "10%") get_config_option("GDAL_CACHEMAX") ## unset: set_config_option("GDAL_CACHEMAX", "")
set_config_option("GDAL_CACHEMAX", "10%") get_config_option("GDAL_CACHEMAX") ## unset: set_config_option("GDAL_CACHEMAX", "")
sieveFilter()
is a wrapper for GDALSieveFilter()
in the GDAL Algorithms
API. It removes raster polygons smaller than a provided threshold size
(in pixels) and replaces them with the pixel value of the largest neighbour
polygon.
sieveFilter( src_filename, src_band, dst_filename, dst_band, size_threshold, connectedness, mask_filename = "", mask_band = 0L, options = NULL, quiet = FALSE )
sieveFilter( src_filename, src_band, dst_filename, dst_band, size_threshold, connectedness, mask_filename = "", mask_band = 0L, options = NULL, quiet = FALSE )
src_filename |
Filename of the source raster to be processed. |
src_band |
Band number in the source raster to be processed. |
dst_filename |
Filename of the output raster. It may be the same as
|
dst_band |
Band number in |
size_threshold |
Integer. Raster polygons with sizes (in pixels) smaller than this value will be merged into their largest neighbour. |
connectedness |
Integer. Either |
mask_filename |
Optional filename of raster to use as a mask. |
mask_band |
Band number in |
options |
Algorithm options as a character vector of name=value pairs. None currently supported. |
quiet |
Logical scalar. If |
Polygons are determined as regions of the raster where the pixels all have the same value, and that are contiguous (connected). Pixels determined to be "nodata" per the mask band will not be treated as part of a polygon regardless of their pixel values. Nodata areas will never be changed nor affect polygon sizes. Polygons smaller than the threshold with no neighbours that are as large as the threshold will not be altered. Polygons surrounded by nodata areas will therefore not be altered.
The algorithm makes three passes over the input file to enumerate the polygons and collect limited information about them. Memory use is proportional to the number of polygons (roughly 24 bytes per polygon), but is not directly related to the size of the raster. So very large raster files can be processed effectively if there aren't too many polygons. But extremely noisy rasters with many one pixel polygons will end up being expensive (in memory) to process.
The input dataset is read as integer data which means that floating point values are rounded to integers.
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
## remove single-pixel polygons from the vegetation type layer (EVT) evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") # create a blank raster to hold the output evt_mmu_file <- file.path(tempdir(), "storml_evt_mmu2.tif") rasterFromRaster(srcfile = evt_file, dstfile = evt_mmu_file, init = 32767) # create a mask to exclude water pixels from the algorithm # recode water (7292) to 0 expr <- "ifelse(EVT == 7292, 0, EVT)" mask_file <- calc(expr = expr, rasterfiles = evt_file, var.names = "EVT") # create a version of EVT with two-pixel minimum mapping unit sieveFilter(src_filename = evt_file, src_band = 1, dst_filename = evt_mmu_file, dst_band = 1, size_threshold = 2, connectedness = 8, mask_filename = mask_file, mask_band = 1)
## remove single-pixel polygons from the vegetation type layer (EVT) evt_file <- system.file("extdata/storml_evt.tif", package="gdalraster") # create a blank raster to hold the output evt_mmu_file <- file.path(tempdir(), "storml_evt_mmu2.tif") rasterFromRaster(srcfile = evt_file, dstfile = evt_mmu_file, init = 32767) # create a mask to exclude water pixels from the algorithm # recode water (7292) to 0 expr <- "ifelse(EVT == 7292, 0, EVT)" mask_file <- calc(expr = expr, rasterfiles = evt_file, var.names = "EVT") # create a version of EVT with two-pixel minimum mapping unit sieveFilter(src_filename = evt_file, src_band = 1, dst_filename = evt_mmu_file, dst_band = 1, size_threshold = 2, connectedness = 8, mask_filename = mask_file, mask_band = 1)
srs_find_epsg()
accepts a spatial reference system definition
in various text formats and tries to find a matching EPSG code.
See srs_to_wkt()
for a description of the possible input formats.
This function is an interface to OSRFindMatches()
in the GDAL Spatial
Reference System API.
srs_find_epsg(srs, all_matches = FALSE)
srs_find_epsg(srs, all_matches = FALSE)
srs |
Character string containing an SRS definition in various formats (e.g., WKT, PROJ.4 string, well known name such as NAD27, NAD83, WGS84, etc). |
all_matches |
Logical scalar. |
Matching may be partial, or may fail. Returned entries will be sorted by decreasing match confidence (first entry has the highest match confidence).
Character string or data frame, or NULL
if matching failed.
srs_find_epsg("WGS84") srs_find_epsg("WGS84", all_matches = TRUE)
srs_find_epsg("WGS84") srs_find_epsg("WGS84", all_matches = TRUE)
srs_get_name()
accepts a spatial reference system definition
in various text formats and returns the name.
See srs_to_wkt()
for a description of the possible input formats.
Wrapper of OSRGetName()
in the GDAL Spatial Reference System API.
srs_get_name(srs)
srs_get_name(srs)
srs |
Character string containing an SRS definition in various formats (e.g., WKT, PROJ.4 string, well known name such as NAD27, NAD83, WGS84, etc). |
Character string containing the name, or empty string.
epsg_to_wkt()
, srs_find_epsg()
, srs_to_wkt()
srs_get_name("EPSG:5070")
srs_get_name("EPSG:5070")
srs_is_geographic()
will attempt to import the given WKT string as a
spatial reference system, and returns TRUE
if the root is a
GEOGCS node. This is a wrapper for OSRIsGeographic()
in the GDAL Spatial
Reference System C API.
srs_is_geographic(srs)
srs_is_geographic(srs)
srs |
Character OGC WKT string for a spatial reference system |
Logical. TRUE
if srs
is geographic, otherwise FALSE
srs_is_projected()
, srs_is_same()
srs_is_geographic(epsg_to_wkt(5070)) srs_is_geographic(srs_to_wkt("WGS84"))
srs_is_geographic(epsg_to_wkt(5070)) srs_is_geographic(srs_to_wkt("WGS84"))
srs_is_projected()
will attempt to import the given WKT string as a
spatial reference system (SRS), and returns TRUE
if the SRS contains a
PROJCS node indicating a it is a projected coordinate system. This is a
wrapper for OSRIsProjected()
in the GDAL Spatial Reference System C API.
srs_is_projected(srs)
srs_is_projected(srs)
srs |
Character OGC WKT string for a spatial reference system |
Logical. TRUE
if srs
is projected, otherwise FALSE
srs_is_geographic()
, srs_is_same()
srs_is_projected(epsg_to_wkt(5070)) srs_is_projected(srs_to_wkt("WGS84"))
srs_is_projected(epsg_to_wkt(5070)) srs_is_projected(srs_to_wkt("WGS84"))
srs_is_same()
returns TRUE
if these two spatial references describe
the same system. This is a wrapper for OSRIsSame()
in the GDAL Spatial
Reference System C API.
srs_is_same( srs1, srs2, criterion = "", ignore_axis_mapping = FALSE, ignore_coord_epoch = FALSE )
srs_is_same( srs1, srs2, criterion = "", ignore_axis_mapping = FALSE, ignore_coord_epoch = FALSE )
srs1 |
Character string. OGC WKT for a spatial reference system. |
srs2 |
Character string. OGC WKT for a spatial reference system. |
criterion |
Character string. One of |
ignore_axis_mapping |
Logical scalar. If |
ignore_coord_epoch |
Logical scalar. If |
Logical. TRUE
if these two spatial references describe the same
system, otherwise FALSE
.
srs_is_geographic()
, srs_is_projected()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file, TRUE) srs_is_same(ds$getProjectionRef(), epsg_to_wkt(26912)) srs_is_same(ds$getProjectionRef(), epsg_to_wkt(5070)) ds$close()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") ds <- new(GDALRaster, elev_file, TRUE) srs_is_same(ds$getProjectionRef(), epsg_to_wkt(26912)) srs_is_same(ds$getProjectionRef(), epsg_to_wkt(5070)) ds$close()
srs_to_wkt()
converts a spatial reference system (SRS) definition
in various text formats to WKT. The function will examine the input SRS,
try to deduce the format, and then export it to WKT.
srs_to_wkt(srs, pretty = FALSE)
srs_to_wkt(srs, pretty = FALSE)
srs |
Character string containing an SRS definition in various formats (see Details). |
pretty |
Logical. |
This is a wrapper for OSRSetFromUserInput()
in the GDAL Spatial
Reference System C API with output to WKT.
The input SRS may take the following forms:
WKT - to convert WKT versions (see below)
EPSG:n - EPSG code n
AUTO:proj_id,unit_id,lon0,lat0 - WMS auto projections
urn:ogc:def:crs:EPSG::n - OGC URNs
PROJ.4 definitions
filename - file to read for WKT, XML or PROJ.4 definition
well known name such as NAD27, NAD83, WGS84 or WGS72
IGNF:xxxx, ESRI:xxxx - definitions from the PROJ database
PROJJSON (PROJ >= 6.2)
This function is intended to be flexible, but by its nature it is
imprecise as it must guess information about the format intended.
epsg_to_wkt()
could be used instead for EPSG codes.
As of GDAL 3.0, the default format for WKT export is OGC WKT 1.
The WKT version can be overridden by using the OSR_WKT_FORMAT
configuration option (see set_config_option()
).
Valid values are one of: SFSQL, WKT1_SIMPLE, WKT1, WKT1_GDAL,
WKT1_ESRI, WKT2_2015, WKT2_2018, WKT2, DEFAULT.
If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or
EXTENSION node is returned. If WKT1_SIMPLE, a WKT1 string without
AXIS, AUTHORITY or EXTENSION node is returned. WKT1 is an alias of
WKT1_GDAL. WKT2 will default to the latest revision implemented
(currently WKT2_2018). WKT2_2019 can be used as an alias of
WKT2_2018 since GDAL 3.2
Character string containing OGC WKT.
srs_to_wkt("NAD83") writeLines(srs_to_wkt("NAD83", pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "WKT2") writeLines(srs_to_wkt("NAD83", pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "")
srs_to_wkt("NAD83") writeLines(srs_to_wkt("NAD83", pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "WKT2") writeLines(srs_to_wkt("NAD83", pretty=TRUE)) set_config_option("OSR_WKT_FORMAT", "")
transform_xy()
transforms geospatial x/y coordinates to a new projection.
transform_xy(pts, srs_from, srs_to)
transform_xy(pts, srs_from, srs_to)
pts |
A two-column data frame or numeric matrix containing geospatial x/y coordinates. |
srs_from |
Character string specifying the spatial reference system
for |
srs_to |
Character string specifying the output spatial reference
system. May be in WKT format or any of the formats supported by
|
Numeric array of geospatial x/y coordinates in the projection
specified by srs_to
.
epsg_to_wkt()
, srs_to_wkt()
, inv_project()
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") pts <- read.csv(pt_file) print(pts) ## id, x, y in NAD83 / UTM zone 12N ## transform to NAD83 / CONUS Albers transform_xy(pts = pts[, -1], srs_from = "EPSG:26912", srs_to = "EPSG:5070")
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster") pts <- read.csv(pt_file) print(pts) ## id, x, y in NAD83 / UTM zone 12N ## transform to NAD83 / CONUS Albers transform_xy(pts = pts[, -1], srs_from = "EPSG:26912", srs_to = "EPSG:5070")
translate()
is a wrapper of the gdal_translate
command-line
utility (see https://gdal.org/programs/gdal_translate.html).
The function can be used to convert raster data between different
formats, potentially performing some operations like subsetting,
resampling, and rescaling pixels in the process. Refer to the GDAL
documentation at the URL above for a list of command-line arguments that
can be passed in cl_arg
.
translate(src_filename, dst_filename, cl_arg = NULL, quiet = FALSE)
translate(src_filename, dst_filename, cl_arg = NULL, quiet = FALSE)
src_filename |
Either a character string giving the filename of the
source raster, or an object of class |
dst_filename |
Character string. Filename of the output raster. |
cl_arg |
Optional character vector of command-line arguments for
|
quiet |
Logical scalar. If |
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
GDALRaster-class
, rasterFromRaster()
, warp()
ogr2ogr()
for vector data
# convert the elevation raster to Erdas Imagine format and resample to 90m elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") img_file <- file.path(tempdir(), "storml_elev_90m.img") # command-line arguments for gdal_translate args <- c("-tr", "90", "90", "-r", "average") args <- c(args, "-of", "HFA", "-co", "COMPRESSED=YES") translate(elev_file, img_file, args) ds <- new(GDALRaster, img_file) ds$info() ds$close()
# convert the elevation raster to Erdas Imagine format and resample to 90m elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") img_file <- file.path(tempdir(), "storml_elev_90m.img") # command-line arguments for gdal_translate args <- c("-tr", "90", "90", "-r", "average") args <- c(args, "-of", "HFA", "-co", "COMPRESSED=YES") translate(elev_file, img_file, args) ds <- new(GDALRaster, img_file) ds$info() ds$close()
vsi_clear_path_options()
clears path specific options previously set
with vsi_set_path_option()
.
Wrapper for VSIClearPathSpecificOptions()
in the GDAL Common Portability
Library. Requires GDAL >= 3.6.
vsi_clear_path_options(path_prefix)
vsi_clear_path_options(path_prefix)
path_prefix |
Character string. If set to |
No return value, called for side effect.
No particular care is taken to remove options from RAM in a secure way.
These are package global constants for convenience in calling
VSIFile$seek()
.
SEEK_SET SEEK_CUR SEEK_END
SEEK_SET SEEK_CUR SEEK_END
An object of class character
of length 1.
An object of class character
of length 1.
An object of class character
of length 1.
vsi_copy_file()
is a wrapper for VSICopyFile()
in the GDAL Common
Portability Library. The GDAL VSI functions allow virtualization of disk
I/O so that non file data sources can be made to appear as files.
See https://gdal.org/user/virtual_file_systems.html.
Requires GDAL >= 3.7.
vsi_copy_file(src_file, target_file, show_progress = FALSE)
vsi_copy_file(src_file, target_file, show_progress = FALSE)
src_file |
Character string. Filename of the source file. |
target_file |
Character string. Filename of the target file. |
show_progress |
Logical scalar. If |
The following copies are made fully on the target server, without local download from source and upload to target:
/vsis3/ -> /vsis3/
/vsigs/ -> /vsigs/
/vsiaz/ -> /vsiaz/
/vsiadls/ -> /vsiadls/
any of the above or /vsicurl/ -> /vsiaz/ (starting with GDAL 3.8)
0
on success or -1
on an error.
If target_file
has the form /vsizip/foo.zip/bar, the default options
described for the function addFilesInZip()
will be in effect.
copyDatasetFiles()
, vsi_stat()
, vsi_sync()
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- "/vsimem/elev_temp.tif" # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { result <- vsi_copy_file(elev_file, tmp_file) print(result) print(vsi_stat(tmp_file, "size")) vsi_unlink(tmp_file) }
elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- "/vsimem/elev_temp.tif" # Requires GDAL >= 3.7 if (as.integer(gdal_version()[2]) >= 3070000) { result <- vsi_copy_file(elev_file, tmp_file) print(result) print(vsi_stat(tmp_file, "size")) vsi_unlink(tmp_file) }
vsi_curl_clear_cache()
cleans the local cache associated with /vsicurl/
(and related file systems). This function is a wrapper for
VSICurlClearCache()
and VSICurlPartialClearCache()
in the GDAL Common
Portability Library. See Details for the GDAL documentation.
vsi_curl_clear_cache(partial = FALSE, file_prefix = "", quiet = TRUE)
vsi_curl_clear_cache(partial = FALSE, file_prefix = "", quiet = TRUE)
partial |
Logical scalar. Whether to clear the cache only for a given filename (see Details). |
file_prefix |
Character string. Filename prefix to use if
|
quiet |
Logical scalar. |
/vsicurl/ (and related file systems like /vsis3/, /vsigs/, /vsiaz/,
/vsioss/, /vsiswift/) cache a number of metadata and data for faster
execution in read-only scenarios. But when the content on the server-side
may change during the same process, those mechanisms can prevent opening
new files, or give an outdated version of them.
If partial = TRUE
, cleans the local cache associated for a given filename
(and its subfiles and subdirectories if it is a directory).
No return value, called for side effects.
vsi_curl_clear_cache()
vsi_curl_clear_cache()
vsi_get_actual_url()
returns the actual URL of a supplied filename.
Currently only returns a non-NULL value for network-based virtual file
systems. For example "/vsis3/bucket/filename" will be expanded as
"https://bucket.s3.amazon.com/filename".
Wrapper for VSIGetActualURL()
in the GDAL API.
vsi_get_actual_url(filename)
vsi_get_actual_url(filename)
filename |
Character string containing a /vsiPREFIX/ filename. |
Character string containing the actual URL, or NULL
if
filename
is not a network-based virtual file system.
## Not run: f <- "/vsiaz/items/io-lulc-9-class.parquet" set_config_option("AZURE_STORAGE_ACCOUNT", "pcstacitems") # token obtained from: # https://planetarycomputer.microsoft.com/api/sas/v1/token/pcstacitems/items set_config_option("AZURE_STORAGE_SAS_TOKEN","<token>") vsi_get_actual_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet" vsi_get_signed_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet?<token>" ## End(Not run)
## Not run: f <- "/vsiaz/items/io-lulc-9-class.parquet" set_config_option("AZURE_STORAGE_ACCOUNT", "pcstacitems") # token obtained from: # https://planetarycomputer.microsoft.com/api/sas/v1/token/pcstacitems/items set_config_option("AZURE_STORAGE_SAS_TOKEN","<token>") vsi_get_actual_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet" vsi_get_signed_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet?<token>" ## End(Not run)
vsi_get_disk_free_space()
returns the free disk space available on the
filesystem. Wrapper for VSIGetDiskFreeSpace()
in the GDAL Common
Portability Library.
vsi_get_disk_free_space(path)
vsi_get_disk_free_space(path)
path |
Character string. A directory of the filesystem to query. |
Numeric scalar. The free space in bytes (as bit64::integer64
type), or -1
in case of error.
tmp_dir <- file.path("/vsimem", "tmpdir") vsi_mkdir(tmp_dir) vsi_get_disk_free_space(tmp_dir) vsi_rmdir(tmp_dir)
tmp_dir <- file.path("/vsimem", "tmpdir") vsi_mkdir(tmp_dir) vsi_get_disk_free_space(tmp_dir) vsi_rmdir(tmp_dir)
vsi_get_file_metadata()
returns metadata for file system objects.
Implemented for network-like filesystems. Starting with GDAL 3.7,
implemented for /vsizip/ with SOZip metadata.
Wrapper of VSIGetFileMetadata()
in the GDAL Common Portability Library.
vsi_get_file_metadata(filename, domain)
vsi_get_file_metadata(filename, domain)
filename |
Character string. The path of the file system object to be queried. |
domain |
Character string. Metadata domain to query. Depends on the file system, see Details. |
The metadata available depends on the file system. The following are supported as of GDAL 3.9:
HEADERS: to get HTTP headers for network-like filesystems (/vsicurl/, /vsis3/, /vsgis/, etc).
TAGS: for /vsis3/, to get S3 Object tagging information. For /vsiaz/, to get blob tags.
STATUS: specific to /vsiadls/: returns all system-defined properties for a path (seems in practice to be a subset of HEADERS).
ACL: specific to /vsiadls/ and /vsigs/: returns the access control list
for a path. For /vsigs/, a single XML=xml_content
string is returned.
METADATA: specific to /vsiaz/: blob metadata (this will be a subset of
what domain=HEADERS
returns).
ZIP: specific to /vsizip/: to obtain ZIP specific metadata, in
particular if a file is SOZIP-enabled (SOZIP_VALID=YES
).
A named list of values, or NULL
in case of error or empty list.
# create an SOZip-enabled file and validate # Requires GDAL >= 3.7 f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") if (as.integer(gdal_version()[2]) >= 3070000) { zip_file <- tempfile(fileext=".zip") addFilesInZip(zip_file, f, full_paths=FALSE, sozip_enabled="YES") zip_vsi <- file.path("/vsizip", zip_file) print("Files in zip archive:") print(vsi_read_dir(zip_vsi)) print("SOZip metadata:") print(vsi_get_file_metadata(zip_vsi, domain="ZIP")) vsi_unlink(zip_file) }
# create an SOZip-enabled file and validate # Requires GDAL >= 3.7 f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster") if (as.integer(gdal_version()[2]) >= 3070000) { zip_file <- tempfile(fileext=".zip") addFilesInZip(zip_file, f, full_paths=FALSE, sozip_enabled="YES") zip_vsi <- file.path("/vsizip", zip_file) print("Files in zip archive:") print(vsi_read_dir(zip_vsi)) print("SOZip metadata:") print(vsi_get_file_metadata(zip_vsi, domain="ZIP")) vsi_unlink(zip_file) }
vsi_get_fs_options()
returns the list of options associated with a virtual
file system handler. Those options may be set as configuration options with
set_config_option()
.
Wrapper for VSIGetFileSystemOptions()
in the GDAL API.
vsi_get_fs_options(filename, as_list = TRUE)
vsi_get_fs_options(filename, as_list = TRUE)
filename |
Filename, or prefix of a virtual file system handler. |
as_list |
Logical scalar. If |
An XML string, or empty string (""
) if no options are declared.
If as_list = TRUE
(the default), the XML string will be coerced to list
with xml2::as_list()
.
set_config_option()
, vsi_get_fs_prefixes()
https://gdal.org/user/virtual_file_systems.html
vsi_get_fs_options("/vsimem/") vsi_get_fs_options("/vsizip/") vsi_get_fs_options("/vsizip/", as_list = FALSE)
vsi_get_fs_options("/vsimem/") vsi_get_fs_options("/vsizip/") vsi_get_fs_options("/vsizip/", as_list = FALSE)
vsi_get_fs_prefixes()
returns the list of prefixes for virtual file
system handlers currently registered (e.g., "/vsimem/"
, "/vsicurl/"
,
etc). Wrapper for VSIGetFileSystemsPrefixes()
in the GDAL API.
vsi_get_fs_prefixes()
vsi_get_fs_prefixes()
Character vector containing prefixes of the virtual file system handlers.
https://gdal.org/user/virtual_file_systems.html
vsi_get_fs_prefixes()
vsi_get_fs_prefixes()
vsi_get_signed_url()
Returns a signed URL of a supplied filename.
Currently only returns a non-NULL value for /vsis3/, /vsigs/, /vsiaz/ and
/vsioss/ For example "/vsis3/bucket/filename" will be expanded as
"https://bucket.s3.amazon.com/filename?X-Amz-Algorithm=AWS4-HMAC-SHA256..."
Configuration options that apply for file opening (typically to provide
credentials), and are returned by vsi_get_fs_options()
, are also valid
in that context.
Wrapper for VSIGetSignedURL()
in the GDAL API.
vsi_get_signed_url(filename, options = NULL)
vsi_get_signed_url(filename, options = NULL)
filename |
Character string containing a /vsiPREFIX/ filename. |
options |
Character vector of |
The options
argument accepts a character vector of name=value pairs.
For /vsis3/, /vsigs/, /vsiaz/ and /vsioss/, the following options are
supported:
START_DATE=YYMMDDTHHMMSSZ
: date and time in UTC following ISO 8601
standard, corresponding to the start of validity of the URL. If not
specified, current date time.
EXPIRATION_DELAY=number_of_seconds
: number between 1 and 604800 (seven
days) for the validity of the signed URL. Defaults to 3600 (one hour).
VERB=GET/HEAD/DELETE/PUT/POST
: HTTP VERB for which the request will be
used. Defaults to GET
.
/vsiaz/ supports additional options:
SIGNEDIDENTIFIER=value
: to relate the given shared access signature to
a corresponding stored access policy.
SIGNEDPERMISSIONS=r|w
: permissions associated with the shared access
signature. Normally deduced from VERB
.
Character string containing the signed URL, or NULL
if
filename
is not a network-based virtual file system.
## Not run: f <- "/vsiaz/items/io-lulc-9-class.parquet" set_config_option("AZURE_STORAGE_ACCOUNT", "pcstacitems") # token obtained from: # https://planetarycomputer.microsoft.com/api/sas/v1/token/pcstacitems/items set_config_option("AZURE_STORAGE_SAS_TOKEN", "<token>") vsi_get_actual_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet" vsi_get_signed_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet?<token>" ## End(Not run)
## Not run: f <- "/vsiaz/items/io-lulc-9-class.parquet" set_config_option("AZURE_STORAGE_ACCOUNT", "pcstacitems") # token obtained from: # https://planetarycomputer.microsoft.com/api/sas/v1/token/pcstacitems/items set_config_option("AZURE_STORAGE_SAS_TOKEN", "<token>") vsi_get_actual_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet" vsi_get_signed_url(f) #> [1] "https://pcstacitems.blob.core.windows.net/items/io-lulc-9-class.parquet?<token>" ## End(Not run)
vsi_mkdir()
creates a new directory with the indicated mode.
For POSIX-style systems, the mode is modified by the file creation mask
(umask). However, some file systems and platforms may not use umask, or
they may ignore the mode completely. So a reasonable cross-platform
default mode value is 0755
.
With recursive = TRUE
, creates a directory and all its ancestors.
This function is a wrapper for VSIMkdir()
and VSIMkdirRecursive()
in
the GDAL Common Portability Library.
vsi_mkdir(path, mode = "0755", recursive = FALSE)
vsi_mkdir(path, mode = "0755", recursive = FALSE)
path |
Character string. The path to the directory to create. |
mode |
Character string. The permissions mode in octal with prefix
|
recursive |
Logical scalar. |
0
on success or -1
on an error.
new_dir <- file.path(tempdir(), "newdir") vsi_mkdir(new_dir) vsi_stat(new_dir, "type") vsi_rmdir(new_dir)
new_dir <- file.path(tempdir(), "newdir") vsi_mkdir(new_dir) vsi_stat(new_dir, "type") vsi_rmdir(new_dir)
vsi_read_dir()
abstracts access to directory contents. It returns a
character vector containing the names of files and directories in this
directory. With recursive = TRUE
, reads the list of entries in the
directory and subdirectories.
This function is a wrapper for VSIReadDirEx()
and VSIReadDirRecursive()
in the GDAL Common Portability Library.
vsi_read_dir(path, max_files = 0L, recursive = FALSE, all_files = FALSE)
vsi_read_dir(path, max_files = 0L, recursive = FALSE, all_files = FALSE)
path |
Character string. The relative or absolute path of a directory to read. |
max_files |
Integer scalar. The maximum number of files after which to
stop, or 0 for no limit (see Note). Ignored if |
recursive |
Logical scalar. |
all_files |
Logical scalar. If ‘FALSE’ (the default), only the names of visible files are returned (following Unix-style visibility, that is files whose name does not start with a dot). If ‘TRUE’, all file names will be returned. |
A character vector containing the names of files and directories
in the directory given by path
. The listing is in alphabetical order, and
does not include the special entries '.' and '..' even if they are present
in the directory. An empty string (""
) is returned if path
does not
exist.
If max_files
is set to a positive number, directory listing will stop
after that limit has been reached. Note that to indicate truncation, at
least one element more than the max_files
limit will be returned. If the
length of the returned character vector is lesser or equal to max_files
,
then no truncation occurred. The max_files
parameter is ignored when
recursive = TRUE
.
vsi_mkdir()
, vsi_rmdir()
, vsi_stat()
, vsi_sync()
# regular file system for illustration data_dir <- system.file("extdata", package="gdalraster") vsi_read_dir(data_dir)
# regular file system for illustration data_dir <- system.file("extdata", package="gdalraster") vsi_read_dir(data_dir)
vsi_rename()
renames a file object in the file system. The GDAL
documentation states it should be possible to rename a file onto a new
filesystem, but it is safest if this function is only used to rename files
that remain in the same directory.
This function goes through the GDAL VSIFileHandler
virtualization and may
work on unusual filesystems such as in memory.
It is a wrapper for VSIRename()
in the GDAL Common Portability Library.
Analog of the POSIX rename()
function.
vsi_rename(oldpath, newpath)
vsi_rename(oldpath, newpath)
oldpath |
Character string. The name of the file to be renamed. |
newpath |
Character string. The name the file should be given. |
0
on success or -1
on an error.
renameDataset()
, vsi_copy_file()
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- tempfile(fileext = ".tif") file.copy(elev_file, tmp_file) new_file <- file.path(dirname(tmp_file), "storml_elev_copy.tif") vsi_rename(tmp_file, new_file) vsi_stat(new_file) vsi_unlink(new_file)
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- tempfile(fileext = ".tif") file.copy(elev_file, tmp_file) new_file <- file.path(dirname(tmp_file), "storml_elev_copy.tif") vsi_rename(tmp_file, new_file) vsi_stat(new_file) vsi_unlink(new_file)
vsi_rmdir()
deletes a directory object from the file system. On some
systems the directory must be empty before it can be deleted.
With recursive = TRUE
, deletes a directory object and its content from
the file system.
This function goes through the GDAL VSIFileHandler
virtualization and may
work on unusual filesystems such as in memory.
It is a wrapper for VSIRmdir()
and VSIRmdirRecursive()
in the GDAL
Common Portability Library.
vsi_rmdir(path, recursive = FALSE)
vsi_rmdir(path, recursive = FALSE)
path |
Character string. The path to the directory to be deleted. |
recursive |
Logical scalar. |
0
on success or -1
on an error.
/vsis3/ has an efficient implementation for deleting recursively. Starting with GDAL 3.4, /vsigs/ has an efficient implementation for deleting recursively, provided that OAuth2 authentication is used.
deleteDataset()
, vsi_mkdir()
, vsi_read_dir()
, vsi_unlink()
new_dir <- file.path(tempdir(), "newdir") vsi_mkdir(new_dir) vsi_rmdir(new_dir)
new_dir <- file.path(tempdir(), "newdir") vsi_mkdir(new_dir) vsi_rmdir(new_dir)
vsi_set_path_option()
sets a path specific option for a given path
prefix. Such an option is typically, but not limited to, setting
credentials for a virtual file system.
Wrapper for VSISetPathSpecificOption()
in the GDAL Common Portability
Library. Requires GDAL >= 3.6.
vsi_set_path_option(path_prefix, key, value)
vsi_set_path_option(path_prefix, key, value)
path_prefix |
Character string. A path prefix of a virtual file system
handler. Typically of the form |
key |
Character string. Option key. |
value |
Character string. Option value. Passing |
Options may also be set with set_config_option()
, but
vsi_set_path_option()
allows specifying them with a granularity at the
level of a file path. This makes it easier if using the same virtual file
system but with different credentials (e.g., different credentials for
buckets "/vsis3/foo" and "/vsis3/bar"). This is supported for the following
virtual file systems: /vsis3/, /vsigs/, /vsiaz/, /vsioss/, /vsiwebhdfs,
/vsiswift.
No return value, called for side effect.
Setting options for a path starting with /vsiXXX/ will also apply for /vsiXXX_streaming/ requests.
No particular care is taken to store options in RAM in a secure way. So they might accidentally hit persistent storage if swapping occurs, or someone with access to the memory allocated by the process may be able to read them.
set_config_option()
, vsi_clear_path_options()
vsi_stat()
fetches status information about a filesystem object (file,
directory, etc).
This function goes through the GDAL VSIFileHandler
virtualization and may
work on unusual filesystems such as in memory.
It is a wrapper for VSIStatExL()
in the GDAL Common Portability Library.
Analog of the POSIX stat()
function.
vsi_stat(filename, info = "exists")
vsi_stat(filename, info = "exists")
filename |
Character string. The path of the filesystem object to be queried. |
info |
Character string. The type of information to fetch, one of
|
If info = "exists"
, returns logical TRUE
if the file system
object exists, otherwise FALSE
. If info = "type"
, returns a character
string with one of "file"
(regular file), "dir"
(directory),
"symlink"
(symbolic link), or empty string (""
). If info = "size"
,
returns the file size in bytes (as bit64::integer64
type), or -1
if an
error occurs.
For portability, vsi_stat()
supports a subset of stat()
-type
information for filesystem objects. This function is primarily intended
for use with GDAL virtual file systems (e.g., URLs, cloud storage systems,
ZIP/GZip/7z/RAR archives, in-memory files).
The base R function utils::file_test()
could be used instead for file
tests on regular local filesystems.
GDAL Virtual File Systems:
https://gdal.org/user/virtual_file_systems.html
data_dir <- system.file("extdata", package="gdalraster") vsi_stat(data_dir) vsi_stat(data_dir, "type") # stat() on a directory doesn't return the sum of the file sizes in it, # but rather how much space is used by the directory entry vsi_stat(data_dir, "size") elev_file <- file.path(data_dir, "storml_elev.tif") vsi_stat(elev_file) vsi_stat(elev_file, "type") vsi_stat(elev_file, "size") nonexistent <- file.path(data_dir, "nonexistent.tif") vsi_stat(nonexistent) vsi_stat(nonexistent, "type") vsi_stat(nonexistent, "size") # /vsicurl/ file system handler base_url <- "https://raw.githubusercontent.com/usdaforestservice/" f <- "gdalraster/main/sample-data/landsat_c2ard_sr_mt_hood_jul2022_utm.tif" url_file <- paste0("/vsicurl/", base_url, f) vsi_stat(url_file) vsi_stat(url_file, "type") vsi_stat(url_file, "size")
data_dir <- system.file("extdata", package="gdalraster") vsi_stat(data_dir) vsi_stat(data_dir, "type") # stat() on a directory doesn't return the sum of the file sizes in it, # but rather how much space is used by the directory entry vsi_stat(data_dir, "size") elev_file <- file.path(data_dir, "storml_elev.tif") vsi_stat(elev_file) vsi_stat(elev_file, "type") vsi_stat(elev_file, "size") nonexistent <- file.path(data_dir, "nonexistent.tif") vsi_stat(nonexistent) vsi_stat(nonexistent, "type") vsi_stat(nonexistent, "size") # /vsicurl/ file system handler base_url <- "https://raw.githubusercontent.com/usdaforestservice/" f <- "gdalraster/main/sample-data/landsat_c2ard_sr_mt_hood_jul2022_utm.tif" url_file <- paste0("/vsicurl/", base_url, f) vsi_stat(url_file) vsi_stat(url_file, "type") vsi_stat(url_file, "size")
vsi_supports_rnd_write()
returns whether the filesystem supports
random write.
Wrapper for VSISupportsRandomWrite()
in the GDAL API.
vsi_supports_rnd_write(filename, allow_local_tmpfile)
vsi_supports_rnd_write(filename, allow_local_tmpfile)
filename |
Character string. The path of the filesystem object to be tested. |
allow_local_tmpfile |
Logical scalar. |
Logical scalar. TRUE
if random write is supported.
The location GDAL uses for temporary files can be forced via the
CPL_TMPDIR
configuration option.
# Requires GDAL >= 3.6 if (as.integer(gdal_version()[2]) >= 3060000) vsi_supports_rnd_write("/vsimem/test-mem-file.gpkg", TRUE)
# Requires GDAL >= 3.6 if (as.integer(gdal_version()[2]) >= 3060000) vsi_supports_rnd_write("/vsimem/test-mem-file.gpkg", TRUE)
vsi_supports_seq_write()
returns whether the filesystem supports
sequential write.
Wrapper for VSISupportsSequentialWrite()
in the GDAL API.
vsi_supports_seq_write(filename, allow_local_tmpfile)
vsi_supports_seq_write(filename, allow_local_tmpfile)
filename |
Character string. The path of the filesystem object to be tested. |
allow_local_tmpfile |
Logical scalar. |
Logical scalar. TRUE
if sequential write is supported.
The location GDAL uses for temporary files can be forced via the
CPL_TMPDIR
configuration option.
# Requires GDAL >= 3.6 if (as.integer(gdal_version()[2]) >= 3060000) vsi_supports_seq_write("/vsimem/test-mem-file.gpkg", TRUE)
# Requires GDAL >= 3.6 if (as.integer(gdal_version()[2]) >= 3060000) vsi_supports_seq_write("/vsimem/test-mem-file.gpkg", TRUE)
vsi_sync()
is a wrapper for VSISync()
in the GDAL Common Portability
Library. The GDAL documentation is given in Details.
vsi_sync(src, target, show_progress = FALSE, options = NULL)
vsi_sync(src, target, show_progress = FALSE, options = NULL)
src |
Character string. Source file or directory. |
target |
Character string. Target file or directory. |
show_progress |
Logical scalar. If |
options |
Character vector of |
VSISync()
is an analog of the Linux rsync
utility. In the current
implementation, rsync
would be more efficient for local file copying,
but VSISync()
main interest is when the source or target is a remote
file system like /vsis3/ or /vsigs/, in which case it can take into account
the timestamps of the files (or optionally the ETag/MD5Sum) to avoid
unneeded copy operations.
This is only implemented efficiently for:
local filesystem <–> remote filesystem
remote filesystem <–> remote filesystem (starting with GDAL 3.1)
Where the source and target remote filesystems are the same and one of
/vsis3/, /vsigs/ or /vsiaz/. Or when the target is /vsiaz/ and the source
is /vsis3/, /vsigs/, /vsiadls/ or /vsicurl/ (starting with GDAL 3.8)
Similarly to rsync
behavior, if the source filename ends with a slash, it
means that the content of the directory must be copied, but not the
directory name. For example, assuming "/home/even/foo" contains a file
"bar", VSISync("/home/even/foo/", "/mnt/media", ...)
will create a
"/mnt/media/bar" file.
Whereas VSISync("/home/even/foo", "/mnt/media", ...)
will create a
"/mnt/media/foo" directory which contains a bar file.
The options
argument accepts a character vector of name=value pairs.
Currently accepted options are:
RECURSIVE=NO
(the default is YES
)
SYNC_STRATEGY=TIMESTAMP/ETAG/OVERWRITE
. Determines which criterion is
used to determine if a target file must be replaced when it already exists
and has the same file size as the source. Only applies for a source or
target being a network filesystem.
The default is TIMESTAMP
(similarly to how 'aws s3 sync' works), that is
to say that for an upload operation, a remote file is replaced if it has a
different size or if it is older than the source. For a download operation,
a local file is replaced if it has a different size or if it is newer than
the remote file.
The ETAG
strategy assumes that the ETag metadata of the remote file is
the MD5Sum of the file content, which is only true in the case of /vsis3/
for files not using KMS server side encryption and uploaded in a single PUT
operation (so smaller than 50 MB given the default used by GDAL). Only to
be used for /vsis3/, /vsigs/ or other filesystems using a MD5Sum as ETAG.
The OVERWRITE
strategy (GDAL >= 3.2) will always overwrite the target
file with the source one.
NUM_THREADS=integer
. Number of threads to use for parallel file
copying. Only use for when /vsis3/, /vsigs/, /vsiaz/ or /vsiadls/ is
in source or target. The default is 10 since GDAL 3.3.
CHUNK_SIZE=integer
. Maximum size of chunk (in bytes) to use to split
large objects when downloading them from /vsis3/, /vsigs/, /vsiaz/
or /vsiadls/ to local file system, or for upload to /vsis3/, /vsiaz/ or
/vsiadls/ from local file system. Only used if NUM_THREADS > 1
.
For upload to /vsis3/, this chunk size must be set at least to 5 MB. The
default is 8 MB since GDAL 3.3.
x-amz-KEY=value
. (GDAL >= 3.5) MIME header to pass during creation of a
/vsis3/ object.
x-goog-KEY=value
. (GDAL >= 3.5) MIME header to pass during creation of a
/vsigs/ object.
x-ms-KEY=value
. (GDAL >= 3.5) MIME header to pass during creation of a
/vsiaz/ or /vsiadls/ object.
Logical scalar, TRUE
on success or FALSE
on an error.
copyDatasetFiles()
, vsi_copy_file()
## Not run: # sample-data is a directory in the git repository for gdalraster that is # not included in the R package: # https://github.com/USDAForestService/gdalraster/tree/main/sample-data # A copy of sample-data in an AWS S3 bucket, and a partial copy in an # Azure Blob container, were used to generate the example below. src <- "/vsis3/gdalraster-sample-data/" # s3://gdalraster-sample-data is not public, set credentials set_config_option("AWS_ACCESS_KEY_ID", "xxxxxxxxxxxxxx") set_config_option("AWS_SECRET_ACCESS_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx") vsi_read_dir(src) #> [1] "README.md" #> [2] "bl_mrbl_ng_jul2004_rgb_720x360.tif" #> [3] "blue_marble_ng_neo_metadata.xml" #> [4] "landsat_c2ard_sr_mt_hood_jul2022_utm.json" #> [5] "landsat_c2ard_sr_mt_hood_jul2022_utm.tif" #> [6] "lf_elev_220_metadata.html" #> [7] "lf_elev_220_mt_hood_utm.tif" #> [8] "lf_fbfm40_220_metadata.html" #> [9] "lf_fbfm40_220_mt_hood_utm.tif" dst <- "/vsiaz/sampledata" set_config_option("AZURE_STORAGE_CONNECTION_STRING", "<connection_string_for_gdalraster_account>") vsi_read_dir(dst) #> [1] "lf_elev_220_metadata.html" "lf_elev_220_mt_hood_utm.tif" # GDAL VSISync() supports direct copy for /vsis3/ -> /vsiaz/ (GDAL >= 3.8) result <- vsi_sync(src, dst, show_progress = TRUE) #> 0...10...20...30...40...50...60...70...80...90...100 - done. print(result) #> [1] TRUE vsi_read_dir(dst) #> [1] "README.md" #> [2] "bl_mrbl_ng_jul2004_rgb_720x360.tif" #> [3] "blue_marble_ng_neo_metadata.xml" #> [4] "landsat_c2ard_sr_mt_hood_jul2022_utm.json" #> [5] "landsat_c2ard_sr_mt_hood_jul2022_utm.tif" #> [6] "lf_elev_220_metadata.html" #> [7] "lf_elev_220_mt_hood_utm.tif" #> [8] "lf_fbfm40_220_metadata.html" #> [9] "lf_fbfm40_220_mt_hood_utm.tif" ## End(Not run)
## Not run: # sample-data is a directory in the git repository for gdalraster that is # not included in the R package: # https://github.com/USDAForestService/gdalraster/tree/main/sample-data # A copy of sample-data in an AWS S3 bucket, and a partial copy in an # Azure Blob container, were used to generate the example below. src <- "/vsis3/gdalraster-sample-data/" # s3://gdalraster-sample-data is not public, set credentials set_config_option("AWS_ACCESS_KEY_ID", "xxxxxxxxxxxxxx") set_config_option("AWS_SECRET_ACCESS_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx") vsi_read_dir(src) #> [1] "README.md" #> [2] "bl_mrbl_ng_jul2004_rgb_720x360.tif" #> [3] "blue_marble_ng_neo_metadata.xml" #> [4] "landsat_c2ard_sr_mt_hood_jul2022_utm.json" #> [5] "landsat_c2ard_sr_mt_hood_jul2022_utm.tif" #> [6] "lf_elev_220_metadata.html" #> [7] "lf_elev_220_mt_hood_utm.tif" #> [8] "lf_fbfm40_220_metadata.html" #> [9] "lf_fbfm40_220_mt_hood_utm.tif" dst <- "/vsiaz/sampledata" set_config_option("AZURE_STORAGE_CONNECTION_STRING", "<connection_string_for_gdalraster_account>") vsi_read_dir(dst) #> [1] "lf_elev_220_metadata.html" "lf_elev_220_mt_hood_utm.tif" # GDAL VSISync() supports direct copy for /vsis3/ -> /vsiaz/ (GDAL >= 3.8) result <- vsi_sync(src, dst, show_progress = TRUE) #> 0...10...20...30...40...50...60...70...80...90...100 - done. print(result) #> [1] TRUE vsi_read_dir(dst) #> [1] "README.md" #> [2] "bl_mrbl_ng_jul2004_rgb_720x360.tif" #> [3] "blue_marble_ng_neo_metadata.xml" #> [4] "landsat_c2ard_sr_mt_hood_jul2022_utm.json" #> [5] "landsat_c2ard_sr_mt_hood_jul2022_utm.tif" #> [6] "lf_elev_220_metadata.html" #> [7] "lf_elev_220_mt_hood_utm.tif" #> [8] "lf_fbfm40_220_metadata.html" #> [9] "lf_fbfm40_220_mt_hood_utm.tif" ## End(Not run)
vsi_unlink()
deletes a file object from the file system.
This function goes through the GDAL VSIFileHandler
virtualization and may
work on unusual filesystems such as in memory.
It is a wrapper for VSIUnlink()
in the GDAL Common Portability Library.
Analog of the POSIX unlink()
function.
vsi_unlink(filename)
vsi_unlink(filename)
filename |
Character string. The path of the file to be deleted. |
0
on success or -1
on an error.
deleteDataset()
, vsi_rmdir()
, vsi_unlink_batch()
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- file.path(tempdir(), "tmp.tif") file.copy(elev_file, tmp_file) vsi_stat(tmp_file) vsi_unlink(tmp_file) vsi_stat(tmp_file)
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tmp_file <- file.path(tempdir(), "tmp.tif") file.copy(elev_file, tmp_file) vsi_stat(tmp_file) vsi_unlink(tmp_file) vsi_stat(tmp_file)
vsi_unlink_batch()
deletes a list of files passed in a character vector.
All files should belong to the same file system handler.
This is implemented efficiently for /vsis3/ and /vsigs/ (provided for
/vsigs/ that OAuth2 authentication is used).
This function is a wrapper for VSIUnlinkBatch()
in the GDAL Common
Portability Library.
vsi_unlink_batch(filenames)
vsi_unlink_batch(filenames)
filenames |
Character vector. The list of files to delete. |
Logical vector of length(filenames)
with values depending
on the success of deletion of the corresponding file.
NULL
might be returned in case of a more general error (for example,
files belonging to different file system handlers).
deleteDataset()
, vsi_rmdir()
, vsi_unlink()
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tcc_file <- system.file("extdata/storml_tcc.tif", package="gdalraster") tmp_elev <- file.path(tempdir(), "tmp_elev.tif") file.copy(elev_file, tmp_elev) tmp_tcc <- file.path(tempdir(), "tmp_tcc.tif") file.copy(tcc_file, tmp_tcc) vsi_unlink_batch(c(tmp_elev, tmp_tcc))
# regular file system for illustration elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") tcc_file <- system.file("extdata/storml_tcc.tif", package="gdalraster") tmp_elev <- file.path(tempdir(), "tmp_elev.tif") file.copy(elev_file, tmp_elev) tmp_tcc <- file.path(tempdir(), "tmp_tcc.tif") file.copy(tcc_file, tmp_tcc) vsi_unlink_batch(c(tmp_elev, tmp_tcc))
VSIFile
provides bindings to the GDAL VSIVirtualHandle API. Encapsulates a
VSIVirtualHandle
(https://gdal.org/api/cpl_cpp.html#_CPPv416VSIVirtualHandle).
This API abstracts binary file I/O across "regular" file systems, URLs,
cloud storage services, Zip/GZip/7z/RAR, and in-memory files.
It provides analogs of several Standard C file I/O functions, allowing
virtualization of disk I/O so that non-file data sources can be made to
appear as files.
filename |
Character string containing the filename to open. It may be a file in a regular local filesystem, or a filename with a GDAL /vsiPREFIX/ (see https://gdal.org/user/virtual_file_systems.html). |
|||||||||||||||
access |
Character string containing the access requested (i.e.,
|
|||||||||||||||
options |
Optional character vector of |
An object of class VSIFile
which contains a pointer to a
VSIVirtualHandle
, and methods that operate on the file as described in
Details. VSIFile
is a C++ class exposed directly to R (via
RCPP_EXPOSED_CLASS
). Methods of the class are accessed using the
$
operator.
## Constructors vf <- new(VSIFile, filename) # specifying access: vf <- new(VSIFile, filename, access) # specifying access and options (both required): vf <- new(VSIFile, filename, access, options) ## Methods vf$seek(offset, origin) vf$tell() vf$rewind() vf$read(nbytes) vf$write(object) vf$eof() vf$truncate(new_size) vf$flush() vf$ingest(max_size) vf$close() vf$open() vf$get_filename() vf$get_access() vf$set_access(access)
new(VSIFile, filename)
Returns an object of class VSIFile
, or an error is raised if a file
handle cannot be obtained.
new(VSIFile, filename, access)
Alternate constructor for passing access
as a character string
(e.g., "r"
, "r+"
, "w"
, "w+"
).
Returns an object of class VSIFile
with an open file handle, or an error
is raised if a file handle cannot be obtained.
new(VSIFile, filename, access, options)
Alternate constructor for passing access
as a character string, and
options
as a character vector of "NAME=VALUE" pairs (all arguments
required, GDAL >= 3.3 required for options
support).
The options
argument is highly file system dependent. Supported options
as of GDAL 3.9 include:
MIME headers such as Content-Type and Content-Encoding are supported for the /vsis3/, /vsigs/, /vsiaz/, /vsiadls/ file systems.
DISABLE_READDIR_ON_OPEN=YES/NO (GDAL >= 3.6) for /vsicurl/ and other network-based file systems. By default, directory file listing is done, unless YES is specified.
WRITE_THROUGH=YES (GDAL >= 3.8) for Windows regular files to set the
FILE_FLAG_WRITE_THROUGH flag to the CreateFile()
function. In that mode,
the data are written to the system cache but are flushed to disk without
delay.
$seek(offset, origin)
Seek to a requested offset
in the file.
offset
is given as a positive numeric scalar, optionally as
bit64::integer64
type.
origin
is given as a character string, one of SEEK_SET
, SEEK_CUR
or
SEEK_END
. Package global constants are defined for convenience, so these
can be passed unquoted. Note that offset
is an unsigned type, so SEEK_CUR
can only be used for positive seek. If negative seek is needed, use:
vf$seek(vf$tell() + negative_offset, SEEK_SET)
Returns 0
on success or -1
on failure.
$tell()
Returns the current file read/write offset in bytes from the beginning of
the file. The return value is a numeric scalar carrying the integer64
class attribute.
$rewind()
Rewind the file pointer to the beginning of the file. This is equivalent to
vf$seek(0, SEEK_SET)
. No return value, called for that side effect.
$read(nbytes)
Read nbytes
bytes from the file at the current offset. Returns a vector
of R raw
type, or NULL
if the operation fails.
$write(object)
Write bytes to the file at the current offset. object
is a raw
vector.
Returns the number of bytes successfully written, as numeric scalar
carrying the integer64
class attribute.
See also base R charToRaw()
/ rawToChar()
, convert to or from raw
vectors, and readBin()
/ writeBin()
which read binary data from or write
binary data to a raw vector.
$eof()
Test for end of file. Returns TRUE
if an end-of-file condition occurred
during the previous read operation. The end-of-file flag is cleared by a
successful call to $seek()
.
$truncate(new_size)
Truncate/expand the file to the specified new_size
, given as a positive
numeric scalar, optionally as bit64::integer64
type.
Returns 0
on success.
$flush()
Flush pending writes to disk. For files in write or update mode and on
file system types where it is applicable, all pending output on the file is
flushed to the physical disk.
On Windows regular files, this method does nothing, unless the
VSI_FLUSH=YES
configuration option is set (and only when the file has not
been opened with the WRITE_THROUGH
option).
Returns 0
on success or -1
on error.
$ingest(max_size)
Ingest a file into memory. Read the whole content of the file into a raw
vector.
max_size
is the maximum size of file allowed, given as a numeric scalar,
optionally as bit64::integer64
type. If no limit, set to a negative value.
Returns a raw
vector, or NULL
if the operation fails.
$close()
Closes the file. The file should always be closed when I/O has been
completed. Returns 0
on success or -1
on error.
$open()
This method can be used to re-open the file after it has been closed, using
the same filename
, and same options
if any are set. The file will be
opened using access
as currently set. The $set_access()
method can be
called to change the requested access while the file is closed.
No return value. An error is raised if a file handle cannot be obtained.
$get_filename()
Returns a character string containing the filename
associated with this
VSIFile
object (the filename
originally used to create the object).
$get_access()
Returns a character string containing the access
as currently set on this
VSIFile
object.
$set_access(access)
Sets the requested read/write access on this VSIFile
object, given as a
character string (i.e., "r"
, "r+"
, "w"
, "w+"
). The access can be
changed only while the VSIFile
object is closed, and will apply when it is
re-opened with a call to $open()
.
Returns 0
on success or -1
on error.
File offsets are given as R numeric
(i.e., double
type), optionally
carrying the bit64::integer64
class attribute. They are returned as
numeric
with the integer64
class attribute attached. The integer64
type is signed, so the maximum file offset supported by this interface
is 9223372036854775807
(the value of bit64::lim.integer64()[2]
).
Some virtual file systems allow only sequential write, so no seeks or read operations are then allowed (e.g., AWS S3 files with /vsis3/). Starting with GDAL 3.2, a configuration option can be set with:
set_config_option("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", "YES")
in which case random-write access is possible (involves the creation of a
temporary local file, whose location is controlled by the CPL_TMPDIR
configuration option). In this case, setting access
to "w+"
may be
needed for writing with seek and read operations (if creating a new file,
otherwise, "r+"
to open an existing file), while "w"
access would
allow sequential write only.
GDAL Virtual File Systems (compressed, network hosted, etc...):
/vsimem, /vsizip, /vsitar, /vsicurl, ...
https://gdal.org/user/virtual_file_systems.html
vsi_copy_file()
, vsi_read_dir()
, vsi_stat()
, vsi_unlink()
# The examples make use of the FARSITE LCP format specification at: # https://gdal.org/drivers/raster/lcp.html # An LCP file is a raw format with a 7,316-byte header. The format # specification gives byte offets and data types for fields in the header. lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") # identify a FARSITE v.4 LCP file # function to check if the first three fields have valid data # input is the first twelve raw bytes in the file is_lcp <- function(bytes) { values <- readBin(bytes, "integer", n = 3) if ((values[1] == 20 || values[1] == 21) && (values[2] == 20 || values[2] == 21) && (values[3] >= -90 && values[3] <= 90)) { return(TRUE) } else { return(FALSE) } } vf <- new(VSIFile, lcp_file) vf$read(12) |> is_lcp() vf$tell() # read the whole file into memory bytes <- vf$ingest(-1) vf$close() # write to a VSI in-memory file mem_file <- "/vsimem/storml_copy.lcp" vf <- new(VSIFile, mem_file, "w+") vf$write(bytes) vf$tell() vf$rewind() vf$tell() vf$seek(0, SEEK_END) (vf$tell() == vsi_stat(lcp_file, "size")) # TRUE vf$rewind() vf$read(12) |> is_lcp() # read/write an integer field # write invalid data for the Latitude field and then set back # save the original first vf$seek(8, SEEK_SET) lat_orig <- vf$read(4) readBin(lat_orig, "integer") # 46 # latitude -99 out of range vf$seek(8, SEEK_SET) writeBin(-99L, raw()) |> vf$write() vf$rewind() vf$read(12) |> is_lcp() # FALSE vf$seek(8, SEEK_SET) vf$read(4) |> readBin("integer") # -99 # set back to original vf$seek(8, SEEK_SET) vf$write(lat_orig) vf$rewind() vf$read(12) |> is_lcp() # TRUE # read a vector of doubles - xmax, xmin, ymax, ymin # 327766.1, 323476.1, 5105082.0, 5101872.0 vf$seek(4172, SEEK_SET) vf$read(32) |> readBin("double", n = 4) # read a short int, the canopy cover units vf$seek(4232, SEEK_SET) vf$read(2) |> readBin("integer", size = 2) # 1 = "percent" # read the Description field vf$seek(6804, SEEK_SET) bytes <- vf$read(512) rawToChar(bytes) # edit the Description desc <- paste(rawToChar(bytes), "Storm Lake AOI,", "Beaverhead-Deerlodge National Forest, Montana.") vf$seek(6804, SEEK_SET) charToRaw(desc) |> vf$write() vf$close() # verify the file as a raster dataset ds <- new(GDALRaster, mem_file) ds$info() # retrieve Description from the metadata # band = 0 for dataset-level metadata, domain = "" for default domain ds$getMetadata(band = 0, domain = "") ds$getMetadataItem(band = 0, mdi_name = "DESCRIPTION", domain = "") ds$close()
# The examples make use of the FARSITE LCP format specification at: # https://gdal.org/drivers/raster/lcp.html # An LCP file is a raw format with a 7,316-byte header. The format # specification gives byte offets and data types for fields in the header. lcp_file <- system.file("extdata/storm_lake.lcp", package="gdalraster") # identify a FARSITE v.4 LCP file # function to check if the first three fields have valid data # input is the first twelve raw bytes in the file is_lcp <- function(bytes) { values <- readBin(bytes, "integer", n = 3) if ((values[1] == 20 || values[1] == 21) && (values[2] == 20 || values[2] == 21) && (values[3] >= -90 && values[3] <= 90)) { return(TRUE) } else { return(FALSE) } } vf <- new(VSIFile, lcp_file) vf$read(12) |> is_lcp() vf$tell() # read the whole file into memory bytes <- vf$ingest(-1) vf$close() # write to a VSI in-memory file mem_file <- "/vsimem/storml_copy.lcp" vf <- new(VSIFile, mem_file, "w+") vf$write(bytes) vf$tell() vf$rewind() vf$tell() vf$seek(0, SEEK_END) (vf$tell() == vsi_stat(lcp_file, "size")) # TRUE vf$rewind() vf$read(12) |> is_lcp() # read/write an integer field # write invalid data for the Latitude field and then set back # save the original first vf$seek(8, SEEK_SET) lat_orig <- vf$read(4) readBin(lat_orig, "integer") # 46 # latitude -99 out of range vf$seek(8, SEEK_SET) writeBin(-99L, raw()) |> vf$write() vf$rewind() vf$read(12) |> is_lcp() # FALSE vf$seek(8, SEEK_SET) vf$read(4) |> readBin("integer") # -99 # set back to original vf$seek(8, SEEK_SET) vf$write(lat_orig) vf$rewind() vf$read(12) |> is_lcp() # TRUE # read a vector of doubles - xmax, xmin, ymax, ymin # 327766.1, 323476.1, 5105082.0, 5101872.0 vf$seek(4172, SEEK_SET) vf$read(32) |> readBin("double", n = 4) # read a short int, the canopy cover units vf$seek(4232, SEEK_SET) vf$read(2) |> readBin("integer", size = 2) # 1 = "percent" # read the Description field vf$seek(6804, SEEK_SET) bytes <- vf$read(512) rawToChar(bytes) # edit the Description desc <- paste(rawToChar(bytes), "Storm Lake AOI,", "Beaverhead-Deerlodge National Forest, Montana.") vf$seek(6804, SEEK_SET) charToRaw(desc) |> vf$write() vf$close() # verify the file as a raster dataset ds <- new(GDALRaster, mem_file) ds$info() # retrieve Description from the metadata # band = 0 for dataset-level metadata, domain = "" for default domain ds$getMetadata(band = 0, domain = "") ds$getMetadataItem(band = 0, mdi_name = "DESCRIPTION", domain = "") ds$close()
warp()
is a wrapper of the gdalwarp
command-line utility for
raster reprojection and warping
(see https://gdal.org/programs/gdalwarp.html).
The function can reproject to any supported spatial reference system (SRS).
It can also be used to crop, mosaic, resample, and optionally write output
to a different raster format. See Details for a list of commonly used
processing options that can be passed as arguments to warp()
.
warp(src_files, dst_filename, t_srs, cl_arg = NULL, quiet = FALSE)
warp(src_files, dst_filename, t_srs, cl_arg = NULL, quiet = FALSE)
src_files |
Either a character vector of source filenames(s) to be
reprojected, or a |
dst_filename |
Either a character string giving the filename of the
output dataset, or an object of class |
t_srs |
Character string. Target spatial reference system. Usually an
EPSG code ("EPSG:#####") or a well known text (WKT) SRS definition.
Can be set to empty string |
cl_arg |
Optional character vector of command-line arguments to
|
quiet |
Logical scalar. If |
Several processing options can be performed in one call to warp()
by
passing the necessary command-line arguments. The following list describes
several commonly used arguments. Note that gdalwarp
supports a large
number of arguments that enable a variety of different processing options.
Users are encouraged to review the original source documentation provided
by the GDAL project at the URL above for the full list.
-te <xmin> <ymin> <xmax> <ymax>
Georeferenced extents of output file to be created (in target SRS by
default).
-te_srs <srs_def>
SRS in which to interpret the coordinates given with -te
(if different than t_srs
).
-tr <xres> <yres>
Output pixel resolution (in target georeferenced units).
-tap
(target aligned pixels) align the coordinates of the extent of the output
file to the values of the -tr
, such that the aligned extent includes
the minimum extent. Alignment means that xmin / resx, ymin / resy,
xmax / resx and ymax / resy are integer values.
-ovr <level>|AUTO|AUTO-<n>|NONE
Specify which overview level of source files must be used. The default
choice, AUTO
, will select the overview level whose resolution is the
closest to the target resolution. Specify an integer value (0-based,
i.e., 0=1st overview level) to select a particular level. Specify
AUTO-n
where n
is an integer greater or equal to 1
, to select an
overview level below the AUTO
one. Or specify NONE
to force the base
resolution to be used (can be useful if overviews have been generated
with a low quality resampling method, and the warping is done using a
higher quality resampling method).
-wo <NAME>=<VALUE>
Set a warp option as described in the GDAL documentation for
GDALWarpOptions
Multiple -wo
may be given. See also -multi
below.
-ot <type>
Force the output raster bands to have a specific data type supported by
the format, which may be one of the following: Byte
, Int8
, UInt16
,
Int16
, UInt32
, Int32
, UInt64
, Int64
, Float32
, Float64
,
CInt16
, CInt32
, CFloat32
or CFloat64
.
-r <resampling_method>
Resampling method to use. Available methods are: near
(nearest
neighbour, the default), bilinear
, cubic
, cubicspline
, lanczos
,
average
, rms
(root mean square, GDAL >= 3.3), mode
, max
, min
,
med
, q1
(first quartile), q3
(third quartile), sum
(GDAL >= 3.1).
-srcnodata "<value>[ <value>]..."
Set nodata masking values for input bands (different values can be
supplied for each band). If more than one value is supplied all values
should be quoted to keep them together as a single operating system
argument. Masked values will not be used in interpolation. Use a value of
None
to ignore intrinsic nodata settings on the source dataset.
If -srcnodata
is not explicitly set, but the source dataset has nodata
values, they will be taken into account by default.
-dstnodata "<value>[ <value>]..."
Set nodata values for output bands (different values can be supplied for
each band). If more than one value is supplied all values should be
quoted to keep them together as a single operating system argument. New
files will be initialized to this value and if possible the nodata value
will be recorded in the output file. Use a value of "None"
to ensure
that nodata is not defined. If this argument is not used then nodata
values will be copied from the source dataset.
-srcband <n>
(GDAL >= 3.7) Specify an input band number to warp (between 1 and the
number of bands of the source dataset). This option is used to warp a
subset of the input bands. All input bands are used when it is not
specified. This option may be repeated multiple times to select several
input bands. The order in which bands are specified will be the order in
which they appear in the output dataset (unless -dstband
is specified).
The alpha band should not be specified in the list, as it will be
automatically retrieved (unless -nosrcalpha
is specified).
-dstband <n>
(GDAL >= 3.7) Specify the output band number in which to warp. In
practice, this option is only useful when updating an existing dataset,
e.g to warp one band at at time. If -srcband
is specified, there must be
as many occurrences of -dstband
as there are of -srcband
.
If -dstband
is not specified, then:c("-dstband", "1", "-dstband", "2", ... "-dstband", "N")
is assumed where N is the number of input bands (implicitly, or specified
explicitly with -srcband
). The alpha band should not be specified in the
list, as it will be automatically retrieved (unless -nosrcalpha
is
specified).
-wm <memory_in_mb>
Set the amount of memory that the warp API is allowed to use for caching.
The value is interpreted as being in megabytes if the value is <10000.
For values >=10000, this is interpreted as bytes. The warper will
total up the memory required to hold the input and output image arrays
and any auxiliary masking arrays and if they are larger than the
"warp memory" allowed it will subdivide the chunk into smaller chunks and
try again. If the -wm
value is very small there is some extra overhead
in doing many small chunks so setting it larger is better but it is a
matter of diminishing returns.
-multi
Use multithreaded warping implementation. Two threads will be used to
process chunks of image and perform input/output operation
simultaneously. Note that computation is not multithreaded itself. To do
that, you can use the -wo NUM_THREADS=val/ALL_CPUS
option, which can be
combined with -multi
.
-of <format>
Set the output raster format. Will be guessed from the extension if not
specified. Use the short format name (e.g., "GTiff"
).
-co <NAME>=<VALUE>
Set one or more format specific creation options for the output dataset.
For example, the GeoTIFF driver supports creation options to control
compression, and whether the file should be tiled.
getCreationOptions()
can be used to look up available creation options,
but the GDAL Raster drivers
documentation is the definitive reference for format specific options.
Multiple -co
may be given, e.g.,
c("-co", "COMPRESS=LZW", "-co", "BIGTIFF=YES")
-overwrite
Overwrite the target dataset if it already exists. Overwriting means
deleting and recreating the file from scratch. Note that if this option
is not specified and the output file already exists, it will be updated
in place.
The documentation for gdalwarp
describes additional command-line options related to spatial reference
systems, alpha bands, masking with polygon cutlines including blending,
and more.
Mosaicing into an existing output file is supported if the output file
already exists. The spatial extent of the existing file will not be
modified to accommodate new data, so you may have to remove it in that
case, or use the -overwrite
option.
Command-line options are passed to warp()
as a character vector. The
elements of the vector are the individual options followed by their
individual values, e.g.,
cl_arg = c("-tr", "30", "30", "-r", "bilinear"))
to set the target pixel resolution to 30 x 30 in target georeferenced units and use bilinear resampling.
Logical indicating success (invisible TRUE
).
An error is raised if the operation fails.
warp()
can be used to reproject and also perform other processing such
as crop, resample, and mosaic.
This processing is generally done with a single function call by passing
arguments for the output ("target") pixel resolution, extent, resampling
method, nodata value, format, and so forth.
If warp()
is called with t_srs = ""
and the output raster does not
already exist, the target spatial reference will be set to that of
src_files[1]
. In that case, the processing options given in cl_arg
will
be performed without reprojecting (if there is one source raster or multiple
sources that all use the same spatial reference system, otherwise would
reproject inputs to the SRS of src_files[1]
where they are different).
If t_srs = ""
and the destination raster already exists, the output SRS
will be the projection of the destination dataset.
GDALRaster-class
, srs_to_wkt()
, translate()
# reproject the elevation raster to NAD83 / CONUS Albers (EPSG:5070) elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") # command-line arguments for gdalwarp # resample to 90-m resolution and keep pixels aligned: args <- c("-tr", "90", "90", "-r", "cubic", "-tap") # write to Erdas Imagine format (HFA) with compression: args <- c(args, "-of", "HFA", "-co", "COMPRESSED=YES") alb83_file <- file.path(tempdir(), "storml_elev_alb83.img") warp(elev_file, alb83_file, t_srs="EPSG:5070", cl_arg = args) ds <- new(GDALRaster, alb83_file) ds$getDriverLongName() ds$getProjectionRef() ds$res() ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close()
# reproject the elevation raster to NAD83 / CONUS Albers (EPSG:5070) elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster") # command-line arguments for gdalwarp # resample to 90-m resolution and keep pixels aligned: args <- c("-tr", "90", "90", "-r", "cubic", "-tap") # write to Erdas Imagine format (HFA) with compression: args <- c(args, "-of", "HFA", "-co", "COMPRESSED=YES") alb83_file <- file.path(tempdir(), "storml_elev_alb83.img") warp(elev_file, alb83_file, t_srs="EPSG:5070", cl_arg = args) ds <- new(GDALRaster, alb83_file) ds$getDriverLongName() ds$getProjectionRef() ds$res() ds$getStatistics(band=1, approx_ok=FALSE, force=TRUE) ds$close()