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

Jgraves/app 2163 #78

Closed
wants to merge 11 commits into from
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# shp-write

Writes shapefile in pure javascript. Uses [dbf](https://github.com/tmcw/dbf)
for the data component, and [jsZIP](http://stuk.github.io/jszip/) to generate
for the data component, [FileSaver](https://github.com/eligrey/FileSaver.js/) and [jsZIP](http://stuk.github.io/jszip/) to generate
ZIP file downloads in-browser.

## Usage
Expand All @@ -22,7 +22,7 @@ Or in a browser
support
* Geometries: Point, LineString, Polygon, MultiLineString, MultiPolygon
* Tabular-style properties export with Shapefile's field name length limit
* Uses jsZip for ZIP files, but [compression is buggy](https://github.com/Stuk/jszip/issues/53) so it uses STORE instead of DEFLATE.
* Uses jsZip for ZIP files.

## Example

Expand All @@ -36,7 +36,8 @@ var options = {
point: 'mypoints',
polygon: 'mypolygons',
line: 'mylines'
}
},
filename: 'my-filename'
}
// a GeoJSON bridge for features
shpwrite.download({
Expand Down Expand Up @@ -104,3 +105,4 @@ object.
## Contributors

* Nick Baugh <[email protected]>
* Jason Graves <[email protected]>
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shp-write",
"version": "0.3.2",
"version": "0.3.5",
"description": "write shapefiles from pure javascript",
"main": "index.js",
"scripts": {
Expand All @@ -27,6 +27,10 @@
{
"name": "Nick Baugh",
"email": "[email protected]"
},
{
"name": "Jason Graves",
"email": "[email protected]"
}
],
"license": "BSD-2-Clause",
Expand All @@ -35,7 +39,8 @@
},
"dependencies": {
"dbf": "0.1.4",
"jszip": "2.5.0"
"jszip": "3.2.0",
"file-saver": "^2.0.2"
},
"devDependencies": {
"browserify": "^13.0.0",
Expand Down
9 changes: 7 additions & 2 deletions src/download.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var zip = require('./zip');
var FileSaver = require('file-saver');

module.exports = function(gj, options) {
var content = zip(gj, options);
location.href = 'data:application/zip;base64,' + content;
zip(gj, options)
.then(content => {
options = options ? options : {};
const filename = options.filename ? `${options.filename}.zip` : "download.zip";
FileSaver.saveAs(content, filename);
});
};
8 changes: 5 additions & 3 deletions src/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(gj, options) {
// geometries
l.geometries,
function(err, files) {
var fileName = options && options.types[l.type.toLowerCase()] ? options.types[l.type.toLowerCase()] : l.type;
var fileName = options && options.types && options.types[l.type.toLowerCase()] ? options.types[l.type.toLowerCase()] : l.type;
layers.file(fileName + '.shp', files.shp.buffer, { binary: true });
layers.file(fileName + '.shx', files.shx.buffer, { binary: true });
layers.file(fileName + '.dbf', files.dbf.buffer, { binary: true });
Expand All @@ -28,11 +28,13 @@ module.exports = function(gj, options) {
}
});

var generateOptions = { compression:'STORE' };
const generateOptions = {
type: "blob"
};

if (!process.browser) {
generateOptions.type = 'nodebuffer';
}

return zip.generate(generateOptions);
return zip.generateAsync(generateOptions);
};