Skip to content

Commit 4564990

Browse files
committed
theoretically working read
1 parent 9c093d0 commit 4564990

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

R/read.R

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,33 @@ process_cpl_read_ogr = function(x, quiet = FALSE, ..., check_ring_dir = FALSE,
204204
x
205205
}
206206

207-
process_cpl_read_ogr_stream = function(x, ...) {
208-
x
207+
process_cpl_read_ogr_stream = function(x, crs, num_features, as_tibble = FALSE, ...) {
208+
is_geometry_column = vapply(
209+
x$get_schema()$children,
210+
function(s) identical(s$metadata[["ARROW:extension:name"]], "ogc.wkb"),
211+
logical(1)
212+
)
213+
214+
crs = sf::st_crs(crs)
215+
if (num_features == -1) {
216+
num_features = NULL
217+
}
218+
df = suppressWarnings(nanoarrow::convert_array_stream(x, size = num_features))
219+
220+
df[is_geometry_column] = lapply(df[is_geometry_column], function(x) {
221+
x = sf::st_as_sfc(wk::as_wkb(x))
222+
st_set_crs(x, crs)
223+
})
224+
225+
if (as_tibble) {
226+
df = tibble::as_tibble(df)
227+
}
228+
229+
if (any(is_geometry_column)) {
230+
st_as_sf(df)
231+
} else {
232+
df
233+
}
209234
}
210235

211236
#' @name st_read
@@ -242,11 +267,9 @@ st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet
242267

243268
if (use_stream) {
244269
stream = nanoarrow::nanoarrow_allocate_array_stream()
245-
CPL_read_gdal_stream(stream, dsn, layer, query, as.character(options), quiet,
270+
info <- CPL_read_gdal_stream(stream, dsn, layer, query, as.character(options), quiet,
246271
drivers, wkt_filter, dsn_exists, dsn_isdb, getOption("width"))
247-
process_cpl_read_ogr_stream(stream, quiet, check_ring_dir = check_ring_dir,
248-
stringsAsFactors = stringsAsFactors, geometry_column = geometry_column,
249-
optional = optional, ...)
272+
process_cpl_read_ogr_stream(stream, crs = info[[1]], num_features = info[[2]], ...)
250273
} else {
251274
x = CPL_read_ogr(dsn, layer, query, as.character(options), quiet, type, fid_column_name,
252275
drivers, wkt_filter, promote_to_multi, int64_as_string, dsn_exists, dsn_isdb, getOption("width"))

src/RcppExports.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ BEGIN_RCPP
346346
END_RCPP
347347
}
348348
// CPL_read_gdal_stream
349-
Rcpp::CharacterVector 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);
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);
350350
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) {
351351
BEGIN_RCPP
352352
Rcpp::RObject rcpp_result_gen;

src/gdal_read_stream.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class GDALStreamWrapper {
6969
};
7070

7171
// [[Rcpp::export]]
72-
Rcpp::CharacterVector CPL_read_gdal_stream(
72+
Rcpp::List CPL_read_gdal_stream(
7373
Rcpp::RObject stream_xptr,
7474
Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer,
7575
Rcpp::CharacterVector query,
@@ -99,7 +99,9 @@ Rcpp::CharacterVector CPL_read_gdal_stream(
9999
}
100100

101101
GDALStreamWrapper::Make(&stream_temp, prep, stream_out);
102-
return Rcpp::CharacterVector::create(wkt_str);
102+
double num_features = (double) poLayer->GetFeatureCount(false);
103+
104+
return Rcpp::List::create(wkt_str, Rcpp::NumericVector::create(num_features));
103105
}
104106

105107
#else

0 commit comments

Comments
 (0)