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

New module @turf/boolean-disjoint #805

Merged
merged 8 commits into from
Jun 20, 2017
Merged

New module @turf/boolean-disjoint #805

merged 8 commits into from
Jun 20, 2017

Conversation

rowanwins
Copy link
Member

@rowanwins rowanwins commented Jun 20, 2017

New Module @turf/boolean-disjoint

Geometry Support

  • Point
  • LineString
  • Polygon
  • MultiPoint
  • MultiLineString
  • MultiPolygon

JSDocs

/**
 * Boolean-disjoint returns (TRUE) if the intersection of the two geometries is an empty set.
 *
 * @name booleanDisjoint
 * @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
 * @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
 * @returns {Boolean} true/false
 * @example
 * const point = {
 *   "type": "Feature",
 *   "properties": {},
 *   "geometry": {
 *     "type": "Point",
 *     "coordinates": [2, 2]
 *   }
 * }
 * const line = {
 *   "type": "Feature",
 *   "properties": {},
 *   "geometry": {
 *     "type": "LineString",
 *     "coordinates": [[1, 1], [1, 2], [1, 3], [1, 4]]
 *   }
 * }
 * turf.booleanDisjoint(line, point);
 * //=true
 */
  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Have read How To Contribute.
  • Run npm test at the sub modules where changes have occurred.
  • Run npm run lint to ensure code style at the turf module level.

Submitting a new TurfJS Module.

  • Overview description of proposed module.
  • Include JSDocs with a basic example.
  • Execute ./scripts/generate-readmes to create README.md.
  • Add yourself to contributors in package.json using "Full Name <@github Username>".

Gday @DenisCarriere

The boolean beast continues! Disjoint returns 1 or t (TRUE) if the intersection of the two geometries is an empty set. In other words, geometries are disjoint if they do not intersect one another.

Think this one is a fair bit more straight forward so hopefully we're all good.

const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
const [feature1, feature2] = geojson.features;
suite.add(name, () => disjoint(feature1, feature2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new benchmark technique we've been adding is the Single process results by adding console.time & console.timeEnd before the suite.add.

Example

console.time(name);
disjoint(feature1, feature2);
console.timeEnd(name);
suite.add(name, () => disjoint(feature1, feature2));

case 'Point':
switch (type2) {
case 'Point':
return !compareCoords(coords1, coords2) || false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure if || false is necessary?

If compareCoords only outputs true/false then if the result is false it would fallback again on false.

I'll look into this, but I'm pretty sure || false can be dropped.

}

// /**
// * Is Polygon (geom1) in Polygon (geom2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// /**
//  *

Double commented?

const [feature1, feature2] = geojson.features;
const result = disjoint(feature1, feature2);

if (process.env.SHAPELY) shapely.disjoint(feature1, feature2).then(result => t.true(result, '[true] shapely - ' + name));
Copy link
Member

@DenisCarriere DenisCarriere Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woot! 👍 Did this work for you @rowanwins ? (It does for me)

- Drop `|| false` from `index.js`
- Remove extra comments
- Add single process benchmark
@DenisCarriere
Copy link
Member

DenisCarriere commented Jun 20, 2017

Updates to boolean-disjoint

  • Drop || false from index.js
  • Remove extra comments
  • Add single process benchmark

Copy link
Member

@DenisCarriere DenisCarriere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, we can always add more test cases to improve it in the future.

@DenisCarriere
Copy link
Member

DenisCarriere commented Jun 20, 2017

Pretty sure all (Multi)Geometries would be valid inputs using boolean-disjoint:

  • MultiPoint
  • MultiLineString (Commit 2f35d08)
  • MultiPolygon (Commit 2f35d08)

@DenisCarriere
Copy link
Member

Seems like Shapely is able to handle disjoint with Multi Geometries.

Polygon - MultiPolygon (Disjoint = True)

image

Polygon - MultiPolygon (Disjoint = False)

image

@DenisCarriere
Copy link
Member

DenisCarriere commented Jun 20, 2017

@stebogit @rowanwins I've organized/grouped the test fixtures, let me know what you think about it.

Example: LineString (Feature1) => Point (Feature2)

  • true/LineString/Point/LineString-Point-1.geojson
  • true/LineString/Point/LineString-Point-2.geojson

@DenisCarriere
Copy link
Member

DenisCarriere commented Jun 20, 2017

@rowanwins I've refactored disjoint to support MultiGeometries, I'm pretty sure this approach could be applied to most of the other boolean modules using flattenEach from @turf/meta.

module.exports = function (feature1, feature2) {
    var boolean;
    flattenEach(feature1, function (flatten1) {
        flattenEach(feature2, function (flatten2) {
            if (boolean === false) return false;
            boolean = disjoint(flatten1.geometry, flatten2.geometry);
        });
    });
    return boolean;
};

This way you only need to focus on support the basic geometries.

/**
 * Disjoint operation for simple Geometries (Point/LineString/Polygon)
 *
 * @private
 * @param {Geometry<any>} geom1 GeoJSON Geometry
 * @param {Geometry<any>} geom2 GeoJSON Geometry
 * @returns {Boolean} true/false
 */
function disjoint(geom1, geom2) {

if (dxl > 0) {
return LineSegmentStart[0] <= Point[0] && Point[0] <= LineSegmentEnd[0];
} else {
return LineSegmentEnd[0] <= Point[0] && Point[0] <= LineSegmentStart[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rowanwins Any thoughts how to hit these lines (146 & 151) with Test Fixtures?

$ tap test.js --coverage
test.js ............................................. 74/74
total ............................................... 74/74

  74 passing (4s)

  ok
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |    96.36 |       85 |      100 |     96.3 |                |
 index.js |    96.36 |       85 |      100 |     96.3 |        146,151 |
----------|----------|----------|----------|----------|----------------|

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put it into a seperate module and test that module 😀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this source code come from?

Lack of comments is hard to troubleshoot.

@DenisCarriere DenisCarriere changed the title Initial commit of @turf/boolean-disjoint New module @turf/boolean-disjoint Jun 20, 2017
@DenisCarriere DenisCarriere merged commit e52488d into master Jun 20, 2017
@DenisCarriere DenisCarriere deleted the boolean-disjoint branch September 5, 2017 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants