duckdb / duckdb_spatial

MIT License
492 stars 41 forks source link

Specify layer name for writing to FileGDB? #265

Open DanCranford opened 9 months ago

DanCranford commented 9 months ago

Hey there -

Is it possible to specify a layer name (feature class or table) when writing to a file geodatabase?

The following code creates a file geodatabase named "testing" and a feature class named "testing".

COPY 
    (SELECT ST_Point(1,2) as geom, 10 as i) 
TO 
    './testing.gdb' 
WITH (FORMAT 'GDAL', 
      DRIVER 'OpenFileGDB', 
      GEOMETRY_TYPE 'POINT', 
      LAYER_CREATION_OPTIONS 'LAYER_ALIAS=alias');

It appears, based on the OpenFileGDB driver documentation from GDAL, that you can specify the file geodatabase and feature class name when copying.

Thanks!

Maxxen commented 9 months ago

What you are looking for is passing the layer_name argument like this

COPY 
    (SELECT ST_Point(1,2) as geom, 10 as i) 
TO 
    './testing.gdb' 
WITH (FORMAT 'GDAL', 
      DRIVER 'OpenFileGDB', 
      GEOMETRY_TYPE 'POINT',
      LAYER_NAME 'My layer name');

The LAYER_ALIAS LAYER_CREATION_OPTION only sets the alias in the metadata. Im not actually sure what that does since I don't use any ESRI products, but it's not something we make use of when reading filegdbs anyway.

DanCranford commented 8 months ago

Thanks @Maxxen! That's exactly what I was looking for. Is it possible to write to an existing file geodatabase? I've noticed that if I try to create two datasets in the same database, I get the following error.

IOException                               Traceback (most recent call last)
~\AppData\Local\Temp\1\ipykernel_14812\3340377106.py in <cell line: 1>()
----> 1 conn.execute("""COPY 
      2     (SELECT ST_Point(1,2) as geom, 10 as i)
      3 TO
      4     './gdb_name.gdb'
      5 WITH (FORMAT 'GDAL', 

IOException: IO Error: GDAL Error (1): ./gdb_name.gdb already exists.
Maxxen commented 8 months ago

No, I don't think this is supported yet, but it might be possible to implement in the future.