wavded / ogr2ogr

An ogr2ogr wrapper library
MIT License
214 stars 46 forks source link

Unknown option name '-spat' #95

Closed bertday closed 1 year ago

bertday commented 1 year ago

Hello!

I'm trying to load in a GeoJSON file into a Postgres database, filtering by a bounding box with -spat (docs).

Here is a minimal example:

import ogr2ogr from 'ogr2ogr';

(async () => {
  await ogr2ogr(
    '/Users/me/parcels.json',
    {
      options: [
        '-spat', '-87.94011408252348 41.64454312178303 -87.5241371038952 42.023038586147585',
      ],
    }
  );
})();

That throws an error:

ERROR 6: Unknown option name '-spat'
    at __node_internal_genericNodeError (internal/errors:863:15)
    at exithandler (child_process:412:12)
    at emit (events:513:28)
    at maybeClose (internal/child_process:1091:16)
    at <anonymous> (internal/child_process:449:11)
    at emit (events:513:28)
    at <anonymous> (net:313:12)
    at callbackTrampoline (internal/async_hooks:130:17)

If I take the cmd from ogr2ogr and run it by hand in the shell, it runs fine.

Interestingly, in more complex examples I can sometimes get -spat to work depending on where it is in my options array. It seems to work if it comes before -nlt, but not after. I've tried quoting it and that didn't help.

bertday commented 1 year ago

Problem solved; I needed to pass the -spat values in as individual arguments. Helpful find:

https://github.com/OSGeo/gdal/issues/1066

I'm sure this is just a child_process thing, but I had a bear of a time with it. I'm wondering if maybe there could be an input-validation step before calling the shell command, maybe one that looks for spaces in arguments and spits out a warning to break them up? I'd be happy to do a PR if that sounds desirable.

wavded commented 1 year ago

Glad you figured it out. I don't think we want to assume arguments should be split up by space. We just pass them to the raw ogr2ogr command. Given the breathe of commands you can run with ogr2ogr, this library doesn't try to make many assumptions as to how it will be used.