From 72f6b6afd0331e290911583644d771c047579bdf Mon Sep 17 00:00:00 2001 From: Stephen Heindel Date: Sun, 20 Aug 2023 22:56:40 -0400 Subject: [PATCH 1/5] Merge with matser --- example/point.js | 2 +- example/test.js | 2 +- example/test_linestring.js | 2 +- example/test_multiple_poly.js | 2 +- example/test_polygon.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) 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; From 1de950a6722de46ba94f6e83faf46cf56aed2218 Mon Sep 17 00:00:00 2001 From: Stephen Heindel Date: Mon, 21 Aug 2023 08:25:33 -0400 Subject: [PATCH 2/5] Fix options, better backwards compatibility, added root shapefile option --- .gitignore | 5 +++++ CHANGELOG.md | 11 +++++++++++ README.md | 6 +----- package.json | 2 +- src/download.js | 13 +------------ src/zip.js | 43 +++++++++++++++++++++---------------------- 6 files changed, 40 insertions(+), 40 deletions(-) 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..05acc552 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,8 @@ Or in a browser ```js var shpwrite = require("shp-write"); -// (minimal) set names for feature types and zipped folder +// (minimal) set names for feature types var options = { - folder: "myshapes", - filename: "mydownload", - outputType: "base64", - compression: "DEFLATE", types: { point: "mypoints", polygon: "mypolygons", 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..f24b65ba 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 && options.folder ? options.folder : "layers"); + } [ geojson.point(gj), @@ -39,27 +34,31 @@ module.exports = function ( options && 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); }; From a53d3ce2e4869b1721390d7d4a8d521ef631a48e Mon Sep 17 00:00:00 2001 From: Stephen Heindel Date: Mon, 21 Aug 2023 08:45:40 -0400 Subject: [PATCH 3/5] Make zip and download options truly optional, added examples to reflect no options and options --- README.md | 50 +++++++++++++++++++++++++++++++++++++++++++------ dist/index.d.ts | 16 +++------------- src/zip.js | 2 +- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 05acc552..e819a3f5 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,56 @@ Or in a browser ```js var shpwrite = require("shp-write"); -// (minimal) set names for feature types -var options = { +// 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", polygon: "mypolygons", line: "mylines", }, }; + // a GeoJSON bridge for features -shpwrite.download( +const zipData = shpwrite.zip( { type: "FeatureCollection", features: [ @@ -63,10 +103,8 @@ 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/src/zip.js b/src/zip.js index f24b65ba..3fef223f 100644 --- a/src/zip.js +++ b/src/zip.js @@ -31,7 +31,7 @@ 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; zipTarget.file(fileName + ".shp", files.shp.buffer, { binary: true }); From 99938fc14282f4c812b08be2a02de05aefa5bc1e Mon Sep 17 00:00:00 2001 From: Stephen Heindel Date: Mon, 21 Aug 2023 08:46:50 -0400 Subject: [PATCH 4/5] Add missing options arg --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e819a3f5..d8aa7a48 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,8 @@ const zipData = shpwrite.zip( }, }, ], - } + }, + options ); ``` From e5b928e000ef7eeed731c44b7522fb4756a6e970 Mon Sep 17 00:00:00 2001 From: Stephen Heindel Date: Mon, 21 Aug 2023 08:49:32 -0400 Subject: [PATCH 5/5] Remove redundant options check --- src/zip.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zip.js b/src/zip.js index 3fef223f..43daf20a 100644 --- a/src/zip.js +++ b/src/zip.js @@ -11,7 +11,7 @@ module.exports = function ( let zip = new JSZip(); let zipTarget = zip; if (options && options.folder) { - zipTarget = zip.folder(options && options.folder ? options.folder : "layers"); + zipTarget = zip.folder(options.folder); } [