mbloch / mapshaper

Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
http://mapshaper.org
Other
3.74k stars 532 forks source link

The -i command cannot be run in the browser #612

Closed lautarodapin-magoya closed 10 months ago

lautarodapin-magoya commented 10 months ago

I'm getting this error UserError: [i] The -i command cannot be run in the browser when trying to use mapshaper in the browser

image

I have the scriipts modules.js and mapshaper.js present, the an input to read the shapefile, then i call the following code

  const toGeojson = (e: any) => {
        const files = [...e.target.files];
        const input = files.reduce((acc, curr) => {
            acc[curr.name] = curr;
            return acc;
        }, {});
        const cmd = `${files.map((file) => `-i ${file.name}`).join(' ')} -o  format=geojson`;
        mapshaper.applyCommands(cmd, input, (err, output) => {
            console.log(err);
            console.log(output);
        });
    };

601 #407 i checked this two issues but i couldnt solve it

lautarodapin-magoya commented 10 months ago

I change the code because ii read the the input filies must be a arraybuffer to but i get the same error


 const files = [...e.target.files];
        const buffers = await Promise.all(files.map((file) => fileRead(file)));
        const input = files.reduce((acc, curr, i) => {
            acc[curr.name] = buffers[i];
            return acc;
        }, {});
        const names = files.map((file) => file.name).join(' -i ');
        const cmd = `${files.map((file) => `-i ${file.name}`).join(' ')} -o  format=geojson`;
        console.log({cmd, input});
        // mapshaper.runCommands(`${files.map((file) => `-i ${file.name}`).join(' ')} -o geojson.json format=geojson`);
        mapshaper.applyCommands(cmd, input, (err, output) => {
            console.log(err);
            console.log(output);
        });
mbloch commented 10 months ago

I see... you want to use the JS api in a web browser. The -i command doesn't work in the console of the mapshaper web UI, which is why I added the error message that you're seeing. I'll try to get this fixed for you in the next release. Thanks for filing this issue :)

lautarodapin-magoya commented 10 months ago

thanks for the fast response! @mbloch how the https://mapshaper.org/ works? i try to search in the source code how it does it, but i couldn't solve it

mbloch commented 10 months ago

Hi, I think that I fixed the problem that you were having using applyCommands in the browser. You can try v0.6.51, which I just published.

https://mapshaper.org just serves a static web page, it's the same page as index.html in the www/ folder of the github repo. Mapshaper will mostly work if you open the index.html file in a web browser.