Skip to content
Closed
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
@@ -0,0 +1 @@
node_modules/*
48 changes: 32 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// look here for help http://svn.osgeo.org/grass/grass/branches/releasebranch_6_4/vector/v.overlay/main.c
//must be array of polygons

// depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html

var jsts = require('jsts');
var gh = require('gh-clipping-algorithm');

/**
* Takes two {@link Polygon|polygons} and returns a combined polygon. If the input polygons are not contiguous, this function returns a {@link MultiPolygon} feature.
Expand Down Expand Up @@ -58,16 +53,37 @@ var jsts = require('jsts');
* //=union
*/
module.exports = function(poly1, poly2) {
var reader = new jsts.io.GeoJSONReader();
var a = reader.read(JSON.stringify(poly1.geometry));
var b = reader.read(JSON.stringify(poly2.geometry));
var union = a.union(b);
var parser = new jsts.io.GeoJSONParser();
// console.log(poly1);
var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates;
var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates;
var u = gh.union(a, b);

union = parser.write(union);
return {
type: 'Feature',
geometry: union,
properties: poly1.properties
var feature = {
"type": "Feature",
"properties": {},
"geometry": {}
};

if (!u || u.length == 0) {
return undefined;
}

if (gh.utils.isMultiPolygon(u)) {
if (u.length > 1) {
feature.geometry.type = "MultiPolygon";
feature.geometry.coordinates = u;
} else {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u[0];
}
} else if (gh.utils.isPolygon(u)) {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u;
}

if (poly1.properties) {
feature.properties = poly1.properties;
}

return feature;
};
17 changes: 0 additions & 17 deletions npm-shrinkwrap.json

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"glob": "~4.3.5",
"tape": "~3.5.0",
"dox": "^0.6.1",
"doxme": "^1.4.3"
"doxme": "^1.4.3",
"geojson-equality": "^0.1.4"
},
"dependencies": {
"jsts": "~0.15.0"
"gh-clipping-algorithm": "^0.*"
}
}
5 changes: 3 additions & 2 deletions test/union.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var union = require('../'),
test = require('tape'),
glob = require('glob'),
fs = require('fs');
fs = require('fs'),
equal = new(require('geojson-equality'));

var REGEN = false;

Expand All @@ -10,7 +11,7 @@ test('union', function(t){
var fcs = JSON.parse(fs.readFileSync(input));
var output = union(fcs[0].features[0], fcs[1].features[0]);
if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output));
t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input);
t.ok(equal.compare(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/')))), input);
});
t.end();
});