Skip to content

Commit

Permalink
integrate mapbox#50
Browse files Browse the repository at this point in the history
Also includes the following:

* adds eslint/editorconfig
* applied eslint formatting for all sources
* some minor fixes discovered through linting
  • Loading branch information
hwbllmnn committed Nov 30, 2018
1 parent 0c979a1 commit 04d4d91
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 305 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
indent_size = 2
trim_trailing_whitespace = false
33 changes: 33 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"eslint:recommended"
],
"plugins": [],
"env": {
"browser": true,
"node": true
},
"globals": {
"Promise": false,
"ArrayBuffer": false,
"DataView": false,
"describe": false,
"it": false
},
"rules": {
"indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"semi": ["error", "always"],
"one-var": ["error", "never"],
"no-confusing-arrow": "error",
"no-unused-vars": ["error", {
"ignoreRestSiblings": true
}],
"key-spacing": ["error", {
"beforeColon": false,
"afterColon": true
}],
"space-infix-ops": ["error", {"int32Hint": false}]
}
}
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://secure.travis-ci.org/mapbox/shp-write.svg?branch=master)](http://travis-ci.org/mapbox/shp-write)

# shp-write

[![Build Status](https://secure.travis-ci.org/mapbox/shp-write.svg?branch=master)](http://travis-ci.org/mapbox/shp-write)

Writes shapefile in pure javascript. Uses [dbf](https://github.com/tmcw/dbf)
for the data component, and [jsZIP](http://stuk.github.io/jszip/) to generate
ZIP file downloads in-browser.
Expand All @@ -16,26 +16,30 @@ 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 = {
folder: 'myshapes',
types: {
point: 'mypoints',
polygon: 'mypolygons',
line: 'mylines'
polyline: 'mylines'
}
}
// a GeoJSON bridge for features
Expand Down Expand Up @@ -95,12 +99,20 @@ object.

## Other Implementations

* https://code.google.com/p/pyshp/
* [https://code.google.com/p/pyshp/](https://code.google.com/p/pyshp/)

## Reference

* http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
* [http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf](http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf)

## Contributors

* Nick Baugh <[email protected]>

## Pull requests contained in this branch

This branch includes the following PRs of the official repository:

* [IE download fix](https://github.com/mapbox/shp-write/pull/50) (merged manually as the source repo is gone)
* [Fix point export](https://github.com/mapbox/shp-write/pull/69)
* [Fixed extraction of polygon coordinates](https://github.com/mapbox/shp-write/pull/65)
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script src='bundle.js'></script>
<script src='shpwrite.js'></script>
85 changes: 85 additions & 0 deletions indexTest.js
Original file line number Diff line number Diff line change
@@ -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'
}
});
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -35,11 +36,13 @@
},
"dependencies": {
"dbf": "0.1.4",
"file-saver": "2.0.0",
"jszip": "3.1.5"
},
"devDependencies": {
"browserify": "16.2.3",
"cz-conventional-changelog": "2.1.0",
"eslint": "5.9.0",
"expect.js": "0.3.1",
"mocha": "5.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/download.js
Original file line number Diff line number Diff line change
@@ -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'); });
};
32 changes: 16 additions & 16 deletions src/extent.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module.exports.enlarge = function enlargeExtent(extent, pt) {
if (pt[0] < extent.xmin) extent.xmin = pt[0];
if (pt[0] > extent.xmax) extent.xmax = pt[0];
if (pt[1] < extent.ymin) extent.ymin = pt[1];
if (pt[1] > extent.ymax) extent.ymax = pt[1];
return extent;
if (pt[0] < extent.xmin) extent.xmin = pt[0];
if (pt[0] > extent.xmax) extent.xmax = pt[0];
if (pt[1] < extent.ymin) extent.ymin = pt[1];
if (pt[1] > extent.ymax) extent.ymax = pt[1];
return extent;
};

module.exports.enlargeExtent = function enlargeExtent(extent, ext) {
if (ext.xmax > extent.xmax) extent.xmax = ext.xmax;
if (ext.xmin < extent.xmin) extent.xmin = ext.xmin;
if (ext.ymax > extent.ymax) extent.ymax = ext.ymax;
if (ext.ymin < extent.ymin) extent.ymin = ext.ymin;
return extent;
if (ext.xmax > extent.xmax) extent.xmax = ext.xmax;
if (ext.xmin < extent.xmin) extent.xmin = ext.xmin;
if (ext.ymax > extent.ymax) extent.ymax = ext.ymax;
if (ext.ymin < extent.ymin) extent.ymin = ext.ymin;
return extent;
};

module.exports.blank = function() {
return {
xmin: Number.MAX_VALUE,
ymin: Number.MAX_VALUE,
xmax: -Number.MAX_VALUE,
ymax: -Number.MAX_VALUE
};
return {
xmin: Number.MAX_VALUE,
ymin: Number.MAX_VALUE,
xmax: -Number.MAX_VALUE,
ymax: -Number.MAX_VALUE
};
};
30 changes: 15 additions & 15 deletions src/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ module.exports.geojson = geojson;
module.exports.obj = obj;

function geojson(features) {
var fields = {};
features.forEach(collect);
function collect(f) { inherit(fields, f.properties); }
return obj(fields);
var fields = {};
features.forEach(collect);
function collect(f) { inherit(fields, f.properties); }
return obj(fields);
}

function inherit(a, b) {
for (var i in b) { a[i] = b[i]; }
return a;
for (var i in b) { a[i] = b[i]; }
return a;
}

function obj(_) {
var fields = {}, o = [];
for (var p in _) fields[p] = typeof _[p];
for (var n in fields) {
o.push({
name: n,
type: types[fields[n]]
});
}
return o;
var fields = {}; var o = [];
for (var p in _) fields[p] = typeof _[p];
for (var n in fields) {
o.push({
name: n,
type: types[fields[n]]
});
}
return o;
}
20 changes: 10 additions & 10 deletions src/geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module.exports.line = justType('LineString', 'POLYLINE');
module.exports.polygon = justType('Polygon', 'POLYGON');

function justType(type, TYPE) {
return function(gj) {
var oftype = gj.features.filter(isType(type));
return {
geometries: oftype.map(justCoords),
properties: oftype.map(justProps),
type: TYPE
};
return function(gj) {
var oftype = gj.features.filter(isType(type));
return {
geometries: oftype.map(justCoords),
properties: oftype.map(justProps),
type: TYPE
};
};
}

function justCoords(t) {
return t.geometry.coordinates;
return t.geometry.coordinates;
}

function justProps(t) {
return t.properties;
return t.properties;
}

function isType(t) {
return function(f) { return f.geometry.type.replace('Multi', '') === t; };
return function(f) { return f.geometry.type.replace('Multi', '') === t; };
}
Loading

0 comments on commit 04d4d91

Please sign in to comment.