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

Catiline worker promise not returning failed promise on bad files #68

Closed dot1q closed 8 years ago

dot1q commented 8 years ago

Howdy,

Maybe this is the wrong place, so direct me if need be. Per this example, the worker adds features to the map upon parsing, and prints out the exception on fail

However, when parsing zips that do not contain valid shp files, an error is retrievable when not using a worker, and when using catiline, no error is thrown. It may be that I just don't know what I am doing, but I can't figure out if the shp file I am processing is failing.

var worker = cw({data:wfunc},2); worker.data(cw.makeUrl('../files/nj_muni.zip')).then(function(data){ geo.addData(data); },function(a){ console.log(a); }); worker.data(cw.makeUrl('../files/maSP.zip')).then(function(data){ geo.addData(data); },function(a){ console.log(a); });

My code is very similar.

` var file = "actions/shape-files/view-file.php?name="+filename;

            var wfunc = function(base,cb){
                importScripts('../../dist/shapefile-js/dist/shp.min.js');
                shp(base).then(cb);
            }

            var worker = cw({data:wfunc},1);

            var worker_result = worker.data(cw.makeUrl(file));
            console.log(worker_result);

            worker_result.then(function(geojson){

                //do something with your geojson
                if(geojson == null){
                    $('#main-alert').html("<div class=\"alert alert-danger\"><i class=\"fa fa-thumbs-o-down\" aria-hidden=\"true\"></i> Failed to parse </div>");
                } else {
                    $('#console').val( $('#console').val() + 'Result: Passed\r\n');

                    if(checkType(geojson) === 'array'){
                        for(var i=0; i<geojson.length; i++){
                            $('#console').val( $('#console').val() + 'Shapefile : '+geojson[i].fileName+' - ['+geojson[i].type+']\r\n');
                        }
                        global_json = geojson;
                        process_data(global_json);
                    } else {
                        $('#console').val( $('#console').val() + 'Shapefile : '+geojson.fileName+' - ['+geojson.type+']\r\n');
                        global_json = [geojson];
                        process_data(global_json);
                    }

                    //console.log(geojson);
                }
            },function(a){console.log(a)});`
calvinmetcalf commented 8 years ago

you should change

  var wfunc = function(base,cb){
                importScripts('../../dist/shapefile-js/dist/shp.min.js');
                shp(base).then(cb);
            }

to be

  var wfunc = function(base,cb){
                importScripts('../../dist/shapefile-js/dist/shp.min.js');
                shp(base).then(function (res) {
                    cb({ok: true, data: res});
                 }, function (err) {
                    cb({ok: false, data: err});
                 });
            }

then you can change

var worker_result = worker.data(cw.makeUrl(file));

to

var worker_result = worker.data(cw.makeUrl(file)).then(function (stuff){
  if (stuff.ok) {
     return stuff.data;
  }
  throw stuff.data;
});