Saltar a contenido

polyline

app.contrib.geospatial.polyline

A Python implementation of Google's Encoded Polyline Algorithm Format.

https://github.com/frederickjansen/polyline version 2.0.0 commit #d25f165 19 Jan 2023

Copyright © 2023 Frederick Jansen Copyright © 2014 Bruno M. Custódio

How to use:

Encoding To get the encoded polyline representation of a given set of (lat, lon) coordinates:

import polyline
polyline.encode([(38.5, -120.2), (40.7, -120.9), (43.2, -126.4)], 5)

This should return '_piFps|U_ulLugC_hgNeq`@'. You can set the required precision with the optional precision parameter. The default value is 5. You can encode (lon, lat) tuples by setting geojson=True.

Decoding To get a set of coordinates represented by a given encoded polyline string:

import polyline
polyline.decode('u{~vFvyys@fS]', 5)

This should return [(40.63179, -8.65708), (40.62855, -8.65693)] in (lat, lon) order.

more info: - https://developers.google.com/maps/documentation/utilities/polylinealgorithm

decode(expression, precision=5, geojson=False)

Decode a polyline string into a set of coordinates.

:param expression: Polyline string, e.g. 'u{~vFvyys@fS]'. :param precision: Precision of the encoded coordinates. Google Maps uses 5, OpenStreetMap uses 6. The default value is 5. :param geojson: Set output of tuples to (lon, lat), as per https://tools.ietf.org/html/rfc7946#section-3.1.1 :return: List of coordinate tuples in (lat, lon) order, unless geojson is set to True.

encode(coordinates, precision=5, geojson=False)

Encode a set of coordinates in a polyline string.

:param coordinates: List of coordinate tuples, e.g. [(0, 0), (1, 0)]. Unless geojson is set to True, the order is expected to be (lat, lon). :param precision: Precision of the coordinates to encode. Google Maps uses 5, OpenStreetMap uses 6. The default value is 5. :param geojson: Set to True in order to encode (lon, lat) tuples. :return: The encoded polyline string.