Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/performance improvements #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
shpwrite.js
node_modules
2 changes: 1 addition & 1 deletion src/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports.obj = obj;

function geojson(features) {
var fields = {};
features.forEach(collect);
forEach(features, collect);
function collect(f) { inherit(fields, f.properties); }
return obj(fields);
}
Expand Down
5 changes: 3 additions & 2 deletions src/points.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var ext = require('./extent');
const { reduce, forEach } = require('./util')

module.exports.write = function writePoints(coordinates, extent, shpView, shxView) {

Expand All @@ -7,7 +8,7 @@ module.exports.write = function writePoints(coordinates, extent, shpView, shxVie
shpI = 0,
shxI = 0;

coordinates.forEach(function writePoint(coords, i) {
forEach(coordinates, function writePoint(coords, i) {
// HEADER
// 4 record number
// 4 content length in 16-bit words (20/2)
Expand All @@ -31,7 +32,7 @@ module.exports.write = function writePoints(coordinates, extent, shpView, shxVie
};

module.exports.extent = function(coordinates) {
return coordinates.reduce(function(extent, coords) {
return reduce(coordinates, function(extent, coords) {
return ext.enlarge(extent, coords);
}, ext.blank());
};
Expand Down
32 changes: 20 additions & 12 deletions src/poly.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { forEach, reduce }= require('./util');
var ext = require('./extent'),
types = require('./types');

Expand All @@ -7,15 +8,15 @@ module.exports.write = function writePoints(geometries, extent, shpView, shxView
shxI = 0,
shxOffset = 100;

geometries.forEach(writePolyLine);
forEach(geometries, writePolyLine);

function writePolyLine(coordinates, i) {

var flattened = justCoords(coordinates),
noParts = parts([coordinates], TYPE),
contentLength = (flattened.length * 16) + 48 + (noParts - 1) * 4;

var featureExtent = flattened.reduce(function(extent, c) {
var featureExtent = reduce(flattened, function(extent, c) {
return ext.enlarge(extent, c);
}, ext.blank());

Expand All @@ -37,7 +38,7 @@ module.exports.write = function writePoints(geometries, extent, shpView, shxView
shpView.setInt32(shpI + 48, flattened.length, true); // POINTS
shpView.setInt32(shpI + 52, 0, true); // The first part - index zero

var onlyParts = coordinates.reduce(function (arr, coords) {
var onlyParts = reduce(coordinates, function (arr, coords) {
if (Array.isArray(coords[0][0])) {
arr = arr.concat(coords);
} else {
Expand All @@ -48,14 +49,14 @@ module.exports.write = function writePoints(geometries, extent, shpView, shxView
for (var p = 1; p < noParts; p++) {
shpView.setInt32( // set part index
shpI + 52 + (p * 4),
onlyParts.reduce(function (a, b, idx) {
reduce(onlyParts, function (a, b, idx) {
return idx < p ? a + b.length : a;
}, 0),
true
);
}

flattened.forEach(function writeLine(coords, i) {
forEach(flattened, function writeLine(coords, i) {
shpView.setFloat64(shpI + 56 + (i * 16) + (noParts - 1) * 4, coords[0], true); // X
shpView.setFloat64(shpI + 56 + (i * 16) + (noParts - 1) * 4 + 8, coords[1], true); // Y
});
Expand All @@ -75,18 +76,18 @@ module.exports.shxLength = function(geometries) {
};

module.exports.extent = function(coordinates) {
return justCoords(coordinates).reduce(function(extent, c) {
return reduce(justCoords(coordinates), function(extent, c) {
return ext.enlarge(extent, c);
}, ext.blank());
};

function parts(geometries, TYPE) {
var no = 1;
if (TYPE === types.geometries.POLYGON || TYPE === types.geometries.POLYLINE) {
no = geometries.reduce(function (no, coords) {
no = reduce(geometries, function (no, coords) {
no += coords.length;
if (Array.isArray(coords[0][0][0])) { // multi
no += coords.reduce(function (no, rings) {
no += reduce(coords, function (no, rings) {
return no + rings.length - 1; // minus outer
}, 0);
}
Expand All @@ -100,18 +101,25 @@ module.exports.parts = parts;

function totalPoints(geometries) {
var sum = 0;
geometries.forEach(function(g) { sum += g.length; });
forEach(geometries, function(g) { sum += g.length; });
return sum;
}

function justCoords(coords, l) {
if (l === undefined) l = [];
if (typeof coords[0][0] == 'object') {
return coords.reduce(function(memo, c) {
return memo.concat(justCoords(c));
}, l);
return reduce(coords, accumulateCoords, l);
} else {
return coords;
}
}

function accumulateCoords(memo, c) {
const memoPosition = memo.length;
const coords = justCoords(c);
forEach(coords, (coord, i) => {
memo[memoPosition + i] = coord;
})
return memo;
}

26 changes: 26 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports.forEach = function forEach(collection, iteratee) {
if(typeof iteratee !== 'function') {
throw Error('iteratee must be a function')
}
if(!Array.isArray(collection)) {
throw Error('collection must be an array')
}
for(let i = 0, len = collection.length; i < len; i++) {
iteratee(collection[i], i)
}
}

module.exports.reduce = function reduce(collection, iteratee, initialAccumulator) {
if(typeof iteratee !== 'function') {
throw Error('iteratee must be a function')
}
if(!Array.isArray(collection)) {
throw Error('collection must be an array')
}
let internalAccumulator = initialAccumulator;
for(let i = 0, len = collection.length; i < len; i++) {
internalAccumulator = iteratee(internalAccumulator, collection[i], i)
}

return internalAccumulator;
}
4 changes: 2 additions & 2 deletions src/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ var write = require('./write'),
geojson = require('./geojson'),
prj = require('./prj'),
JSZip = require('jszip');
const { forEach } = require('./util');

module.exports = function(gj, options) {

var zip = new JSZip(),
layers = zip.folder(options && options.folder ? options.folder : 'layers');

[geojson.point(gj), geojson.line(gj), geojson.polygon(gj)]
.forEach(function(l) {
forEach([geojson.point(gj), geojson.line(gj), geojson.polygon(gj)], function(l) {
if (l.geometries.length && l.geometries[0].length) {
write(
// field definitions
Expand Down