Skip to content

Commit 9183e8b

Browse files
committed
used shared DuckDB connection
1 parent 36f3aaa commit 9183e8b

5 files changed

Lines changed: 19 additions & 15 deletions

File tree

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ importFrom(dplyr,sql)
164164
importFrom(dplyr,tally)
165165
importFrom(duckspatial,as_duckspatial_df)
166166
importFrom(duckspatial,ddbs_bbox)
167-
importFrom(duckspatial,ddbs_create_conn)
168167
importFrom(duckspatial,ddbs_intersects)
169168
importFrom(duckspatial,ddbs_open_dataset)
170169
importFrom(duckspatial,ddbs_write_table)

R/mask.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ setMethod(".mask", c("SpatialDataImage", "SpatialDataLabel"), \(i, j, how=NULL,
120120
"POINT"=mutate(data(j), geometry=ST_Buffer(geometry, radius)),
121121
data(j))
122122
ddbs_intersects(df_j, data(i), sparse=TRUE)
123+
123124
}
124125

125126
#' @noRd

R/read.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ readPoint <- function(x, ...) {
7575
pq <- list.files(x, "\\.parquet$", full.names=TRUE)
7676
md <- read_zarr_attributes(x)
7777
ax <- unlist(md$axes)
78-
df <- ddbs_open_dataset(pq) |>
78+
df <- ddbs_open_dataset(pq, conn=CONN) |>
7979
mutate(geometry=sql(sprintf("ST_Point(%s, %s)", ax[1], ax[2]))) |>
8080
as_duckspatial_df(crs=NA_character_) |>
8181
select(-all_of(ax))
8282
SpatialDataPoint(data=df, meta=SpatialDataAttrs(md))
8383
}
8484

85+
# create DuckDB connection
86+
# (to be used everywhere!)
87+
CONN <- duckspatial::ddbs_create_conn()
88+
8589
#' @rdname readSpatialData
8690
#' @importFrom Rarr read_zarr_attributes
8791
#' @importFrom duckspatial ddbs_open_dataset
@@ -90,7 +94,8 @@ readPoint <- function(x, ...) {
9094
readShape <- function(x, ...) {
9195
md <- read_zarr_attributes(x)
9296
pq <- list.files(x, "\\.parquet$", full.names=TRUE)
93-
SpatialDataShape(data=ddbs_open_dataset(pq), meta=SpatialDataAttrs(md))
97+
df <- ddbs_open_dataset(pq, conn=CONN, crs=NA_character_)
98+
SpatialDataShape(data=df, meta=SpatialDataAttrs(md))
9499
}
95100

96101
#' @export

R/sdFrame.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ SpatialDataPoint <- \(data=NULL, meta=SpatialDataAttrs(type="frame"), metadata=l
114114
"found: ", paste(gt, collapse=", "))
115115
# always ensure internal data is 'duckspatial_df'
116116
if (!is(data, "duckspatial_df"))
117-
data <- as_duckspatial_df(data, crs=NA)
117+
data <- as_duckspatial_df(data, crs=NA_character_)
118118
}
119119
# update 'spatialdata_attrs' if keys are provided
120120
za <- as.list(meta)
@@ -138,23 +138,23 @@ SpatialDataPoint <- \(data=NULL, meta=SpatialDataAttrs(type="frame"), metadata=l
138138
#' @rdname SpatialDataFrame
139139
#' @importFrom methods is
140140
#' @importFrom S4Vectors metadata<-
141-
#' @importFrom duckspatial ddbs_create_conn
142141
#' @importFrom duckspatial ddbs_write_table
143142
#' @importFrom duckspatial as_duckspatial_df
144143
SpatialDataShape <- \(data=NULL, meta=SpatialDataAttrs(type="frame"), metadata=list(), ...) {
145144
data <- .df_to_sf(data, "POLYGON")
146145
# always ensure internal data is 'duckspatial_df'
147-
if (isTRUE(nrow(data) > 0L) &&
148-
!is(data, "duckspatial_df")) {
149-
conn <- ddbs_create_conn()
146+
if (!is(data, "duckspatial_df") && isTRUE(nrow(data) > 0L)) {
147+
suppressMessages( # silent complaint re: missing CRS
150148
ddbs_write_table(
151-
conn=conn,
149+
conn=CONN,
152150
data=data,
153151
name="sdShape",
154152
overwrite=TRUE,
155-
temp_view=FALSE)
153+
temp_view=FALSE))
156154
data <- as_duckspatial_df(
157-
x="sdShape", conn=conn, crs=NA,
155+
x="sdShape",
156+
conn=CONN,
157+
crs=NA_character_,
158158
geom_col=attr(data, "sf_column"))
159159
}
160160
x <- .SpatialDataShape(data=data, meta=meta, ...)

tests/testthat/test-mask.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test_that("mask,unaligned", {
2121
# non-existent
2222
expect_error(
2323
mask(x, i, j, "x"),
24-
"should be \"global\"")
24+
"should be global")
2525

2626
# not shared
2727
za <- meta(image(x, i))
@@ -157,9 +157,8 @@ test_that("mask,sdShape,sdShape", {
157157
y <- setTable(x, i, se)
158158

159159
# out-of-bounds masking
160-
t <- translation(s, c(1e3,1e3))
161-
shape(y, "out") <- t
162-
expect_error(mask(y, i, "out"))
160+
shape(y, "out") <- translation(s, c(1e3,1e3))
161+
expect_error(mask(y, i, "out", how="sum"))
163162

164163
# note: data at "0" are from non-intersecting instances;
165164
# here, all data should be aggregated to column "1"

0 commit comments

Comments
 (0)