yocontra / node-gdal-next

Node.js bindings for GDAL (Geospatial Data Abstraction Library) [Fork]
https://contra.io/node-gdal-next/
Apache License 2.0
75 stars 35 forks source link

'AS_XY' is an unexpected value for GEOMETRY creation option of type string-select. #53

Closed stevage closed 2 years ago

stevage commented 2 years ago

I'm trying to generate a CSV file with distinct lat/lon columns.

This works ok:

gdal.drivers.get('csv').create('out.csv', 0, 0, 0, gdal.GDT_Byte, {
  GEOMETRY: 'AS_WKT'
})
...

However if instead of AS_WKT, I use AS_XY, then the later layer.features.add(feature) step throws this error:

'AS_XY' is an unexpected value for GEOMETRY creation option of type string-select.

Doing a command-line test (with gdal 3.3.0) works fine:

 ogr2ogr -f csv test.csv -lco GEOMETRY=AS_XY test.geojson

I don't know know GDAL terribly well, but when I look through the code installed for node-gdal-next (2.8.0), I see support for this option in deps/libgdal/gdal/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp:

        if( EQUAL(pszGeometry, "AS_WKT") )
        {
            poCSVLayer->SetWriteGeometry(
                eGType, OGR_CSV_GEOM_AS_WKT,
                CSLFetchNameValueDef(papszOptions, "GEOMETRY_NAME", "WKT"));
        }
        else if( EQUAL(pszGeometry, "AS_XYZ") ||
                 EQUAL(pszGeometry, "AS_XY") ||
                 EQUAL(pszGeometry, "AS_YX") )
        {

I can see that the message itself comes from here but diagnosing any further in C++ is a bit beyond me.

As a bit of a stab in the dark I note that here only AS_WKT is given as an option, whereas just below, the other options are available.

Maybe that indicates I'm doing something wrong?

The context is here.

stevage commented 2 years ago

Ah, by moving the creation options to output.layers.create(...) rather than outputDriver.create(...) it works. I don't intuitively understand why AS_XY would be an option for creating a "layer" in a CSV file and not for the dataset as a whole, but...I'm glad I have a solution now.

yocontra commented 2 years ago

@stevage Seems like a gdal issue and not something we can fix in this binding - GDAL can be really confusing and inconsistent with the options they provided. If you feel really passionately about it you could follow up by opening an issue on their tracker about it.

stevage commented 2 years ago

Yeah, I wasn't really clear on where it sat when I started the issue. One thing that would be helpful would be clearer documentation about the format of options. For instance:

An array or object containing driver-specific dataset creation options

It's not really obvious how to translate command-line options (-lco GEOMETRY=AS_WKT) into "an array or object". I mean, I guessed { GEOMETRY: 'AS_WKT' } but it was hard to be certain. Not sure what the array format would be - ['GEOMETRY=AS_WKT'] ?