mapbox / polyline Goto Github PK
View Code? Open in Web Editor NEWpolyline encoding and decoding in javascript
License: BSD 3-Clause "New" or "Revised" License
polyline encoding and decoding in javascript
License: BSD 3-Clause "New" or "Revised" License
$> echo '{"type":"LineString", "coordinates": [[10,52],[10.1,52.1]]}' | polyline --fromGeoJSON | polyline
[[-0.00002,52],[9.99998,52.1],[10.09998,52.09998]]
The rounding errors are probably by design but the coordinates are somehow corrupted.
@mapbox/[email protected]
A user reported a strange response from the classic static API and it looks like this may have something to do with the way directions v5 polylines are encoded.
Here is the user's request:
Which returns this encoded polyline:
ajkwGd{tpVeNwt@sp@iEyLwv@X_p@l[_n@Bm`@ukAwwAag@_M}\\wm@agAcZkWie@yG}wCb`@a\\rKkcB`[i]sFgh@{nAixAk|@}`@yYq}Am{@eWt@c|B`l@}q@dMeu@}Cs|@pg@caAaa@axAa^qDeq@f^_v@ub@s~@zj@qc@wFqbArz@iPzi@i`@wEw[r\\wNyUkw@hAdCaOkKwGgh@fIsfAuK_J_MjDscAicAsyAcRgu@tHaiAjOiBoEmWf[wo@oFsaE~T{_@zf@stEaP}lBdK}qAqD}wAqYcxBbV}{HqZ_cB|KytB~Ras@wfCezBb@a`C}k@uWaM~Y
The route should look like:
When you plug the above polyline into the classic static api (request), you get the following:
Which resembles the original geometry, but with a couple of wrong turns. @yhahn noted that this could have something to do with escaped backslashes being removed from the encoded polyline. He also noted that directions v5 encodes polylines with a lower precision than directions v4, which is likely contributing to the issue.
I've realized that the library only has support to encode geojson feature LineString, but neither always a region is converted to a single LineString (sometimes it is a MultilineString).
It would be great if there is also a solution for these cases
Thanks for open-sourcing polyline, I never quite got the encoding right on my own :).
I just installed polyline
and I noticed that in the node_modules
folder there's a project folder from IntelliJ /.idea
. Is it possible to ignore it on .gitignore
?
Swap lat & long so is consistent with Mapbox Map coordinate
I just ran a conversion using polyline.fromGeoJSON(feature)
on some data and when I reconverted back to geojson using polyline.toGeoJSON(mPolyline)
I got some weird results.
I dug into my source geojson then noticed it had [x, y, elevation] coordinates, so I trimmed to [x, y], and this produced the expected results.
I'd guess these lines are responsible
https://github.com/mapbox/polyline/blob/master/src/polyline.js#L109-L113
Anyway just a word of warning to others, check your inputs :)
Decoding the following Encoded polyline yields incorrect data (A number of lat,lng pairs were missing)
z``mEm~zv[rNgaALmc@kFg~AMae@zV_gCZqR{y@}F_u@wJ_^_DkN{@cHrkBfd@znA_X|uCsReBcF|j@{fAo\cNsOwIaBqJnFwIge@rFgeAyrBaTsHvdAyJa@cFtj@}GQxt@fhCq@zQp@tEpDxEg@pCP
DFvDgA|CiYza@sBrSaS~XMG_A
DiMQwBbHOnFmBtS~PnCiKhqAn{@_A
eAtHdtAfYhf@mEtGeGb[_k@zA_o@aKwm@nKg@rRsGl\fJvQxJz@uDhCsEpIyChCoB|E{OcF}C@oIfDiTnBcQlCqYlAkHnOe_@jJc^
I discovered I could recover the missing [lat,lngs] by double escaping the '\' characters. The complete and correct encoded polyline becomes:
z``mEm~zv[rNgaALmc@kFg~AMae@zV_gCZqR{y@}F_u@wJ_^_DkN{@cHrkBfd@znA_X|uCsReBcF|j@{fAo\\cNsOwIaBqJnFwIge@rFgeAyrBaTsHvdAyJa@cFtj@}GQxt@fhCq@zQp@tEpDxEg@pCP
DFvDgA|CiYza@sBrSaS~XMG_A
DiMQwBbHOnFmBtS~PnCiKhqAn{@_A
eAtHdtAfYhf@mEtGeGb[_k@zA_o@aKwm@nKg@rRsGl\\fJvQxJz@uDhCsEpIyChCoB|E{OcF}C@oIfDiTnBcQlCqYlAkHnOe_@jJc^
As can be seen on https://www.npmjs.com/package/@mapbox/polyline/v/1.2.1?activeTab=code the released version is still missing the license field in the package.json
. This does not make any sense to me as the package.json
contains the meow version update, which is later in the commit history here... Anyways, could you maybe release a 1.2.2 which includes the license filed in the packaged artifact?
There has been a bunch of updates, but the most recent release (0.2) is from august 2015.
A new release would be awesome! ⛵️
New to this repo, but I just noticed that decoding a route with different values of the precision
param does not make a different. For my example, it will output the same number of waypoints regardless of the precision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eFc@oEkBgKmK}i@il@cD{FaBcJ}AmLaDeIuDmE}UwQiTsOiFkEoDmFiBqFcAcG_Gof@cAwFcCyHoDwGuBmC{FcFcQ{JcIoFuJcJmGyEkCu@iDc@kEJcCf@wGrBmEFIWSKFuD^{@Q_@TkHH_BBs@WAq@@q@ByATmBf@mG|@}ES}M}CeAK{QcFgYaIiNkFeJsEoN}IoKuI_FwEwKiMgFuH_E_I_HoQcFiIaF}DkDwAwE}@_GQcSc@uGEuRNiYVmIJiDCqCa@aC{@eCaB{PoUiFiHcBwA}J}D{DoB]gBvA_IdA{EVJt@qD';
// console.log(`${JSON.stringify(overview_polyline)}`);
for (var i = 1; i < 50; i += 3) {
const decoded = polyline.decode(overview_polyline, i);
console.log(`Precision ${i} : ${JSON.stringify(decoded.length)} waypoints`);
}
Outputs:
Precision 1 : 341 waypoints
Precision 4 : 341 waypoints
Precision 7 : 341 waypoints
Precision 10 : 341 waypoints
Precision 13 : 341 waypoints
Precision 16 : 341 waypoints
Precision 19 : 341 waypoints
Precision 22 : 341 waypoints
Precision 25 : 341 waypoints
Precision 28 : 341 waypoints
Precision 31 : 341 waypoints
Precision 34 : 341 waypoints
Precision 37 : 341 waypoints
Precision 40 : 341 waypoints
Precision 43 : 341 waypoints
Precision 46 : 341 waypoints
Precision 49 : 341 waypoints
I've got a somewhat simple polyline string that I want to decode (used as a route in mapbox-gl-directions):
er_iIaun{@AgE?mDBeB?{@FeH@YD_IBqCBmGBkD@uA@_B
Rendering this route with mapbox-gl-directions results in this:
Decoding the same polyline string with the Google polyline utility results in the correct polyline:
As far as I could debug, the latitude is wrong at two points, while the longitude is correct. It seems that the decoding is wrong if the latitude does not change between two points. I'd need to dig a bit further into the decoding to verify this.
The current version depends on "meow": "^5.0.0", which in turn depends on "yargs-parser": "^10.0.0". And that one has a known audit issue: https://www.npmjs.com/advisories/1500.
The solution would be to update meow to ^6.1.1 (https://github.com/sindresorhus/meow/releases/tag/v6.1.1) which has this issue fixed.
Hitting the OSRM endpoint:
https://router.project-osrm.org/viaroute?z=15&output=json&loc=47.603832,-122.330062&loc=47.097946,-122.424342&instructions=true,
and then using polyline.decode(route_geometry);
returns and array of coords that look like this:
0: 472.10022
1: -1224.33977
Being in Seattle, I recognized that these are 1 decimal off. Setting the precision to 6 like this:
polyline.decode(route_geometry, [6]);
Does the trick.
0: 47.210022
1: -122.433977
Seems like the default should do that? Or did I miss something?
Thanks!
For consistency with GeoJSON, GL JS we should bump the major version number and swap to lon/lat order.
@jfirebaugh @jlivni reports we may be x10'ing coordinates in the Directions API polyline encoding. Might this be the factor
bit here? https://github.com/mapbox/polyline/blob/master/src/polyline.js#L77
When attempting to save the result from the endcode
flag, the outputted result is not the same as the outputted result from the GeoJSON node script. There is an issue with the parser of the input or output.
Steps to reproduce:
[[-114.05130386352539,51.112359778280194],[-114.05130386352539,51.11947193091105],[-114.05611038208008,51.12324308281078][-114.06538009643555,51.126152046793365], [-114.0736198425293,51.12916855668462]]
cat polyline.json | ./bin/polyline.bin.js --encode > encoded-poly.txt
:result in encoded-poly.txt
:
"??????????_}hQ?????_qo]?~flW?_seK?~|hQ?_}hQ?_qo]?~reK?~|hQ?_seK?~|hQ?_}hQ?~reK?_{rc@?????~flW???????_ibE?_ibE?_seK?_glW?~reK???_ibE?~zrc@?_{rc@?~nyo@?_ibE?_oyo@?~po]?????????????????????????????????????_}hQ?????_qo]?~flW?_seK?~|hQ?_}hQ?_qo]?~reK?~|hQ?_seK?~|hQ?_}hQ?~reK?_{rc@?????~flW???????_oyo@?~po]?_}hQ?~zrc@?_oyo@?~zrc@?~|hQ?_y|u@?~nyo@???~hbE?_qo]?????????????????????????????????????_}hQ?????_qo]?_ibE?~po]???~hbE?_}hQ?_qo]?~zrc@?~reK?_oyo@?~nyo@???_oyo@?????~flW?????_ibE?_ibE?~hbE?_seK?~hbE?~|hQ?_oyo@?~zrc@?_{rc@?~dvi@?~hbE?_evi@?_ibE?????????????????????????????????????_}hQ?????_{rc@?~hbE?~reK?_qo]?~nyo@???_y|u@?~|hQ?~reK?~hbE?_seK?????????~flW?????_ibE?_glW?~po]?_glW?~|hQ?~reK?_glW?_seK?_ibE?_seK?~zrc@???_}hQ?~hbE?????????????????????????????????????_}hQ?????_evi@?~flW?_}hQ?~po]?_oyo@?~hbE?~flW?~reK?_}hQ?~|hQ?_evi@?~zrc@?????~flW?????_ibE?_evi@?~nyo@?_qo]?_seK?~|hQ???_ibE???_seK?~flW?_seK?~flW???????"
Compare to the result from the GeoJSON node script:
const polyline = require('@mapbox/polyline')
undefined
polyline.fromGeoJSON({ "type": "Feature",
... "geometry": {
..... "type": "LineString",
..... "coordinates": [[-114.05130386352539,51.112359778280194], [-114.05130386352539,51.11947193091105],[-114.05611038208008,51.12324308281078], [-114.06538009643555,51.126152046793365],[-114.0736198425293,51.12916855668462] ]
..... },
... "properties": {}
... });
'g{}vHrsbwTmk@?qV]eQ|x@{Qnr@'
I've seen this library and just wondering what the differences this library has with the one that google maps has ?
in my knowledge, you could just decode the encoded path using
google.maps.geometry.encoding.decodePath(param)
right ?
I see that @mapbox/polyline
shows as v1.0.0 in npm
and I see the related commits ((54aeca2)[https://github.com/mapbox/polyline/commit/54aeca247d2fb81b237006666067be798b4b9689] through (eaa1f05)[https://github.com/mapbox/polyline/commit/eaa1f056a908fdf275f1d966070d3decd23c0483]) from 2018-04-01, but there's no tag or release on the project. It's helpful to have these, esp. for those who might not be using Node or aren't paying attention to commit details.
These are options in the external API, but aren't mentioned in docs.
When using the polyline6
option from the Mapbox API, you need to decode the resulting polyline using
Polyline.decode(str, 6)
It would be great to arguments that encode
and decode
expect.
polyline.decode(polyline.encode([[0, -108]], 7), 7)
// [[ 0, -0.0000016 ]], should be [[0, -108]]
Currently there's no option to pass in a precision to polyline.encode or polyline.decode in the CLI, so it's not possible to encode/decode polyline6 geometries from the command line.
The library already supports variable precision, all that should be needed is to add a command line flag like --precision 6
to the binary script and pass the value into the encode and decode functions.
Bower's version of polyline 0.0.3 doesn't have commits made after Dec 17, including 58de265 which fixes issues that prevent polyline working in the browser.
A simple version bump should fix this.
This should return the coordinates in the opposite order (is: lat,lng
should be lng,lat
because geojson expects the data in x,y
order.)
# source data order is lat=51, lng=10
$> echo "_}gvH_c`|@_ibE_ibE" | polyline --decode
[[51,10],[52,11],[51.99994,11]]
Version: @mapbox/[email protected]
TypeError: undefined is not an object (evaluating 'respJson.routes[0].overview_polyline')
This thing pops up when the route is not available, where can i edit this alert or completely remove it ? please help.
system: centos 7
python: Python 3.5.0 :: Anaconda 2.4.0 (64-bit)
when install, it down-version "six" from 1.10.0 to 1.8.0
below is the log
$pip install polyline
Collecting polyline
Downloading polyline-1.1.tar.gz
Collecting six==1.8.0 (from polyline)
Downloading six-1.8.0-py2.py3-none-any.whl
Building wheels for collected packages: polyline
Running setup.py bdist_wheel for polyline ... done
Stored in directory: /home/vzj182/.cache/pip/wheels/d9/d8/f4/2ccd2aecef798e84458df1b30a49e17cd7d6dcb937ff8abbd8
Successfully built polyline
Installing collected packages: six, polyline
Found existing installation: six 1.10.0
DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
Uninstalling six-1.10.0:
Successfully uninstalled six-1.10.0
Successfully installed polyline-1.1 six-1.8.0
by the way, I manually re-upgrade "six" and polyline worked fine for importing and decoding.
Scenario - after cloning my repo and running npm install, this is the only package not being installed. after manually running npm install @mapbox/polyline it works just fine.
`Module does not exist in the module map or in these directories: /path/to/MyReactNativeProject/node_modules/@mapbox``
Cheers
Should be moved to devDependency section
i ran this command npm install @mapbox/polyline
and i see 404 error
npm ERR! code E404
npm ERR! 404 Not Found: @mapbox/polyline@^0.2.0
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/rnagulapalle/.npm/_logs/2017-09-25T06_25_51_620Z-debug.log
LM-SJN-00877623:driver-android rnagulapalle$
I seen that this library in encoding skipping the third dimension if present in the coordinates. [lon, lat, z]
is it possible to somehow include it in encoding?
Is there a TypeScript type declaration file for this project? If not, It would be helpful to provide a TypeScript type declaration file for this project.
using meow really increase the dependency by a lot: https://npmgraph.js.org/?q=%40mapbox%2Fpolyline
is it really necessary to have this dependency? i only use it for scripting purpose and do not need polyline from CLI...
The license property is missing from the package.json file. This would make automated licence processing easier. Your license seems to be BSD-3-Clause.
Documentation: https://docs.npmjs.com/files/package.json#license
edit: switch to us-english spelling
using polyline.toGeoJSON
, if i pass precision of 5
or omit precision the parsing works fine. but if i try to use precision of 4
it is messing up decimals places of the lat/lon pairs.
example, default precision or precision 5:
response: [-72.99583, 41.92463]
if i specify precision 4
response: [-729.9583, 419.2463]
There has been many nice additions, but the version still stand at 0.2.0.
Awesome if you could mint a new release to npm. 💯
Hello,
i have juste use this tool, and when using it, its return me an string with lot of '?' do you know why ?
im using it with browser so the (src/polyline.js) as script in my webpage
what i do :
`
var arr = [..with lot of coords..]
var poly = polyline.encode(arr)
console.log('poly', poly)
`
result :
poly ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
is there an option for importing it in angular with the import keyword instead of require?
Looks MapBox polyline
depends on meow
which has dependency on normalize-package-data
and semver
.
But the latest MapBox polyline
is referencing the [email protected]
which has CVE-2022-25883 security issue.
Versions of the package semver before 7.5.2 are vulnerable to Regular Expression Denial of Service (ReDoS) via the function new Range, when untrusted user data is provided as a range.
Could you please help to upgrade the meow
to latest? Thanks.
Are 3D polylines supported?
Hi!
First of all, thank you for the great work!
We have run into an issue with the encoding of some of our coordinates. We have a listing of multiple records, each of them showing a static map with a polyline on. Most of them work fine consistently, but here and there ones arent showing, instead returning a 404 from mapbox.
Ive spent some time investigating it and these are my findings, but please feel free to point out if Im missing anything obvious.
We are using the logic from https://github.com/mapbox/polyline/blob/master/src/polyline.js as is to do the encoding/decoding.
Using the following coordinates:
[-1.322705,36.923886],[-0.039050,37.460814],[0.111344,37.190244],[-2.335203,40.817358],[-1.322705,36.923886]
It returns the encoded string of:
|iaGiuj`F{uyFwzgB}j\`zs@zy|MolcUawdEdmwV
Passing that through to the api, the browser does some more encoding, which results in:
%7CiaGiuj%60F%7BuyFwzgB%7Dj/%60zs@zy%7CMolcUawdEdmwV
With the final 404'ing URL then being something like:
https://api.mapbox.com/v4/mapbox.streets/path-10+3c74b4-1(%7CiaGiuj%60F%7BuyFwzgB%7Dj/%60zs@zy%7CMolcUawdEdmwV)/auto/[email protected]?access_token=[your_token]
After some investigation and further URI encoding I noticed that the browser automatically swaps out a backslash \
for a forwardslash /
as you can notice in the above. Replacing that then with the URI safe equivalent (of the forward slash - %2F
) results in:
%7CiaGiuj%60F%7BuyFwzgB%7Dj%2F%60zs@zy%7CMolcUawdEdmwV
And decoding that back again using decodeURIComponent results in:
|iaGiuj`F{uyFwzgB}j/`zs@zy|MolcUawdEdmwV
Which is identical to the orginal encode, par the slash inversion:
|iaGiuj`F{uyFwzgB}j\`zs@zy|MolcUawdEdmwV
Could it be of value to add a safeguard for the above in the code?
Maybe just something like:
output = output.replace(/\\/g , '/');
Thanks
> polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');
[ [ 38.5, -120.2 ], [ 40.7, -120.95 ], [ 43.252, -126.453 ] ]
> polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@', precision=0);
[ [ 38.5, -120.2 ], [ 40.7, -120.95 ], [ 43.252, -126.453 ] ]
It's cause that zero precision was treated as undefined
in polyline.js#L57
factor = Math.pow(10, precision || 5);
Hi!
I know this is not a bug with polyline per se, but I am unsure where to open an issue for this.
I am using the Mapbox static API (https://docs.mapbox.com/api/maps/#static-images) which supports polylines as overlay.
However, sometimes my polyline contains question marks (the ?
character), and it seems that the static API interprets this as the beginning of the query parameters and fails to parse a token.
One example is this one:
https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/path-4+DAF34E(_vztHoqk%5CiNnLsBaI%60FaE?g@%60DmBIi@zC%7DAfApJ)/4.814415,50.770556,14/200x220?access_token=ACCESS_TOKEN_HERE
This polyline was encoded with @mapbox/polyline
, but the query responds with a 401 unauthorized
as the access token is not parsed.
Are polylines allowed to have this character ? If yes, is it an issue with the static API url parser?
I'd be happy to help dig into this if needed. Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.