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

Error reading file with JS vanilla #80

Closed
IngenieroGeomatico opened this issue Jul 26, 2024 · 2 comments
Closed

Error reading file with JS vanilla #80

IngenieroGeomatico opened this issue Jul 26, 2024 · 2 comments

Comments

@IngenieroGeomatico
Copy link

Hi.

I am using the library to implement a WEB cartographic application, and for testing, I am using this code:

    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/package/gdal3.js"
    integrity_no="sha384-yW4c2Jx7lsREjJg58+ZI5U6gAso2bRAPw3LdzPWm7z8+rMJ24R7AS+EFyXDPxgYM"
    crossorigin="anonymous">
   </script>


    ....


     initGdalJs({ path: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/package', useWorker: false }).then((Gdal) => {

        const count = Object.keys(Gdal.drivers.raster).length + Object.keys(Gdal.drivers.vector).length;
        console.log(Gdal.drivers);

        Gdal.open(['/vsicurl/https://gist.githubusercontent.com/wavded/1200773/raw/e122cf709898c09758aecfef349964a8d73a83f3/sample.json']).then((dataset) => {
            const data = dataset.datasets[0]
            console.log('data:   ',data)

            Gdal.ogrinfo(data).then((info)=>{
                console.log(info)
            });

        });

    });

but it gives me the following error:

TypeError: t.arrayBuffer is not a function

Thanks forward.

@mthh
Copy link
Contributor

mthh commented Jul 26, 2024

I don't think that vsicurl is supported, but you can download the file and open it with gdal3.js; the library author gives an example here in which he saves the file in the Emscripten FileSystem before opening it: #67 (comment).

@IngenieroGeomatico
Copy link
Author

IngenieroGeomatico commented Jul 28, 2024

Brilliant! Thank you very much, it worked perfectly!

I share the code that has worked for me:

    fetch('https://gist.githubusercontent.com/wavded/1200773/raw/e122cf709898c09758aecfef349964a8d73a83f3/sample.json').then( (response) => {
        // response = response.text()
        response = response.arrayBuffer()
        return response
    })
    .then((response)=>{
        
        console.log('response: ', response)

        initGdalJs({ path: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/package', useWorker: false })
        .then((Gdal) => {
            const count = Object.keys(Gdal.drivers.raster).length + Object.keys(Gdal.drivers.vector).length;
        
            console.log('----------------')
            console.log('GDAL-OGR drivers')
            console.log(Gdal.drivers);
            console.log('----------------')

            if(url.includes('zip' )){
                Gdal.Module.FS.writeFile('/input/file.zip', new Int8Array(response) );
                dataset = Gdal.open('/input/file.zip',[],['vsizip'])
                return Promise.all([dataset, Gdal])

            }
            else{
                Gdal.Module.FS.writeFile('/input/file.file', new Int8Array(response) );
                dataset = Gdal.open('/input/file.file')
                return Promise.all([dataset, Gdal])
            }
            
        })
        .then((values) => {
            dataset = values[0]
            Gdal = values[1]
            console.log('dataset: ',dataset)
            console.log('dataset: ',dataset.datasets[0])
            dataLayer = dataset.datasets[0]
            return Promise.all([dataLayer, Gdal])
        })
        .then((values)=>{
            dataLayer = values[0]
            Gdal = values[1]
            info = Gdal.ogrinfo(dataLayer,['-features','-geom=YES'])
            return Promise.all([info, Gdal])
        })
        .then((values)=>{
            info = values[0]
            Gdal = values[1]
            console.log(info)
        });
    })

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