gtfs/common/geo
Geographic Utilities
Provides geographic calculations for GTFS data including:
- Haversine distance calculation
- Bearing calculation
- Google Polyline encoding/decoding
- Point-in-polygon testing
Example
import gtfs/common/geo
import gtfs/common/types.{Coordinate}
pub fn main() {
let nyc = Coordinate(40.7128, -74.0060)
let la = Coordinate(34.0522, -118.2437)
let km = geo.distance_km(nyc, la)
}
Values
pub fn bearing(
from: types.Coordinate,
to: types.Coordinate,
) -> Float
Calculate the initial bearing from one coordinate to another. Returns bearing in degrees (0-360, where 0 is north).
pub fn bounding_box(
coords: List(types.Coordinate),
) -> Result(#(types.Coordinate, types.Coordinate), String)
Calculate the bounding box for a list of coordinates
pub fn decode_polyline(
encoded: String,
) -> Result(List(types.Coordinate), String)
Decode a Google encoded polyline string into a list of coordinates. Used for GTFS Realtime shapes and static shapes.txt visualization.
pub fn distance_km(
from: types.Coordinate,
to: types.Coordinate,
) -> Float
Calculate the great-circle distance between two coordinates using the Haversine formula. Returns distance in kilometers.
pub fn distance_m(
from: types.Coordinate,
to: types.Coordinate,
) -> Float
Calculate the great-circle distance between two coordinates using the Haversine formula. Returns distance in meters.
pub fn encode_polyline(coords: List(types.Coordinate)) -> String
Encode a list of coordinates into a Google encoded polyline string.
pub fn is_valid_coordinate(coord: types.Coordinate) -> Bool
Check if a coordinate is valid (within WGS84 bounds)
pub fn point_in_polygon(
point: types.Coordinate,
polygon: List(types.Coordinate),
) -> Bool
Check if a coordinate is inside a polygon using the ray casting algorithm. The polygon is defined as a list of coordinates forming a closed ring.