Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
shpwrite.js
node_modules
example/*.dbf
example/*.shp
example/*.shx
yarn.lock
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.



<a name="0.4.0"></a>
## [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)
*

<a name="0.3.2"></a>
## [0.3.2](https://github.com/mapbox/shp-write/compare/v0.3.1...v0.3.2) (2016-12-06)

Expand Down
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,56 @@ 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",
polygon: "mypolygons",
line: "mylines",
},
};

// a GeoJSON bridge for features
shpwrite.download(
const zipData = shpwrite.zip(
{
type: "FeatureCollection",
features: [
Expand Down Expand Up @@ -70,7 +106,6 @@ shpwrite.download(
},
options
);
// triggers a download of a zip file with shapefiles contained within.
```

## API
Expand Down
16 changes: 3 additions & 13 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module "shp-write" {
export interface DownloadOptions {
folder?: string;
filename?: string;
types: {
types?: {
point?: string;
polygon?: string;
line?: string;
Expand Down Expand Up @@ -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(
Expand All @@ -79,6 +69,6 @@ declare module "shp-write" {

export function zip<T extends OutputType>(
geojson: GeoJSON.FeatureCollection,
options: DownloadOptions & ZipOptions = DEFAULT_ZIP_OPTIONS,
options: DownloadOptions & ZipOptions = {},
stream = false): Promise<OutputByType[T]>;
}
2 changes: 1 addition & 1 deletion example/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/test_linestring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/test_multiple_poly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/test_polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 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.4.0",
"description": "write shapefiles from pure javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 1 addition & 12 deletions src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 22 additions & 23 deletions src/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
};