@@ -3,6 +3,7 @@ import 'package:turf/turf.dart';
3
3
4
4
import 'boolean_point_in_polygon.dart' ;
5
5
import 'boolean_point_on_line.dart' ;
6
+ import 'boolean_helper.dart' ;
6
7
7
8
/// [booleanContains] returns [true] if the second geometry is completely contained
8
9
/// by the first geometry.
@@ -11,162 +12,75 @@ import 'boolean_point_on_line.dart';
11
12
/// [booleanContains] returns the exact opposite result of the [booleanWithin] .
12
13
/// example:
13
14
/// ```dart
14
- /// var line = LineString(coordinates: [
15
+ /// final line = LineString(coordinates: [
15
16
/// Position.of([1, 1]),
16
17
/// Position.of([1, 2]),
17
18
/// Position.of([1, 3]),
18
19
/// Position.of([1, 4])
19
20
/// ]);
20
- /// var point = Point(coordinates: Position.of([1, 2]));
21
+ /// final point = Point(coordinates: Position.of([1, 2]));
21
22
/// booleanContains(line, point);
22
23
/// //=true
23
24
/// ```
24
25
bool booleanContains (GeoJSONObject feature1, GeoJSONObject feature2) {
25
- var geom1 = getGeom (feature1);
26
- var geom2 = getGeom (feature2);
26
+ final geom1 = getGeom (feature1);
27
+ final geom2 = getGeom (feature2);
27
28
28
- var coords1 = (geom1 as GeometryType ).coordinates;
29
- var coords2 = (geom2 as GeometryType ).coordinates;
30
- final exception = Exception ("{feature2 $geom2 geometry not supported}" );
29
+ final coords1 = (geom1 as GeometryType ).coordinates;
30
+ final coords2 = (geom2 as GeometryType ).coordinates;
31
31
if (geom1 is Point ) {
32
32
if (geom2 is Point ) {
33
33
return coords1 == coords2;
34
34
} else {
35
- throw exception ;
35
+ throw FeatureNotSupported (geom1, geom2) ;
36
36
}
37
37
} else if (geom1 is MultiPoint ) {
38
38
if (geom2 is Point ) {
39
- return _isPointInMultiPoint (geom1, geom2 );
39
+ return isPointInMultiPoint (geom2, geom1 );
40
40
} else if (geom2 is MultiPoint ) {
41
- return _isMultiPointInMultiPoint (geom1, geom2 );
41
+ return isMultiPointInMultiPoint (geom2, geom1 );
42
42
} else {
43
- throw exception ;
43
+ throw FeatureNotSupported (geom1, geom2) ;
44
44
}
45
45
} else if (geom1 is LineString ) {
46
46
if (geom2 is Point ) {
47
47
return booleanPointOnLine (geom2, geom1, ignoreEndVertices: true );
48
48
} else if (geom2 is LineString ) {
49
- return _isLineOnLine (geom1, geom2 );
49
+ return isLineOnLine (geom2, geom1 );
50
50
} else if (geom2 is MultiPoint ) {
51
- return _isMultiPointOnLine (geom1, geom2 );
51
+ return isMultiPointOnLine (geom2, geom1 );
52
52
} else {
53
- throw exception ;
53
+ throw FeatureNotSupported (geom1, geom2) ;
54
54
}
55
55
} else if (geom1 is Polygon ) {
56
56
if (geom2 is Point ) {
57
57
return booleanPointInPolygon ((geom2).coordinates, geom1,
58
58
ignoreBoundary: true );
59
59
} else if (geom2 is LineString ) {
60
- return _isLineInPoly (geom1, geom2 );
60
+ return isLineInPolygon (geom2, geom1 );
61
61
} else if (geom2 is Polygon ) {
62
62
return _isPolyInPoly (geom1, geom2);
63
63
} else if (geom2 is MultiPoint ) {
64
- return _isMultiPointInPoly (geom1, geom2 );
64
+ return isMultiPointInPolygon (geom2, geom1 );
65
65
} else {
66
- throw exception ;
66
+ throw FeatureNotSupported (geom1, geom2) ;
67
67
}
68
68
} else {
69
- throw exception ;
69
+ throw FeatureNotSupported (geom1, geom2) ;
70
70
}
71
71
}
72
72
73
- bool _isPointInMultiPoint (MultiPoint multiPoint, Point pt) {
74
- for (int i = 0 ; i < multiPoint.coordinates.length; i++ ) {
75
- if ((multiPoint.coordinates[i] == pt.coordinates)) {
76
- return true ;
77
- }
78
- }
79
- return false ;
80
- }
81
-
82
- bool _isMultiPointInMultiPoint (MultiPoint multiPoint1, MultiPoint multiPoint2) {
83
- for (Position coord2 in multiPoint2.coordinates) {
84
- bool match = false ;
85
- for (Position coord1 in multiPoint1.coordinates) {
86
- if (coord2 == coord1) {
87
- match = true ;
88
- }
89
- }
90
- if (! match) return false ;
91
- }
92
- return true ;
93
- }
94
-
95
- bool _isMultiPointOnLine (LineString lineString, MultiPoint multiPoint) {
96
- var haveFoundInteriorPoint = false ;
97
- for (var coord in multiPoint.coordinates) {
98
- if (booleanPointOnLine (Point (coordinates: coord), lineString,
99
- ignoreEndVertices: true )) {
100
- haveFoundInteriorPoint = true ;
101
- }
102
- if (! booleanPointOnLine (Point (coordinates: coord), lineString)) {
103
- return false ;
104
- }
105
- }
106
- return haveFoundInteriorPoint;
107
- }
108
-
109
- bool _isMultiPointInPoly (Polygon polygon, MultiPoint multiPoint) {
110
- for (var coord in multiPoint.coordinates) {
111
- if (! booleanPointInPolygon (coord, polygon, ignoreBoundary: true )) {
112
- return false ;
113
- }
114
- }
115
- return true ;
116
- }
117
-
118
- bool _isLineOnLine (LineString lineString1, LineString lineString2) {
119
- var haveFoundInteriorPoint = false ;
120
- for (Position coord in lineString2.coordinates) {
121
- if (booleanPointOnLine (
122
- Point (coordinates: coord),
123
- lineString1,
124
- ignoreEndVertices: true ,
125
- )) {
126
- haveFoundInteriorPoint = true ;
127
- }
128
- if (! booleanPointOnLine (
129
- Point (coordinates: coord),
130
- lineString1,
131
- ignoreEndVertices: false ,
132
- )) {
133
- return false ;
134
- }
135
- }
136
- return haveFoundInteriorPoint;
137
- }
138
-
139
- bool _isLineInPoly (Polygon polygon, LineString linestring) {
140
- var polyBbox = bbox (polygon);
141
- var lineBbox = bbox (linestring);
142
- if (! _doBBoxesOverlap (polyBbox, lineBbox)) {
143
- return false ;
144
- }
145
- for (var i = 0 ; i < linestring.coordinates.length - 1 ; i++ ) {
146
- var midPoint =
147
- midpointRaw (linestring.coordinates[i], linestring.coordinates[i + 1 ]);
148
- if (booleanPointInPolygon (
149
- midPoint,
150
- polygon,
151
- ignoreBoundary: true ,
152
- )) {
153
- return true ;
154
- }
155
- }
156
- return false ;
157
- }
158
-
159
73
/// Is Polygon2 in Polygon1
160
74
/// Only takes into account outer rings
161
75
bool _isPolyInPoly (GeoJSONObject geom1, GeoJSONObject geom2) {
162
- var poly1Bbox = bbox (geom1);
163
- var poly2Bbox = bbox (geom2);
76
+ final poly1Bbox = bbox (geom1);
77
+ final poly2Bbox = bbox (geom2);
164
78
if (! _doBBoxesOverlap (poly1Bbox, poly2Bbox)) {
165
79
return false ;
166
80
}
167
81
168
- for (var ring in (geom2 as GeometryType ).coordinates) {
169
- for (var coord in ring) {
82
+ for (final ring in (geom2 as GeometryType ).coordinates) {
83
+ for (final coord in ring) {
170
84
if (! booleanPointInPolygon (coord, geom1)) {
171
85
return false ;
172
86
}
0 commit comments