bugra9 / gdal3.js

Convert raster and vector geospatial data to various formats and coordinate systems entirely in the browser.
https://gdal3.js.org
GNU Lesser General Public License v2.1
300 stars 45 forks source link

using vsicurl in node / browser ? #67

Open vsharma-next opened 5 months ago

vsharma-next commented 5 months ago

Hello. Love this work. I think this might be one of those watershed experiments that will have a big impact.

Q: Is vsicurl supported ? I would love to replace geotiff.js dependency and just use gdal's vsicurl. This is what I have tried.

code :

//vsicurl.js

const initGdalJs = require('gdal3.js/node');

async function main() {
  try {
    const Gdal = await initGdalJs();

    const result = await Gdal.open(
      'http://127.0.0.1:8080/data/simple-polygon-line-point.tif',
      [],
      ['vsicurl']
    );
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}

main();

result:

node vsicurl.js

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "ErrnoError: FS error".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

Node.js v18.16.0

Since @rouault seems to also be following this effort, perhaps he can comment as well ?

I have tried it in the browser and it does not work there either - perhaps not too much of a surprise will all the sandbox/security stuff.

Any suggestions ?

Thanks !

bugra9 commented 5 months ago

Hi @vsharma-next,

I am glad you love this work, thank you.

/vsicurl/ is not currently supported. I compiled Gdal with Curl to support it but couldn't get it to work. The issue is that Curl is not fully compatible with emscripten. I'll try again when I have more time.

Alternatively, I can release a new version by making the Emscripten Filesystem API public so you can use the following code:

const data = await fetch('http://127.0.0.1:8080/data/simple-polygon-line-point.tif').then(response => response.arrayBuffer());
Gdal.FS.writeFile('/input/simple-polygon-line-point.tif', new Int8Array(data));
const result = await Gdal.open('/input/simple-polygon-line-point.tif');

Would that work for you?

vsharma-next commented 5 months ago

This is a great idea. I think given that OPFS will become more and more prevalent, this API will come in handy. I googled a bit and it seems that Emscripten Filesystem API will be overhauld by WasmFS API which will most likely include support for OPFS (https://github.com/emscripten-core/emscripten/blob/main/test/wasmfs/wasmfs_opfs.c).

bugra9 commented 5 months ago

@vsharma-next, I exported the Module from Emscripten to version 2.8.0. Can you try the code below with this version?

const Gdal = await initGdalJs();

const data = await fetch('https://gdal3.js.org/test/data/simple-polygon-line-point.tif').then(response => response.arrayBuffer());
Gdal.Module.FS.writeFile('/input/simple-polygon-line-point.tif', new Int8Array(data));
const result = await Gdal.open('/input/simple-polygon-line-point.tif');
console.log(result);