Skip to content

Commit a032ecc

Browse files
committed
start supporting more options
1 parent 31ea858 commit a032ecc

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

R/RcppExports.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ CPL_read_ogr <- function(datasource, layer, query, options, quiet, toTypeUser, f
113113
.Call(`_sf_CPL_read_ogr`, datasource, layer, query, options, quiet, toTypeUser, fid_column_name, drivers, wkt_filter, promote_to_multi, int64_as_string, dsn_exists, dsn_isdb, width)
114114
}
115115

116+
<<<<<<< HEAD
116117
<<<<<<< HEAD
117118
CPL_gdalinfo <- function(obj, options, oo, co) {
118119
.Call(`_sf_CPL_gdalinfo`, obj, options, oo, co)
119120
=======
120121
CPL_read_gdal_stream <- function(stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists = TRUE, dsn_isdb = FALSE, width = 80L) {
121122
.Call('_sf_CPL_read_gdal_stream', PACKAGE = 'sf', stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists, dsn_isdb, width)
123+
=======
124+
CPL_read_gdal_stream <- function(stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists, dsn_isdb, fid_column, width) {
125+
.Call('_sf_CPL_read_gdal_stream', PACKAGE = 'sf', stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists, dsn_isdb, fid_column, width)
126+
>>>>>>> 93da2e60 (start supporting more options)
122127
}
123128

124129
CPL_gdalinfo <- function(obj, options, oo) {

R/read.R

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ process_cpl_read_ogr = function(x, quiet = FALSE, ..., check_ring_dir = FALSE,
192192
for (i in seq_along(lc.other))
193193
x[[ nm.lc[i] ]] = list.cols[[i]]
194194

195-
for (i in seq_along(geom))
196-
x[[ nm[i] ]] = st_sfc(geom[[i]], crs = attr(geom[[i]], "crs")) # computes bbox
195+
for (i in seq_along(geom)) {
196+
if (is.null(attr(geom[[i]], "bbox"))) {
197+
x[[ nm[i] ]] = st_sfc(geom[[i]], crs = attr(geom[[i]], "crs")) # computes bbox
198+
} else {
199+
x[[ nm[i] ]] = geom[[i]]
200+
}
201+
}
197202

198203
x = st_as_sf(x, ...,
199204
sf_column_name = if (is.character(geometry_column)) geometry_column else nm[geometry_column],
@@ -204,7 +209,17 @@ process_cpl_read_ogr = function(x, quiet = FALSE, ..., check_ring_dir = FALSE,
204209
x
205210
}
206211

207-
process_cpl_read_ogr_stream = function(x, crs, num_features, as_tibble = FALSE, ...) {
212+
# Allow setting the default to TRUE to make it easier to run existing tests
213+
# of st_read() through the stream interface
214+
default_st_read_use_stream = function() {
215+
getOption(
216+
"sf.st_read_use_stream",
217+
identical(Sys.getenv("R_SF_ST_READ_USE_STREAM"), "true")
218+
)
219+
}
220+
221+
process_cpl_read_ogr_stream = function(x, crs, num_features, fid_column_name,
222+
...) {
208223
is_geometry_column = vapply(
209224
x$get_schema()$children,
210225
function(s) identical(s$metadata[["ARROW:extension:name"]], "ogc.wkb"),
@@ -222,15 +237,17 @@ process_cpl_read_ogr_stream = function(x, crs, num_features, as_tibble = FALSE,
222237
st_set_crs(x, crs)
223238
})
224239

225-
if (as_tibble) {
226-
df = tibble::as_tibble(df)
240+
# Prefer "geometry" as the geometry column name
241+
if (any(is_geometry_column) && !("geometry" %in% names(df))) {
242+
names(df)[which(is_geometry_column)[1]] = "geometry"
227243
}
228244

229-
if (any(is_geometry_column)) {
230-
st_as_sf(df)
231-
} else {
232-
df
245+
# Rename OGC_FID to fid_column_name
246+
if (length(fid_column_name) == 1 && "OGC_FID" %in% names(df)) {
247+
names(df)[names(df) == "OGC_FID"] = fid_column_name
233248
}
249+
250+
process_cpl_read_ogr(df, ...)
234251
}
235252

236253
#' @name st_read
@@ -248,7 +265,7 @@ st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet
248265
type = 0, promote_to_multi = TRUE, stringsAsFactors = sf_stringsAsFactors(),
249266
int64_as_string = FALSE, check_ring_dir = FALSE, fid_column_name = character(0),
250267
drivers = character(0), wkt_filter = character(0), optional = FALSE,
251-
use_stream = FALSE) {
268+
use_stream = default_st_read_use_stream()) {
252269

253270
layer = if (missing(layer))
254271
character(0)
@@ -269,8 +286,9 @@ st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet
269286
if (use_stream) {
270287
stream = nanoarrow::nanoarrow_allocate_array_stream()
271288
info = CPL_read_gdal_stream(stream, dsn, layer, query, as.character(options), quiet,
272-
drivers, wkt_filter, dsn_exists, dsn_isdb, getOption("width"))
273-
process_cpl_read_ogr_stream(stream, crs = info[[1]], num_features = info[[2]], ...)
289+
drivers, wkt_filter, dsn_exists, dsn_isdb, fid_column_name, getOption("width"))
290+
process_cpl_read_ogr_stream(stream, crs = info[[1]], num_features = info[[2]],
291+
fid_column_name = fid_column_name, ...)
274292
} else {
275293
x = CPL_read_ogr(dsn, layer, query, as.character(options), quiet, type, fid_column_name,
276294
drivers, wkt_filter, promote_to_multi, int64_as_string, dsn_exists, dsn_isdb, getOption("width"))

src/RcppExports.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ BEGIN_RCPP
346346
END_RCPP
347347
}
348348
// CPL_read_gdal_stream
349-
Rcpp::List CPL_read_gdal_stream(Rcpp::RObject stream_xptr, Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer, Rcpp::CharacterVector query, Rcpp::CharacterVector options, bool quiet, Rcpp::CharacterVector drivers, Rcpp::CharacterVector wkt_filter, bool dsn_exists, bool dsn_isdb, int width);
350-
RcppExport SEXP _sf_CPL_read_gdal_stream(SEXP stream_xptrSEXP, SEXP datasourceSEXP, SEXP layerSEXP, SEXP querySEXP, SEXP optionsSEXP, SEXP quietSEXP, SEXP driversSEXP, SEXP wkt_filterSEXP, SEXP dsn_existsSEXP, SEXP dsn_isdbSEXP, SEXP widthSEXP) {
349+
Rcpp::List CPL_read_gdal_stream(Rcpp::RObject stream_xptr, Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer, Rcpp::CharacterVector query, Rcpp::CharacterVector options, bool quiet, Rcpp::CharacterVector drivers, Rcpp::CharacterVector wkt_filter, bool dsn_exists, bool dsn_isdb, Rcpp::CharacterVector fid_column, int width);
350+
RcppExport SEXP _sf_CPL_read_gdal_stream(SEXP stream_xptrSEXP, SEXP datasourceSEXP, SEXP layerSEXP, SEXP querySEXP, SEXP optionsSEXP, SEXP quietSEXP, SEXP driversSEXP, SEXP wkt_filterSEXP, SEXP dsn_existsSEXP, SEXP dsn_isdbSEXP, SEXP fid_columnSEXP, SEXP widthSEXP) {
351351
BEGIN_RCPP
352352
Rcpp::RObject rcpp_result_gen;
353353
Rcpp::RNGScope rcpp_rngScope_gen;
@@ -361,8 +361,9 @@ BEGIN_RCPP
361361
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type wkt_filter(wkt_filterSEXP);
362362
Rcpp::traits::input_parameter< bool >::type dsn_exists(dsn_existsSEXP);
363363
Rcpp::traits::input_parameter< bool >::type dsn_isdb(dsn_isdbSEXP);
364+
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type fid_column(fid_columnSEXP);
364365
Rcpp::traits::input_parameter< int >::type width(widthSEXP);
365-
rcpp_result_gen = Rcpp::wrap(CPL_read_gdal_stream(stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists, dsn_isdb, width));
366+
rcpp_result_gen = Rcpp::wrap(CPL_read_gdal_stream(stream_xptr, datasource, layer, query, options, quiet, drivers, wkt_filter, dsn_exists, dsn_isdb, fid_column, width));
366367
return rcpp_result_gen;
367368
END_RCPP
368369
}
@@ -1463,6 +1464,7 @@ static const R_CallMethodDef CallEntries[] = {
14631464
{"_sf_CPL_gdal_linestring_sample", (DL_FUNC) &_sf_CPL_gdal_linestring_sample, 2},
14641465
{"_sf_CPL_get_layers", (DL_FUNC) &_sf_CPL_get_layers, 3},
14651466
{"_sf_CPL_read_ogr", (DL_FUNC) &_sf_CPL_read_ogr, 14},
1467+
<<<<<<< HEAD
14661468
<<<<<<< HEAD
14671469
{"_sf_CPL_gdalinfo", (DL_FUNC) &_sf_CPL_gdalinfo, 4},
14681470
{"_sf_CPL_ogrinfo", (DL_FUNC) &_sf_CPL_ogrinfo, 4},
@@ -1480,6 +1482,9 @@ static const R_CallMethodDef CallEntries[] = {
14801482
{"_sf_CPL_gdal_warper", (DL_FUNC) &_sf_CPL_gdal_warper, 7},
14811483
=======
14821484
{"_sf_CPL_read_gdal_stream", (DL_FUNC) &_sf_CPL_read_gdal_stream, 11},
1485+
=======
1486+
{"_sf_CPL_read_gdal_stream", (DL_FUNC) &_sf_CPL_read_gdal_stream, 12},
1487+
>>>>>>> 93da2e60 (start supporting more options)
14831488
{"_sf_CPL_gdalinfo", (DL_FUNC) &_sf_CPL_gdalinfo, 3},
14841489
{"_sf_CPL_gdaladdo", (DL_FUNC) &_sf_CPL_gdaladdo, 7},
14851490
{"_sf_CPL_gdalwarp", (DL_FUNC) &_sf_CPL_gdalwarp, 7},

src/gdal_read_stream.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ Rcpp::List CPL_read_gdal_stream(
7575
Rcpp::CharacterVector query,
7676
Rcpp::CharacterVector options, bool quiet, Rcpp::CharacterVector drivers,
7777
Rcpp::CharacterVector wkt_filter,
78-
bool dsn_exists = true,
79-
bool dsn_isdb = false,
80-
int width = 80) {
78+
bool dsn_exists,
79+
bool dsn_isdb,
80+
Rcpp::CharacterVector fid_column,
81+
int width) {
82+
83+
const char* array_stream_options[] = {"INCLUDE_FID=NO", nullptr};
84+
if (fid_column.size() == 1) {
85+
array_stream_options[0] = "INCLUDE_FID=YES";
86+
}
87+
8188
Rcpp::List prep = CPL_ogr_layer_setup(datasource, layer, query, options,
8289
quiet, drivers,
8390
wkt_filter,
@@ -94,7 +101,7 @@ Rcpp::List CPL_read_gdal_stream(
94101
CPLFree(wkt_out);
95102

96103
struct ArrowArrayStream stream_temp;
97-
if (!poLayer->GetArrowStream(&stream_temp, nullptr)) {
104+
if (!poLayer->GetArrowStream(&stream_temp, array_stream_options)) {
98105
Rcpp::stop("Failed to open ArrayStream from Layer");
99106
}
100107

0 commit comments

Comments
 (0)