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

Improvement for @turf/boolean-overlap #868

Merged
merged 16 commits into from
Jul 27, 2017
Merged
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 packages/turf-boolean-overlap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# booleanOverlap

Compares two geometries of the same dimension and returns true if their intersection set results in a geometry different from both but of the same dimension. It applies to Polygon/Polygon, LineString/LineString, Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon
Compares two geometries of the same dimension and returns true if their intersection set results in a geometry different from both but of the same dimension. It applies to Polygon/Polygon, LineString/LineString, Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon. See [spatial relations](http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm).

**Parameters**

Expand Down
66 changes: 33 additions & 33 deletions packages/turf-boolean-overlap/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ const Benchmark = require('benchmark');
/**
* Benchmark Results
*
* equal-linear-rings: 2.348ms
* equal-lines-inverse: 0.319ms
* equal-multipoints: 0.703ms
* equal-polygons: 0.343ms
* linear-rings: 19.876ms
* lines: 1.244ms
* multipoints: 1.003ms
* polygon-with-hole-polygon: 1.822ms
* polygons: 0.242ms
* linear-rings: 5.881ms
* lines: 8.569ms
* multipoints: 0.082ms
* polygon-with-hole-polygon: 1.158ms
* polygons: 0.114ms
* simple-lines: 0.724ms
* single-multipoints: 0.055ms
* equal-linear-rings x 39,585 ops/sec ±24.47% (40 runs sampled)
* equal-lines-inverse x 52,454 ops/sec ±24.98% (59 runs sampled)
* equal-multipoints x 24,361 ops/sec ±10.90% (59 runs sampled)
* equal-polygons x 36,479 ops/sec ±22.33% (48 runs sampled)
* linear-rings x 2,625 ops/sec ±16.13% (47 runs sampled)
* lines x 6,756 ops/sec ±5.54% (70 runs sampled)
* multipoints x 234,229 ops/sec ±7.44% (64 runs sampled)
* polygon-with-hole-polygon x 34,922 ops/sec ±6.70% (61 runs sampled)
* polygons x 79,687 ops/sec ±2.98% (79 runs sampled)
* linear-rings x 3,252 ops/sec ±5.96% (66 runs sampled)
* lines x 4,328 ops/sec ±8.67% (63 runs sampled)
* multipoints x 285,695 ops/sec ±2.91% (77 runs sampled)
* polygon-with-hole-polygon x 39,154 ops/sec ±2.30% (80 runs sampled)
* polygons x 79,419 ops/sec ±2.21% (77 runs sampled)
* simple-lines x 7,479 ops/sec ±3.99% (71 runs sampled)
* single-multipoints x 232,414 ops/sec ±13.49% (67 runs sampled)
* equal-linear-rings: 1.286ms
* equal-lines: 0.573ms
* equal-multipoints: 0.823ms
* equal-polygons: 0.312ms
* linear-rings: 8.417ms
* lines: 0.939ms
* multipoints: 0.471ms
* polygon-with-hole-polygon: 0.827ms
* polygons: 0.306ms
* linear-rings: 2.558ms
* lines: 3.282ms
* multipoints: 0.058ms
* polygon-with-hole-polygon: 1.105ms
* polygons: 0.067ms
* simple-lines: 1.083ms
* single-multipoints: 0.041ms
* equal-linear-rings x 86,843 ops/sec ±4.89% (78 runs sampled)
* equal-lines x 75,485 ops/sec ±9.21% (75 runs sampled)
* equal-multipoints x 33,422 ops/sec ±2.26% (77 runs sampled)
* equal-polygons x 71,869 ops/sec ±1.76% (84 runs sampled)
* linear-rings x 5,006 ops/sec ±4.26% (73 runs sampled)
* lines x 7,781 ops/sec ±3.84% (75 runs sampled)
* multipoints x 287,008 ops/sec ±1.61% (77 runs sampled)
* polygon-with-hole-polygon x 43,735 ops/sec ±1.83% (83 runs sampled)
* polygons x 91,882 ops/sec ±1.55% (81 runs sampled)
* linear-rings x 4,008 ops/sec ±3.07% (72 runs sampled)
* lines x 5,632 ops/sec ±4.55% (71 runs sampled)
* multipoints x 271,445 ops/sec ±3.77% (75 runs sampled)
* polygon-with-hole-polygon x 41,716 ops/sec ±1.67% (82 runs sampled)
* polygons x 81,217 ops/sec ±2.46% (81 runs sampled)
* simple-lines x 8,880 ops/sec ±3.63% (75 runs sampled)
* single-multipoints x 281,914 ops/sec ±2.27% (76 runs sampled)
*/
const suite = new Benchmark.Suite('turf-boolean-disjoint');
const suite = new Benchmark.Suite('turf-boolean-overlap');
glob.sync(path.join(__dirname, 'test', '**', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
Expand Down
29 changes: 4 additions & 25 deletions packages/turf-boolean-overlap/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
var meta = require('@turf/meta');
var helpers = require('@turf/helpers');
var invariant = require('@turf/invariant');
var lineOverlap = require('@turf/line-overlap');
var lineIntersect = require('@turf/line-intersect');
var GeojsonEquality = require('geojson-equality');
var coordAll = meta.coordAll;
var lineString = helpers.lineString;
var flattenEach = meta.flattenEach;
var coordReduce = meta.coordReduce;
var segmentEach = meta.segmentEach;
var getGeomType = invariant.getGeomType;

/**
* Compares two geometries of the same dimension and returns true if their intersection set results in a geometry
* different from both but of the same dimension. It applies to Polygon/Polygon, LineString/LineString,
* Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon
* Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon.
*
* @name booleanOverlap
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature1 input
Expand All @@ -38,9 +35,8 @@ module.exports = function (feature1, feature2) {
if (type1 !== type2) throw new Error('features must be of the same type');
if (type1 === 'Point') throw new Error('Point geometry not supported');

// false if features are equal
var considerDirection = (type1 === 'LineString' && feature1.coor);
var equality = new GeojsonEquality({direction: considerDirection, precision: 6});
// features must be not equal
var equality = new GeojsonEquality({precision: 6});
if (equality.compare(feature1, feature2)) return false;

var overlap = 0;
Expand Down Expand Up @@ -77,20 +73,3 @@ module.exports = function (feature1, feature2) {

return overlap > 0;
};

// todo: replace with new @turf/meta.segmentEach
function segmentEach(geojson, callback) {
var currentIndex = 0;
flattenEach(geojson, function (feature, featureIndex, featureSubIndex) {
// (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
var type = feature.geometry.type;
if (type === 'Point' || type === 'MultiPoint') return;
// Generate 2-vertex line segments
coordReduce(feature, function (previousCoords, currentCoords) {
var currentSegment = lineString([previousCoords, currentCoords], feature.properties);
callback(currentSegment, currentIndex, featureIndex, featureSubIndex);
currentIndex++;
return currentCoords;
});
});
}
5 changes: 3 additions & 2 deletions packages/turf-boolean-overlap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"gis",
"boolean",
"de-9im",
"overlap"
"overlap",
"boolean-overlap"
],
"author": "Turf Authors",
"contributors": [
Expand All @@ -34,13 +35,13 @@
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "^4.5.2",
"benchmark": "^2.1.4",
"glob": "^7.1.1",
"load-json-file": "^2.0.0",
"tape": "^4.6.3"
},
"dependencies": {
"@turf/helpers": "^4.5.2",
"@turf/invariant": "^4.5.2",
"@turf/line-overlap": "^4.5.2",
"@turf/line-intersect": "^4.5.2",
Expand Down
25 changes: 18 additions & 7 deletions packages/turf-boolean-overlap/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const glob = require('glob');
const path = require('path');
const test = require('tape');
const load = require('load-json-file');
const {point, lineString} = require('@turf/helpers');
const {point, lineString, polygon} = require('@turf/helpers');
const overlap = require('./');

test('turf-boolean-overlap', t => {
Expand All @@ -29,13 +29,24 @@ test('turf-boolean-overlap', t => {
t.end();
});

test('turf-boolean-overlap -- throws', t => {
const pt = point([9, 50]);
const line = lineString([[7, 50], [8, 50], [9, 50]]);
const pt = point([9, 50]);
const line1 = lineString([[7, 50], [8, 50], [9, 50]]);
const line2 = lineString([[8, 50], [9, 50], [10, 50]]);
const poly1 = polygon([[[8.5, 50], [9.5, 50], [9.5, 49], [8.5, 49], [8.5, 50]]]);
const poly2 = polygon([[[8, 50], [9, 50], [9, 49], [8, 49], [8, 50]]]);
const poly3 = polygon([[[10, 50], [10.5, 50], [10.5, 49], [10, 49], [10, 50]]]);

t.throws(() => overlap(null, line), /feature1 is required/, 'missing feature1');
t.throws(() => overlap(line, null), /feature2 is required/, 'missing feature2');
t.throws(() => overlap(pt, line), /features must be of the same type/, 'different types');
test('turf-boolean-overlap -- geometries', t => {
t.true(overlap(line1.geometry, line2.geometry), `[true] LineString geometry`);
t.true(overlap(poly1.geometry, poly2.geometry), `[true] Polygon geometry`);
t.false(overlap(poly1.geometry, poly3.geometry), `[false] Polygon geometry`);
t.end();
});

test('turf-boolean-overlap -- throws', t => {
t.throws(() => overlap(null, line1), /feature1 is required/, 'missing feature1');
t.throws(() => overlap(line1, null), /feature2 is required/, 'missing feature2');
t.throws(() => overlap(pt, line1), /features must be of the same type/, 'different types');
t.throws(() => overlap(pt, pt), /Point geometry not supported/, 'geometry not supported');

t.end();
Expand Down