geokit/polyline

Encoded polylines: a list of positions as a short ASCII string, as described in Google’s Encoded Polyline Algorithm Format. Each coordinate is rounded to precision decimal digits and stored as the difference from the previous position. Google uses precision 5; some routing services use 6. The same precision must be used to encode and decode.

Types

The reason a polyline operation failed.

pub type PolylineError {
  TruncatedInput
  InvalidCharacter(character: String, position: Int)
  PrecisionOutOfRange(precision: Int)
  CoordinateOutOfRange(index: Int)
}

Constructors

  • TruncatedInput

    The input ended in the middle of a value.

  • InvalidCharacter(character: String, position: Int)

    A character outside ? to ~ (ASCII 63 to 126), or a value longer than ten chunks. character is the grapheme that holds the bad byte and position is its 0-based grapheme index.

  • PrecisionOutOfRange(precision: Int)

    The precision was outside [1, 11].

  • CoordinateOutOfRange(index: Int)

    The decoded position at this 0-based index is outside the latitude or longitude range.

Values

pub fn decode(
  input: String,
  precision precision: Int,
) -> Result(List(geokit.LatLng), PolylineError)

Returns the positions in an encoded polyline. precision must be the one used to encode it, in [1, 11]. Returns an error for a malformed string or a position outside the latitude or longitude range. One position out of range fails the whole decode with CoordinateOutOfRange(index:), the index of that position.

Examples

decode("_p~iF~ps|U_ulLnnqC_mqNvxq`@", precision: 5)
|> result.map(list.map(_, geokit.latitude))
// -> Ok([38.5, 40.7, 43.252])
decode("_p~iF", precision: 5)
// -> Error(TruncatedInput)
pub fn encode(
  points: List(geokit.LatLng),
  precision precision: Int,
) -> Result(String, PolylineError)

Returns the encoded polyline for points, with each coordinate rounded to precision decimal digits. precision must be in [1, 11]; above 11 the values no longer fit in the integers JavaScript represents exactly.

Examples

[
  geokit.wrap_lat_lng(latitude: 38.5, longitude: -120.2),
  geokit.wrap_lat_lng(latitude: 40.7, longitude: -120.95),
  geokit.wrap_lat_lng(latitude: 43.252, longitude: -126.453),
]
|> encode(precision: 5)
// -> Ok("_p~iF~ps|U_ulLnnqC_mqNvxq`@")
encode([], precision: 12)
// -> Error(PrecisionOutOfRange(precision: 12))
✨ Search Document