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

Added support for PointZ, PolylineZ, PolygonZ, MultiPoint, MultiPointZ #60

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Or in a browser

* Requires a capable fancy modern browser with [Typed Arrays](http://caniuse.com/#feat=typedarrays)
support
* Geometries: Point, LineString, Polygon, MultiLineString, MultiPolygon
* Geometries: Point, PointZ, MultiPoint, MultiPointZ, LineString, LineStringZ, Polygon, PolygonZ, 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.

Expand Down
31 changes: 31 additions & 0 deletions example/test_linestringZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var write = require('../src/write'),
fs = require('fs');

var lines = [[[
[0, 0, 1000],
[10, 0, 2000],
[15, 5, 3000],
[20, -5, 4000]
]]];

write(
// feature data
[{ id: 0 }],
// geometry type
'POLYLINEZ',
// geometries
lines,
finish);

function finish(err, files) {
fs.writeFileSync('linesz.shp', toBuffer(files.shp.buffer));
fs.writeFileSync('linesz.shx', toBuffer(files.shx.buffer));
fs.writeFileSync('linesz.dbf', toBuffer(files.dbf.buffer));
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; }
return buffer;
}
31 changes: 31 additions & 0 deletions example/test_multipoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var write = require('../src/write'),
fs = require('fs');

var points = [[
[0, 0],
[10, 0],
[10, 10],
[0, 10]
]];

write(
// feature data
[{ id: 0 }],
// geometry type
'MULTIPOINT',
// geometries
points,
finish);

function finish(err, files) {
fs.writeFileSync('multipoint.shp', toBuffer(files.shp.buffer));
fs.writeFileSync('multipoint.shx', toBuffer(files.shx.buffer));
fs.writeFileSync('multipoint.dbf', toBuffer(files.dbf.buffer));
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; }
return buffer;
}
31 changes: 31 additions & 0 deletions example/test_multipointZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var write = require('../src/write'),
fs = require('fs');

var points = [[
[0, 0, 300, 56],
[10, 0, 400, 34],
[10, 10, 500, 12],
[0, 10, 100]
]];

write(
// feature data
[{ id: 0 }],
// geometry type
'MULTIPOINTZ',
// geometries
points,
finish);

function finish(err, files) {
fs.writeFileSync('multipointz.shp', toBuffer(files.shp.buffer));
fs.writeFileSync('multipointz.shx', toBuffer(files.shx.buffer));
fs.writeFileSync('multipointz.dbf', toBuffer(files.dbf.buffer));
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; }
return buffer;
}
31 changes: 31 additions & 0 deletions example/test_pointZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var write = require('../src/write'),
fs = require('fs');

var points = [
[0, 0, 500],
[10, 0, 400],
[10, 10, 600],
[0, 10, 800]
];

write(
// feature data
[{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }],
// geometry type
'POINTZ',
// geometries
points,
finish);

function finish(err, files) {
fs.writeFileSync('pointz.shp', toBuffer(files.shp.buffer));
fs.writeFileSync('pointz.shx', toBuffer(files.shx.buffer));
fs.writeFileSync('pointz.dbf', toBuffer(files.dbf.buffer));
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; }
return buffer;
}
32 changes: 32 additions & 0 deletions example/test_polygonZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var write = require('../src/write'),
fs = require('fs');

var polygons = [[[
[0, 0, 8000],
[10, 0, 9000],
[15, 5, 10000],
[20, -5, 11000],
[0, 0, 8000]
]]];

write(
// feature data
[{ id: 0 }],
// geometry type
'POLYGONZ',
// geometries
polygons,
finish);

function finish(err, files) {
fs.writeFileSync('polygonz.shp', toBuffer(files.shp.buffer));
fs.writeFileSync('polygonz.shx', toBuffer(files.shx.buffer));
fs.writeFileSync('polygonz.dbf', toBuffer(files.dbf.buffer));
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; }
return buffer;
}
14 changes: 13 additions & 1 deletion src/extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module.exports.enlarge = function enlargeExtent(extent, pt) {
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];
if ((pt[2] || 0) < extent.zmin) extent.zmin = pt[2] || 0;
if ((pt[2] || 0) > extent.zmax) extent.zmax = pt[2] || 0;
if ((pt[3] || 0) < extent.mmin) extent.mmin = pt[3] || 0;
if ((pt[3] || 0) > extent.mmax) extent.mmax = pt[3] || 0;
return extent;
};

Expand All @@ -11,14 +15,22 @@ module.exports.enlargeExtent = function enlargeExtent(extent, ext) {
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;
if (ext.zmax && ext.zmax > extent.zmax) extent.zmax = ext.zmax;
if (ext.zmin && ext.zmin < extent.zmin) extent.zmin = ext.zmin;
if (ext.mmax && ext.mmax > extent.mmax) extent.mmax = ext.mmax;
if (ext.mmin && ext.mmin < extent.mmin) extent.mmin = ext.mmin;
return extent;
};

module.exports.blank = function() {
return {
xmin: Number.MAX_VALUE,
ymin: Number.MAX_VALUE,
zmin: Number.MAX_VALUE,
mmin: Number.MAX_VALUE,
xmax: -Number.MAX_VALUE,
ymax: -Number.MAX_VALUE
ymax: -Number.MAX_VALUE,
zmax: -Number.MAX_VALUE,
mmax: -Number.MAX_VALUE
};
};
36 changes: 30 additions & 6 deletions src/geojson.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module.exports.point = justType('Point', 'POINT');
module.exports.line = justType('LineString', 'POLYLINE');
module.exports.polygon = justType('Polygon', 'POLYGON');
module.exports.point = justType('Point', 'POINT', false);
module.exports.pointZ = justType('Point', 'POINTZ', true);
module.exports.multipoint = justType('MultiPoint', 'MULTIPOINT', false);
module.exports.multipointZ = justType('MultiPoint', 'MULTIPOINTZ', true);
module.exports.line = justType('LineString', 'POLYLINE', false);
module.exports.lineZ = justType('LineString', 'POLYLINEZ', true);
module.exports.polygon = justType('Polygon', 'POLYGON', false);
module.exports.polygonZ = justType('Polygon', 'POLYGONZ', true);

function justType(type, TYPE) {
function justType(type, TYPE, just3D) {
return function(gj) {
var oftype = gj.features.filter(isType(type));
var ofDimension = oftype.filter(isOfDimension(TYPE, just3D));

return {
geometries: (TYPE === 'POLYGON' || TYPE === 'POLYLINE') ? [oftype.map(justCoords)] : oftype.map(justCoords),
properties: oftype.map(justProps),
geometries: (TYPE === 'POLYGON' || TYPE === 'POLYLINE' || TYPE === 'POLYGONZ' || TYPE === 'POLYLINEZ') ? [ofDimension.map(justCoords)] : ofDimension.map(justCoords),
properties: ofDimension.map(justProps),
type: TYPE
};
};
Expand All @@ -30,3 +37,20 @@ function justProps(t) {
function isType(t) {
return function(f) { return f.geometry.type === t; };
}

function isOfDimension(TYPE, just3d) {
return function(f) {
var coordinates;
if (TYPE === 'POINT' || TYPE === 'POINTZ') {
coordinates = [f.geometry.coordinates];
}
else {
coordinates = Array.isArray(f.geometry.coordinates[0][0]) ?
f.geometry.coordinates.reduce(function(agg, c) {
return agg.concat(c);
}, []) :
f.geometry.coordinates;
}
return just3d ? coordinates.some(function(c) { return c.length >= 3 }) : coordinates.every(function(c) { return c.length === 2 });
}
}
125 changes: 125 additions & 0 deletions src/multipoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
var ext = require('./extent');
var types = require('./types');

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

var shpI = 0,
shxI = 0;
shxOffset = 100,
is3D = TYPE === types.geometries.MULTIPOINTZ;

coordinates.forEach(writeMultipoint);

function writeMultipoint(coords, i) {
// Length of multipoint record
var contentLength = 40 + (coords.length * 16);
if (is3D) {
contentLength += 32 + (coords.length * 16);
}
var totalLength = contentLength + 8;

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

// Write entry to index file.
shxView.setInt32(shxI, shxOffset / 2); // Offset in shx file.
shxView.setInt32(shxI + 4, totalLength / 2); // Record length.
shxI += 8;
shxOffset += totalLength;

// Write record to shape file.
shpView.setInt32(shpI, i); // Record number
shpView.setInt32(shpI + 4, contentLength / 2); // Record length (in 16 bit words);
shpView.setInt32(shpI + 8, TYPE, true); // Record type. MULTIPOINT=8,MULTIPOINTZ=18
shpView.setFloat64(shpI + 12, featureExtent.xmin, true); // Bounding box.
shpView.setFloat64(shpI + 20, featureExtent.ymin, true);
shpView.setFloat64(shpI + 28, featureExtent.xmax, true);
shpView.setFloat64(shpI + 36, featureExtent.ymax, true);
shpView.setInt32(shpI + 44, coords.length, true); // Number of points.
shpI += 48;

// Write points.
var zMin = Number.MAX_VALUE;
var zMax = -Number.MAX_VALUE;
var mMin = Number.MAX_VALUE;
var mMax = -Number.MAX_VALUE;
coords.forEach(function(p, i) {
if ((coords[2] || 0) < zMin) {
zMin = coords[2] || 0;
}

if ((coords[2] || 0) > zMax) {
zMax = coords[2] || 0;
}

if ((coords[3] || 0) < mMin) {
mMin = coords[3] || 0;
}

if ((coords[3] || 0) > mMax) {
mMax = coords[3] || 0;
}

shpView.setFloat64(shpI, p[0], true); // X
shpView.setFloat64(shpI + 8, p[1], true); // Y
shpI += 16;
});

if (is3D) {
// Write z value range
shpView.setFloat64(shpI, zMin, true);
shpView.setFloat64(shpI + 8, zMax, true);
shpI += 16

// Write z values.
coords.forEach(function(p, i) {
shpView.setFloat64(shpI, p[2] || 0, true);
shpI += 8;
});

// Write m value range.
shpView.setFloat64(shpI, mMin, true);
shpView.setFloat64(shpI + 8, mMax, true);
shpI += 16;

// Write m values;
coords.forEach(function(p, i) {
shpView.setFloat64(shpI, p[3] || 0, true);
shpI += 8;
});
}
}
};

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

module.exports.parts = function parts(geometries, TYPE) {
return geometries.length;
};

module.exports.shxLength = function(coordinates) {
return coordinates.length * 8;
};

module.exports.shpLength = function(coordinates, TYPE) {
var flattented = justCoords(coordinates);
var length = coordinates.length * 48 + flattented.length * 16;
if (TYPE === types.geometries.MULTIPOINTZ) {
length += 32 + (flattented.length * 16);
}

return length;
};

function justCoords(coords) {
var points = [];
return coords.reduce(function(agg, c) {
return agg.concat(c);
}, points);
}
Loading