Updated 2013-02-23 20:42:35 by RLE

anoved: arcencode is my Tcl implementation of the coordinate list compression algorithm used by MapQuest and Google Maps. It is useful for encoding a long sequence of coordinate data as a single comparatively compact URL parameter. The code is available at GitHub. Here is an excerpt from the Readme:

About

The arcencode package provides a Tcl implementation of the coordinate list compression algorithm used by MapQuest Platform Services and the Google Maps API. The algorithm converts a list of coordinate pairs to a string of printable non-space ASCII characters. Compression limits coordinate precision to three digits left of decimal and a default precision of five digits right of decimal.

References

  1. MapQuest Platform Services: Compressed Lat/Lng Encoding/Decoding
  2. Google Maps API: Encoded Polygon Algorithm Format

The arcencode code is derived from the MapQuest JavaScript Sample Source. The default five digit right-of-decimal precision is compatible with Google Maps API convention.

API

The arcencode package exports two procedures: arcencode and arcdecode. No input validation or error checking is currently performed.
arcencode::arcencode points ?precision?

The points argument is expected to be a list of coordinate values containing an even number of elements (conventionally, an alternating sequence of latitude and longitude values). The precision argument specifies how many digits to preserve right-of-decimal; the default precision is 5. The procedure returns an encoded string representation of the point list.
arcencode::arcdecode strval ?precision?

The strval argument is expected to be a string value generated by arcencode or a compatible source. The precision argument specifies how many digits to recover right-of-decimal; the default precision is 5. The procedure returns a list containing an even number of coordinate values (conventionally presumed to be an alternating sequence of latitude and longitude values).

Example

These example values are drawn from the default MapQuest Interactive Example.
package require arcencode

set coordinates {
        45.967 -83.928700032549
        55 -83.928420000
        35 -83.97948699748273
        25.000000 -83.000000
        15.00000000000 -83.9279400000
        0.9600 -83.9275623435
        35.90 -0.90
        35.900 -83.00
        35.000 -83.000
        35.90000 -83.0000
        35.00000 -83.00000
        35.000004190 -83.00000123490
}

set encoded [arcencode::arcencode $coordinates]
set decoded [arcencode::arcdecode $encoded]

The contents of encoded:
[email protected]@[email protected][email protected]~dutAkA_fgtEgiwyN?~cbtN~wnD?_xnD?~wnD???

The contents of decoded (compare to the input coordinates):
45.96700 -83.92870 55.00000 -83.92842 35.00000 -83.97949 25.00000 -83.00000 15.00000 -83.92794 0.96000 -83.92756 35.90000 -0.90000 35.90000 -83.00000 35.00000 -83.00000 35.90000 -83.00000 35.00000 -83.00000 35.00000 -83.00000

Note how the decoded values have been rounded to a uniform fixed precision.