You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose I have a list of line strings, I want to connect the green line to the shortest polygon line (The pink line), forming a new closed polygon like in the screenshot below.
This is what I have done so far...
var gf = JTS.GeometryFactory.defaultPrecision();
JTS.Point startPoint = gf.createPoint(newCoords.first);
JTS.Point endPoint = gf.createPoint(newCoords.last);
JTS.Geometry inSameLine;
double defaultBuffer = 0.000001;
int index = 0;
Map<List<JTS.LineString>, double> shortestPathSeries = {};
for (int i = 0; i < snappingGeometry.lineSegments.length; i++) {
List<JTS.LineString> connectedLineSeries = [];
double connectedLineSeriesDistance = 0;
JTS.LineString currentLine =
snappingGeometry.lineSegments[i].toGeometry(gf);
if (currentLine.getLength() > 0) {
if (startPoint.intersects(currentLine.buffer(defaultBuffer)) &&
endPoint.intersects(currentLine.buffer(defaultBuffer))) {
inSameLine = currentLine;
shortestPathSeries = {};
break;
}
if (startPoint.intersects(currentLine.buffer(defaultBuffer))) {
JTS.LineString activeLine = currentLine.copy();
List<VisitedNode> visitedLineSegments = [];
snappingGeometry.lineSegments.forEach((element) {
visitedLineSegments.add(VisitedNode(element, false, index));
index++;
});
while (activeLine != null &&
(visitedLineSegments
.indexWhere((element) => !element.visited) !=
-1)) {
JTS.LineString checkGeomActiveChain;
var checkGeomNull = visitedLineSegments.where((element) {
var isLineIntersectsActiveLine = element.line
.toGeometry(gf)
.intersects(activeLine.buffer(defaultBuffer));
return !element.visited &&
element.line.getLength() > 0 &&
isLineIntersectsActiveLine;
}).firstOrNull;
if (checkGeomNull != null) {
visitedLineSegments[checkGeomNull.index].visited = true;
checkGeomActiveChain = checkGeomNull.line.toGeometry(gf);
JTS.Point endPointForChain =
checkGeomActiveChain.getStartPoint();
connectedLineSeries.add(checkGeomActiveChain);
connectedLineSeriesDistance +=
endPointForChain.distance(activeLine);
} else if (checkGeomNull == null) {
int index = visitedLineSegments
.indexWhere((element) => !element.visited);
if (index != -1) checkGeomActiveChain = currentLine.copy();
connectedLineSeries = [];
connectedLineSeriesDistance = 0;
}
activeLine = checkGeomActiveChain;
}
if (connectedLineSeries.isNotEmpty)
shortestPathSeries.putIfAbsent(
connectedLineSeries, () => connectedLineSeriesDistance);
}
}
}
if (inSameLine != null) {
newCoords.addAll(inSameLine.getCoordinates());
polylineGeometry = gf.createLineString(newCoords);
print('ADDING ON SAME LINE');
} else if (shortestPathSeries.isNotEmpty) {
print('ADDING Connected Edge ${shortestPathSeries.length}');
var sortedGraphDistances = shortestPathSeries.entries.toList()
..sort((e1, e2) {
var diff = e1.value.compareTo(e2.value);
return diff;
});
shortestPathSeries
..clear()
..addEntries(sortedGraphDistances);
List<JTS.LineString> notconnectedLineSeries;
if (shortestPathSeries.values.first != 0)
notconnectedLineSeries = shortestPathSeries.keys.first;
else
notconnectedLineSeries = shortestPathSeries.keys.elementAt(1);
List<JTS.Coordinate> newCoordsX = [];
newCoordsX.add(newCoords[0]);
newCoordsX.addAll(notconnectedLineSeries
.map((e) => e.getCoordinates())
.reduce((value, element) => element)
.toList());
for (int i = 1; i < newCoords.length; i++) newCoordsX.add(newCoords[i]);
testResult(gf.createLineString(newCoordsX).buffer(0.00001), context);
return;
polylineGeometry = gf.createLineString(newCoordsX);
}
The problem is only one line is detected which is not the shortest, not the longest.
I want ideas or hints that solve my problem
Note: I am using dart_jts which is a port to flutter.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
[Repost]
This is a problem with almost the same as my problem but I could not understand R language nor the algorithm he used
Suppose I have a list of line strings, I want to connect the green line to the shortest polygon line (The pink line), forming a new closed polygon like in the screenshot below.
This is what I have done so far...
The problem is only one line is detected which is not the shortest, not the longest.
I want ideas or hints that solve my problem
Note: I am using dart_jts which is a port to flutter.
Beta Was this translation helpful? Give feedback.
All reactions