duckdb / duckdb_spatial

MIT License
483 stars 39 forks source link

distinguish between a column in WKB and a column in native 'geometry' format? #299

Closed cboettig closed 6 months ago

cboettig commented 7 months ago

Sorry if this is a silly question, but what is the best way to tell if a geometry column in the native geom representation or the WKB blob?

Did I get that right? As such, in writing a workflow or toolchain that is designed to operate on either possible input format, I'm a bit lost as to how best to decide when need to do conversion to a native format and when I already have one. Naively, it feels like the simplest solution would be if the coercions didn't fail if the input column was already in the desired format, e.g. if st_AsWKB() was happy doing a no-op when given something that was already WKB, and vice versa?

Apologies if I'm thinking about this all wrong, advice appreciated!

Maxxen commented 7 months ago

Did I get that right?

Yes. We've been talking about adding something like a generic try() operator that would enable similar behavior to TRY_CAST but for non-cast conversion functions. I guess for now you could also roll your own with something like

SELECT 
  CASE 
    WHEN typeof(geom_or_blob) = 'GEOMETRY' 
    THEN ST_AsWKB(geom_or_blob) 
    ELSE geom_or_blob 
  END AS wkb
FROM t1;

Additionally, ST_Read takes an additional keep_wkb = true parameter that will make it output geometries as WKB_BLOB type, which is just an alias over BLOB that are implicitly castable to BLOB as well as GEOMETRY. You can pass these to COPY (FORMAT GDAL) too.