A Crystal implementation of the Google polyline algorithm.
Greatly inspired by the Ruby implementation fast-polylines, it is mostly existing for performance comparison between Ruby C extensions and Crystal!
If you are curious: encoding is roughly the same, decoding is way faster in Crystal.
If you are even more curious, make benchmark
here and in fast-polylines 😉.
With a MacBook pro 1,4 GHz i5, here's the result:
——————————————————————————————— ENCODING ————————————————————————————————
CrystalPolylines 706.82k ( 1.41µs) (± 4.42%) 466B/op fastest
——————————————————————————————— DECODING ————————————————————————————————
CrystalPolylines 1.43M (699.73ns) (± 3.21%) 1.62kB/op fastest
Add the dependency to your shard.yml
:
dependencies:
polylines:
github: BuonOmo/polylines.cr
Run shards install
require "polylines"
Polylines.encode([{lat: 38.5, lng: -120.2}, {lat: 40.7, lng: -120.95}])
Polylines.encode([[38.5, -120.2], [40.7, -120.95]])
Polylines.encode([{38.5, -120.2}, {40.7, -120.95}])
# => "_p~iF~ps|U_ulLnnqC"
Polylines.decode("_p~iF~ps|U_ulLnnqC")
# => [{lat: 38.5, lng: -120.2}, {lat: 40.7, lng: -120.95}]
Polylines.encode([{lat: 12.1232493, lng: 2.9481493}, {lat: 13.8474392, lng: 16.8373924}], 7)
# => "yemffFi`lfw@uqj{_@}gi|gG"
Polylines.decode("yemffFi`lfw@uqj{_@}gi|gG", 7)
# => [{lat: 12.1232493, lng: 2.9481493}, {lat: 13.8474392, lng: 16.8373924}]
- Fork it (BuonOmo/polylines.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request