{"id":3746,"library":"polyline","title":"Google's Encoded Polyline Algorithm (Python)","description":"The `polyline` library is a Python implementation of Google's Encoded Polyline Algorithm Format. It provides functionality to encode a list of latitude/longitude coordinates into a compact polyline string and to decode such a string back into coordinates. The library is actively maintained, with the current stable version being 2.0.4, and is released on an as-needed basis for bug fixes and feature enhancements.","status":"active","version":"2.0.4","language":"en","source_language":"en","source_url":"https://github.com/frederickjansen/python-polyline","tags":["geometry","polyline","encoding","decoding","maps","google maps","gis"],"install":[{"cmd":"pip install polyline","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary functions `encode` and `decode` are directly available under the `polyline` module namespace, not as classes.","wrong":"from polyline import PolylineEncoder","symbol":"encode","correct":"import polyline\npolyline.encode(...)"},{"note":"The primary functions `encode` and `decode` are directly available under the `polyline` module namespace, not as classes.","wrong":"from polyline import PolylineDecoder","symbol":"decode","correct":"import polyline\npolyline.decode(...)"}],"quickstart":{"code":"import polyline\n\n# Example coordinates: (latitude, longitude)\ncoords = [\n    (38.5, -120.2),\n    (40.7, -120.9),\n    (43.2, -126.4)\n]\n\n# Encode coordinates into a polyline string with default precision (5)\nencoded_polyline = polyline.encode(coords)\nprint(f\"Encoded Polyline: {encoded_polyline}\")\n\n# Decode the polyline string back to coordinates\ndecoded_coords = polyline.decode(encoded_polyline)\nprint(f\"Decoded Coordinates: {decoded_coords}\")\n\n# Example with GeoJSON style (lon, lat) and custom precision\ngeojson_coords = [\n    (-120.2, 38.5),\n    (-120.9, 40.7),\n    (-126.4, 43.2)\n]\nencoded_geojson = polyline.encode(geojson_coords, precision=6, geojson=True)\nprint(f\"Encoded GeoJSON Polyline (precision 6): {encoded_geojson}\")\ndecoded_geojson = polyline.decode(encoded_geojson, precision=6, geojson=True)\nprint(f\"Decoded GeoJSON Coordinates (precision 6): {decoded_geojson}\")","lang":"python","description":"This quickstart demonstrates encoding a list of `(latitude, longitude)` tuples into a polyline string and then decoding it back. It also shows how to handle GeoJSON-style `(longitude, latitude)` order using the `geojson=True` parameter and how to specify a custom precision."},"warnings":[{"fix":"Upgrade to Python 3.7+ or pin the `polyline` library version to 1.4.0 (e.g., `pip install polyline==1.4.0`).","message":"Version 2.0.0 and above of `polyline` only supports Python 3.7+. For applications requiring Python 2 support, you must install version 1.4.0.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use the `geojson=True` parameter in both `polyline.encode()` and `polyline.decode()` to work with `(longitude, latitude)` coordinate tuples.","message":"The `polyline` library uses `(latitude, longitude)` order by default for `encode` input and `decode` output, which is common in GIS but differs from the `(longitude, latitude)` order used in GeoJSON standards.","severity":"gotcha","affected_versions":"All"},{"fix":"Explicitly set the `precision` parameter during both `encode` and `decode` if a non-default precision is desired or expected.","message":"The `precision` parameter (defaulting to 5) must be consistent between encoding and decoding operations to ensure accurate round-tripping of coordinates.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that backslashes in the polyline string are correctly escaped or unescaped during parsing. For example, if a `\\\\` in a CSV becomes `\\` in Python, you might need `.replace('\\\\','\\')` before decoding.","message":"When loading polyline strings from external sources like CSV files, be mindful of backslash escaping. Python's CSV reader or other parsing mechanisms might interpret `\\` differently, leading to corrupted polyline strings.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}