|
| 1 | +package com.kathmandulivinglabs.navigationlibrary; |
| 2 | + |
| 3 | +import com.kathmandulivinglabs.navigationlibrary.models.Geometry; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class BaatoUtil { |
| 9 | + public static Geometry getGeoJsonFromEncodedPolyLine(String encoded) { |
| 10 | + return new Geometry("LineString", decodePolyline(encoded, false)); |
| 11 | + } |
| 12 | + |
| 13 | + public static List<List<Double>> decodePolyline(String encoded, boolean is3D) { |
| 14 | + List<List<Double>> pointList = new ArrayList<>(); |
| 15 | + int index = 0; |
| 16 | + int len = encoded.length(); |
| 17 | + int lat = 0, lng = 0, ele = 0; |
| 18 | + while (index < len) { |
| 19 | + // latitude |
| 20 | + int b, shift = 0, result = 0; |
| 21 | + do { |
| 22 | + b = encoded.charAt(index++) - 63; |
| 23 | + result |= (b & 0x1f) << shift; |
| 24 | + shift += 5; |
| 25 | + } while (b >= 0x20); |
| 26 | + int deltaLatitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); |
| 27 | + lat += deltaLatitude; |
| 28 | + |
| 29 | + // longitute |
| 30 | + shift = 0; |
| 31 | + result = 0; |
| 32 | + do { |
| 33 | + b = encoded.charAt(index++) - 63; |
| 34 | + result |= (b & 0x1f) << shift; |
| 35 | + shift += 5; |
| 36 | + } while (b >= 0x20); |
| 37 | + int deltaLongitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); |
| 38 | + lng += deltaLongitude; |
| 39 | + |
| 40 | + if (is3D) { |
| 41 | + // elevation |
| 42 | + shift = 0; |
| 43 | + result = 0; |
| 44 | + do { |
| 45 | + b = encoded.charAt(index++) - 63; |
| 46 | + result |= (b & 0x1f) << shift; |
| 47 | + shift += 5; |
| 48 | + } while (b >= 0x20); |
| 49 | + int deltaElevation = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); |
| 50 | + ele += deltaElevation; |
| 51 | + List<Double> list = new ArrayList<>(); |
| 52 | + list.add((double) lat / 1e5); |
| 53 | + list.add((double) lng / 1e5); |
| 54 | + list.add((double) ele / 100); |
| 55 | + pointList.add(list); |
| 56 | + } else { |
| 57 | + List<Double> list = new ArrayList<>(); |
| 58 | + list.add((double) lat / 1e5); |
| 59 | + list.add((double) lng / 1e5); |
| 60 | + pointList.add(list); |
| 61 | + } |
| 62 | + } |
| 63 | + return pointList; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +} |
0 commit comments