wavded / ogr2ogr

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

Print ogr2ogr CLI version #107

Closed molynerd closed 2 days ago

molynerd commented 1 month ago

In order to validate that my local machine and deployed environment are running the same version of ogr2ogr, it would be nice if this lib had an easy way to print out the details from ogr2ogr --version.

Yes, one could use child_process to run a shell and attempt to gather this information separately, but there isn't a guarantee that child_process and this lib will find the same CLI executable. Additionally, the implementer of this lib would be required to know the path to the ogr2ogr executable, which is already taken care of by the lib.

Possible solution:

import ogr2ogr from 'ogr2ogr'

console.log(ogr2ogr.version());

// GDAL 3.9.0, released 2024/05/07

I have tried using existing functionality, but I'm not able to get around an issue where the lib expects a path to be passed in for the file I'm operating on (in this case, there is no file). Strangely, the command the lib spits out actually does work when run separately, but doesn't work in the lib.

const m = require('ogr2ogr').default;

m('/path/to/a/real/file', {command: '/opt/homebrew/bin/ogr2ogr --version'}).then(console.log);
> Uncaught Error: spawn /opt/homebrew/bin/ogr2ogr --version ENOENT
    at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
    at __node_internal_errnoException (node:internal/errors:623:12)
    at ChildProcess._handle.onexit (node:internal/child_process:284:19)
    at onErrorNT (node:internal/child_process:477:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn /opt/homebrew/bin/ogr2ogr --version',
  path: '/opt/homebrew/bin/ogr2ogr --version',
  spawnargs: [
    '-f',
    'GeoJSON',
    '-skipfailures',
    '/vsistdout/',
    '/Users/nicholas.molyneux/Downloads/X.pdf'
  ],
  cmd: '/opt/homebrew/bin/ogr2ogr --version -f GeoJSON -skipfailures /vsistdout/ /path/to/a/real/file'
}

the command /opt/homebrew/bin/ogr2ogr --version -f GeoJSON -skipfailures /vsistdout/ /path/to/a/real/file does actually work separately.

$ /opt/homebrew/bin/ogr2ogr --version -f GeoJSON -skipfailures /vsistdout/ /path/to/a/real/file
GDAL 3.9.0, released 2024/05/07
wavded commented 1 month ago

I like this idea, should be fairly simple to add. Care to take a swing at a PR?

molynerd commented 4 weeks ago

I like this idea, should be fairly simple to add. Care to take a swing at a PR?

yeah I'll give it a shot! might take me a while though ;)