Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Kill jsts #10

Closed
wants to merge 11 commits into from
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
46 changes: 26 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// 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 finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined.
Expand Down Expand Up @@ -58,25 +57,32 @@ var jsts = require('jsts');
* //=intersection
*/
module.exports = function(poly1, poly2) {
var geom1, geom2;
if(poly1.type === 'Feature') geom1 = poly1.geometry;
else geom1 = poly1;
if(poly2.type === 'Feature') geom2 = poly2.geometry;
else geom2 = poly2;
var reader = new jsts.io.GeoJSONReader();
var a = reader.read(JSON.stringify(geom1));
var b = reader.read(JSON.stringify(geom2));
var intersection = a.intersection(b);
var parser = new jsts.io.GeoJSONParser();
var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates;
var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates;
var u = gh.intersect(a, b);

intersection = parser.write(intersection);
if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
var feature = {
"type": "Feature",
"properties": {},
"geometry": {}
};

if (!u || u.length == 0) {
return undefined;
} else {
return {
type: 'Feature',
properties: {},
geometry: intersection
};
}

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;
}

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 @@ -27,9 +27,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.*"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/out/Intersect1.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-79.94623496447946,32.89900638172028],[-79.88571166992188,32.887659962078956],[-79.89395141601562,32.75551989829049],[-79.92322780260464,32.73910022106017],[-79.93789672851562,32.74108223150125],[-79.93034362792969,32.76475877693074],[-79.97360229492188,32.76071688548088],[-79.97428894042969,32.83690450361482],[-79.94623496447946,32.89900638172028]]]}}
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-79.92322780260464,32.73910022106017],[-79.89395141601562,32.75551989829049],[-79.88571166992188,32.887659962078956],[-79.94623496447946,32.89900638172028],[-79.97428894042969,32.83690450361482],[-79.97360229492188,32.76071688548088],[-79.93034362792969,32.76475877693074],[-79.93789672851562,32.74108223150125],[-79.92322780260464,32.73910022106017]]]}}
2 changes: 1 addition & 1 deletion test/fixtures/out/armenia.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[43.582746,41.092143],[44.97248,41.248129],[45.179496,40.985354],[45.560351,40.81229],[45.359175,40.561504],[45.891907,40.218476],[45.610012,39.899994],[46.034534,39.628021],[46.483499,39.464155],[46.50572,38.770605],[46.143623,38.741201],[45.735379,39.319719],[45.739978,39.473999],[45.298145,39.471751],[45.001987,39.740004],[44.79399,39.713003],[44.400009,40.005],[43.656436,40.253564],[43.752658,40.740201],[43.582746,41.092143]]]}}
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[43.582746,41.092143],[44.97248,41.248129],[45.179496,40.985354],[45.560351,40.81229],[45.359175,40.561504],[45.891907,40.218476],[45.610012,39.899994],[46.034534,39.628021],[46.483499,39.464155],[46.50572,38.770605],[46.143623,38.741201],[45.735379,39.319719],[45.739978,39.473999],[45.298145,39.471751],[45.001987,39.740004],[44.79399,39.713003],[44.400009,40.005],[43.656436,40.253564],[43.752658,40.740201],[43.582746,41.092143]]]}}
11 changes: 6 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
var intersect = require('../'),
test = require('tape'),
glob = require('glob'),
fs = require('fs');
fs = require('fs')
equal = new(require('geojson-equality'));

var REGEN = true;
var REGEN = false;

test('intersect -- features', function(t){
glob.sync(__dirname + '/fixtures/in/*.json').forEach(function(input) {
var features = JSON.parse(fs.readFileSync(input));
var output = intersect(features[0], features[1]);
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();
});
Expand All @@ -20,7 +21,7 @@ test('intersect -- geometries', function(t){
var features = JSON.parse(fs.readFileSync(input));
var output = intersect(features[0].geometry, features[1].geometry);
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();
});
Expand All @@ -30,4 +31,4 @@ test('intersect -- no overlap', function(t){
var output = intersect(noOverlap[0].geometry, noOverlap[1].geometry);
t.deepEqual(output, undefined);
t.end();
});
});