@@ -18,48 +18,7 @@ as_geoarrow_vctr <- function(x, ..., schema = NULL) {
1818 }
1919
2020 stream <- as_geoarrow_array_stream(x , ... , schema = schema )
21- chunks <- nanoarrow :: collect_array_stream(stream , validate = FALSE )
22- new_geoarrow_vctr(chunks , stream $ get_schema())
23- }
24-
25- new_geoarrow_vctr <- function (chunks , schema , indices = NULL ) {
26- offsets <- .Call(geoarrow_c_vctr_chunk_offsets , chunks )
27- if (is.null(indices )) {
28- indices <- seq_len(offsets [length(offsets )])
29- }
30-
31- structure(
32- indices ,
33- schema = schema ,
34- chunks = chunks ,
35- offsets = offsets ,
36- class = c(" geoarrow_vctr" , " wk_vctr" )
37- )
38- }
39-
40- # ' @export
41- `[.geoarrow_vctr` <- function (x , i ) {
42- attrs <- attributes(x )
43- x <- NextMethod()
44-
45- if (is.null(vctr_as_slice(x ))) {
46- stop(
47- " Can't subset geoarrow_vctr with non-slice (e.g., only i:j indexing is supported)"
48- )
49- }
50-
51- attributes(x ) <- attrs
52- x
53- }
54-
55- # ' @export
56- `[<-.geoarrow_vctr` <- function (x , i , value ) {
57- stop(" subset assignment for geoarrow_vctr is not supported" )
58- }
59-
60- # ' @export
61- `[[<-.geoarrow_vctr` <- function (x , i , value ) {
62- stop(" subset assignment for geoarrow_vctr is not supported" )
21+ nanoarrow :: as_nanoarrow_vctr(stream , subclass = " geoarrow_vctr" )
6322}
6423
6524# ' @export
@@ -102,129 +61,8 @@ as.character.geoarrow_vctr <- function(x, ...) {
10261 format(x , ... )
10362}
10463
105- # ' @export
106- infer_nanoarrow_schema.geoarrow_vctr <- function (x , ... ) {
107- attr(x , " schema" , exact = TRUE )
108- }
109-
110- # Because zero-length vctrs are R's way of communicating "type", implement
111- # as_nanoarrow_schema() here so that it works in places that expect a type
112- # ' @importFrom nanoarrow as_nanoarrow_schema
113- # ' @export
114- as_nanoarrow_schema.geoarrow_vctr <- function (x , ... ) {
115- attr(x , " schema" , exact = TRUE )
116- }
117-
11864# ' @export
11965as_geoarrow_array_stream.geoarrow_vctr <- function (x , ... , schema = NULL ) {
120- as_nanoarrow_array_stream.geoarrow_vctr(x , ... , schema = schema )
121- }
122-
123- # ' @importFrom nanoarrow as_nanoarrow_array_stream
124- # ' @export
125- as_nanoarrow_array_stream.geoarrow_vctr <- function (x , ... , schema = NULL ) {
126- if (! is.null(schema )) {
127- stream <- as_nanoarrow_array_stream(x , schema = NULL )
128- return (as_geoarrow_array_stream(stream , schema = schema ))
129- }
130-
131- slice <- vctr_as_slice(x )
132- if (is.null(slice )) {
133- stop(" Can't resolve non-slice geoarrow_vctr to nanoarrow_array_stream" )
134- }
135-
136- x_schema <- attr(x , " schema" , exact = TRUE )
137-
138- # Zero-size slice can be an array stream with zero batches
139- if (slice [2 ] == 0 ) {
140- return (nanoarrow :: basic_array_stream(list (), schema = x_schema ))
141- }
142-
143- # Full slice doesn't need slicing logic
144- offsets <- attr(x , " offsets" , exact = TRUE )
145- batches <- attr(x , " chunks" , exact = TRUE )
146- if (slice [1 ] == 1 && slice [2 ] == max(offsets )) {
147- return (
148- nanoarrow :: basic_array_stream(
149- batches ,
150- schema = x_schema ,
151- validate = FALSE
152- )
153- )
154- }
155-
156- # Calculate first and last slice information
157- first_index <- slice [1 ] - 1L
158- end_index <- first_index + slice [2 ]
159- last_index <- end_index - 1L
160- first_chunk_index <- vctr_resolve_chunk(first_index , offsets )
161- last_chunk_index <- vctr_resolve_chunk(last_index , offsets )
162-
163- first_chunk_offset <- first_index - offsets [first_chunk_index + 1L ]
164- first_chunk_length <- offsets [first_chunk_index + 2L ] - first_index
165- last_chunk_offset <- 0L
166- last_chunk_length <- end_index - offsets [last_chunk_index + 1L ]
167-
168- # Calculate first and last slices
169- if (first_chunk_index == last_chunk_index ) {
170- batch <- vctr_array_slice(
171- batches [[first_chunk_index + 1L ]],
172- first_chunk_offset ,
173- last_chunk_length - first_chunk_offset
174- )
175-
176- return (
177- nanoarrow :: basic_array_stream(
178- list (batch ),
179- schema = x_schema ,
180- validate = FALSE
181- )
182- )
183- }
184-
185- batch1 <- vctr_array_slice(
186- batches [[first_chunk_index + 1L ]],
187- first_chunk_offset ,
188- first_chunk_length
189- )
190-
191- batchn <- vctr_array_slice(
192- batches [[last_chunk_index + 1L ]],
193- last_chunk_offset ,
194- last_chunk_length
195- )
196-
197- seq_mid <- seq_len(last_chunk_index - first_chunk_index - 1 )
198- batch_mid <- batches [first_chunk_index + seq_mid ]
199-
200- nanoarrow :: basic_array_stream(
201- c(
202- list (batch1 ),
203- batch_mid ,
204- list (batchn )
205- ),
206- schema = x_schema ,
207- validate = FALSE
208- )
209- }
210-
211-
212- # Utilities for vctr methods
213-
214- vctr_resolve_chunk <- function (x , offsets ) {
215- .Call(geoarrow_c_vctr_chunk_resolve , x , offsets )
216- }
217-
218- vctr_as_slice <- function (x ) {
219- .Call(geoarrow_c_vctr_as_slice , x )
220- }
221-
222- vctr_array_slice <- function (x , offset , length ) {
223- new_offset <- x $ offset + offset
224- new_length <- length
225- nanoarrow :: nanoarrow_array_modify(
226- x ,
227- list (offset = new_offset , length = new_length ),
228- validate = FALSE
229- )
66+ stream <- nanoarrow :: as_nanoarrow_array_stream(x )
67+ as_geoarrow_array_stream(stream , schema = schema )
23068}
0 commit comments