calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
MIT License
735 stars 230 forks source link

local files #48

Closed michaelvaneck closed 7 years ago

michaelvaneck commented 9 years ago

So is there any way how to use this with local files? I'm using AngularJS with File API to upload my files. I'd rather process it locally then upload it to my server, return the path and then process it.

or maybe use dataUrl?

calvinmetcalf commented 9 years ago

yes you can pass it an array buffer

michaelvaneck commented 9 years ago

I continued working on it but even when giving it an ArrayBuffer it somehow doesn't work?

(file is just a .shp file OR a .zip with everything in it)

function readFile(file) {
        $log.info('mapcontroller: reading File...');
        $log.info('mapcontroller: Filetype: ' + file.type);
        $log.info(file);

        var reader = new FileReader();
        var byteArray = undefined;

        reader.onload = function (e) {
            byteArray = e.target.result;
            $log.info('mapcontroller: creating ArrayBuffer');
            $log.info(byteArray);
        }
        reader.onerror = function (event) {
            console.error("File could not be read! Code " + event.target.error.code);
        };

        //reader.readAsText(file);
        reader.readAsArrayBuffer(file);

        shp(byteArray).then(function (floep) {
            $log.info(floep);
        });

    }
calvinmetcalf commented 9 years ago

Add a .catch(onErrFn) to the end of the promise chain (after the .then()) to see any errors that are thrown.

On Mon, Oct 5, 2015, 5:53 AM Michael van Eck notifications@github.com wrote:

I continued working on it but even when giving it an ArrayBuffer it somehow doesn't work?

function readFile(file) { $log.info('mapcontroller: reading File...'); $log.info('mapcontroller: Filetype: ' + file.type); $log.info(file);

    var reader = new FileReader();
    var byteArray = undefined;

    reader.onload = function (e) {
        byteArray = e.target.result;
        $log.info('mapcontroller: creating ArrayBuffer');
        $log.info(byteArray);
    }
    reader.onerror = function (event) {
        console.error("File could not be read! Code " + event.target.error.code);
    };

    //reader.readAsText(file);
    reader.readAsArrayBuffer(file);

    shp(byteArray).then(function (floep) {
        $log.info(floep);
    });

}

— Reply to this email directly or view it on GitHub https://github.com/calvinmetcalf/shapefile-js/issues/48#issuecomment-145484007 .

michaelvaneck commented 9 years ago

Got it working, thx for the fast responses!!

Shraddhaz commented 6 years ago

@michaelvaneck @calvinmetcalf What is the fix for this ? I am having the same issue. I want to convert the shp file read by arraybuffer to geojson. The .zip works fine, but not the .shp. Any idea ?

calvinmetcalf commented 6 years ago

by default you can only do .zip locally and .shp from remote urls

Shraddhaz commented 6 years ago

Is there no way to do .shp locally using shapefile-js ?

calvinmetcalf commented 6 years ago

yeah you need to call shp.combine([shp.parseShp(shpArrayBuffer, projString), shp.parseDbf(dbfArrayBuffer)])

so to break that down, shp.parseShp takes an array buffer corresponding to the .shp file and an optional string corisponding to the .prj file, then shp.parseDbf take an array buffer corresponding to the .dbf file and the shp.combine takes an array of the results of those 2 functions.