@@ -48,7 +48,6 @@ FeatureCollection<LineString> lineOverlap(
48
48
maxX: bb.lng2.toDouble (),
49
49
maxY: bb.lat2.toDouble ());
50
50
}
51
- // Optional parameters
52
51
53
52
// Containers
54
53
var features = < Feature <LineString >> [];
@@ -59,108 +58,127 @@ FeatureCollection<LineString> lineOverlap(
59
58
getMinX: (Feature <LineString > feature) => bbox (feature).lng1.toDouble (),
60
59
toBBox: (feature) => _toRBBox (feature));
61
60
62
- var line = lineSegment (line1);
61
+ FeatureCollection < LineString > line = lineSegment (line1);
63
62
tree.load (line.features);
64
63
Feature <LineString >? overlapSegment;
65
64
var additionalSegments = < Feature <LineString >> [];
66
65
67
66
// Line Intersection
68
67
69
68
// Iterate over line segments
70
- segmentEach (line2, (Feature <LineString > currentSegment, int featureIndex,
71
- int ? multiFeatureIndex, int ? geometryIndex, int segmentIndex) {
72
- var doesOverlaps = false ;
69
+ segmentEach (
70
+ line2,
71
+ (Feature <LineString > currentSegment, int featureIndex,
72
+ int ? multiFeatureIndex, int ? geometryIndex, int segmentIndex) {
73
+ var doesOverlap = false ;
73
74
74
- // Iterate over each segments which falls within the same bounds
75
- featureEach (
75
+ // Iterate over each segments which falls within the same bounds
76
+ featureEach (
76
77
FeatureCollection <LineString >(
77
- features: tree.search (_toRBBox (currentSegment))), (match, index) {
78
- if (! doesOverlaps) {
79
- List <Position > coordsSegment = () {
80
- List <Position > list = getCoords (currentSegment) as List <Position >;
81
- list.sort ();
82
- return list;
83
- }();
84
- List <Position > coordsMatch = () {
85
- List <Position > list = getCoords (match) as List <Position >;
86
- list.sort ();
87
- return list;
88
- }();
78
+ features: tree.search (_toRBBox (currentSegment))),
79
+ (match, index) {
80
+ if (! doesOverlap) {
81
+ List <Position > coordsSegment = () {
82
+ List <Position > list = getCoords (currentSegment) as List <Position >;
83
+ list.sort (((a, b) {
84
+ return a.lng < b.lng
85
+ ? - 1
86
+ : a.lng > b.lng
87
+ ? 1
88
+ : 0 ;
89
+ }));
90
+ return list;
91
+ }();
92
+ List <Position > coordsMatch = () {
93
+ List <Position > list = getCoords (match) as List <Position >;
94
+ list.sort (((a, b) {
95
+ return a.lng < b.lng
96
+ ? - 1
97
+ : a.lng > b.lng
98
+ ? 1
99
+ : 0 ;
100
+ }));
101
+ return list;
102
+ }();
89
103
90
- Equality eq = Equality ();
91
- // Segment overlaps feature - with dummy LineStrings just to use eq.
92
- if (eq.compare (LineString (coordinates: coordsSegment),
93
- LineString (coordinates: coordsMatch))) {
94
- doesOverlaps = true ;
95
- // Overlaps already exists - only append last coordinate of segment
96
- if (overlapSegment != null ) {
97
- overlapSegment = concatSegment (overlapSegment! , currentSegment) ??
98
- overlapSegment;
99
- } else {
100
- overlapSegment = currentSegment;
101
- }
102
- // Match segments which don't share nodes (Issue #901)
103
- } else if (tolerance == 0
104
- ? booleanPointOnLine (Point (coordinates: coordsSegment[0 ]),
105
- match.geometry as LineString ) &&
106
- booleanPointOnLine (Point (coordinates: coordsSegment[1 ]),
107
- match.geometry as LineString )
108
- : nearestPointOnLine (match.geometry as LineString ,
109
- Point (coordinates: coordsSegment[0 ]))
110
- .properties! ['dist' ] <=
111
- tolerance &&
112
- nearestPointOnLine (match.geometry as LineString ,
113
- Point (coordinates: coordsSegment[1 ]))
114
- .properties! ['dist' ] <=
115
- tolerance) {
116
- doesOverlaps = true ;
117
- if (overlapSegment != null ) {
118
- overlapSegment = concatSegment (overlapSegment! , currentSegment) ??
119
- overlapSegment;
120
- } else {
121
- overlapSegment = currentSegment;
122
- }
123
- } else if (tolerance == 0
124
- ? booleanPointOnLine (Point (coordinates: coordsMatch[0 ]),
125
- currentSegment.geometry as LineString ) &&
126
- booleanPointOnLine (Point (coordinates: coordsMatch[1 ]),
127
- currentSegment.geometry as LineString )
128
- : nearestPointOnLine (currentSegment.geometry as LineString ,
129
- Point (coordinates: coordsMatch[0 ]))
130
- .properties! ['dist' ] <=
131
- tolerance &&
132
- nearestPointOnLine (currentSegment.geometry as LineString ,
133
- Point (coordinates: coordsMatch[1 ]))
134
- .properties! ['dist' ] <=
135
- tolerance) {
136
- // Do not define doesOverlap = true since more matches can occur
137
- // within the same segment
138
- // doesOverlaps = true;
139
- if (overlapSegment != null ) {
140
- var combinedSegment =
141
- concatSegment (overlapSegment! , match as Feature <LineString >);
142
- if (combinedSegment != null ) {
143
- overlapSegment = combinedSegment;
144
- } else {
145
- additionalSegments.add (match);
104
+ Equality eq = Equality ();
105
+ // Segment overlaps feature - with dummy LineStrings just to use eq.
106
+ if (eq.compare (LineString (coordinates: coordsSegment),
107
+ LineString (coordinates: coordsMatch))) {
108
+ doesOverlap = true ;
109
+ // Overlaps already exists - only append last coordinate of segment
110
+ if (overlapSegment != null ) {
111
+ overlapSegment =
112
+ concatSegment (overlapSegment! , currentSegment) ??
113
+ overlapSegment;
114
+ } else {
115
+ overlapSegment = currentSegment;
116
+ }
117
+ // Match segments which don't share nodes (Issue #901)
118
+ } else if (tolerance == 0
119
+ ? booleanPointOnLine (Point (coordinates: coordsSegment[0 ]),
120
+ match.geometry as LineString ) &&
121
+ booleanPointOnLine (Point (coordinates: coordsSegment[1 ]),
122
+ match.geometry as LineString )
123
+ : nearestPointOnLine (match.geometry as LineString ,
124
+ Point (coordinates: coordsSegment[0 ]))
125
+ .properties! ['dist' ] <=
126
+ tolerance &&
127
+ nearestPointOnLine (match.geometry as LineString ,
128
+ Point (coordinates: coordsSegment[1 ]))
129
+ .properties! ['dist' ] <=
130
+ tolerance) {
131
+ doesOverlap = true ;
132
+ if (overlapSegment != null ) {
133
+ overlapSegment =
134
+ concatSegment (overlapSegment! , currentSegment) ??
135
+ overlapSegment;
136
+ } else {
137
+ overlapSegment = currentSegment;
138
+ }
139
+ } else if (tolerance == 0
140
+ ? booleanPointOnLine (Point (coordinates: coordsMatch[0 ]),
141
+ currentSegment.geometry as LineString ) &&
142
+ booleanPointOnLine (Point (coordinates: coordsMatch[1 ]),
143
+ currentSegment.geometry as LineString )
144
+ : nearestPointOnLine (currentSegment.geometry as LineString ,
145
+ Point (coordinates: coordsMatch[0 ]))
146
+ .properties! ['dist' ] <=
147
+ tolerance &&
148
+ nearestPointOnLine (currentSegment.geometry as LineString ,
149
+ Point (coordinates: coordsMatch[1 ]))
150
+ .properties! ['dist' ] <=
151
+ tolerance) {
152
+ // Do not define doesOverlap = true since more matches can occur
153
+ // within the same segment
154
+ // doesOverlaps = true;
155
+ if (overlapSegment != null ) {
156
+ Feature <LineString >? combinedSegment = concatSegment (
157
+ overlapSegment! , match as Feature <LineString >);
158
+ if (combinedSegment != null ) {
159
+ overlapSegment = combinedSegment;
160
+ } else {
161
+ additionalSegments.add (match);
162
+ }
163
+ } else {
164
+ overlapSegment = match as Feature <LineString >;
165
+ }
146
166
}
147
- } else {
148
- overlapSegment = match as Feature <LineString >;
149
167
}
150
- }
151
- }
152
- });
168
+ },
169
+ );
153
170
154
- // Segment doesn't overlap - add overlaps to results & reset
155
- if (doesOverlaps == false && overlapSegment != null ) {
156
- features.add (overlapSegment! );
157
- if (additionalSegments.isNotEmpty) {
158
- features = [...features, ...additionalSegments];
159
- additionalSegments = [];
171
+ // Segment doesn't overlap - add overlaps to results & reset
172
+ if (doesOverlap == false && overlapSegment != null ) {
173
+ features.add (overlapSegment! );
174
+ if (additionalSegments.isNotEmpty) {
175
+ features = [...features, ...additionalSegments];
176
+ additionalSegments = [];
177
+ }
178
+ overlapSegment = null ;
160
179
}
161
- overlapSegment = null ;
162
- }
163
- });
180
+ },
181
+ );
164
182
// Add last segment if exists
165
183
if (overlapSegment != null ) features.add (overlapSegment! );
166
184
@@ -173,21 +191,21 @@ Feature<LineString>? concatSegment(
173
191
var lineCoords = getCoords (line);
174
192
var start = lineCoords[0 ];
175
193
var end = lineCoords[lineCoords.length - 1 ];
176
- List <Position > geom = (line.geometry as LineString ).coordinates;
194
+ List <Position > positions = (line.geometry as LineString ). clone ( ).coordinates;
177
195
178
196
if (coords[0 ] == start) {
179
- geom .insert (0 , coords[1 ]);
197
+ positions .insert (0 , coords[1 ]);
180
198
} else if (coords[0 ] == end) {
181
- geom .add (coords[1 ]);
199
+ positions .add (coords[1 ]);
182
200
} else if (coords[1 ] == start) {
183
- geom .insert (0 , coords[0 ]);
201
+ positions .insert (0 , coords[0 ]);
184
202
} else if (coords[1 ] == end) {
185
- geom .add (coords[0 ]);
203
+ positions .add (coords[0 ]);
186
204
} else {
187
205
return null ;
188
206
} // If the overlap leaves the segment unchanged, return null so that this can be
189
207
// identified.
190
208
191
209
// Otherwise return the mutated line.
192
- return line ;
210
+ return Feature (geometry : LineString (coordinates : positions)) ;
193
211
}
0 commit comments