Skip to content

Commit

Permalink
for #1389
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 4, 2024
1 parent 326c360 commit 10d80b2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
5 changes: 3 additions & 2 deletions R/vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ setMethod("vect", signature(x="SpatVectorCollection"),


setMethod("vect", signature(x="character"),
function(x, layer="", query="", extent=NULL, filter=NULL, crs="", proxy=FALSE, what="") {
function(x, layer="", query="", extent=NULL, filter=NULL, crs="", proxy=FALSE, what="", opts=NULL) {

what <- trimws(tolower(what))
if (what != "") what <- match.arg(trimws(tolower(what)), c("geoms", "attributes"))
Expand Down Expand Up @@ -98,7 +98,8 @@ setMethod("vect", signature(x="character"),
} else {
extent <- as.vector(ext(extent))
}
p@cpp$read(x, layer, query, extent, filter, proxy, what)
if (is.null(opts)) opts <- ""[0]
p@cpp$read(x, layer, query, extent, filter, proxy, what, opts)
if (isTRUE(crs != "")) {
crs(p, warn=FALSE) <- crs
}
Expand Down
3 changes: 2 additions & 1 deletion man/vect.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SpatVectors can also be created from "Well Known Text", and from spatial vector

\usage{
\S4method{vect}{character}(x, layer="", query="", extent=NULL, filter=NULL,
crs="", proxy=FALSE, what="")
crs="", proxy=FALSE, what="", opts=NULL)

\S4method{vect}{matrix}(x, type="points", atts=NULL, crs="")

Expand All @@ -60,6 +60,7 @@ SpatVectors can also be created from "Well Known Text", and from spatial vector
\item{crs}{character. The coordinate reference system in one of the following formats: WKT/WKT2, <authority>:<code>, or PROJ-string notation (see \code{\link{crs}}). See \code{\link{crs}}}
\item{proxy}{logical. If \code{TRUE} a SpatVectorProxy is returned}
\item{what}{character indicating what to read. Either \code{""} for geometries and attributes, or \code{"geoms"} to only read the geometries, \code{"attributes"} to only read the attributes (that are returned as a data.frame)}
\item{opts}{character. GDAL dataset open options}
\item{geom}{character. The field name(s) with the geometry data. Either two names for x and y coordinates of points, or a single name for a single column with WKT geometries)}
\item{keepgeom}{logical. If \code{TRUE} the geom variable(s) is (are) also included in the attributes}
}
Expand Down
2 changes: 1 addition & 1 deletion src/read_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ bool GetVAT(std::string filename, SpatCategories &vat) {
SpatVector v, fvct;
std::vector<double> fext;

v.read(filename, "", "", fext, fvct, false, "");
v.read(filename, "", "", fext, fvct, false, "", {});
if (v.df.nrow() == 0) return false;


Expand Down
14 changes: 11 additions & 3 deletions src/read_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,17 @@ bool SpatVector::read_ogr(GDALDataset *&poDS, std::string layer, std::string que
}


bool SpatVector::read(std::string fname, std::string layer, std::string query, std::vector<double> extent, SpatVector filter, bool as_proxy, std::string what) {
//OGRRegisterAll();
GDALDataset *poDS = static_cast<GDALDataset*>(GDALOpenEx( fname.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL ));
bool SpatVector::read(std::string fname, std::string layer, std::string query, std::vector<double> extent, SpatVector filter, bool as_proxy, std::string what, std::vector<std::string> options) {

char ** openops = NULL;
for (size_t i=0; i<options.size(); i++) {
std::vector<std::string> opt = strsplit(options[i], "=");
if (opt.size() == 2) {
openops = CSLSetNameValue(openops, opt[0].c_str(), opt[1].c_str());
}
}

GDALDataset *poDS = static_cast<GDALDataset*>(GDALOpenEx( fname.c_str(), GDAL_OF_VECTOR, NULL, openops, NULL ));
if( poDS == NULL ) {
if (!file_exists(fname)) {
setError("file does not exist: " + fname);
Expand Down
2 changes: 1 addition & 1 deletion src/spatVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class SpatVector {
SpatVector set_holes(SpatVector x, size_t i);
SpatVector remove_duplicate_nodes(int digits);

bool read(std::string fname, std::string layer, std::string query, std::vector<double> extent, SpatVector filter, bool as_proxy, std::string what);
bool read(std::string fname, std::string layer, std::string query, std::vector<double> extent, SpatVector filter, bool as_proxy, std::string what, std::vector<std::string> options);

bool write(std::string filename, std::string lyrname, std::string driver, bool append, bool overwrite, std::vector<std::string>);

Expand Down

0 comments on commit 10d80b2

Please sign in to comment.