Skip to content

Commit

Permalink
New module @turf/boolean-equal (#869)
Browse files Browse the repository at this point in the history
* implemented @turf/boolean-equal

* Add boolean-shapely to tests

* Add failing tests
From @rowanwins's comment #869 (comment)

* Add reversed lines (passes)

* Update boolean-contains

* Publish boolean-equal
  • Loading branch information
stebogit authored and DenisCarriere committed Aug 2, 2017
1 parent 5c1e19e commit d3e024f
Show file tree
Hide file tree
Showing 27 changed files with 1,259 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/turf-boolean-equal/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 TurfJS

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 changes: 49 additions & 0 deletions packages/turf-boolean-equal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# @turf/boolean-equal

# booleanEqual

Determine whether two geometries of the same type have identical X,Y coordinate values. See [spatial relations](http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm).

**Parameters**

- `feature1` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** GeoJSON Feature or Geometry
- `feature2` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** GeoJSON Feature or Geometry

**Examples**
```javascript
var pt1 = turf.point([0, 0]);
var pt2 = turf.point([0, 0]);
var pt3 = turf.point([1, 1]);

turf.equal(pt1, pt2);
//= true
turf.equal(pt2, pt3);
//= false
```

Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true/false

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->

---

This module is part of the [Turfjs project](http://turfjs.org/), an open source
module collection dedicated to geographic algorithms. It is maintained in the
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
PRs and issues.

### Installation

Install this module individually:

```sh
$ npm install @turf/boolean-equal
```

Or install the Turf module that includes it as a function:

```sh
$ npm install @turf/turf
```
50 changes: 50 additions & 0 deletions packages/turf-boolean-equal/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const path = require('path');
const glob = require('glob');
const load = require('load-json-file');
const equal = require('./');
const Benchmark = require('benchmark');

/**
* Benchmark Results
*
* linear-rings: 1.167ms
* lines: 0.132ms
* multipoints: 1.283ms
* points: 0.043ms
* polygons: 0.387ms
* linear-rings: 0.166ms
* lines: 0.068ms
* multipoints: 0.148ms
* points: 0.029ms
* polygons: 0.131ms
* reverse-lines: 0.711ms
* reverse-polygons: 0.090ms
* linear-rings x 337,778 ops/sec ±2.95% (76 runs sampled)
* lines x 367,227 ops/sec ±2.50% (78 runs sampled)
* multipoints x 54,325 ops/sec ±1.71% (80 runs sampled)
* points x 529,881 ops/sec ±3.62% (74 runs sampled)
* polygons x 177,515 ops/sec ±2.34% (80 runs sampled)
* linear-rings x 92,709 ops/sec ±1.51% (82 runs sampled)
* lines x 94,539 ops/sec ±2.52% (81 runs sampled)
* multipoints x 34,458 ops/sec ±2.21% (81 runs sampled)
* points x 384,832 ops/sec ±2.74% (81 runs sampled)
* polygons x 71,289 ops/sec ±2.67% (77 runs sampled)
* reverse-lines x 83,612 ops/sec ±3.31% (77 runs sampled)
* reverse-polygons x 64,686 ops/sec ±1.91% (76 runs sampled)
*/
const suite = new Benchmark.Suite('turf-boolean-equal');
glob.sync(path.join(__dirname, 'test', '**', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
const [feature1, feature2] = geojson.features;
console.time(name);
equal(feature1, feature2);
console.timeEnd(name);
suite.add(name, () => equal(feature1, feature2));
});

suite
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();

10 changes: 10 additions & 0 deletions packages/turf-boolean-equal/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="geojson" />

type Feature = GeoJSON.Feature<any> | GeoJSON.GeometryObject

/**
* http://turfjs.org/docs/#boolean-equal
*/
declare function equal(feature1: Feature, feature2: Feature): boolean;
declare namespace equal {}
export = equal;
34 changes: 34 additions & 0 deletions packages/turf-boolean-equal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var GeojsonEquality = require('geojson-equality');
var cleanCoords = require('@turf/clean-coords');
var invariant = require('@turf/invariant');
var getGeomType = invariant.getGeomType;

/**
* Determine whether two geometries of the same type have identical X,Y coordinate values.
* See http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm
*
* @name booleanEqual
* @param {Geometry|Feature} feature1 GeoJSON input
* @param {Geometry|Feature} feature2 GeoJSON input
* @returns {boolean} true if the objects are equal, false otherwise
* @example
* var pt1 = turf.point([0, 0]);
* var pt2 = turf.point([0, 0]);
* var pt3 = turf.point([1, 1]);
*
* turf.equal(pt1, pt2);
* //= true
* turf.equal(pt2, pt3);
* //= false
*/
module.exports = function (feature1, feature2) {
// validation
if (!feature1) throw new Error('feature1 is required');
if (!feature2) throw new Error('feature2 is required');
var type1 = getGeomType(feature1);
var type2 = getGeomType(feature2);
if (type1 !== type2) return false;

var equality = new GeojsonEquality({precision: 6});
return equality.compare(cleanCoords(feature1), cleanCoords(feature2));
};
51 changes: 51 additions & 0 deletions packages/turf-boolean-equal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@turf/boolean-equal",
"version": "4.5.0",
"description": "turf boolean-equal module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
],
"scripts": {
"test": "node test.js",
"bench": "node bench.js"
},
"repository": {
"type": "git",
"url": "git://github.com/Turfjs/turf.git"
},
"keywords": [
"turf",
"gis",
"boolean",
"de-9im",
"equal",
"boolean-equal"
],
"author": "Turf Authors",
"contributors": [
"Tom MacWright <@tmcw>",
"Tim Channell <@tcql>",
"Stefano Borghi <@stebogit>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "^4.5.2",
"benchmark": "^2.1.4",
"boolean-shapely": "^0.1.2",
"glob": "^7.1.1",
"load-json-file": "^2.0.0",
"tape": "^4.6.3"
},
"dependencies": {
"@turf/clean-coords": "^4.5.0",
"@turf/invariant": "^4.5.2",
"geojson-equality": "0.1.6"
}
}
53 changes: 53 additions & 0 deletions packages/turf-boolean-equal/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const glob = require('glob');
const path = require('path');
const test = require('tape');
const load = require('load-json-file');
const shapely = require('boolean-shapely');
const {point, lineString, polygon} = require('@turf/helpers');
const equal = require('./');

test('turf-boolean-equal', t => {
// True Fixtures
glob.sync(path.join(__dirname, 'test', 'true', '**', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
const [feature1, feature2] = geojson.features;
const result = equal(feature1, feature2);

if (process.env.SHAPELY) shapely.contains(feature1, feature2).then(result => t.true(result, '[true] shapely - ' + name));
t.true(result, '[true] ' + name);
});
// False Fixtures
glob.sync(path.join(__dirname, 'test', 'false', '**', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
const [feature1, feature2] = geojson.features;
const result = equal(feature1, feature2);

if (process.env.SHAPELY) shapely.contains(feature1, feature2).then(result => t.false(result, '[false] shapely - ' + name));
t.false(result, '[false] ' + name);
});
t.end();
});

const pt = point([9, 50]);
const line1 = lineString([[7, 50], [8, 50], [9, 50]]);
const line2 = lineString([[7, 50], [8, 50], [9, 50]]);
const poly1 = polygon([[[8.5, 50], [9.5, 50], [9.5, 49], [8.5, 49], [8.5, 50]]]);
const poly2 = polygon([[[8.5, 50], [9.5, 50], [9.5, 49], [8.5, 49], [8.5, 50]]]);
const poly3 = polygon([[[10, 50], [10.5, 50], [10.5, 49], [10, 49], [10, 50]]]);

test('turf-boolean-equal -- geometries', t => {
t.true(equal(line1.geometry, line2.geometry), '[true] LineString geometry');
t.true(equal(poly1.geometry, poly2.geometry), '[true] Polygon geometry');
t.false(equal(poly1.geometry, poly3.geometry), '[false] Polygon geometry');
t.false(equal(pt, line1), '[false] different types');
t.end();
});


test('turf-boolean-equal -- throws', t => {
t.throws(() => equal(null, line1), /feature1 is required/, 'missing feature1');
t.throws(() => equal(line1, null), /feature2 is required/, 'missing feature2');
t.end();
});
55 changes: 55 additions & 0 deletions packages/turf-boolean-equal/test/false/linear-rings.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-53.674866943359375,
28.30135788537988
],
[
-53.337249755859375,
28.47412369059679
],
[
-53.524017333984375,
28.17492820114568
],
[
-53.674866943359375,
28.30135788537988
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-55.674866943359375,
28.30135788537988
],
[
-55.337249755859375,
28.47412369059679
],
[
-55.524017333984375,
28.17492820114568
],
[
-55.674866943359375,
28.30135788537988
]
]
}
}
]
}
55 changes: 55 additions & 0 deletions packages/turf-boolean-equal/test/false/lines.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
0,
5
],
[
5,
5
],
[
5,
0
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
1,
0
],
[
1,
5
],
[
6,
5
],
[
6,
0
]
]
}
}
]
}
Loading

0 comments on commit d3e024f

Please sign in to comment.