Skip to content

Commit 26fc58c

Browse files
committed
feat: enhance ddbs_describe_geometry_col to improve geometry type detection and add test for handling non-spatial 'geometry' columns
1 parent 335f252 commit 26fc58c

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

R/io_open_dataset.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,26 @@ ddbs_describe_geometry_col <- function(desc, geom_col = NULL) {
403403
if (!is.null(geom_col) || is.null(desc) || nrow(desc) == 0) {
404404
return(geom_col)
405405
}
406-
406+
407407
col_type <- if ("column_type" %in% names(desc)) desc$column_type else desc$data_type
408-
geom_cols <- desc$column_name[grepl("^GEOMETRY(\\(|$)", col_type, ignore.case = TRUE)]
408+
409+
is_geometry_type <- grepl("^GEOMETRY(\\(|$)", col_type, ignore.case = TRUE)
410+
is_wkb_type <- grepl("WKB_BLOB|^BLOB$", col_type, ignore.case = TRUE)
411+
is_struct_type <- grepl("^STRUCT", col_type, ignore.case = TRUE)
412+
is_spatial_type <- is_geometry_type | is_wkb_type | is_struct_type
413+
414+
geom_cols <- desc$column_name[is_geometry_type]
409415
if (length(geom_cols) > 0) {
410416
return(geom_cols[1])
411417
}
412418

413419
known <- c("geom", "geometry", "wkb_geometry")
414-
found <- desc$column_name[desc$column_name %in% known]
420+
found <- desc$column_name[desc$column_name %in% known & is_spatial_type]
415421
if (length(found) > 0) {
416422
return(found[1])
417423
}
418424

419-
wkb_cols <- desc$column_name[grepl("WKB_BLOB", col_type, ignore.case = TRUE)]
425+
wkb_cols <- desc$column_name[is_wkb_type]
420426
if (length(wkb_cols) > 0) {
421427
return(wkb_cols[1])
422428
}

tests/testthat/test-ddbs_write_dataset.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ test_that("ddbs_write_dataset works for Parquet", {
3131

3232
tmp_file <- tempfile(fileext = ".parquet")
3333
on.exit(unlink(tmp_file), add = TRUE)
34-
on.exit(unlink(tmp_file), add = TRUE)
3534
expect_no_error(ddbs_write_dataset(ds, tmp_file, quiet = TRUE))
3635
expect_true(file.exists(tmp_file))
3736

tests/testthat/test-formats_autodetect.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,22 @@ test_that("ddbs_open_dataset warns when file has no CRS", {
119119
expect_true(is.na(sf::st_crs(attr(ds, "crs"))$epsg))
120120
})
121121

122+
test_that("ddbs_open_dataset ignores non-spatial columns named 'geometry'", {
123+
skip_if_not_installed("duckdb")
124+
125+
conn <- tryCatch(ddbs_default_conn(), error = function(e) DBI::dbConnect(duckdb::duckdb()))
126+
ddbs_install(conn, quiet = TRUE)
127+
ddbs_load(conn, quiet = TRUE)
128+
129+
# Create a CSV with a column named "geometry" that is just text
130+
tmp_csv <- tempfile(fileext = ".csv")
131+
writeLines("id,geometry,value\n1,this is not a geometry,10", tmp_csv)
132+
on.exit(unlink(tmp_csv), add = TRUE)
133+
134+
# Should open as a regular table, not a duckspatial_df
135+
ds <- duckspatial::ddbs_open_dataset(tmp_csv, conn = conn)
136+
137+
expect_false(inherits(ds, "duckspatial_df"))
138+
expect_true(inherits(ds, "tbl_lazy"))
139+
})
140+

0 commit comments

Comments
 (0)