GithubHelp home page GithubHelp logo

bdon / encoded_polyline Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 3.0 116 KB

Encode an array of points to plot with the Google Maps or MapQuest API

Home Page: http://github.com/bdon/encoded_polyline

License: MIT License

Ruby 100.00%

encoded_polyline's Introduction

EncodedPolyline

EncodedPolyline implements Google's algorithm for compressing arrays of geographic points. The compressed string can then be passed to the Google Maps API. The Maps API limits URL requests to 2048 characters. This library does not enforce this limit on the encoding length. This coordinate encoding scheme is also compatible with MapQuest Platform Services, which supports variable decimal precision. The default precision of 5 is appropriate for compatibility with the Google Maps API.

Installation

gem install encoded_polyline

Examples

Using the library with default decimal precision of 5:

EncodedPolyline.encode(-179.9832104)                  # "`~oia@"
EncodedPolyline.decode("`~oia@")                      # -179.98321
EncodedPolyline.encode_points([[37.782,-122.406]])    # "ohreFnlbjV"
EncodedPolyline.decode_points("ohreFnlbjV")           # [[37.782,-122.406]]

Using the library with explicit decimal precisions:

EncodedPolyline.encode(-179.9832104, 7)               # "na`|gjB"
EncodedPolyline.decode("na`|gjB", 7)                  # -179.9832104
EncodedPolyline.encode_points([[37.782,-122.406]], 2) # "ckF`|V"
EncodedPolyline.decode_points("ckF`|V", 2)            # [[37.78, -122.41]]

It is important to specify the same precision when encoding and decoding. Otherwise, decoded coordinates may be wrong by orders of magnitude.

Performance

Included is a benchmark that creates a 100-point random walk, gives you a URL, and encodes/decodes it 10,000 times (1 million points total).

Raw length(JSON):     3682 characters
Encoded length:       495 characters
Compression ratio:    0.134437805540467

                          user     system      total        real
encoding              9.090000   0.000000   9.090000 (  9.092519)
decoding             16.470000   0.010000  16.480000 ( 16.473547)

Contributors

bdon - author

anoved - explicit precision, MapQuest support

Copyright

Copyright (c) 2013 Brandon Liu. See LICENSE for details.

encoded_polyline's People

Contributors

anoved avatar bdon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

anoved gijs durkie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.