Skip to content

Commit 335f252

Browse files
committed
feat: update get_parquet_crs to handle JSON extraction for CRS and add tests for column names with special characters
1 parent 31c9945 commit 335f252

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

R/utils_format.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ get_parquet_crs <- function(path, conn) {
6868
WHERE decode(key) = 'geo'
6969
)
7070
SELECT
71-
json_extract_string(meta, '$.columns.' || (meta->>'primary_column') || '.crs') as crs
71+
json_extract(meta->'columns', meta->>'primary_column')->>'crs' as crs
7272
FROM geo_meta
7373
LIMIT 1
7474
")

tests/testthat/test-utils_format.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
test_that("get_parquet_crs handles column names with dots and special characters", {
2+
skip_if_not_installed("sf")
3+
skip_if_not_installed("duckdb")
4+
5+
# Create a simple sf dataframe with a geometry column containing a dot
6+
# Use a non-4326 CRS (like 3857) so GDAL explicitly writes the PROJJSON crs property
7+
df <- data.frame(id = 1L)
8+
df[["geom.1"]] <- sf::st_sfc(sf::st_point(c(0, 0)), crs = 3857)
9+
df_sf <- sf::st_sf(df, sf_column_name = "geom.1")
10+
11+
tmp_pq <- tempfile(fileext = ".parquet")
12+
# Use layer_options to force GDAL to keep the geometry column name
13+
sf::st_write(df_sf, tmp_pq, driver = "Parquet", layer_options = "GEOMETRY_NAME=geom.1", quiet = TRUE)
14+
on.exit(unlink(tmp_pq), add = TRUE)
15+
16+
conn <- tryCatch(ddbs_default_conn(), error = function(e) DBI::dbConnect(duckdb::duckdb()))
17+
on.exit(DBI::dbDisconnect(conn, shutdown = TRUE), add = TRUE)
18+
19+
duckspatial::ddbs_install(conn, extension = "json", quiet = TRUE)
20+
duckspatial::ddbs_load(conn, extension = "json", quiet = TRUE)
21+
22+
# Call the unexported function
23+
crs <- duckspatial:::get_parquet_crs(tmp_pq, conn)
24+
25+
expect_false(is.null(crs))
26+
expect_equal(crs$epsg, 3857)
27+
})
28+
29+
test_that("get_parquet_crs handles column names with spaces", {
30+
skip_if_not_installed("sf")
31+
skip_if_not_installed("duckdb")
32+
33+
# Create a simple sf dataframe with a geometry column containing a space
34+
df <- data.frame(id = 1L)
35+
df[["my geom"]] <- sf::st_sfc(sf::st_point(c(0, 0)), crs = 3857)
36+
df_sf <- sf::st_sf(df, sf_column_name = "my geom")
37+
38+
tmp_pq <- tempfile(fileext = ".parquet")
39+
sf::st_write(df_sf, tmp_pq, driver = "Parquet", layer_options = "GEOMETRY_NAME=my geom", quiet = TRUE)
40+
on.exit(unlink(tmp_pq), add = TRUE)
41+
42+
conn <- tryCatch(ddbs_default_conn(), error = function(e) DBI::dbConnect(duckdb::duckdb()))
43+
on.exit(DBI::dbDisconnect(conn, shutdown = TRUE), add = TRUE)
44+
45+
duckspatial::ddbs_install(conn, extension = "json", quiet = TRUE)
46+
duckspatial::ddbs_load(conn, extension = "json", quiet = TRUE)
47+
48+
crs <- duckspatial:::get_parquet_crs(tmp_pq, conn)
49+
50+
expect_false(is.null(crs))
51+
expect_equal(crs$epsg, 3857)
52+
})

0 commit comments

Comments
 (0)