Skip to content

Commit c4ab632

Browse files
author
Chan Tran
committed
refactor: abstract duplicate code to a method
1 parent debac40 commit c4ab632

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

web-bundle/src/main/java/com/graphhopper/resources/BufferResource.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -502,25 +502,8 @@ private PointList computePointAtDistanceThreshold(BufferFeature startFeature, Do
502502

503503
Double currentDistance = endFeature.getDistance();
504504
GHPoint3D previousPoint = pointList.get(0);
505-
PointList pathList = new PointList();
506505

507-
for (GHPoint3D currentPoint : pointList) {
508-
// Filter zero-points made by PointList() scaling
509-
if (currentPoint.lat != 0 && currentPoint.lon != 0) {
510-
// Check if exceeds thresholdDistance
511-
currentDistance += DistancePlaneProjection.DIST_PLANE.calcDist(currentPoint.getLat(),
512-
currentPoint.getLon(), previousPoint.getLat(), previousPoint.getLon());
513-
if (currentDistance >= thresholdDistance) {
514-
return pathList;
515-
}
516-
517-
pathList.add(currentPoint);
518-
previousPoint = currentPoint;
519-
}
520-
}
521-
522-
// Default to full path in case the threshold isn't hit
523-
return pathList;
506+
return computePathListWithinThreshold(thresholdDistance, pointList, currentDistance, previousPoint);
524507
}
525508

526509
/**
@@ -564,9 +547,22 @@ private PointList computeWayGeometryOfStartingEdge(BufferFeature startFeature, B
564547

565548
double currentDistance = 0.0;
566549
GHPoint3D previousPoint = tempList.get(0);
567-
pathList = new PointList();
568550

569-
for (GHPoint3D currentPoint : tempList) {
551+
return computePathListWithinThreshold(thresholdDistance, tempList, currentDistance, previousPoint);
552+
}
553+
554+
/**
555+
* Generates a list of points to create a path that does not exceed a threshold.
556+
*
557+
* @param thresholdDistance max distance to not exceed
558+
* @param origList the original list to loop through during calculations
559+
* @param currentDistance sum of the distance while moving through the original list
560+
* @param previousPoint point to compute the distance from
561+
* @return a PointList containing a path not exceeding the threshold
562+
*/
563+
private PointList computePathListWithinThreshold(Double thresholdDistance, PointList origList, double currentDistance, GHPoint3D previousPoint) {
564+
PointList pathList = new PointList();
565+
for (GHPoint3D currentPoint : origList) {
570566
// Filter zero-points made by PointList() scaling
571567
if (currentPoint.lat != 0 && currentPoint.lon != 0) {
572568
// Check if exceeds thresholdDistance
@@ -580,7 +576,7 @@ private PointList computeWayGeometryOfStartingEdge(BufferFeature startFeature, B
580576
previousPoint = currentPoint;
581577
}
582578
}
583-
579+
// Default to full path in case the threshold isn't hit
584580
return pathList;
585581
}
586582

0 commit comments

Comments
 (0)