Skip to content

Commit

Permalink
Add support for arraybuffer in workers
Browse files Browse the repository at this point in the history
  • Loading branch information
elesdoar committed Aug 13, 2015
1 parent 478969a commit c8f7eb2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 31 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[{package,bower}.json]
indent_style = space
indent_size = 2

[*.{js,less}]
indent_style = space
indent_size = 2
24 changes: 24 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true,
"jquery": true,
"browser": true,
"predef": ["shp", "define", "cw", "L"]
}
85 changes: 54 additions & 31 deletions leaflet.shpfile.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
'use strict';

/* global cw, shp */
L.Shapefile =L.GeoJSON.extend({
initialize: function (file, options, importUrl) {
if(typeof cw !== 'undefined'){
importUrl = importUrl || 'shp.js';
this.worker = cw(new Function('data', 'cb', 'importScripts("' + importUrl + '");shp(data).then(cb);'));
}
L.GeoJSON.prototype.initialize.call(this,{features:[]},options);
this.addFileData(file);
},
addFileData:function(file){
var self = this;
self.fire('data:loading');
if(typeof file !== 'string' && !('byteLength' in file)){
var data = self.addData(file);
self.fire('data:loaded');
return data;
}
if(self.worker){
self.worker.data(cw.makeUrl(file)).then(function(data){
self.addData(data);
self.fire('data:loaded');
self.worker.close();
});
}else{
shp(file).then(function(data){
self.addData(data);
self.fire('data:loaded');
});
}
return this;
options: {
isArrayBufer: false,
importUrl: 'shp.js'
},

initialize: function (file, options) {
L.Util.setOptions(this, options);
if(typeof cw !== 'undefined'){
/*jslint evil: true */
if(!options.isArrayBufer) {
this.worker = cw(new Function('data', 'cb', 'importScripts("' + this.options.importUrl + '");shp(data).then(cb);'));
} else {
this.worker = cw(new Function('data', 'importScripts("' + this.options.importUrl + '"); return shp.parseZip(data);'));
}
}
L.GeoJSON.prototype.initialize.call(this, {features:[]}, options);
this.addFileData(file);
},

addFileData:function(file) {
var self = this;
self.fire('data:loading');
if(typeof file !== 'string' && !('byteLength' in file)) {
var data = self.addData(file);
self.fire('data:loaded');
return data;
}

if(self.worker) {
if(!self.options.isArrayBufer) {
self.worker.data(cw.makeUrl(file)).then(function(data){
self.addData(data);
self.fire('data:loaded');
self.worker.close();
});
} else {
self.worker.data(file, [file]).then(function(data){
self.addData(data);
self.fire('data:loaded');
self.worker.close();
});
}
} else{
shp(file).then(function(data){
self.addData(data);
self.fire('data:loaded');
});
}
return this;
}
});

L.shapefile= function(a,b,c){
return new L.Shapefile(a,b,c);
}
return new L.Shapefile(a,b,c);
};

0 comments on commit c8f7eb2

Please sign in to comment.