Tcl implementation of the polyline coordinate list compression algorithm used by MapQuest and Google Maps.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Examples
LICENSE
Readme.md
arcencode.benchmark.tcl
arcencode.tcl
pkgIndex.tcl

Readme.md

arcencode

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 based on the default MapQuest Interactive Example.

package require arcencode

set collection {
	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 $collection]
set decoded [arcencode::arcdecode $encoded]

The contents of encoded:

w|pwGjig_Oggcv@w@~fayBd~H~b`|@yh~D~b`|@rftD~dutAkA_fgtEgiwyN?~cbtN~wnD?_xnD?~wnD???

The contents of decoded (compare to the input collection):

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.

License

arcencode is freely distributed under an open source MIT License:

Copyright (c) 2013 Jim DeVona

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.