Skip to content

Commit c63f452

Browse files
boldtrnkarussell
authored andcommitted
Allow configuring the precision of the polyline encoding (graphhopper#1442)
1 parent 9f2b5c9 commit c63f452

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

web-api/src/main/java/com/graphhopper/http/WebHelper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ public static String encodePolyline(PointList poly) {
102102
}
103103

104104
public static String encodePolyline(PointList poly, boolean includeElevation) {
105+
return encodePolyline(poly, includeElevation, 1e5);
106+
}
107+
108+
public static String encodePolyline(PointList poly, boolean includeElevation, double precision) {
105109
StringBuilder sb = new StringBuilder();
106110
int size = poly.getSize();
107111
int prevLat = 0;
108112
int prevLon = 0;
109113
int prevEle = 0;
110114
for (int i = 0; i < size; i++) {
111-
int num = (int) Math.floor(poly.getLatitude(i) * 1e5);
115+
int num = (int) Math.floor(poly.getLatitude(i) * precision);
112116
encodeNumber(sb, num - prevLat);
113117
prevLat = num;
114-
num = (int) Math.floor(poly.getLongitude(i) * 1e5);
118+
num = (int) Math.floor(poly.getLongitude(i) * precision);
115119
encodeNumber(sb, num - prevLon);
116120
prevLon = num;
117121
if (includeElevation) {

web-api/src/test/java/com/graphhopper/http/WebHelperTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ public void testEncode3D() throws Exception {
7474
assertEquals("_p~iF~ps|Uo}@_ulLnnqC_anF_mqNvxq`@?", WebHelper.encodePolyline(
7575
Helper.createPointList3D(38.5, -120.2, 10, 40.7, -120.95, 1234, 43.252, -126.453, 1234)));
7676
}
77+
78+
@Test
79+
public void testEncode1e6() throws Exception {
80+
assertEquals("ohdfzAgt}bVoEL", WebHelper.encodePolyline(Helper.createPointList(47.827608, 12.123476, 47.827712, 12.123469), false, 1e6));
81+
}
7782
}

0 commit comments

Comments
 (0)