Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load buffer or arraybuffer in load function #164

Open
ghost opened this issue Aug 14, 2020 · 6 comments
Open

Load buffer or arraybuffer in load function #164

ghost opened this issue Aug 14, 2020 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 14, 2020

Is your feature request related to a problem? Please describe.

My tiff file is in the local system. I would like to load buffer or arraybuffer instead of giving url to load function.
By looking at the code it seems pretty simple to implement this feature

async function load (urlOrFile) {

  if (!inBrowser && typeof urlOrFile === 'object') {
    throw new Error(ERROR_LOAD_FILE_OUTSIDE_BROWSER);
  }

  const url = typeof urlOrFile === 'object' ? URL.createObjectURL(urlOrFile) : urlOrFile;

  if (!cache[url]) {
    const response = await fetchWithErrorHandling(url);
    const arrayBuffer = await toArrayBuffer(response);
    const georaster = await parseGeorasterWithErrorHandling(arrayBuffer);
    cache[url] = georaster;
  }
  return cache[url];
}

export default load;

I am running this code in node.

Describe the solution you'd like

I would like to add a PR to accept a buffer or array buffer as an input.

@DanielJDufour
Copy link
Member

Hi, @rahulbreezo . Good idea! A Pull Request would be much appreciated! Thank you!

@ghost
Copy link
Author

ghost commented Aug 14, 2020

Thanks, will do that ASAP.

@sudoStatus200
Copy link

sudoStatus200 commented Aug 16, 2020

@rahulbreezo is my work account.
@DanielJDufour Hi, I was thinking How should I take buffer input?

  1. I can create a new small function just to accept arrayBuffer then call parseGeorasterWithErrorHandling
    or
  2. In the same load function I can accept a second argument option where user can specify the type of input( buffer, arraybuffer, url).

like this


load($arraybyffer, {type: arraybuffer});

//or
load($arraybyffer, {type: buffer});

//or
load($url, {type: url});



Can you suggest which approach should I pick? In my opinion I think I should go with the 2nd approach.

@DanielJDufour
Copy link
Member

I know it's a lot to ask, but could we try to make it work, so that it automatically determines the type of input and processes it accordingly. In other words, make it so the following work:

load(arrayBuffer); 
load(buffer); // NodeJS' buffer https://nodejs.org/api/buffer.html
load(url);
load(file); // https://developer.mozilla.org/en-US/docs/Web/API/File

I sorta see load as a higher level functions that makes it easier for users to give anything to load and it works. Here's a few functions that might help.

Let me know what you think!

@sudoStatus200
Copy link

I also wanted to do the same thing but then I checked Buffer.isBuffer does not work in the browser, So I thought I would take option param in the load where the user itself mention the input type.

But no problem, I will make it work as you said.

@DanielJDufour
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants