Skip to content

Commit 4146e58

Browse files
authored
Merge pull request #96 from Agriculture-Intelligence/master
deps, `options: filename`, multipolygon, `.d.ts`, added compression options to `zip` and `download` interfaces, added deprecation warning for `download`
2 parents 9681b58 + 653ec2a commit 4146e58

9 files changed

+259
-114
lines changed

README.md

+59-47
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,62 @@ Or in a browser
1818

1919
## Caveats
2020

21-
* Requires a capable fancy modern browser with [Typed Arrays](http://caniuse.com/#feat=typedarrays)
21+
- Requires a capable fancy modern browser with [Typed Arrays](http://caniuse.com/#feat=typedarrays)
2222
support
23-
* Geometries: Point, LineString, Polygon, MultiLineString, MultiPolygon
24-
* Tabular-style properties export with Shapefile's field name length limit
25-
* Uses jsZip for ZIP files, but [compression is buggy](https://github.com/Stuk/jszip/issues/53) so it uses STORE instead of DEFLATE.
23+
- Geometries: Point, LineString, Polygon, MultiLineString, MultiPolygon
24+
- Tabular-style properties export with Shapefile's field name length limit
25+
- Uses jsZip for ZIP files, but [compression is buggy](https://github.com/Stuk/jszip/issues/53) so it uses STORE instead of DEFLATE.
2626

27-
## Example
27+
## Minimal Example
2828

2929
```js
30-
var shpwrite = require('shp-write');
30+
var shpwrite = require("shp-write");
3131

32-
// (optional) set names for feature types and zipped folder
32+
// (minimal) set names for feature types and zipped folder
3333
var options = {
34-
folder: 'myshapes',
35-
types: {
36-
point: 'mypoints',
37-
polygon: 'mypolygons',
38-
line: 'mylines'
39-
}
40-
}
34+
folder: "myshapes",
35+
filename: "mydownload",
36+
outputType: "base64",
37+
compression: "DEFLATE",
38+
types: {
39+
point: "mypoints",
40+
polygon: "mypolygons",
41+
line: "mylines",
42+
},
43+
};
4144
// a GeoJSON bridge for features
42-
shpwrite.download({
43-
type: 'FeatureCollection',
45+
shpwrite.download(
46+
{
47+
type: "FeatureCollection",
4448
features: [
45-
{
46-
type: 'Feature',
47-
geometry: {
48-
type: 'Point',
49-
coordinates: [0, 0]
50-
},
51-
properties: {
52-
name: 'Foo'
53-
}
49+
{
50+
type: "Feature",
51+
geometry: {
52+
type: "Point",
53+
coordinates: [0, 0],
54+
},
55+
properties: {
56+
name: "Foo",
57+
},
58+
},
59+
{
60+
type: "Feature",
61+
geometry: {
62+
type: "Point",
63+
coordinates: [0, 10],
64+
},
65+
properties: {
66+
name: "Bar",
5467
},
55-
{
56-
type: 'Feature',
57-
geometry: {
58-
type: 'Point',
59-
coordinates: [0, 10]
60-
},
61-
properties: {
62-
name: 'Bar'
63-
}
64-
}
65-
]
66-
}, options);
68+
},
69+
],
70+
},
71+
options
72+
);
6773
// triggers a download of a zip file with shapefiles contained within.
6874
```
6975

7076
## API
71-
72-
### `download(geojson)`
73-
74-
Given a [GeoJSON](http://geojson.org/) FeatureCollection as an object,
75-
converts convertible features into Shapefiles and triggers a download.
76-
7777
### `write(data, geometrytype, geometries, callback)`
7878

7979
Given data, an array of objects for each row of data, geometry, the OGC standard
@@ -88,19 +88,31 @@ arrays, generate a shapfile and call the callback with `err` and an object with
8888
}
8989
```
9090

91-
### `zip(geojson)`
91+
### `zip(geojson, [options])`
9292

9393
Generate a ArrayBuffer of a zipped shapefile, dbf, and prj, from a GeoJSON
9494
object.
9595

96+
### DEPRECTEAD! May be removed in a future version
97+
### `download(geojson, [options])`
98+
99+
Given a [GeoJSON](http://geojson.org/) FeatureCollection as an object,
100+
converts convertible features into Shapefiles and triggers a download.
101+
102+
The additional `options` parameter is passed to the underlying `zip` call.
103+
104+
This is now marked as deprecated because it applies to browsers only and the
105+
user should instead rely on an external library for this functionality like
106+
`file-saver` or `downloadjs`
107+
96108
## Other Implementations
97109

98-
* https://code.google.com/p/pyshp/
110+
- https://code.google.com/p/pyshp/
99111

100112
## Reference
101113

102-
* http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
114+
- http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
103115

104116
## Contributors
105117

106-
* Nick Baugh <[email protected]>
118+
- Nick Baugh <[email protected]>

dist/index.d.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
declare module "shp-write" {
2+
export enum OGCGeometry {
3+
NULL,
4+
POINT,
5+
POLYLINE,
6+
POLYGON,
7+
MULTIPOINT,
8+
POINTZ,
9+
POLYLINEZ,
10+
POLYGONZ,
11+
MULTIPOINTZ,
12+
POINTM,
13+
POLYLINEM,
14+
POLYGONM,
15+
MULTIPOINTM,
16+
MULTIPATCH,
17+
}
18+
19+
export interface DownloadOptions {
20+
folder?: string;
21+
filename?: string;
22+
types: {
23+
point?: string;
24+
polygon?: string;
25+
line?: string;
26+
multipolygon?: string;
27+
multiline?: string;
28+
};
29+
}
30+
31+
type Compression = 'STORE' | 'DEFLATE';
32+
interface OutputByType {
33+
base64: string;
34+
string: string;
35+
text: string;
36+
binarystring: string;
37+
array: number[];
38+
uint8array: Uint8Array;
39+
arraybuffer: ArrayBuffer;
40+
blob: Blob;
41+
nodebuffer: Buffer;
42+
stream: ReadableStream;
43+
}
44+
type OutputType = keyof OutputByType;
45+
46+
export interface ZipOptions {
47+
compression: Compression,
48+
outputType: OutputType
49+
}
50+
51+
DEFAULT_ZIP_OPTIONS = {
52+
compression: 'STORE',
53+
outputType: 'base64',
54+
types: {
55+
point: "mypoints",
56+
polygon: "mypolygons",
57+
line: "mylines",
58+
},
59+
};
60+
61+
export function download(
62+
geojson: GeoJSON.FeatureCollection,
63+
options?: DownloadOptions & ZipOptions = DEFAULT_ZIP_OPTIONS
64+
): void;
65+
66+
export function write(
67+
data: Array<object>,
68+
geometrytype: OGCGeometry,
69+
geometries: Array<object>,
70+
callback: (
71+
err: any,
72+
data: {
73+
shp: any;
74+
shx: any;
75+
dbf: any;
76+
}
77+
) => void
78+
): void;
79+
80+
export function zip<T extends OutputType>(
81+
geojson: GeoJSON.FeatureCollection,
82+
options: DownloadOptions & ZipOptions = DEFAULT_ZIP_OPTIONS,
83+
stream = false): Promise<OutputByType[T]>;
84+
}

dist/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports.download = require("../src/download");
2+
module.exports.write = require("../src/write");
3+
module.exports.zip = require("../src/zip");

index.js

-3
This file was deleted.

package.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "shp-write",
33
"version": "0.3.2",
44
"description": "write shapefiles from pure javascript",
5-
"main": "index.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
67
"scripts": {
78
"test": "mocha -R spec",
89
"prepublish": "npm run make",
@@ -13,7 +14,8 @@
1314
"url": "git://github.com/mapbox/shp-write.git"
1415
},
1516
"files": [
16-
"index.js",
17+
"dist/index.js",
18+
"dist/index.d.ts",
1719
"src",
1820
"shpwrite.js"
1921
],
@@ -23,19 +25,14 @@
2325
"js"
2426
],
2527
"author": "Tom MacWright",
26-
"contributors": [
27-
{
28-
"name": "Nick Baugh",
29-
"email": "[email protected]"
30-
}
31-
],
3228
"license": "BSD-2-Clause",
3329
"bugs": {
3430
"url": "https://github.com/mapbox/shp-write/issues"
3531
},
3632
"dependencies": {
37-
"dbf": "0.1.4",
38-
"jszip": "2.5.0"
33+
"dbf": "0.2.0",
34+
"jszip": "3.6.0",
35+
"file-saver": "2.0.5"
3936
},
4037
"devDependencies": {
4138
"browserify": "^13.0.0",

src/download.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
var zip = require('./zip');
2+
var saveAs = require("file-saver").saveAs;
23

3-
module.exports = function(gj, options) {
4-
var content = zip(gj, options);
5-
location.href = 'data:application/zip;base64,' + content;
4+
/**
5+
* @deprecated may be removed in a future version, please use an external
6+
* download library
7+
*/
8+
module.exports = function (
9+
gj,
10+
options = {
11+
compression: 'STORE',
12+
outputType: 'base64',
13+
types: {
14+
point: "mypoints",
15+
polygon: "mypolygons",
16+
line: "mylines",
17+
},
18+
},
19+
) {
20+
let filename = 'download';
21+
22+
// since we only need a single filename object, we can use either the folder or
23+
// filename, depending on what was passed in
24+
if (options && (options.filename || options.folder)) {
25+
filename = (options.filename || options.folder);
26+
}
27+
zip(gj, options).then(function (blob) { saveAs(blob, filename + '.zip'); });
628
};

src/geojson.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
module.exports.point = justType('Point', 'POINT');
2-
module.exports.line = justType('LineString', 'POLYLINE');
3-
module.exports.polygon = justType('Polygon', 'POLYGON');
1+
module.exports.point = justType("Point", "POINT");
2+
module.exports.line = justType("LineString", "POLYLINE");
3+
module.exports.multiline = justType("MultiLineString", "POLYLINE");
4+
module.exports.polygon = justType("Polygon", "POLYGON");
5+
module.exports.multipolygon = justType("MultiPolygon", "POLYGON");
46

57
function justType(type, TYPE) {
6-
return function(gj) {
7-
var oftype = gj.features.filter(isType(type));
8-
return {
9-
geometries: (TYPE === 'POLYGON' || TYPE === 'POLYLINE') ? [oftype.map(justCoords)] : oftype.map(justCoords),
10-
properties: oftype.map(justProps),
11-
type: TYPE
12-
};
8+
return function (gj) {
9+
var oftype = gj.features.filter(isType(type));
10+
return {
11+
geometries: oftype.map(justCoords),
12+
properties: oftype.map(justProps),
13+
type: TYPE,
1314
};
15+
};
1416
}
1517

1618
function justCoords(t) {
17-
if (t.geometry.coordinates[0] !== undefined &&
18-
t.geometry.coordinates[0][0] !== undefined &&
19-
t.geometry.coordinates[0][0][0] !== undefined) {
20-
return t.geometry.coordinates[0];
21-
} else {
22-
return t.geometry.coordinates;
23-
}
19+
return t.geometry.coordinates;
2420
}
2521

2622
function justProps(t) {
27-
return t.properties;
23+
return t.properties;
2824
}
2925

3026
function isType(t) {
31-
return function(f) { return f.geometry.type === t; };
27+
if (Array.isArray(t))
28+
return function (f) {
29+
return t.includes(f.geometry.type);
30+
};
31+
else
32+
return function (f) {
33+
return f.geometry.type === t;
34+
};
3235
}

src/points.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports.write = function writePoints(coordinates, extent, shpView, shxVie
1111
// HEADER
1212
// 4 record number
1313
// 4 content length in 16-bit words (20/2)
14-
shpView.setInt32(shpI, i);
14+
shpView.setInt32(shpI, i + 1);
1515
shpView.setInt32(shpI + 4, 10);
1616

1717
// record

0 commit comments

Comments
 (0)