Skip to content

polyline

Google Encoded Polyline encoder and decoder supporting precision 5 and 6.

unescape_polyline

unescape_polyline(encoded: str) -> str

Unescape double-escaped backslashes in a polyline string.

Parameters:

Name Type Description Default
encoded str

Polyline string containing escaped characters like \\.

required

Returns:

Type Description
str

Cleaned polyline string.

decode_polyline

decode_polyline(encoded: str, precision: int = 5, unescape: bool = False) -> list[tuple[float, float]]

Decode an encoded polyline string into a list of (longitude, latitude) coordinates.

Parameters:

Name Type Description Default
encoded str

The polyline ASCII string.

required
precision int

Decimal digits of precision (5 for Google/OSRM, 6 for Valhalla/Mapbox).

5
unescape bool

Whether to unescape double-escaped backslashes before decoding.

False

Returns:

Type Description
list[tuple[float, float]]

A list of (lng, lat) coordinate tuples in GeoJSON order.

encode_polyline

encode_polyline(coordinates: Sequence[Sequence[float]], precision: int = 5) -> str

Encode a sequence of (longitude, latitude) coordinate pairs into a polyline string.

Parameters:

Name Type Description Default
coordinates Sequence[Sequence[float]]

A sequence of [lng, lat] or (lng, lat) coordinates.

required
precision int

Decimal digits of precision (5 for Google/OSRM, 6 for Valhalla/Mapbox).

5

Returns:

Type Description
str

The encoded polyline ASCII string.

polyline_to_geojson

polyline_to_geojson(polyline: str | Sequence[str], precision: int = 5, unescape: bool = False, delimiter: str | None = None, properties: dict[str, Any] | None = None) -> dict[str, Any]

Convert one or more encoded polyline strings into a GeoJSON FeatureCollection.

Parameters:

Name Type Description Default
polyline str | Sequence[str]

A single polyline string or a sequence of polyline strings.

required
precision int

Decimal digits of precision (5 for Google/OSRM, 6 for Valhalla/Mapbox).

5
unescape bool

Whether to automatically unescape double-escaped backslashes.

False
delimiter str | None

Optional delimiter to split a multiline string (defaults to newline).

None
properties dict[str, Any] | None

Optional properties dict to attach to the GeoJSON feature(s).

None

Returns:

Type Description
dict[str, Any]

A GeoJSON FeatureCollection dict containing LineString features.