diff --git a/.gitignore b/.gitignore index 3db12b15..f0eb7ffc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ shpwrite.js +node_modules +example/*.dbf +example/*.shp +example/*.shx +yarn.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 27db80c6..70d9b20d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + + + +## [0.4.0](https://github.com/mapbox/shp-write/compare/v0.3.2...v0.4.0) (2023-08-20) + +* Upgrade JSZip and dbf dependencies +* Added types +* Added options for compression and type output to `zip` and `download` +* Added deprecation warning to `download` (not needed for this library) +* + ## [0.3.2](https://github.com/mapbox/shp-write/compare/v0.3.1...v0.3.2) (2016-12-06) diff --git a/README.md b/README.md index fd5c1fa2..d8aa7a48 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,46 @@ Or in a browser ```js var shpwrite = require("shp-write"); -// (minimal) set names for feature types and zipped folder -var options = { - folder: "myshapes", - filename: "mydownload", - outputType: "base64", +// a GeoJSON bridge for features +const zipData = shpwrite.zip( + { + type: "FeatureCollection", + features: [ + { + type: "Feature", + geometry: { + type: "Point", + coordinates: [0, 0], + }, + properties: { + name: "Foo", + }, + }, + { + type: "Feature", + geometry: { + type: "Point", + coordinates: [0, 10], + }, + properties: { + name: "Bar", + }, + }, + ], + } +); + +``` + +## Options Example + +```js +var shpwrite = require("shp-write"); + +const options = { + folder: "my_internal_shapes_folder", + filename: "my_zip_filename", + outputType: "blob", compression: "DEFLATE", types: { point: "mypoints", @@ -41,8 +76,9 @@ var options = { line: "mylines", }, }; + // a GeoJSON bridge for features -shpwrite.download( +const zipData = shpwrite.zip( { type: "FeatureCollection", features: [ @@ -70,7 +106,6 @@ shpwrite.download( }, options ); -// triggers a download of a zip file with shapefiles contained within. ``` ## API diff --git a/dist/index.d.ts b/dist/index.d.ts index c71e99e5..39217522 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -19,7 +19,7 @@ declare module "shp-write" { export interface DownloadOptions { folder?: string; filename?: string; - types: { + types?: { point?: string; polygon?: string; line?: string; @@ -48,19 +48,9 @@ declare module "shp-write" { outputType: OutputType } - DEFAULT_ZIP_OPTIONS = { - compression: 'STORE', - outputType: 'base64', - types: { - point: "mypoints", - polygon: "mypolygons", - line: "mylines", - }, - }; - export function download( geojson: GeoJSON.FeatureCollection, - options?: DownloadOptions & ZipOptions = DEFAULT_ZIP_OPTIONS + options?: DownloadOptions & ZipOptions = {} ): void; export function write( @@ -79,6 +69,6 @@ declare module "shp-write" { export function zip( geojson: GeoJSON.FeatureCollection, - options: DownloadOptions & ZipOptions = DEFAULT_ZIP_OPTIONS, + options: DownloadOptions & ZipOptions = {}, stream = false): Promise; } diff --git a/example/point.js b/example/point.js index 787e0ac6..3a122d5c 100644 --- a/example/point.js +++ b/example/point.js @@ -26,7 +26,7 @@ function finish(err, files) { } function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength), + var buffer = Buffer.alloc(ab.byteLength), view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; diff --git a/example/test.js b/example/test.js index 3abef1fd..15a2198f 100644 --- a/example/test.js +++ b/example/test.js @@ -27,7 +27,7 @@ function finish(err, files) { } function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength), + var buffer = Buffer.alloc(ab.byteLength), view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; diff --git a/example/test_linestring.js b/example/test_linestring.js index 1ab83600..62a9c5cd 100644 --- a/example/test_linestring.js +++ b/example/test_linestring.js @@ -24,7 +24,7 @@ function finish(err, files) { } function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength), + var buffer = Buffer.alloc(ab.byteLength), view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; diff --git a/example/test_multiple_poly.js b/example/test_multiple_poly.js index adec9c46..7e6a1fb4 100644 --- a/example/test_multiple_poly.js +++ b/example/test_multiple_poly.js @@ -22,7 +22,7 @@ function finish(err, files) { } function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength), + var buffer = Buffer.alloc(ab.byteLength), view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; diff --git a/example/test_polygon.js b/example/test_polygon.js index 9f49e584..45cc9375 100644 --- a/example/test_polygon.js +++ b/example/test_polygon.js @@ -25,7 +25,7 @@ function finish(err, files) { } function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength), + var buffer = Buffer.alloc(ab.byteLength), view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; diff --git a/package.json b/package.json index c03505c8..bf572cc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shp-write", - "version": "0.3.2", + "version": "0.4.0", "description": "write shapefiles from pure javascript", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/download.js b/src/download.js index 42801478..c95b4d77 100644 --- a/src/download.js +++ b/src/download.js @@ -5,18 +5,7 @@ var saveAs = require("file-saver").saveAs; * @deprecated may be removed in a future version, please use an external * download library */ -module.exports = function ( - gj, - options = { - compression: 'STORE', - outputType: 'base64', - types: { - point: "mypoints", - polygon: "mypolygons", - line: "mylines", - }, - }, -) { +module.exports = function (gj, options) { let filename = 'download'; // since we only need a single filename object, we can use either the folder or diff --git a/src/zip.js b/src/zip.js index 24738780..43daf20a 100644 --- a/src/zip.js +++ b/src/zip.js @@ -5,19 +5,14 @@ var write = require("./write"), module.exports = function ( gj, - options = { - compression: 'STORE', - outputType: 'base64', - types: { - point: "mypoints", - polygon: "mypolygons", - line: "mylines", - }, - }, + options, stream = false ) { - var zip = new JSZip(), - layers = zip.folder(options && options.folder ? options.folder : "layers"); + let zip = new JSZip(); + let zipTarget = zip; + if (options && options.folder) { + zipTarget = zip.folder(options.folder); + } [ geojson.point(gj), @@ -36,30 +31,34 @@ module.exports = function ( l.geometries, function (err, files) { var fileName = - options && options.types[l.type.toLowerCase()] + 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 }); - layers.file(fileName + ".prj", prj); + zipTarget.file(fileName + ".shp", files.shp.buffer, { binary: true }); + zipTarget.file(fileName + ".shx", files.shx.buffer, { binary: true }); + zipTarget.file(fileName + ".dbf", files.dbf.buffer, { binary: true }); + zipTarget.file(fileName + ".prj", prj); } ); } }); - var generateOptions = {}; - if (options && options.outputType) { - generateOptions.type = options.outputType; + var zipOptions = {}; + if (!options || !options.outputType) { + zipOptions.type = "base64"; + } else { + zipOptions.type = options.outputType; } - if (options && options.compresssion) { - generateOptions.compression = options.compression; + if (!options || !options.compression) { + zipOptions.compression = "DEFLATE"; + } else { + zipOptions.compression = options.compression; } if (stream) { - return zip.generateNodeStream({ ...generateOptions, streamFiles: true }); + return zip.generateNodeStream({ ...zipOptions, streamFiles: true }); } - return zip.generateAsync(generateOptions); + return zip.generateAsync(zipOptions); };