Skip to content

Commit

Permalink
#88 crosses, disjoint, equal, intersects, pip @lukas-h
Browse files Browse the repository at this point in the history
  • Loading branch information
armantorkzaban committed Aug 29, 2022
1 parent 29ff2b8 commit 65eddf8
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 64 deletions.
3 changes: 1 addition & 2 deletions lib/src/booleans/boolean_crosses.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '../../helpers.dart';
import '../invariant.dart';
import '../line_intersect.dart';
import '../polygon_to_line.dart';
import 'boolean_point_in_polygon.dart';
Expand Down Expand Up @@ -115,7 +114,7 @@ bool doLineStringsCross(LineString lineString1, LineString lineString2) {
}

bool doLineStringAndPolygonCross(LineString lineString, Polygon polygon) {
LineString line = polygonToLine(polygon);
Feature line = polygonToLine(polygon) as Feature;
var doLinesIntersect = lineIntersect(lineString, line);
if (doLinesIntersect.features.isNotEmpty) {
return true;
Expand Down
9 changes: 4 additions & 5 deletions lib/src/booleans/boolean_disjoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ bool booleanDisjoint(GeoJSONObject feature1, GeoJSONObject feature2) {
flattenEach(
feature2,
(flatten2, featureIndex, multiFeatureIndex) {
if (bool == false) {
return false;
if (!bool) {
return bool;
}
bool = _disjoint(flatten1.geometry!, flatten2.geometry!);
},
Expand All @@ -39,7 +39,7 @@ bool booleanDisjoint(GeoJSONObject feature1, GeoJSONObject feature2) {
}

/// Disjoint operation for simple Geometries (Point/LineString/Polygon)
_disjoint(GeometryType geom1, GeometryType geom2) {
bool _disjoint(GeometryType geom1, GeometryType geom2) {
if (geom1 is Point) {
if (geom2 is Point) {
return !_compareCoords(geom1.coordinates, geom2.coordinates);
Expand All @@ -64,9 +64,8 @@ _disjoint(GeometryType geom1, GeometryType geom2) {
} else if (geom2 is Polygon) {
return !_isPolyInPoly(geom2, geom1);
}
} else {
return false;
}
return false;
}

// http://stackoverflow.com/a/11908158/1979085
Expand Down
4 changes: 2 additions & 2 deletions lib/src/booleans/boolean_equal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool booleanEqual(

var equality = Equality(
precision: precision,
shiftedPolygon: shiftedPolygon,
direction: direction);
shiftedPolygons: shiftedPolygon,
reversedGeometries: direction);
return equality.compare(cleanCoords(feature1), cleanCoords(feature2));
}
3 changes: 1 addition & 2 deletions lib/src/booleans/boolean_intersect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import '../../helpers.dart';
import '../../meta.dart';
import 'boolean_disjoint.dart';


Boolean-intersects returns (TRUE) two geometries intersect.
/// returns (TRUE) when two geometries intersect.
/// @name booleanIntersects
/// @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
/// @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
Expand Down
12 changes: 6 additions & 6 deletions lib/src/booleans/boolean_point_in_polygon.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
// modified from: https://github.com/substack/point-in-polygon/blob/master/index.js
// which was modified from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
import 'package:pip/pip.dart';

import 'package:turf_pip/turf_pip.dart';

import '../../helpers.dart';
import '../invariant.dart';

/// Takes a [Point], and a [Polygon] or [MultiPolygon]and determines if the
/// [Point] resides inside the [Polygon]. The polygon can be convex or concave.
Expand Down Expand Up @@ -58,11 +58,11 @@ bool booleanPointInPolygon(Position point, GeoJSONObject polygon,

var result = false;
for (var i = 0; i < polys!.length; ++i) {
var polyResult =
pip(Point(coordinates: point), Polygon(coordinates: polys[i]));
if (polyResult == 0) {
var polyResult = pointInPolygon(
Point(coordinates: point), Polygon(coordinates: polys[i]));
if (polyResult == PointInPolygonResult.isOnEdge) {
return ignoreBoundary ? false : true;
} else if (polyResult) {
} else if (polyResult == PointInPolygonResult.isInside) {
result = true;
}
}
Expand Down
77 changes: 34 additions & 43 deletions lib/src/line_intersect.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:sweepline_intersections/sweepline_intersections.dart';

import '../helpers.dart';

Expand All @@ -8,71 +9,61 @@ import '../helpers.dart';
/// example:
/// ```dart
/// var line1 = LineString(coordinates:[
/// Position.of([126, -11]),
/// Position.of([126, -11]),
/// Position.of([129, -21]),
/// ]);
/// var line2 = LineString(coordinates:[
/// Position.of([123, -18]),
/// Position.of([123, -18]),
/// Position.of([131, -14]),
/// ]);
/// var intersects = lineIntersect(line1, line2);
/// //addToMap
/// var addToMap = [line1, line2, intersects]
FeatureCollection<Point> lineIntersect(
GeoJSONObject line1,
GeoJSONObject line2,
{
bool removeDuplicates = true,
bool ignoreSelfIntersections = false
}
){
var features= <Feature>[];
if (line1 is FeatureCollection)
{ features = features..addAll(line1.features);
} else if (line1 is Feature) {features.add(line1);}
else if (
line1 is LineString ||
line1 is Polygon ||
line1 is MultiLineString ||
line1 is MultiPolygon
) {
FeatureCollection<Point> lineIntersect(GeoJSONObject line1, GeoJSONObject line2,
{bool removeDuplicates = true, bool ignoreSelfIntersections = false}) {
var features = <Feature>[];
if (line1 is FeatureCollection) {
features = features..addAll(line1.features);
} else if (line1 is Feature) {
features.add(line1);
} else if (line1 is LineString ||
line1 is Polygon ||
line1 is MultiLineString ||
line1 is MultiPolygon) {
features.add(Feature(geometry: line1 as GeometryObject));
}
if (line2 is FeatureCollection)
{features = features..addAll(line2.features);}
else if (line2 is Feature) {features.add(line2);}
else if (
line2 is LineString ||
line2 is Polygon ||
line2 is MultiLineString ||
line2 is MultiPolygon
) {
if (line2 is FeatureCollection) {
features = features..addAll(line2.features);
} else if (line2 is Feature) {
features.add(line2);
} else if (line2 is LineString ||
line2 is Polygon ||
line2 is MultiLineString ||
line2 is MultiPolygon) {
features.add(Feature(geometry: line2 as GeometryObject));
}
var intersections = findIntersections(
FeatureCollection(features: features),
ignoreSelfIntersections
);
var intersections = sweeplineIntersections(
FeatureCollection(features: features), ignoreSelfIntersections);
var results: Intersection[] = [];
var results = [];
if (removeDuplicates) {
const unique: Record<string, boolean> = {};
intersections.forEach((intersection) => {
var key = intersection.join(",");
if (!unique[key]) {
unique[key] = true;
results.push(intersection);
Set unique = {};
intersections.forEach((intersection) {
if (!unique.contains(intersection)) {
unique.add(intersection);
results.add(intersection);
}
});
} else {
results = intersections;
}
return FeatureCollection(features: results.map((r) => Feature(geometry: Point(coordinates:r))));
return FeatureCollection(
features: results
.map((r) => Feature(geometry: Point(coordinates: r)))
.toList());
}
/**
* import { feature, featureCollection, point } from "@turf/helpers";
import {
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ publish_to: none
dependencies:
json_annotation: ^4.4.0
turf_equality: ^0.0.1
sweepline_intersections:
git: https://github.com/dartclub/sweepline_intersections.git
turf_pip: ^0.0.1+1

dev_dependencies:
dartclub_lint: ^0.4.2
test: ^1.19.3
Expand Down
7 changes: 4 additions & 3 deletions test/booleans/disjoint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:turf/src/booleans/boolean_disjoint.dart';

main() {
group(
'boolean_crosses',
'boolean_disjoint',
() {
var inDir = Directory('./test/examples/booleans/disjoint/true');
var inDir = Directory('./test/examples/booleans/disjoint/test/true');
for (var file in inDir.listSync(recursive: true)) {
if (file is File && file.path.endsWith('.geojson')) {
test(
Expand All @@ -26,7 +26,8 @@ main() {
},
);
// False Fixtures
var inDir1 = Directory('./test/examples/booleans/disjoint/false');
var inDir1 =
Directory('./test/examples/booleans/disjoint/test/false');
for (var file in inDir1.listSync(recursive: true)) {
if (file is File && file.path.endsWith('.geojson')) {
test(
Expand Down
2 changes: 1 addition & 1 deletion test/booleans/intersect_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:test/test.dart';
import 'package:turf/helpers.dart';
import 'package:turf/src/booleans/boolean_intersect.dart';

// main() {
main() {
var featureCollection = FeatureCollection(features: [
Feature(
properties: {"fill": "#ff0000"},
Expand Down

3 comments on commit 65eddf8

@lukas-h
Copy link
Member

Choose a reason for hiding this comment

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

@armantorkzaban Is this the commit, where the test behaves strangely?

@armantorkzaban
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I think you are talking about SweepLine's test. Or have had something funny here as well? I am now confused.

@lukas-h
Copy link
Member

Choose a reason for hiding this comment

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

No, then it's Sweepline

Please sign in to comment.