@@ -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" ))
0 commit comments