如何解码谷歌的折线算法?

时间:2012-02-18 12:09:05

标签: java google-maps language-agnostic

Google's Encoded Polyline Algorithm Format

  

enter image description here

你如何解码?

也许向后运行算法;但是我坚持第5步:没有初始值,我怎么知道它是正面还是负面?

2 个答案:

答案 0 :(得分:3)

如果为负数,则编码左移然后反转,例如:

1: 0000_0001 =>0000_0010
2: 0000_0010 =>0000_0100
3: 0000_0011 =>0000_0110
4: 0000_0100 =>0000_1000
5: 0000_0101 =>0000_1010
6: 0000_0110 =>0000_1100
7: 0000_0111 =>0000_1110
8: 0000_1000 =>0001_0000

-1: 1111_1111 =>1111_1110 =>0000_0001
-2: 1111_1110 =>1111_1100 =>0000_0011
-3: 1111_1101 =>1111_1010 =>0000_0101
-4: 1111_1100 =>1111_1000 =>0000_0111
-5: 1111_1011 =>1111_0110 =>0000_1001
-6: 1111_1010 =>1111_0100 =>0000_1011
-7: 1111_1001 =>1111_0010 =>0000_1101
-8: 1111_1000 =>1111_0000 =>0000_1111

因此,解码的最后一位是0,初始是正的,如果最后一位是1,则初始为负。

<小时/>的附录

完整解码演示:

public class Test {
 public static void main(String args[]) {
  for (int point : Decode("_p~iF~ps|U_ulLnnqC_mqNvxq`@",10)) {
   System.out.println(point); // Be aware that point is in E5
  }
 }

 private static java.util.List<java.lang.Integer> Decode(String encoded_polylines, int initial_capacity) {
  java.util.List<java.lang.Integer> trucks = new java.util.ArrayList<java.lang.Integer>(initial_capacity);
  int truck = 0;
  int carriage_q = 0;
  for (int x = 0, xx = encoded_polylines.length(); x < xx; ++x) {
   int i = encoded_polylines.charAt(x);
   i -= 63;
   int _5_bits = i << (32 - 5) >>> (32 - 5);
   truck |= _5_bits << carriage_q;
   carriage_q += 5;
   boolean is_last = (i & (1 << 5)) == 0;
   if (is_last) {
    boolean is_negative = (truck & 1) == 1;
    truck >>>= 1;
    if (is_negative) {
     truck = ~truck;
    }
    trucks.add(truck);
    carriage_q = 0;
    truck = 0;
   }
  }
  return trucks;
 }
}

答案 1 :(得分:0)

由于这是一个与语言无关的问题,我将从Peter Chng's unitstep blog添加此PHP解决方案(因为PHP中不存在>>>运算符):

function decodePolylineToArray($encoded)
{
  $length = strlen($encoded);
  $index = 0;
  $points = array();
  $lat = 0;
  $lng = 0;

  while ($index < $length)
  {
    $b = 0;
    $shift = 0;
    $result = 0;
    do
    {
      $b = ord(substr($encoded, $index++)) - 63;
      $result |= ($b & 0x1f) << $shift;
      $shift += 5;
    }
    while ($b >= 0x20);
    $dlat = (($result & 1) ? ~($result >> 1) : ($result >> 1));
    $lat += $dlat;

    $shift = 0;
    $result = 0;
    do
    {
      $b = ord(substr($encoded, $index++)) - 63;
      $result |= ($b & 0x1f) << $shift;
      $shift += 5;
    }
    while ($b >= 0x20);

    $dlng = (($result & 1) ? ~($result >> 1) : ($result >> 1));
    $lng += $dlng;

    $points[] = array($lat * 1e-5, $lng * 1e-5);
  }
  return $points;
}

Additional instructions from Google developers

更新: 解码指令几乎是直截了当的,为了找到原始值,您可以通过已经从ASCII字符转换的每个值的最后一位计算它是正还是负。

例如:

步骤5.如果你的价值分块(5块)有最后一位&#39; 0x1f&#39;然后它是负面的,你应该反转它 如:|= ($foo & 0x1f) << $shift;

00000010 00100101 01000011 11100001

4将二进制值右移一位:

11111101 11011010 10111100 00011110

3将二进制值转换为十进制,请记住,如果您意识到这是一个负数,那么您必须将它从两个补码Two's complement转换而来:如果它是正数然后只是转换像往常一样的二进制文件

11111110 11101101 01011110 00001111

11111110 11101101 01011110 00001110

00000001 00010010 10100001 11110001 &lt; - 我们的原始值未签名

2小数值乘以1e5,将其除以得到初始值: 的 179.98321

1添加原始值的符号(如果需要) 的 -179.98321 (这是一点点数据丢失,但它无关紧要)