View source: R/ogr_reproject.R
ogr_reproject | R Documentation |
ogr_reproject()
reprojects the features of a vector layer to a different
spatial reference system and writes the new layer to a specified output
dataset. The output may be in a different vector file format than the input
dataset. A source SRS definition must be available in the source layer
for reprojection to occur.
ogr_reproject(
src_dsn,
src_layer,
out_dsn,
out_srs,
out_fmt = NULL,
overwrite = FALSE,
append = FALSE,
nln = NULL,
nlt = NULL,
dsco = NULL,
lco = NULL,
dialect = NULL,
spat_bbox = NULL,
src_open_options = NULL,
progress = FALSE,
add_cl_arg = NULL,
return_obj = TRUE
)
src_dsn |
Character string. The filename or database connection string specifying the vector data source containing the input layer. |
src_layer |
Character string. The name of the input layer in |
out_dsn |
Character string. The filename or database connection string specifying the vector data source to which the output layer will be written. |
out_srs |
Character string specifying the output spatial reference
system. May be in WKT format or any of the formats supported by
|
out_fmt |
Optional character string giving the GDAL short name
of the output dataset format. Only used if |
overwrite |
Logical value. |
append |
Logical value. |
nln |
Optional character string giving an alternate name to assign the
new layer. By default, |
nlt |
Optional character string to define the geometry type for the
output layer. Mainly useful when |
dsco |
Optional character vector of format-specific creation options
for |
lco |
Optional character vector of format-specific creation options
for the output layer ( |
dialect |
Optional character string specifying the SQL dialect to use.
The OGR SQL engine ( |
spat_bbox |
Optional numeric vector of length four specifying a spatial
bounding box (xmin, ymin, xmax, ymax), in the SRS of the source layer.
Only features whose geometry intersects |
src_open_options |
Optional character vector of dataset open options
for |
progress |
Logical value, |
add_cl_arg |
Optional character vector of additional command-line
arguments to be passed to |
return_obj |
Logical value, |
ogr_reproject()
is a convenience wrapper to perform vector reprojection
via ogr2ogr()
, which in turn is an API binding to GDAL's ogr2ogr
command-line utility.
Upon successful completion, an object of class GDALVector
is
returned by default (if return_obj = TRUE
), or logical TRUE
is returned
(invisibly) if return_obj = FALSE
. An error is raised if reprojection
fails.
For advanced use, additional command-line arguments may be passed to
ogr2ogr()
in add_cl_arg
(e.g., advanced geometry and SRS related
options). Users should be aware of possible implications and
compatibility with the arguments already implied by the parameterization
of ogr_reproject()
.
The function will attempt to create the output datasource if it does not already exist. Some formats (e.g., PostgreSQL) do not support creation of new datasets (i.e., a database within PostgreSQL), but output layers can be written to an existing database.
ogr2ogr()
, warp()
for raster reprojection
GDAL documentation for ogr2ogr
:
https://gdal.org/en/stable/programs/ogr2ogr.html
# MTBS fire perimeters
f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster")
(mtbs <- new(GDALVector, f, "mtbs_perims"))
mtbs$getSpatialRef() |> srs_is_projected() # TRUE
# YNP boundary
f <- system.file("extdata/ynp_features.zip", package = "gdalraster")
ynp_dsn <- file.path("/vsizip", f, "ynp_features.gpkg")
(bnd <- new(GDALVector, ynp_dsn, "ynp_bnd"))
bnd$getSpatialRef() |> srs_is_projected() # FALSE
# project the boundary to match the MTBS layer
out_dsn <- tempfile(fileext = ".gpkg")
(bnd_mtsp <- ogr_reproject(ynp_dsn, "ynp_bnd", out_dsn, mtbs$getSpatialRef()))
bnd_mtsp$getFeatureCount()
plot(bnd_mtsp$getNextFeature(), col = "wheat")
mtbs$setAttributeFilter("incid_name = 'MAPLE'")
mtbs$getFeatureCount() # 1
(feat <- mtbs$getNextFeature())
plot(feat, col = "red", border = NA, add = TRUE)
mtbs$close()
bnd$close()
bnd_mtsp$close()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.