diff --git a/.gitignore b/.gitignore index 3db12b15..2f6661c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ shpwrite.js +node_modules diff --git a/README.md b/README.md index b04c03e0..fb3dd36d 100644 --- a/README.md +++ b/README.md @@ -16,26 +16,31 @@ Or in a browser https://unpkg.com/shp-write@latest/shpwrite.js +## Testing + +To test the download functionality run `npm run make-test` and open index.html in browser. +This should start an immediate download of test features defined in `indexTest.js`. + ## Caveats * Requires a capable fancy modern browser with [Typed Arrays](http://caniuse.com/#feat=typedarrays) 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. ## Example ```js var shpwrite = require('shp-write'); -// (optional) set names for feature types and zipped folder +// (optional) set names for zip file, zipped folder and feature types var options = { + file: 'myshapes' folder: 'myshapes', types: { point: 'mypoints', polygon: 'mypolygons', - line: 'mylines' + polyline: 'mylines' } } // a GeoJSON bridge for features diff --git a/index.html b/index.html index f856a0a1..46db3182 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ - + diff --git a/indexTest.js b/indexTest.js new file mode 100644 index 00000000..aa3a9dcd --- /dev/null +++ b/indexTest.js @@ -0,0 +1,85 @@ +require('./src/download')({ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.936046600341797, + 60.175245406790246 + ], + [ + 24.920597076416016, + 60.15577400466598 + ], + [ + 24.953556060791016, + 60.1570553725571 + ], + [ + 24.936046600341797, + 60.175245406790246 + ] + ], + [ + [ + 24.93523120880127, + 60.169247224327165 + ], + [ + 24.945573806762695, + 60.15874243076889 + ], + [ + 24.928064346313477, + 60.15825127085746 + ], + [ + 24.93523120880127, + 60.169247224327165 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 24.920940399169922, + 60.17977000114811 + ], + [ + 24.953556060791016, + 60.17486121440947 + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 24.925403594970703, + 60.171830205844614 + ] + } + } + ] +}, { + file: 'shapefiles', + folder: 'shapefiles', + types: { + point: 'points', + polygon: 'polygons', + polyline: 'lines' + } +}); diff --git a/package.json b/package.json index 8bf6a416..a9e94a17 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "mocha -R spec", "prepublish": "npm run make", - "make": "browserify -s shpwrite ./ > shpwrite.js" + "make": "browserify -s shpwrite ./ > shpwrite.js", + "make-test": "browserify indexTest.js > shpwrite.js" }, "repository": { "type": "git", @@ -35,7 +36,8 @@ }, "dependencies": { "dbf": "0.1.4", - "jszip": "2.5.0" + "file-saver": "1.3.3", + "jszip": "3.1.3" }, "devDependencies": { "browserify": "^13.0.0", diff --git a/src/download.js b/src/download.js index d198addf..e30bd027 100644 --- a/src/download.js +++ b/src/download.js @@ -1,6 +1,6 @@ var zip = require('./zip'); +var saveAs = require("file-saver").saveAs; module.exports = function(gj, options) { - var content = zip(gj, options); - location.href = 'data:application/zip;base64,' + content; + zip(gj, options).then(function(blob) { saveAs(blob, options.file + '.zip'); }); }; diff --git a/src/zip.js b/src/zip.js index b8e89d85..7c2c2de1 100644 --- a/src/zip.js +++ b/src/zip.js @@ -28,11 +28,8 @@ module.exports = function(gj, options) { } }); - var generateOptions = { compression:'STORE' }; - - if (!process.browser) { - generateOptions.type = 'nodebuffer'; - } - - return zip.generate(generateOptions); + return zip.generateAsync({ + type: process.browser === undefined ? 'nodebuffer' : 'blob', + compression: 'DEFLATE' + }); };